Usability
[llpp.git] / main.ml
blob1e131e8274687d3537b02c08906f0481d2f3fe66
1 exception Quit;;
3 type under =
4 | Unone
5 | Ulinkuri of string
6 | Ulinkgoto of (int * int)
7 | Utext of facename
8 | Uunexpected of string
9 | Ulaunch of string
10 | Unamed of string
11 | Uremote of (string * int)
12 and facename = string;;
14 let dolog fmt = Printf.kprintf prerr_endline fmt;;
15 let now = Unix.gettimeofday;;
17 type params = (angle * proportional * trimparams
18 * texcount * sliceheight * memsize
19 * colorspace * fontpath)
20 and pageno = int
21 and width = int
22 and height = int
23 and leftx = int
24 and opaque = string
25 and recttype = int
26 and pixmapsize = int
27 and angle = int
28 and proportional = bool
29 and trimmargins = bool
30 and interpagespace = int
31 and texcount = int
32 and sliceheight = int
33 and gen = int
34 and top = float
35 and fontpath = string
36 and memsize = int
37 and aalevel = int
38 and irect = (int * int * int * int)
39 and trimparams = (trimmargins * irect)
40 and colorspace = | Rgb | Bgr | Gray
43 type link =
44 | Lnolock
45 | Lnotfound
46 | Lfound of (int * int * int * int * int)
47 and linkdir =
48 | LDfirst
49 | LDlast
50 | LDfirstvisible of (int * int * int)
51 | LDleft of int
52 | LDright of int
53 | LDdown of int
54 | LDup of int
57 type pagewithlinks =
58 | Pwlnotfound
59 | Pwl of int
62 type keymap =
63 | KMinsrt of key
64 | KMinsrl of key list
65 | KMmulti of key list * key list
66 and key = int * int
67 and keyhash = (key, keymap) Hashtbl.t
68 and keystate =
69 | KSnone
70 | KSinto of (key list * key list)
73 type platform = | Punknown | Plinux | Posx | Psun | Pfreebsd
74 | Pdragonflybsd | Popenbsd | Pnetbsd | Pcygwin;;
76 type pipe = (Unix.file_descr * Unix.file_descr);;
78 external init : pipe -> params -> unit = "ml_init";;
79 external seltext : string -> (int * int * int * int) -> unit = "ml_seltext";;
80 external copysel : string -> opaque -> unit = "ml_copysel";;
81 external getpdimrect : int -> float array = "ml_getpdimrect";;
82 external whatsunder : string -> int -> int -> under = "ml_whatsunder";;
83 external zoomforh : int -> int -> int -> float = "ml_zoom_for_height";;
84 external drawstr : int -> int -> int -> string -> float = "ml_draw_string";;
85 external measurestr : int -> string -> float = "ml_measure_string";;
86 external getmaxw : unit -> float = "ml_getmaxw";;
87 external postprocess : opaque -> bool -> int -> int -> unit = "ml_postprocess";;
88 external pagebbox : opaque -> (int * int * int * int) = "ml_getpagebox";;
89 external platform : unit -> platform = "ml_platform";;
90 external setaalevel : int -> unit = "ml_setaalevel";;
91 external realloctexts : int -> bool = "ml_realloctexts";;
92 external cloexec : Unix.file_descr -> unit = "ml_cloexec";;
93 external findlink : opaque -> linkdir -> link = "ml_findlink";;
94 external getlink : opaque -> int -> under = "ml_getlink";;
95 external findpwl: int -> int -> pagewithlinks = "ml_find_page_with_links"
97 let platform_to_string = function
98 | Punknown -> "unknown"
99 | Plinux -> "Linux"
100 | Posx -> "OSX"
101 | Psun -> "Sun"
102 | Pfreebsd -> "FreeBSD"
103 | Pdragonflybsd -> "DragonflyBSD"
104 | Popenbsd -> "OpenBSD"
105 | Pnetbsd -> "NetBSD"
106 | Pcygwin -> "Cygwin"
109 let platform = platform ();;
111 type x = int
112 and y = int
113 and tilex = int
114 and tiley = int
115 and tileparams = (x * y * width * height * tilex * tiley)
118 external drawtile : tileparams -> opaque -> unit = "ml_drawtile";;
120 type mpos = int * int
121 and mstate =
122 | Msel of (mpos * mpos)
123 | Mpan of mpos
124 | Mscrolly | Mscrollx
125 | Mzoom of (int * int)
126 | Mzoomrect of (mpos * mpos)
127 | Mnone
130 type textentry = string * string * onhist option * onkey * ondone
131 and onkey = string -> int -> te
132 and ondone = string -> unit
133 and histcancel = unit -> unit
134 and onhist = ((histcmd -> string) * histcancel)
135 and histcmd = HCnext | HCprev | HCfirst | HClast
136 and te =
137 | TEstop
138 | TEdone of string
139 | TEcont of string
140 | TEswitch of textentry
143 type 'a circbuf =
144 { store : 'a array
145 ; mutable rc : int
146 ; mutable wc : int
147 ; mutable len : int
151 let bound v minv maxv =
152 max minv (min maxv v);
155 let cbnew n v =
156 { store = Array.create n v
157 ; rc = 0
158 ; wc = 0
159 ; len = 0
163 let drawstring size x y s =
164 Gl.enable `blend;
165 Gl.enable `texture_2d;
166 ignore (drawstr size x y s);
167 Gl.disable `blend;
168 Gl.disable `texture_2d;
171 let drawstring1 size x y s =
172 drawstr size x y s;
175 let drawstring2 size x y fmt =
176 Printf.kprintf (drawstring size (x+1) (y+size+1)) fmt
179 let cbcap b = Array.length b.store;;
181 let cbput b v =
182 let cap = cbcap b in
183 b.store.(b.wc) <- v;
184 b.wc <- (b.wc + 1) mod cap;
185 b.rc <- b.wc;
186 b.len <- min (b.len + 1) cap;
189 let cbempty b = b.len = 0;;
191 let cbgetg b circular dir =
192 if cbempty b
193 then b.store.(0)
194 else
195 let rc = b.rc + dir in
196 let rc =
197 if circular
198 then (
199 if rc = -1
200 then b.len-1
201 else (
202 if rc = b.len
203 then 0
204 else rc
207 else max 0 (min rc (b.len-1))
209 b.rc <- rc;
210 b.store.(rc);
213 let cbget b = cbgetg b false;;
214 let cbgetc b = cbgetg b true;;
216 type page =
217 { pageno : int
218 ; pagedimno : int
219 ; pagew : int
220 ; pageh : int
221 ; pagex : int
222 ; pagey : int
223 ; pagevw : int
224 ; pagevh : int
225 ; pagedispx : int
226 ; pagedispy : int
230 let debugl l =
231 dolog "l %d dim=%d {" l.pageno l.pagedimno;
232 dolog " WxH %dx%d" l.pagew l.pageh;
233 dolog " vWxH %dx%d" l.pagevw l.pagevh;
234 dolog " pagex,y %d,%d" l.pagex l.pagey;
235 dolog " dispx,y %d,%d" l.pagedispx l.pagedispy;
236 dolog "}";
239 let debugrect (x0, y0, x1, y1, x2, y2, x3, y3) =
240 dolog "rect {";
241 dolog " x0,y0=(% f, % f)" x0 y0;
242 dolog " x1,y1=(% f, % f)" x1 y1;
243 dolog " x2,y2=(% f, % f)" x2 y2;
244 dolog " x3,y3=(% f, % f)" x3 y3;
245 dolog "}";
248 type columns =
249 multicol * ((pdimno * x * y * (pageno * width * height * leftx)) array)
250 and multicol = columncount * covercount * covercount
251 and pdimno = int
252 and columncount = int
253 and covercount = int;;
255 type conf =
256 { mutable scrollbw : int
257 ; mutable scrollh : int
258 ; mutable icase : bool
259 ; mutable preload : bool
260 ; mutable pagebias : int
261 ; mutable verbose : bool
262 ; mutable debug : bool
263 ; mutable scrollstep : int
264 ; mutable maxhfit : bool
265 ; mutable crophack : bool
266 ; mutable autoscrollstep : int
267 ; mutable maxwait : float option
268 ; mutable hlinks : bool
269 ; mutable underinfo : bool
270 ; mutable interpagespace : interpagespace
271 ; mutable zoom : float
272 ; mutable presentation : bool
273 ; mutable angle : angle
274 ; mutable winw : int
275 ; mutable winh : int
276 ; mutable savebmarks : bool
277 ; mutable proportional : proportional
278 ; mutable trimmargins : trimmargins
279 ; mutable trimfuzz : irect
280 ; mutable memlimit : memsize
281 ; mutable texcount : texcount
282 ; mutable sliceheight : sliceheight
283 ; mutable thumbw : width
284 ; mutable jumpback : bool
285 ; mutable bgcolor : float * float * float
286 ; mutable bedefault : bool
287 ; mutable scrollbarinpm : bool
288 ; mutable tilew : int
289 ; mutable tileh : int
290 ; mutable mustoresize : memsize
291 ; mutable checkers : bool
292 ; mutable aalevel : int
293 ; mutable urilauncher : string
294 ; mutable pathlauncher : string
295 ; mutable colorspace : colorspace
296 ; mutable invert : bool
297 ; mutable colorscale : float
298 ; mutable redirectstderr : bool
299 ; mutable ghyllscroll : (int * int * int) option
300 ; mutable columns : columns option
301 ; mutable beyecolumns : columncount option
302 ; mutable selcmd : string
303 ; mutable updatecurs : bool
304 ; mutable keyhashes : (string * keyhash) list
308 type anchor = pageno * top;;
310 type outline = string * int * anchor;;
312 type rect = float * float * float * float * float * float * float * float;;
314 type tile = opaque * pixmapsize * elapsed
315 and elapsed = float;;
316 type pagemapkey = pageno * gen;;
317 type tilemapkey = pageno * gen * colorspace * angle * width * height * col * row
318 and row = int
319 and col = int;;
321 let emptyanchor = (0, 0.0);;
323 type infochange = | Memused | Docinfo | Pdim;;
325 class type uioh = object
326 method display : unit
327 method key : int -> int -> uioh
328 method button : int -> bool -> int -> int -> int -> uioh
329 method motion : int -> int -> uioh
330 method pmotion : int -> int -> uioh
331 method infochanged : infochange -> unit
332 method scrollpw : (int * float * float)
333 method scrollph : (int * float * float)
334 method modehash : keyhash
335 end;;
337 type mode =
338 | Birdseye of (conf * leftx * pageno * pageno * anchor)
339 | Textentry of (textentry * onleave)
340 | View
341 | LinkNav of linktarget
342 and onleave = leavetextentrystatus -> unit
343 and leavetextentrystatus = | Cancel | Confirm
344 and helpitem = string * int * action
345 and action =
346 | Noaction
347 | Action of (uioh -> uioh)
348 and linktarget =
349 | Ltexact of ((page * opaque * int) * (int * int * int * int))
350 | Ltgendir of int
353 let isbirdseye = function Birdseye _ -> true | _ -> false;;
354 let istextentry = function Textentry _ -> true | _ -> false;;
356 type currently =
357 | Idle
358 | Loading of (page * gen)
359 | Tiling of (
360 page * opaque * colorspace * angle * gen * col * row * width * height
362 | Outlining of outline list
365 let emptykeyhash = Hashtbl.create 0;;
366 let nouioh : uioh = object (self)
367 method display = ()
368 method key _ _ = self
369 method button _ _ _ _ _ = self
370 method motion _ _ = self
371 method pmotion _ _ = self
372 method infochanged _ = ()
373 method scrollpw = (0, nan, nan)
374 method scrollph = (0, nan, nan)
375 method modehash = emptykeyhash
376 end;;
378 type state =
379 { mutable sr : Unix.file_descr
380 ; mutable sw : Unix.file_descr
381 ; mutable wsfd : Unix.file_descr
382 ; mutable errfd : Unix.file_descr option
383 ; mutable stderr : Unix.file_descr
384 ; mutable errmsgs : Buffer.t
385 ; mutable newerrmsgs : bool
386 ; mutable w : int
387 ; mutable x : int
388 ; mutable y : int
389 ; mutable scrollw : int
390 ; mutable hscrollh : int
391 ; mutable anchor : anchor
392 ; mutable ranchors : (string * string * anchor) list
393 ; mutable maxy : int
394 ; mutable layout : page list
395 ; pagemap : (pagemapkey, opaque) Hashtbl.t
396 ; tilemap : (tilemapkey, tile) Hashtbl.t
397 ; tilelru : (tilemapkey * opaque * pixmapsize) Queue.t
398 ; mutable pdims : (pageno * width * height * leftx) list
399 ; mutable pagecount : int
400 ; mutable currently : currently
401 ; mutable mstate : mstate
402 ; mutable searchpattern : string
403 ; mutable rects : (pageno * recttype * rect) list
404 ; mutable rects1 : (pageno * recttype * rect) list
405 ; mutable text : string
406 ; mutable fullscreen : (width * height) option
407 ; mutable mode : mode
408 ; mutable uioh : uioh
409 ; mutable outlines : outline array
410 ; mutable bookmarks : outline list
411 ; mutable path : string
412 ; mutable password : string
413 ; mutable invalidated : int
414 ; mutable memused : memsize
415 ; mutable gen : gen
416 ; mutable throttle : (page list * int * float) option
417 ; mutable autoscroll : int option
418 ; mutable ghyll : (int option -> unit)
419 ; mutable help : helpitem array
420 ; mutable docinfo : (int * string) list
421 ; mutable texid : GlTex.texture_id option
422 ; hists : hists
423 ; mutable prevzoom : float
424 ; mutable progress : float
425 ; mutable redisplay : bool
426 ; mutable mpos : mpos
427 ; mutable keystate : keystate
429 and hists =
430 { pat : string circbuf
431 ; pag : string circbuf
432 ; nav : anchor circbuf
433 ; sel : string circbuf
437 let defconf =
438 { scrollbw = 7
439 ; scrollh = 12
440 ; icase = true
441 ; preload = true
442 ; pagebias = 0
443 ; verbose = false
444 ; debug = false
445 ; scrollstep = 24
446 ; maxhfit = true
447 ; crophack = false
448 ; autoscrollstep = 2
449 ; maxwait = None
450 ; hlinks = false
451 ; underinfo = false
452 ; interpagespace = 2
453 ; zoom = 1.0
454 ; presentation = false
455 ; angle = 0
456 ; winw = 900
457 ; winh = 900
458 ; savebmarks = true
459 ; proportional = true
460 ; trimmargins = false
461 ; trimfuzz = (0,0,0,0)
462 ; memlimit = 32 lsl 20
463 ; texcount = 256
464 ; sliceheight = 24
465 ; thumbw = 76
466 ; jumpback = true
467 ; bgcolor = (0.5, 0.5, 0.5)
468 ; bedefault = false
469 ; scrollbarinpm = true
470 ; tilew = 2048
471 ; tileh = 2048
472 ; mustoresize = 128 lsl 20
473 ; checkers = true
474 ; aalevel = 8
475 ; urilauncher =
476 (match platform with
477 | Plinux | Pfreebsd | Pdragonflybsd
478 | Popenbsd | Pnetbsd | Psun -> "xdg-open \"%s\""
479 | Posx -> "open \"%s\""
480 | Pcygwin -> "cygstart %s"
481 | Punknown -> "echo %s")
482 ; pathlauncher = "lp \"%s\""
483 ; selcmd =
484 (match platform with
485 | Plinux | Pfreebsd | Pdragonflybsd
486 | Popenbsd | Pnetbsd | Psun -> "xsel -i"
487 | Posx -> "pbcopy"
488 | Pcygwin -> "wsel"
489 | Punknown -> "cat")
490 ; colorspace = Rgb
491 ; invert = false
492 ; colorscale = 1.0
493 ; redirectstderr = false
494 ; ghyllscroll = None
495 ; columns = None
496 ; beyecolumns = None
497 ; updatecurs = false
498 ; keyhashes =
499 let mk n = (n, Hashtbl.create 1) in
500 [ mk "global"
501 ; mk "info"
502 ; mk "help"
503 ; mk "outline"
504 ; mk "listview"
505 ; mk "birdseye"
506 ; mk "textentry"
507 ; mk "links"
512 let findkeyhash c name =
513 try List.assoc name c.keyhashes
514 with Not_found -> failwith ("invalid mode name `" ^ name ^ "'")
517 let conf = { defconf with angle = defconf.angle };;
519 type fontstate =
520 { mutable fontsize : int
521 ; mutable wwidth : float
522 ; mutable maxrows : int
526 let fstate =
527 { fontsize = 14
528 ; wwidth = nan
529 ; maxrows = -1
533 let setfontsize n =
534 fstate.fontsize <- n;
535 fstate.wwidth <- measurestr fstate.fontsize "w";
536 fstate.maxrows <- (conf.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
539 let geturl s =
540 let colonpos = try String.index s ':' with Not_found -> -1 in
541 let len = String.length s in
542 if colonpos >= 0 && colonpos + 3 < len
543 then (
544 if s.[colonpos+1] = '/' && s.[colonpos+2] = '/'
545 then
546 let schemestartpos =
547 try String.rindex_from s colonpos ' '
548 with Not_found -> -1
550 let scheme =
551 String.sub s (schemestartpos+1) (colonpos-1-schemestartpos)
553 match scheme with
554 | "http" | "ftp" | "mailto" ->
555 let epos =
556 try String.index_from s colonpos ' '
557 with Not_found -> len
559 String.sub s (schemestartpos+1) (epos-1-schemestartpos)
560 | _ -> ""
561 else ""
563 else ""
566 let popen =
567 let shell, farg = "/bin/sh", "-c" in
568 fun s ->
569 let args = [|shell; farg; s|] in
570 ignore (Unix.create_process shell args Unix.stdin Unix.stdout Unix.stderr)
573 let gotouri uri =
574 if String.length conf.urilauncher = 0
575 then print_endline uri
576 else (
577 let url = geturl uri in
578 if String.length url = 0
579 then print_endline uri
580 else
581 let re = Str.regexp "%s" in
582 let command = Str.global_replace re url conf.urilauncher in
583 try popen command
584 with exn ->
585 Printf.eprintf
586 "failed to execute `%s': %s\n" command (Printexc.to_string exn);
587 flush stderr;
591 let version () =
592 Printf.sprintf "llpp version %s (%s/%dbit, ocaml %s)" Help.version
593 (platform_to_string platform) Sys.word_size Sys.ocaml_version
596 let makehelp () =
597 let strings = version () :: "" :: Help.keys in
598 Array.of_list (
599 List.map (fun s ->
600 let url = geturl s in
601 if String.length url > 0
602 then (s, 0, Action (fun u -> gotouri url; u))
603 else (s, 0, Noaction)
604 ) strings);
607 let noghyll _ = ();;
609 let state =
610 { sr = Unix.stdin
611 ; sw = Unix.stdin
612 ; wsfd = Unix.stdin
613 ; errfd = None
614 ; stderr = Unix.stderr
615 ; errmsgs = Buffer.create 0
616 ; newerrmsgs = false
617 ; x = 0
618 ; y = 0
619 ; w = 0
620 ; scrollw = 0
621 ; hscrollh = 0
622 ; anchor = emptyanchor
623 ; ranchors = []
624 ; layout = []
625 ; maxy = max_int
626 ; tilelru = Queue.create ()
627 ; pagemap = Hashtbl.create 10
628 ; tilemap = Hashtbl.create 10
629 ; pdims = []
630 ; pagecount = 0
631 ; currently = Idle
632 ; mstate = Mnone
633 ; rects = []
634 ; rects1 = []
635 ; text = ""
636 ; mode = View
637 ; fullscreen = None
638 ; searchpattern = ""
639 ; outlines = [||]
640 ; bookmarks = []
641 ; path = ""
642 ; password = ""
643 ; invalidated = 0
644 ; hists =
645 { nav = cbnew 10 (0, 0.0)
646 ; pat = cbnew 10 ""
647 ; pag = cbnew 10 ""
648 ; sel = cbnew 10 ""
650 ; memused = 0
651 ; gen = 0
652 ; throttle = None
653 ; autoscroll = None
654 ; ghyll = noghyll
655 ; help = makehelp ()
656 ; docinfo = []
657 ; texid = None
658 ; prevzoom = 1.0
659 ; progress = -1.0
660 ; uioh = nouioh
661 ; redisplay = false
662 ; mpos = (-1, -1)
663 ; keystate = KSnone
667 let vlog fmt =
668 if conf.verbose
669 then
670 Printf.kprintf prerr_endline fmt
671 else
672 Printf.kprintf ignore fmt
675 let launchpath () =
676 if String.length conf.pathlauncher = 0
677 then print_endline state.path
678 else (
679 let re = Str.regexp "%s" in
680 let command = Str.global_replace re state.path conf.pathlauncher in
681 try popen command
682 with exn ->
683 Printf.eprintf
684 "failed to execute `%s': %s\n" command (Printexc.to_string exn);
685 flush stderr;
689 let redirectstderr () =
690 if conf.redirectstderr
691 then
692 let rfd, wfd = Unix.pipe () in
693 state.stderr <- Unix.dup Unix.stderr;
694 state.errfd <- Some rfd;
695 Unix.dup2 wfd Unix.stderr;
696 else (
697 state.newerrmsgs <- false;
698 begin match state.errfd with
699 | Some fd ->
700 Unix.close fd;
701 Unix.dup2 state.stderr Unix.stderr;
702 state.errfd <- None;
703 | None -> ()
704 end;
705 prerr_string (Buffer.contents state.errmsgs);
706 flush stderr;
707 Buffer.clear state.errmsgs;
711 module G =
712 struct
713 let postRedisplay who =
714 if conf.verbose
715 then prerr_endline ("redisplay for " ^ who);
716 state.redisplay <- true;
718 end;;
720 let getopaque pageno =
721 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
722 with Not_found -> None
725 let putopaque pageno opaque =
726 Hashtbl.replace state.pagemap (pageno, state.gen) opaque
729 let pagetranslatepoint l x y =
730 let dy = y - l.pagedispy in
731 let y = dy + l.pagey in
732 let dx = x - l.pagedispx in
733 let x = dx + l.pagex in
734 (x, y);
737 let getunder x y =
738 let rec f = function
739 | l :: rest ->
740 begin match getopaque l.pageno with
741 | Some opaque ->
742 let x0 = l.pagedispx in
743 let x1 = x0 + l.pagevw in
744 let y0 = l.pagedispy in
745 let y1 = y0 + l.pagevh in
746 if y >= y0 && y <= y1 && x >= x0 && x <= x1
747 then
748 let px, py = pagetranslatepoint l x y in
749 match whatsunder opaque px py with
750 | Unone -> f rest
751 | under -> under
752 else f rest
753 | _ ->
754 f rest
756 | [] -> Unone
758 f state.layout
761 let showtext c s =
762 state.text <- Printf.sprintf "%c%s" c s;
763 G.postRedisplay "showtext";
766 let updateunder x y =
767 match getunder x y with
768 | Unone -> Wsi.setcursor Wsi.CURSOR_INHERIT
769 | Ulinkuri uri ->
770 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
771 Wsi.setcursor Wsi.CURSOR_INFO
772 | Ulinkgoto (page, _) ->
773 if conf.underinfo
774 then showtext 'p' ("age: " ^ string_of_int (page+1));
775 Wsi.setcursor Wsi.CURSOR_INFO
776 | Utext s ->
777 if conf.underinfo then showtext 'f' ("ont: " ^ s);
778 Wsi.setcursor Wsi.CURSOR_TEXT
779 | Uunexpected s ->
780 if conf.underinfo then showtext 'u' ("nexpected: " ^ s);
781 Wsi.setcursor Wsi.CURSOR_INHERIT
782 | Ulaunch s ->
783 if conf.underinfo then showtext 'l' ("launch: " ^ s);
784 Wsi.setcursor Wsi.CURSOR_INHERIT
785 | Unamed s ->
786 if conf.underinfo then showtext 'n' ("named: " ^ s);
787 Wsi.setcursor Wsi.CURSOR_INHERIT
788 | Uremote (filename, pageno) ->
789 if conf.underinfo then showtext 'r'
790 (Printf.sprintf "emote: %s (%d)" filename pageno);
791 Wsi.setcursor Wsi.CURSOR_INFO
794 let addchar s c =
795 let b = Buffer.create (String.length s + 1) in
796 Buffer.add_string b s;
797 Buffer.add_char b c;
798 Buffer.contents b;
801 let colorspace_of_string s =
802 match String.lowercase s with
803 | "rgb" -> Rgb
804 | "bgr" -> Bgr
805 | "gray" -> Gray
806 | _ -> failwith "invalid colorspace"
809 let int_of_colorspace = function
810 | Rgb -> 0
811 | Bgr -> 1
812 | Gray -> 2
815 let colorspace_of_int = function
816 | 0 -> Rgb
817 | 1 -> Bgr
818 | 2 -> Gray
819 | n -> failwith ("invalid colorspace index " ^ string_of_int n)
822 let colorspace_to_string = function
823 | Rgb -> "rgb"
824 | Bgr -> "bgr"
825 | Gray -> "gray"
828 let intentry_with_suffix text key =
829 let c =
830 if key >= 32 && key < 127
831 then Char.chr key
832 else '\000'
834 match Char.lowercase c with
835 | '0' .. '9' ->
836 let text = addchar text c in
837 TEcont text
839 | 'k' | 'm' | 'g' ->
840 let text = addchar text c in
841 TEcont text
843 | _ ->
844 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
845 TEcont text
848 let columns_to_string (n, a, b) =
849 if a = 0 && b = 0
850 then Printf.sprintf "%d" n
851 else Printf.sprintf "%d,%d,%d" n a b;
854 let columns_of_string s =
856 (int_of_string s, 0, 0)
857 with _ ->
858 Scanf.sscanf s "%u,%u,%u" (fun n a b -> (n, a, b));
861 let readcmd fd =
862 let s = "xxxx" in
863 let n = Unix.read fd s 0 4 in
864 if n != 4 then failwith "incomplete read(len)";
865 let len = 0
866 lor (Char.code s.[0] lsl 24)
867 lor (Char.code s.[1] lsl 16)
868 lor (Char.code s.[2] lsl 8)
869 lor (Char.code s.[3] lsl 0)
871 let s = String.create len in
872 let n = Unix.read fd s 0 len in
873 if n != len then failwith "incomplete read(data)";
877 let btod b = if b then 1 else 0;;
879 let wcmd fmt =
880 let b = Buffer.create 16 in
881 Buffer.add_string b "llll";
882 Printf.kbprintf
883 (fun b ->
884 let s = Buffer.contents b in
885 let n = String.length s in
886 let len = n - 4 in
887 s.[0] <- Char.chr ((len lsr 24) land 0xff);
888 s.[1] <- Char.chr ((len lsr 16) land 0xff);
889 s.[2] <- Char.chr ((len lsr 8) land 0xff);
890 s.[3] <- Char.chr (len land 0xff);
891 let n' = Unix.write state.sw s 0 n in
892 if n' != n then failwith "write failed";
893 ) b fmt;
896 let calcips h =
897 if conf.presentation
898 then
899 let d = conf.winh - h in
900 max 0 ((d + 1) / 2)
901 else
902 conf.interpagespace
905 let calcheight () =
906 let rec f pn ph pi fh l =
907 match l with
908 | (n, _, h, _) :: rest ->
909 let ips = calcips h in
910 let fh =
911 if conf.presentation
912 then fh+ips
913 else (
914 if isbirdseye state.mode && pn = 0
915 then fh + ips
916 else fh
919 let fh = fh + ((n - pn) * (ph + pi)) in
920 f n h ips fh rest;
922 | [] ->
923 let inc =
924 if conf.presentation || (isbirdseye state.mode && pn = 0)
925 then 0
926 else -pi
928 let fh = fh + ((state.pagecount - pn) * (ph + pi)) + inc in
929 max 0 fh
931 let fh = f 0 0 0 0 state.pdims in
935 let calcheight () =
936 match conf.columns with
937 | None -> calcheight ()
938 | Some (_, b) ->
939 if Array.length b > 0
940 then
941 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
942 y + h
943 else 0
946 let getpageyh pageno =
947 let rec f pn ph pi y l =
948 match l with
949 | (n, _, h, _) :: rest ->
950 let ips = calcips h in
951 if n >= pageno
952 then
953 let h = if n = pageno then h else ph in
954 if conf.presentation && n = pageno
955 then
956 y + (pageno - pn) * (ph + pi) + pi, h
957 else
958 y + (pageno - pn) * (ph + pi), h
959 else
960 let y = y + (if conf.presentation then pi else 0) in
961 let y = y + (n - pn) * (ph + pi) in
962 f n h ips y rest
964 | [] ->
965 y + (pageno - pn) * (ph + pi), ph
967 f 0 0 0 0 state.pdims
970 let getpageyh pageno =
971 match conf.columns with
972 | None -> getpageyh pageno
973 | Some (_, b) ->
974 let (_, _, y, (_, _, h, _)) = b.(pageno) in
975 y, h
978 let getpagedim pageno =
979 let rec f ppdim l =
980 match l with
981 | (n, _, _, _) as pdim :: rest ->
982 if n >= pageno
983 then (if n = pageno then pdim else ppdim)
984 else f pdim rest
986 | [] -> ppdim
988 f (-1, -1, -1, -1) state.pdims
991 let getpagey pageno = fst (getpageyh pageno);;
993 let layout1 y sh =
994 let sh = sh - state.hscrollh in
995 let rec f ~pageno ~pdimno ~prev ~py ~dy ~pdims ~accu =
996 let ((w, h, ips, xoff) as curr), rest, pdimno, yinc =
997 match pdims with
998 | (pageno', w, h, xoff) :: rest when pageno' = pageno ->
999 let ips = calcips h in
1000 let yinc =
1001 if conf.presentation || (isbirdseye state.mode && pageno = 0)
1002 then ips
1003 else 0
1005 (w, h, ips, xoff), rest, pdimno + 1, yinc
1006 | _ ->
1007 prev, pdims, pdimno, 0
1009 let dy = dy + yinc in
1010 let py = py + yinc in
1011 if pageno = state.pagecount || dy >= sh
1012 then
1013 accu
1014 else
1015 let vy = y + dy in
1016 if py + h <= vy - yinc
1017 then
1018 let py = py + h + ips in
1019 let dy = max 0 (py - y) in
1020 f ~pageno:(pageno+1)
1021 ~pdimno
1022 ~prev:curr
1025 ~pdims:rest
1026 ~accu
1027 else
1028 let pagey = vy - py in
1029 let pagevh = h - pagey in
1030 let pagevh = min (sh - dy) pagevh in
1031 let off = if yinc > 0 then py - vy else 0 in
1032 let py = py + h + ips in
1033 let pagex, dx =
1034 let xoff = xoff +
1035 if state.w < conf.winw - state.scrollw
1036 then (conf.winw - state.scrollw - state.w) / 2
1037 else 0
1039 let dispx = xoff + state.x in
1040 if dispx < 0
1041 then (-dispx, 0)
1042 else (0, dispx)
1044 let pagevw =
1045 let lw = w - pagex in
1046 min lw (conf.winw - state.scrollw)
1048 let e =
1049 { pageno = pageno
1050 ; pagedimno = pdimno
1051 ; pagew = w
1052 ; pageh = h
1053 ; pagex = pagex
1054 ; pagey = pagey + off
1055 ; pagevw = pagevw
1056 ; pagevh = pagevh - off
1057 ; pagedispx = dx
1058 ; pagedispy = dy + off
1061 let accu = e :: accu in
1062 f ~pageno:(pageno+1)
1063 ~pdimno
1064 ~prev:curr
1066 ~dy:(dy+pagevh+ips)
1067 ~pdims:rest
1068 ~accu
1070 if state.invalidated = 0
1071 then (
1072 let accu =
1074 ~pageno:0
1075 ~pdimno:~-1
1076 ~prev:(0,0,0,0)
1077 ~py:0
1078 ~dy:0
1079 ~pdims:state.pdims
1080 ~accu:[]
1082 List.rev accu
1084 else
1088 let layoutN ((columns, coverA, coverB), b) y sh =
1089 let sh = sh - state.hscrollh in
1090 let rec fold accu n =
1091 if n = Array.length b
1092 then accu
1093 else
1094 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
1095 if (vy - y) > sh &&
1096 (n = coverA - 1
1097 || n = state.pagecount - coverB
1098 || (n - coverA) mod columns = columns - 1)
1099 then accu
1100 else
1101 let accu =
1102 if vy + h > y
1103 then
1104 let pagey = max 0 (y - vy) in
1105 let pagedispy = if pagey > 0 then 0 else vy - y in
1106 let pagedispx, pagex, pagevw =
1107 let pdx =
1108 if n = coverA - 1 || n = state.pagecount - coverB
1109 then state.x + (conf.winw - state.scrollw - w) / 2
1110 else dx + xoff + state.x
1112 if pdx < 0
1113 then 0, -pdx, w + pdx
1114 else pdx, 0, min (conf.winw - state.scrollw) w
1116 let pagevh = min (h - pagey) (sh - pagedispy) in
1117 if pagedispx < conf.winw - state.scrollw && pagevw > 0 && pagevh > 0
1118 then
1119 let e =
1120 { pageno = n
1121 ; pagedimno = pdimno
1122 ; pagew = w
1123 ; pageh = h
1124 ; pagex = pagex
1125 ; pagey = pagey
1126 ; pagevw = pagevw
1127 ; pagevh = pagevh
1128 ; pagedispx = pagedispx
1129 ; pagedispy = pagedispy
1132 e :: accu
1133 else
1134 accu
1135 else
1136 accu
1138 fold accu (n+1)
1140 if state.invalidated = 0
1141 then List.rev (fold [] 0)
1142 else []
1145 let layout y sh =
1146 match conf.columns with
1147 | None -> layout1 y sh
1148 | Some c -> layoutN c y sh
1151 let clamp incr =
1152 let y = state.y + incr in
1153 let y = max 0 y in
1154 let y = min y (state.maxy - (if conf.maxhfit then conf.winh else 0)) in
1158 let itertiles l f =
1159 let tilex = l.pagex mod conf.tilew in
1160 let tiley = l.pagey mod conf.tileh in
1162 let col = l.pagex / conf.tilew in
1163 let row = l.pagey / conf.tileh in
1165 let vw =
1166 let a = l.pagew - l.pagex in
1167 let b = conf.winw - state.scrollw in
1168 min a b
1169 and vh = l.pagevh in
1171 let rec rowloop row y0 dispy h =
1172 if h = 0
1173 then ()
1174 else (
1175 let dh = conf.tileh - y0 in
1176 let dh = min h dh in
1177 let rec colloop col x0 dispx w =
1178 if w = 0
1179 then ()
1180 else (
1181 let dw = conf.tilew - x0 in
1182 let dw = min w dw in
1184 f col row dispx dispy x0 y0 dw dh;
1185 colloop (col+1) 0 (dispx+dw) (w-dw)
1188 colloop col tilex l.pagedispx vw;
1189 rowloop (row+1) 0 (dispy+dh) (h-dh)
1192 if vw > 0 && vh > 0
1193 then rowloop row tiley l.pagedispy vh;
1196 let gettileopaque l col row =
1197 let key =
1198 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
1200 try Some (Hashtbl.find state.tilemap key)
1201 with Not_found -> None
1204 let puttileopaque l col row gen colorspace angle opaque size elapsed =
1205 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
1206 Hashtbl.add state.tilemap key (opaque, size, elapsed)
1209 let drawtiles l color =
1210 GlDraw.color color;
1211 let f col row x y tilex tiley w h =
1212 match gettileopaque l col row with
1213 | Some (opaque, _, t) ->
1214 let params = x, y, w, h, tilex, tiley in
1215 if conf.invert
1216 then (
1217 Gl.enable `blend;
1218 GlFunc.blend_func `zero `one_minus_src_color;
1220 drawtile params opaque;
1221 if conf.invert
1222 then Gl.disable `blend;
1223 if conf.debug
1224 then (
1225 let s = Printf.sprintf
1226 "%d[%d,%d] %f sec"
1227 l.pageno col row t
1229 let w = measurestr fstate.fontsize s in
1230 GlMisc.push_attrib [`current];
1231 GlDraw.color (0.0, 0.0, 0.0);
1232 GlDraw.rect
1233 (float (x-2), float (y-2))
1234 (float (x+2) +. w, float (y + fstate.fontsize + 2));
1235 GlDraw.color (1.0, 1.0, 1.0);
1236 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
1237 GlMisc.pop_attrib ();
1240 | _ ->
1241 let w =
1242 let lw = conf.winw - state.scrollw - x in
1243 min lw w
1244 and h =
1245 let lh = conf.winh - y in
1246 min lh h
1248 Gl.enable `texture_2d;
1249 begin match state.texid with
1250 | Some id ->
1251 GlTex.bind_texture `texture_2d id;
1252 let x0 = float x
1253 and y0 = float y
1254 and x1 = float (x+w)
1255 and y1 = float (y+h) in
1257 let tw = float w /. 64.0
1258 and th = float h /. 64.0 in
1259 let tx0 = float tilex /. 64.0
1260 and ty0 = float tiley /. 64.0 in
1261 let tx1 = tx0 +. tw
1262 and ty1 = ty0 +. th in
1263 GlDraw.begins `quads;
1264 GlTex.coord2 (tx0, ty0); GlDraw.vertex2 (x0, y0);
1265 GlTex.coord2 (tx0, ty1); GlDraw.vertex2 (x0, y1);
1266 GlTex.coord2 (tx1, ty1); GlDraw.vertex2 (x1, y1);
1267 GlTex.coord2 (tx1, ty0); GlDraw.vertex2 (x1, y0);
1268 GlDraw.ends ();
1270 Gl.disable `texture_2d;
1271 | None ->
1272 GlDraw.color (1.0, 1.0, 1.0);
1273 GlDraw.rect
1274 (float x, float y)
1275 (float (x+w), float (y+h));
1276 end;
1277 if w > 128 && h > fstate.fontsize + 10
1278 then (
1279 GlDraw.color (0.0, 0.0, 0.0);
1280 let c, r =
1281 if conf.verbose
1282 then (col*conf.tilew, row*conf.tileh)
1283 else col, row
1285 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
1287 GlDraw.color color;
1289 itertiles l f
1292 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
1294 let tilevisible1 l x y =
1295 let ax0 = l.pagex
1296 and ax1 = l.pagex + l.pagevw
1297 and ay0 = l.pagey
1298 and ay1 = l.pagey + l.pagevh in
1300 let bx0 = x
1301 and by0 = y in
1302 let bx1 = min (bx0 + conf.tilew) l.pagew
1303 and by1 = min (by0 + conf.tileh) l.pageh in
1305 let rx0 = max ax0 bx0
1306 and ry0 = max ay0 by0
1307 and rx1 = min ax1 bx1
1308 and ry1 = min ay1 by1 in
1310 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
1311 nonemptyintersection
1314 let tilevisible layout n x y =
1315 let rec findpageinlayout = function
1316 | l :: _ when l.pageno = n -> tilevisible1 l x y
1317 | _ :: rest -> findpageinlayout rest
1318 | [] -> false
1320 findpageinlayout layout
1323 let tileready l x y =
1324 tilevisible1 l x y &&
1325 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
1328 let tilepage n p layout =
1329 let rec loop = function
1330 | l :: rest ->
1331 if l.pageno = n
1332 then
1333 let f col row _ _ _ _ _ _ =
1334 if state.currently = Idle
1335 then
1336 match gettileopaque l col row with
1337 | Some _ -> ()
1338 | None ->
1339 let x = col*conf.tilew
1340 and y = row*conf.tileh in
1341 let w =
1342 let w = l.pagew - x in
1343 min w conf.tilew
1345 let h =
1346 let h = l.pageh - y in
1347 min h conf.tileh
1349 wcmd "tile %s %d %d %d %d" p x y w h;
1350 state.currently <-
1351 Tiling (
1352 l, p, conf.colorspace, conf.angle, state.gen, col, row,
1353 conf.tilew, conf.tileh
1356 itertiles l f;
1357 else
1358 loop rest
1360 | [] -> ()
1362 if state.invalidated = 0 then loop layout;
1365 let preloadlayout visiblepages =
1366 let presentation = conf.presentation in
1367 let interpagespace = conf.interpagespace in
1368 let maxy = state.maxy in
1369 conf.presentation <- false;
1370 conf.interpagespace <- 0;
1371 state.maxy <- calcheight ();
1372 let y =
1373 match visiblepages with
1374 | [] -> 0
1375 | l :: _ -> getpagey l.pageno + l.pagey
1377 let y = if y < conf.winh then 0 else y - conf.winh in
1378 let h = state.y - y + conf.winh*3 in
1379 let pages = layout y h in
1380 conf.presentation <- presentation;
1381 conf.interpagespace <- interpagespace;
1382 state.maxy <- maxy;
1383 pages;
1386 let load pages =
1387 let rec loop pages =
1388 if state.currently != Idle
1389 then ()
1390 else
1391 match pages with
1392 | l :: rest ->
1393 begin match getopaque l.pageno with
1394 | None ->
1395 wcmd "page %d %d" l.pageno l.pagedimno;
1396 state.currently <- Loading (l, state.gen);
1397 | Some opaque ->
1398 tilepage l.pageno opaque pages;
1399 loop rest
1400 end;
1401 | _ -> ()
1403 if state.invalidated = 0 then loop pages
1406 let preload pages =
1407 load pages;
1408 if conf.preload && state.currently = Idle
1409 then load (preloadlayout pages);
1412 let layoutready layout =
1413 let rec fold all ls =
1414 all && match ls with
1415 | l :: rest ->
1416 let seen = ref false in
1417 let allvisible = ref true in
1418 let foo col row _ _ _ _ _ _ =
1419 seen := true;
1420 allvisible := !allvisible &&
1421 begin match gettileopaque l col row with
1422 | Some _ -> true
1423 | None -> false
1426 itertiles l foo;
1427 fold (!seen && !allvisible) rest
1428 | [] -> true
1430 let alltilesvisible = fold true layout in
1431 alltilesvisible;
1434 let gotoy y =
1435 let y = bound y 0 state.maxy in
1436 let y, layout, proceed =
1437 match conf.maxwait with
1438 | Some time when state.ghyll == noghyll ->
1439 begin match state.throttle with
1440 | None ->
1441 let layout = layout y conf.winh in
1442 let ready = layoutready layout in
1443 if not ready
1444 then (
1445 load layout;
1446 state.throttle <- Some (layout, y, now ());
1448 else G.postRedisplay "gotoy showall (None)";
1449 y, layout, ready
1450 | Some (_, _, started) ->
1451 let dt = now () -. started in
1452 if dt > time
1453 then (
1454 state.throttle <- None;
1455 let layout = layout y conf.winh in
1456 load layout;
1457 G.postRedisplay "maxwait";
1458 y, layout, true
1460 else -1, [], false
1463 | _ ->
1464 let layout = layout y conf.winh in
1465 if true || layoutready layout
1466 then G.postRedisplay "gotoy ready";
1467 y, layout, true
1469 if proceed
1470 then (
1471 state.y <- y;
1472 state.layout <- layout;
1473 begin match state.mode with
1474 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
1475 if not (pagevisible layout pageno)
1476 then (
1477 match state.layout with
1478 | [] -> ()
1479 | l :: _ ->
1480 state.mode <- Birdseye (
1481 conf, leftx, l.pageno, hooverpageno, anchor
1484 | LinkNav (Ltgendir dir as lt) ->
1485 let linknav =
1486 let rec loop = function
1487 | [] -> lt
1488 | l :: rest ->
1489 match getopaque l.pageno with
1490 | None -> loop rest
1491 | Some opaque ->
1492 let link =
1493 findlink opaque (LDfirstvisible (l.pagex, l.pagey, dir))
1495 match link with
1496 | Lnolock -> showtext '!' "failed to acquire lock"; lt
1497 | Lnotfound -> loop rest
1498 | Lfound (n, x0, y0, x1, y1) ->
1499 Ltexact ((l, opaque, n), (x0, y0, x1, y1))
1501 loop state.layout
1503 state.mode <- LinkNav linknav
1504 | _ -> ()
1505 end;
1506 preload layout;
1508 state.ghyll <- noghyll;
1511 let conttiling pageno opaque =
1512 tilepage pageno opaque
1513 (if conf.preload then preloadlayout state.layout else state.layout)
1516 let gotoy_and_clear_text y =
1517 gotoy y;
1518 if not conf.verbose then state.text <- "";
1521 let getanchor () =
1522 match state.layout with
1523 | [] -> emptyanchor
1524 | l :: _ -> (l.pageno, float l.pagey /. float l.pageh)
1527 let getanchory (n, top) =
1528 let y, h = getpageyh n in
1529 y + (truncate (top *. float h));
1532 let gotoanchor anchor =
1533 gotoy (getanchory anchor);
1536 let addnav () =
1537 cbput state.hists.nav (getanchor ());
1540 let getnav dir =
1541 let anchor = cbgetc state.hists.nav dir in
1542 getanchory anchor;
1545 let gotoghyll y =
1546 let rec scroll f n a b =
1547 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
1548 let snake f a b =
1549 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
1550 if f < a
1551 then s (float f /. float a)
1552 else (
1553 if f > b
1554 then 1.0 -. s ((float (f-b) /. float (n-b)))
1555 else 1.0
1558 snake f a b
1559 and summa f n a b =
1560 (* courtesy:
1561 http://integrals.wolfram.com/index.jsp?expr=3x%5E2-2x%5E3&random=false *)
1562 let iv x = -.((-.2.0 +. x)*.x**3.0)/.2.0 in
1563 let iv1 = iv f in
1564 let ins = float a *. iv1
1565 and outs = float (n-b) *. iv1 in
1566 let ones = b - a in
1567 ins +. outs +. float ones
1569 let rec set (_N, _A, _B) y sy =
1570 let sum = summa 1.0 _N _A _B in
1571 let dy = float (y - sy) in
1572 state.ghyll <- (
1573 let rec gf n y1 o =
1574 if n >= _N
1575 then state.ghyll <- noghyll
1576 else
1577 let go n =
1578 let s = scroll n _N _A _B in
1579 let y1 = y1 +. ((s *. dy) /. sum) in
1580 gotoy_and_clear_text (truncate y1);
1581 state.ghyll <- gf (n+1) y1;
1583 match o with
1584 | None -> go n
1585 | Some y' -> set (_N/2, 0, 0) y' state.y
1587 gf 0 (float state.y)
1590 match conf.ghyllscroll with
1591 | None ->
1592 gotoy_and_clear_text y
1593 | Some nab ->
1594 if state.ghyll == noghyll
1595 then set nab y state.y
1596 else state.ghyll (Some y)
1599 let gotopage n top =
1600 let y, h = getpageyh n in
1601 let y = y + (truncate (top *. float h)) in
1602 gotoghyll y
1605 let gotopage1 n top =
1606 let y = getpagey n in
1607 let y = y + top in
1608 gotoghyll y
1611 let invalidate () =
1612 state.layout <- [];
1613 state.pdims <- [];
1614 state.rects <- [];
1615 state.rects1 <- [];
1616 state.invalidated <- state.invalidated + 1;
1619 let writeopen path password =
1620 wcmd "open %s\000%s\000" path password;
1623 let opendoc path password =
1624 invalidate ();
1625 state.path <- path;
1626 state.password <- password;
1627 state.gen <- state.gen + 1;
1628 state.docinfo <- [];
1630 setaalevel conf.aalevel;
1631 writeopen path password;
1632 Wsi.settitle ("llpp " ^ Filename.basename path);
1633 wcmd "geometry %d %d" state.w conf.winh;
1636 let scalecolor c =
1637 let c = c *. conf.colorscale in
1638 (c, c, c);
1641 let scalecolor2 (r, g, b) =
1642 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
1645 let represent () =
1646 let docolumns = function
1647 | None -> ()
1648 | Some ((columns, coverA, coverB), _) ->
1649 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1650 let rec loop pageno pdimno pdim x y rowh pdims =
1651 if pageno = state.pagecount
1652 then ()
1653 else
1654 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1655 match pdims with
1656 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1657 pdimno+1, pdim, rest
1658 | _ ->
1659 pdimno, pdim, pdims
1661 let x, y, rowh' =
1662 if pageno = coverA - 1 || pageno = state.pagecount - coverB
1663 then (
1664 (conf.winw - state.scrollw - w) / 2,
1665 y + rowh + conf.interpagespace, h
1667 else (
1668 if (pageno - coverA) mod columns = 0
1669 then 0, y + rowh + conf.interpagespace, h
1670 else x, y, max rowh h
1673 let rec fixrow m = if m = pageno then () else
1674 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
1675 if h < rowh
1676 then (
1677 let y = y + (rowh - h) / 2 in
1678 a.(m) <- (pdimno, x, y, pdim);
1680 fixrow (m+1)
1682 if pageno > 1 && (pageno - coverA) mod columns = 0
1683 then fixrow (pageno - columns);
1684 a.(pageno) <- (pdimno, x, y, pdim);
1685 let x = x + w + xoff*2 + conf.interpagespace in
1686 loop (pageno+1) pdimno pdim x y rowh' pdims
1688 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
1689 conf.columns <- Some ((columns, coverA, coverB), a);
1691 docolumns conf.columns;
1692 state.maxy <- calcheight ();
1693 state.hscrollh <-
1694 if state.w <= conf.winw - state.scrollw
1695 then 0
1696 else state.scrollw
1698 match state.mode with
1699 | Birdseye (_, _, pageno, _, _) ->
1700 let y, h = getpageyh pageno in
1701 let top = (conf.winh - h) / 2 in
1702 gotoy (max 0 (y - top))
1703 | _ -> gotoanchor state.anchor
1706 let reshape =
1707 let firsttime = ref true in
1708 fun ~w ~h ->
1709 GlDraw.viewport 0 0 w h;
1710 if state.invalidated = 0 && not !firsttime
1711 then state.anchor <- getanchor ();
1713 firsttime := false;
1714 conf.winw <- w;
1715 let w = truncate (float w *. conf.zoom) - state.scrollw in
1716 let w = max w 2 in
1717 state.w <- w;
1718 conf.winh <- h;
1719 setfontsize fstate.fontsize;
1720 GlMat.mode `modelview;
1721 GlMat.load_identity ();
1723 GlMat.mode `projection;
1724 GlMat.load_identity ();
1725 GlMat.rotate ~x:1.0 ~angle:180.0 ();
1726 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
1727 GlMat.scale3 (2.0 /. float conf.winw, 2.0 /. float conf.winh, 1.0);
1729 let w =
1730 match conf.columns with
1731 | None -> w
1732 | Some ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
1734 invalidate ();
1735 wcmd "geometry %d %d" w h;
1738 let enttext () =
1739 let len = String.length state.text in
1740 let drawstring s =
1741 let hscrollh =
1742 match state.mode with
1743 | View -> state.hscrollh
1744 | _ -> 0
1746 let rect x w =
1747 GlDraw.rect
1748 (x, float (conf.winh - (fstate.fontsize + 4) - hscrollh))
1749 (x+.w, float (conf.winh - hscrollh))
1752 let w = float (conf.winw - state.scrollw - 1) in
1753 if state.progress >= 0.0 && state.progress < 1.0
1754 then (
1755 GlDraw.color (0.3, 0.3, 0.3);
1756 let w1 = w *. state.progress in
1757 rect 0.0 w1;
1758 GlDraw.color (0.0, 0.0, 0.0);
1759 rect w1 (w-.w1)
1761 else (
1762 GlDraw.color (0.0, 0.0, 0.0);
1763 rect 0.0 w;
1766 GlDraw.color (1.0, 1.0, 1.0);
1767 drawstring fstate.fontsize
1768 (if len > 0 then 8 else 2) (conf.winh - hscrollh - 5) s;
1770 let s =
1771 match state.mode with
1772 | Textentry ((prefix, text, _, _, _), _) ->
1773 let s =
1774 if len > 0
1775 then
1776 Printf.sprintf "%s%s_ [%s]" prefix text state.text
1777 else
1778 Printf.sprintf "%s%s_" prefix text
1782 | _ -> state.text
1784 let s =
1785 if state.newerrmsgs
1786 then (
1787 if not (istextentry state.mode)
1788 then
1789 let s1 = "(press 'e' to review error messasges)" in
1790 if String.length s > 0 then s ^ " " ^ s1 else s1
1791 else s
1793 else s
1795 if String.length s > 0
1796 then drawstring s
1799 let gctiles () =
1800 let len = Queue.length state.tilelru in
1801 let rec loop qpos =
1802 if state.memused <= conf.memlimit
1803 then ()
1804 else (
1805 if qpos < len
1806 then
1807 let (k, p, s) as lruitem = Queue.pop state.tilelru in
1808 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
1809 let (_, pw, ph, _) = getpagedim n in
1811 gen = state.gen
1812 && colorspace = conf.colorspace
1813 && angle = conf.angle
1814 && pagew = pw
1815 && pageh = ph
1816 && (
1817 let layout =
1818 match state.throttle with
1819 | None ->
1820 if conf.preload
1821 then preloadlayout state.layout
1822 else state.layout
1823 | Some (layout, _, _) ->
1824 layout
1826 let x = col*conf.tilew
1827 and y = row*conf.tileh in
1828 tilevisible layout n x y
1830 then Queue.push lruitem state.tilelru
1831 else (
1832 wcmd "freetile %s" p;
1833 state.memused <- state.memused - s;
1834 state.uioh#infochanged Memused;
1835 Hashtbl.remove state.tilemap k;
1837 loop (qpos+1)
1840 loop 0
1843 let flushtiles () =
1844 Queue.iter (fun (k, p, s) ->
1845 wcmd "freetile %s" p;
1846 state.memused <- state.memused - s;
1847 state.uioh#infochanged Memused;
1848 Hashtbl.remove state.tilemap k;
1849 ) state.tilelru;
1850 Queue.clear state.tilelru;
1851 load state.layout;
1854 let logcurrently = function
1855 | Idle -> dolog "Idle"
1856 | Loading (l, gen) ->
1857 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
1858 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
1859 dolog
1860 "Tiling %d[%d,%d] page=%s cs=%s angle"
1861 l.pageno col row pageopaque
1862 (colorspace_to_string colorspace)
1864 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
1865 angle gen conf.angle state.gen
1866 tilew tileh
1867 conf.tilew conf.tileh
1869 | Outlining _ ->
1870 dolog "outlining"
1873 let act cmds =
1874 (* dolog "%S" cmds; *)
1875 let op, args =
1876 let spacepos =
1877 try String.index cmds ' '
1878 with Not_found -> -1
1880 if spacepos = -1
1881 then cmds, ""
1882 else
1883 let l = String.length cmds in
1884 let op = String.sub cmds 0 spacepos in
1885 op, begin
1886 if l - spacepos < 2 then ""
1887 else String.sub cmds (spacepos+1) (l-spacepos-1)
1890 match op with
1891 | "clear" ->
1892 state.uioh#infochanged Pdim;
1893 state.pdims <- [];
1895 | "clearrects" ->
1896 state.rects <- state.rects1;
1897 G.postRedisplay "clearrects";
1899 | "continue" ->
1900 let n =
1901 try Scanf.sscanf args "%u" (fun n -> n)
1902 with exn ->
1903 dolog "error processing 'continue' %S: %s"
1904 cmds (Printexc.to_string exn);
1905 exit 1;
1907 state.pagecount <- n;
1908 state.invalidated <- state.invalidated - 1;
1909 begin match state.currently with
1910 | Outlining l ->
1911 state.currently <- Idle;
1912 state.outlines <- Array.of_list (List.rev l)
1913 | _ -> ()
1914 end;
1915 if state.invalidated = 0
1916 then represent ();
1917 if conf.maxwait = None
1918 then G.postRedisplay "continue";
1920 | "title" ->
1921 Wsi.settitle args
1923 | "msg" ->
1924 showtext ' ' args
1926 | "vmsg" ->
1927 if conf.verbose
1928 then showtext ' ' args
1930 | "progress" ->
1931 let progress, text =
1933 Scanf.sscanf args "%f %n"
1934 (fun f pos ->
1935 f, String.sub args pos (String.length args - pos))
1936 with exn ->
1937 dolog "error processing 'progress' %S: %s"
1938 cmds (Printexc.to_string exn);
1939 exit 1;
1941 state.text <- text;
1942 state.progress <- progress;
1943 G.postRedisplay "progress"
1945 | "firstmatch" ->
1946 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1948 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
1949 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1950 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1951 with exn ->
1952 dolog "error processing 'firstmatch' %S: %s"
1953 cmds (Printexc.to_string exn);
1954 exit 1;
1956 let y = (getpagey pageno) + truncate y0 in
1957 addnav ();
1958 gotoy y;
1959 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
1961 | "match" ->
1962 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1964 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
1965 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1966 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1967 with exn ->
1968 dolog "error processing 'match' %S: %s"
1969 cmds (Printexc.to_string exn);
1970 exit 1;
1972 state.rects1 <-
1973 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
1975 | "page" ->
1976 let pageopaque, t =
1978 Scanf.sscanf args "%s %f" (fun p t -> p, t)
1979 with exn ->
1980 dolog "error processing 'page' %S: %s"
1981 cmds (Printexc.to_string exn);
1982 exit 1;
1984 begin match state.currently with
1985 | Loading (l, gen) ->
1986 vlog "page %d took %f sec" l.pageno t;
1987 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
1988 begin match state.throttle with
1989 | None ->
1990 let preloadedpages =
1991 if conf.preload
1992 then preloadlayout state.layout
1993 else state.layout
1995 let evict () =
1996 let module IntSet =
1997 Set.Make (struct type t = int let compare = (-) end) in
1998 let set =
1999 List.fold_left (fun s l -> IntSet.add l.pageno s)
2000 IntSet.empty preloadedpages
2002 let evictedpages =
2003 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
2004 if not (IntSet.mem pageno set)
2005 then (
2006 wcmd "freepage %s" opaque;
2007 key :: accu
2009 else accu
2010 ) state.pagemap []
2012 List.iter (Hashtbl.remove state.pagemap) evictedpages;
2014 evict ();
2015 state.currently <- Idle;
2016 if gen = state.gen
2017 then (
2018 tilepage l.pageno pageopaque state.layout;
2019 load state.layout;
2020 load preloadedpages;
2021 if pagevisible state.layout l.pageno
2022 && layoutready state.layout
2023 then G.postRedisplay "page";
2026 | Some (layout, _, _) ->
2027 state.currently <- Idle;
2028 tilepage l.pageno pageopaque layout;
2029 load state.layout
2030 end;
2032 | _ ->
2033 dolog "Inconsistent loading state";
2034 logcurrently state.currently;
2035 exit 1
2038 | "tile" ->
2039 let (x, y, opaque, size, t) =
2041 Scanf.sscanf args "%u %u %s %u %f"
2042 (fun x y p size t -> (x, y, p, size, t))
2043 with exn ->
2044 dolog "error processing 'tile' %S: %s"
2045 cmds (Printexc.to_string exn);
2046 exit 1;
2048 begin match state.currently with
2049 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
2050 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
2052 if tilew != conf.tilew || tileh != conf.tileh
2053 then (
2054 wcmd "freetile %s" opaque;
2055 state.currently <- Idle;
2056 load state.layout;
2058 else (
2059 puttileopaque l col row gen cs angle opaque size t;
2060 state.memused <- state.memused + size;
2061 state.uioh#infochanged Memused;
2062 gctiles ();
2063 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
2064 opaque, size) state.tilelru;
2066 let layout =
2067 match state.throttle with
2068 | None -> state.layout
2069 | Some (layout, _, _) -> layout
2072 state.currently <- Idle;
2073 if gen = state.gen
2074 && conf.colorspace = cs
2075 && conf.angle = angle
2076 && tilevisible layout l.pageno x y
2077 then conttiling l.pageno pageopaque;
2079 begin match state.throttle with
2080 | None ->
2081 preload state.layout;
2082 if gen = state.gen
2083 && conf.colorspace = cs
2084 && conf.angle = angle
2085 && tilevisible state.layout l.pageno x y
2086 then G.postRedisplay "tile nothrottle";
2088 | Some (layout, y, _) ->
2089 let ready = layoutready layout in
2090 if ready
2091 then (
2092 state.y <- y;
2093 state.layout <- layout;
2094 state.throttle <- None;
2095 G.postRedisplay "throttle";
2097 else load layout;
2098 end;
2101 | _ ->
2102 dolog "Inconsistent tiling state";
2103 logcurrently state.currently;
2104 exit 1
2107 | "pdim" ->
2108 let pdim =
2110 Scanf.sscanf args "%u %u %u %u" (fun n w h x -> n, w, h, x)
2111 with exn ->
2112 dolog "error processing 'pdim' %S: %s"
2113 cmds (Printexc.to_string exn);
2114 exit 1;
2116 state.uioh#infochanged Pdim;
2117 state.pdims <- pdim :: state.pdims
2119 | "o" ->
2120 let (l, n, t, h, pos) =
2122 Scanf.sscanf args "%u %u %d %u %n"
2123 (fun l n t h pos -> l, n, t, h, pos)
2124 with exn ->
2125 dolog "error processing 'o' %S: %s"
2126 cmds (Printexc.to_string exn);
2127 exit 1;
2129 let s = String.sub args pos (String.length args - pos) in
2130 let outline = (s, l, (n, float t /. float h)) in
2131 begin match state.currently with
2132 | Outlining outlines ->
2133 state.currently <- Outlining (outline :: outlines)
2134 | Idle ->
2135 state.currently <- Outlining [outline]
2136 | currently ->
2137 dolog "invalid outlining state";
2138 logcurrently currently
2141 | "info" ->
2142 state.docinfo <- (1, args) :: state.docinfo
2144 | "infoend" ->
2145 state.uioh#infochanged Docinfo;
2146 state.docinfo <- List.rev state.docinfo
2148 | _ ->
2149 dolog "unknown cmd `%S'" cmds
2152 let onhist cb =
2153 let rc = cb.rc in
2154 let action = function
2155 | HCprev -> cbget cb ~-1
2156 | HCnext -> cbget cb 1
2157 | HCfirst -> cbget cb ~-(cb.rc)
2158 | HClast -> cbget cb (cb.len - 1 - cb.rc)
2159 and cancel () = cb.rc <- rc
2160 in (action, cancel)
2163 let search pattern forward =
2164 if String.length pattern > 0
2165 then
2166 let pn, py =
2167 match state.layout with
2168 | [] -> 0, 0
2169 | l :: _ ->
2170 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
2172 wcmd "search %d %d %d %d,%s\000"
2173 (btod conf.icase) pn py (btod forward) pattern;
2176 let intentry text key =
2177 let c =
2178 if key >= 32 && key < 127
2179 then Char.chr key
2180 else '\000'
2182 match c with
2183 | '0' .. '9' ->
2184 let text = addchar text c in
2185 TEcont text
2187 | _ ->
2188 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2189 TEcont text
2192 let textentry text key =
2193 if key land 0xff00 = 0xff00
2194 then TEcont text
2195 else TEcont (text ^ Wsi.toutf8 key)
2198 let reqlayout angle proportional =
2199 match state.throttle with
2200 | None ->
2201 if state.invalidated = 0 then state.anchor <- getanchor ();
2202 conf.angle <- angle mod 360;
2203 if conf.angle != 0
2204 then (
2205 match state.mode with
2206 | LinkNav _ -> state.mode <- View
2207 | _ -> ()
2209 conf.proportional <- proportional;
2210 invalidate ();
2211 wcmd "reqlayout %d %d" conf.angle (btod proportional);
2212 | _ -> ()
2215 let settrim trimmargins trimfuzz =
2216 if state.invalidated = 0 then state.anchor <- getanchor ();
2217 conf.trimmargins <- trimmargins;
2218 conf.trimfuzz <- trimfuzz;
2219 let x0, y0, x1, y1 = trimfuzz in
2220 invalidate ();
2221 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1;
2222 Hashtbl.iter (fun _ opaque ->
2223 wcmd "freepage %s" opaque;
2224 ) state.pagemap;
2225 Hashtbl.clear state.pagemap;
2228 let setzoom zoom =
2229 match state.throttle with
2230 | None ->
2231 let zoom = max 0.01 zoom in
2232 if zoom <> conf.zoom
2233 then (
2234 state.prevzoom <- conf.zoom;
2235 let relx =
2236 if zoom <= 1.0
2237 then (state.x <- 0; 0.0)
2238 else float state.x /. float state.w
2240 conf.zoom <- zoom;
2241 reshape conf.winw conf.winh;
2242 if zoom > 1.0
2243 then (
2244 let x = relx *. float state.w in
2245 state.x <- truncate x;
2247 state.text <- Printf.sprintf "zoom is now %-5.1f" (zoom *. 100.0);
2250 | Some (layout, y, started) ->
2251 let time =
2252 match conf.maxwait with
2253 | None -> 0.0
2254 | Some t -> t
2256 let dt = now () -. started in
2257 if dt > time
2258 then (
2259 state.y <- y;
2260 load layout;
2264 let setcolumns columns coverA coverB =
2265 if columns < 2
2266 then (
2267 conf.columns <- None;
2268 state.x <- 0;
2269 setzoom 1.0;
2271 else (
2272 conf.columns <- Some ((columns, coverA, coverB), [||]);
2273 conf.zoom <- 1.0;
2275 reshape conf.winw conf.winh;
2278 let enterbirdseye () =
2279 let zoom = float conf.thumbw /. float conf.winw in
2280 let birdseyepageno =
2281 let cy = conf.winh / 2 in
2282 let fold = function
2283 | [] -> 0
2284 | l :: rest ->
2285 let rec fold best = function
2286 | [] -> best.pageno
2287 | l :: rest ->
2288 let d = cy - (l.pagedispy + l.pagevh/2)
2289 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2290 if abs d < abs dbest
2291 then fold l rest
2292 else best.pageno
2293 in fold l rest
2295 fold state.layout
2297 state.mode <- Birdseye (
2298 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2300 conf.zoom <- zoom;
2301 conf.presentation <- false;
2302 conf.interpagespace <- 10;
2303 conf.hlinks <- false;
2304 state.x <- 0;
2305 state.mstate <- Mnone;
2306 conf.maxwait <- None;
2307 conf.columns <- (
2308 match conf.beyecolumns with
2309 | Some c ->
2310 conf.zoom <- 1.0;
2311 Some ((c, 0, 0), [||])
2312 | None -> None
2314 Wsi.setcursor Wsi.CURSOR_INHERIT;
2315 if conf.verbose
2316 then
2317 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2318 (100.0*.zoom)
2319 else
2320 state.text <- ""
2322 reshape conf.winw conf.winh;
2325 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2326 state.mode <- View;
2327 conf.zoom <- c.zoom;
2328 conf.presentation <- c.presentation;
2329 conf.interpagespace <- c.interpagespace;
2330 conf.maxwait <- c.maxwait;
2331 conf.hlinks <- c.hlinks;
2332 conf.beyecolumns <- (
2333 match conf.columns with
2334 | Some ((c, _, _), _) -> Some c
2335 | None -> None
2337 conf.columns <- (
2338 match c.columns with
2339 | Some (c, _) -> Some (c, [||])
2340 | None -> None
2342 state.x <- leftx;
2343 if conf.verbose
2344 then
2345 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2346 (100.0*.conf.zoom)
2348 reshape conf.winw conf.winh;
2349 state.anchor <- if goback then anchor else (pageno, 0.0);
2352 let togglebirdseye () =
2353 match state.mode with
2354 | Birdseye vals -> leavebirdseye vals true
2355 | View -> enterbirdseye ()
2356 | _ -> ()
2359 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2360 let pageno = max 0 (pageno - incr) in
2361 let rec loop = function
2362 | [] -> gotopage1 pageno 0
2363 | l :: _ when l.pageno = pageno ->
2364 if l.pagedispy >= 0 && l.pagey = 0
2365 then G.postRedisplay "upbirdseye"
2366 else gotopage1 pageno 0
2367 | _ :: rest -> loop rest
2369 loop state.layout;
2370 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2373 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2374 let pageno = min (state.pagecount - 1) (pageno + incr) in
2375 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2376 let rec loop = function
2377 | [] ->
2378 let y, h = getpageyh pageno in
2379 let dy = (y - state.y) - (conf.winh - h - conf.interpagespace) in
2380 gotoy (clamp dy)
2381 | l :: _ when l.pageno = pageno ->
2382 if l.pagevh != l.pageh
2383 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
2384 else G.postRedisplay "downbirdseye"
2385 | _ :: rest -> loop rest
2387 loop state.layout
2390 let optentry mode _ key =
2391 let btos b = if b then "on" else "off" in
2392 if key >= 32 && key < 127
2393 then
2394 let c = Char.chr key in
2395 match c with
2396 | 's' ->
2397 let ondone s =
2398 try conf.scrollstep <- int_of_string s with exc ->
2399 state.text <- Printf.sprintf "bad integer `%s': %s"
2400 s (Printexc.to_string exc)
2402 TEswitch ("scroll step: ", "", None, intentry, ondone)
2404 | 'A' ->
2405 let ondone s =
2407 conf.autoscrollstep <- int_of_string s;
2408 if state.autoscroll <> None
2409 then state.autoscroll <- Some conf.autoscrollstep
2410 with exc ->
2411 state.text <- Printf.sprintf "bad integer `%s': %s"
2412 s (Printexc.to_string exc)
2414 TEswitch ("auto scroll step: ", "", None, intentry, ondone)
2416 | 'C' ->
2417 let ondone s =
2419 let n, a, b = columns_of_string s in
2420 setcolumns n a b;
2421 with exc ->
2422 state.text <- Printf.sprintf "bad columns `%s': %s"
2423 s (Printexc.to_string exc)
2425 TEswitch ("columns: ", "", None, textentry, ondone)
2427 | 'Z' ->
2428 let ondone s =
2430 let zoom = float (int_of_string s) /. 100.0 in
2431 setzoom zoom
2432 with exc ->
2433 state.text <- Printf.sprintf "bad integer `%s': %s"
2434 s (Printexc.to_string exc)
2436 TEswitch ("zoom: ", "", None, intentry, ondone)
2438 | 't' ->
2439 let ondone s =
2441 conf.thumbw <- bound (int_of_string s) 2 4096;
2442 state.text <-
2443 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
2444 begin match mode with
2445 | Birdseye beye ->
2446 leavebirdseye beye false;
2447 enterbirdseye ();
2448 | _ -> ();
2450 with exc ->
2451 state.text <- Printf.sprintf "bad integer `%s': %s"
2452 s (Printexc.to_string exc)
2454 TEswitch ("thumbnail width: ", "", None, intentry, ondone)
2456 | 'R' ->
2457 let ondone s =
2458 match try
2459 Some (int_of_string s)
2460 with exc ->
2461 state.text <- Printf.sprintf "bad integer `%s': %s"
2462 s (Printexc.to_string exc);
2463 None
2464 with
2465 | Some angle -> reqlayout angle conf.proportional
2466 | None -> ()
2468 TEswitch ("rotation: ", "", None, intentry, ondone)
2470 | 'i' ->
2471 conf.icase <- not conf.icase;
2472 TEdone ("case insensitive search " ^ (btos conf.icase))
2474 | 'p' ->
2475 conf.preload <- not conf.preload;
2476 gotoy state.y;
2477 TEdone ("preload " ^ (btos conf.preload))
2479 | 'v' ->
2480 conf.verbose <- not conf.verbose;
2481 TEdone ("verbose " ^ (btos conf.verbose))
2483 | 'd' ->
2484 conf.debug <- not conf.debug;
2485 TEdone ("debug " ^ (btos conf.debug))
2487 | 'h' ->
2488 conf.maxhfit <- not conf.maxhfit;
2489 state.maxy <-
2490 state.maxy + (if conf.maxhfit then -conf.winh else conf.winh);
2491 TEdone ("maxhfit " ^ (btos conf.maxhfit))
2493 | 'c' ->
2494 conf.crophack <- not conf.crophack;
2495 TEdone ("crophack " ^ btos conf.crophack)
2497 | 'a' ->
2498 let s =
2499 match conf.maxwait with
2500 | None ->
2501 conf.maxwait <- Some infinity;
2502 "always wait for page to complete"
2503 | Some _ ->
2504 conf.maxwait <- None;
2505 "show placeholder if page is not ready"
2507 TEdone s
2509 | 'f' ->
2510 conf.underinfo <- not conf.underinfo;
2511 TEdone ("underinfo " ^ btos conf.underinfo)
2513 | 'P' ->
2514 conf.savebmarks <- not conf.savebmarks;
2515 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
2517 | 'S' ->
2518 let ondone s =
2520 let pageno, py =
2521 match state.layout with
2522 | [] -> 0, 0
2523 | l :: _ ->
2524 l.pageno, l.pagey
2526 conf.interpagespace <- int_of_string s;
2527 state.maxy <- calcheight ();
2528 let y = getpagey pageno in
2529 gotoy (y + py)
2530 with exc ->
2531 state.text <- Printf.sprintf "bad integer `%s': %s"
2532 s (Printexc.to_string exc)
2534 TEswitch ("vertical margin: ", "", None, intentry, ondone)
2536 | 'l' ->
2537 reqlayout conf.angle (not conf.proportional);
2538 TEdone ("proportional display " ^ btos conf.proportional)
2540 | 'T' ->
2541 settrim (not conf.trimmargins) conf.trimfuzz;
2542 TEdone ("trim margins " ^ btos conf.trimmargins)
2544 | 'I' ->
2545 conf.invert <- not conf.invert;
2546 TEdone ("invert colors " ^ btos conf.invert)
2548 | 'x' ->
2549 let ondone s =
2550 cbput state.hists.sel s;
2551 conf.selcmd <- s;
2553 TEswitch ("selection command: ", "", Some (onhist state.hists.sel),
2554 textentry, ondone)
2556 | _ ->
2557 state.text <- Printf.sprintf "bad option %d `%c'" key c;
2558 TEstop
2559 else
2560 TEcont state.text
2563 class type lvsource = object
2564 method getitemcount : int
2565 method getitem : int -> (string * int)
2566 method hasaction : int -> bool
2567 method exit :
2568 uioh:uioh ->
2569 cancel:bool ->
2570 active:int ->
2571 first:int ->
2572 pan:int ->
2573 qsearch:string ->
2574 uioh option
2575 method getactive : int
2576 method getfirst : int
2577 method getqsearch : string
2578 method setqsearch : string -> unit
2579 method getpan : int
2580 end;;
2582 class virtual lvsourcebase = object
2583 val mutable m_active = 0
2584 val mutable m_first = 0
2585 val mutable m_qsearch = ""
2586 val mutable m_pan = 0
2587 method getactive = m_active
2588 method getfirst = m_first
2589 method getqsearch = m_qsearch
2590 method getpan = m_pan
2591 method setqsearch s = m_qsearch <- s
2592 end;;
2594 let withoutlastutf8 s =
2595 let len = String.length s in
2596 if len = 0
2597 then s
2598 else
2599 let rec find pos =
2600 if pos = 0
2601 then pos
2602 else
2603 let b = Char.code s.[pos] in
2604 if b land 0b110000 = 0b11000000
2605 then find (pos-1)
2606 else pos-1
2608 let first =
2609 if Char.code s.[len-1] land 0x80 = 0
2610 then len-1
2611 else find (len-1)
2613 String.sub s 0 first;
2616 let textentrykeyboard key _mask ((c, text, opthist, onkey, ondone), onleave) =
2617 let enttext te =
2618 state.mode <- Textentry (te, onleave);
2619 state.text <- "";
2620 enttext ();
2621 G.postRedisplay "textentrykeyboard enttext";
2623 let histaction cmd =
2624 match opthist with
2625 | None -> ()
2626 | Some (action, _) ->
2627 state.mode <- Textentry (
2628 (c, action cmd, opthist, onkey, ondone), onleave
2630 G.postRedisplay "textentry histaction"
2632 match key with
2633 | 0xff08 -> (* backspace *)
2634 let s = withoutlastutf8 text in
2635 let len = String.length s in
2636 if len = 0
2637 then (
2638 onleave Cancel;
2639 G.postRedisplay "textentrykeyboard after cancel";
2641 else (
2642 enttext (c, s, opthist, onkey, ondone)
2645 | 0xff0d ->
2646 ondone text;
2647 onleave Confirm;
2648 G.postRedisplay "textentrykeyboard after confirm"
2650 | 0xff52 -> histaction HCprev
2651 | 0xff54 -> histaction HCnext
2652 | 0xff50 -> histaction HCfirst
2653 | 0xff57 -> histaction HClast
2655 | 0xff1b -> (* escape*)
2656 if String.length text = 0
2657 then (
2658 begin match opthist with
2659 | None -> ()
2660 | Some (_, onhistcancel) -> onhistcancel ()
2661 end;
2662 onleave Cancel;
2663 state.text <- "";
2664 G.postRedisplay "textentrykeyboard after cancel2"
2666 else (
2667 enttext (c, "", opthist, onkey, ondone)
2670 | 0xff9f | 0xffff -> () (* delete *)
2672 | _ when key != 0 && key land 0xff00 != 0xff00 ->
2673 begin match onkey text key with
2674 | TEdone text ->
2675 ondone text;
2676 onleave Confirm;
2677 G.postRedisplay "textentrykeyboard after confirm2";
2679 | TEcont text ->
2680 enttext (c, text, opthist, onkey, ondone);
2682 | TEstop ->
2683 onleave Cancel;
2684 G.postRedisplay "textentrykeyboard after cancel3"
2686 | TEswitch te ->
2687 state.mode <- Textentry (te, onleave);
2688 G.postRedisplay "textentrykeyboard switch";
2689 end;
2691 | _ ->
2692 vlog "unhandled key %s" (Wsi.keyname key)
2695 let firstof first active =
2696 if first > active || abs (first - active) > fstate.maxrows - 1
2697 then max 0 (active - (fstate.maxrows/2))
2698 else first
2701 let calcfirst first active =
2702 if active > first
2703 then
2704 let rows = active - first in
2705 if rows > fstate.maxrows then active - fstate.maxrows else first
2706 else active
2709 let scrollph y maxy =
2710 let sh = (float (maxy + conf.winh) /. float conf.winh) in
2711 let sh = float conf.winh /. sh in
2712 let sh = max sh (float conf.scrollh) in
2714 let percent =
2715 if y = state.maxy
2716 then 1.0
2717 else float y /. float maxy
2719 let position = (float conf.winh -. sh) *. percent in
2721 let position =
2722 if position +. sh > float conf.winh
2723 then float conf.winh -. sh
2724 else position
2726 position, sh;
2729 let coe s = (s :> uioh);;
2731 class listview ~(source:lvsource) ~trusted ~modehash =
2732 object (self)
2733 val m_pan = source#getpan
2734 val m_first = source#getfirst
2735 val m_active = source#getactive
2736 val m_qsearch = source#getqsearch
2737 val m_prev_uioh = state.uioh
2739 method private elemunder y =
2740 let n = y / (fstate.fontsize+1) in
2741 if m_first + n < source#getitemcount
2742 then (
2743 if source#hasaction (m_first + n)
2744 then Some (m_first + n)
2745 else None
2747 else None
2749 method display =
2750 Gl.enable `blend;
2751 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2752 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2753 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
2754 GlDraw.color (1., 1., 1.);
2755 Gl.enable `texture_2d;
2756 let fs = fstate.fontsize in
2757 let nfs = fs + 1 in
2758 let ww = fstate.wwidth in
2759 let tabw = 30.0*.ww in
2760 let itemcount = source#getitemcount in
2761 let rec loop row =
2762 if (row - m_first) * nfs > conf.winh
2763 then ()
2764 else (
2765 if row >= 0 && row < itemcount
2766 then (
2767 let (s, level) = source#getitem row in
2768 let y = (row - m_first) * nfs in
2769 let x = 5.0 +. float (level + m_pan) *. ww in
2770 if row = m_active
2771 then (
2772 Gl.disable `texture_2d;
2773 GlDraw.polygon_mode `both `line;
2774 GlDraw.color (1., 1., 1.) ~alpha:0.9;
2775 GlDraw.rect (1., float (y + 1))
2776 (float (conf.winw - conf.scrollbw - 1), float (y + fs + 3));
2777 GlDraw.polygon_mode `both `fill;
2778 GlDraw.color (1., 1., 1.);
2779 Gl.enable `texture_2d;
2782 let drawtabularstring s =
2783 let drawstr x s = drawstring1 fs (truncate x) (y+nfs) s in
2784 if trusted
2785 then
2786 let tabpos = try String.index s '\t' with Not_found -> -1 in
2787 if tabpos > 0
2788 then
2789 let len = String.length s - tabpos - 1 in
2790 let s1 = String.sub s 0 tabpos
2791 and s2 = String.sub s (tabpos + 1) len in
2792 let nx = drawstr x s1 in
2793 let sw = nx -. x in
2794 let x = x +. (max tabw sw) in
2795 drawstr x s2
2796 else
2797 drawstr x s
2798 else
2799 drawstr x s
2801 let _ = drawtabularstring s in
2802 loop (row+1)
2806 loop m_first;
2807 Gl.disable `blend;
2808 Gl.disable `texture_2d;
2810 method updownlevel incr =
2811 let len = source#getitemcount in
2812 let curlevel =
2813 if m_active >= 0 && m_active < len
2814 then snd (source#getitem m_active)
2815 else -1
2817 let rec flow i =
2818 if i = len then i-1 else if i = -1 then 0 else
2819 let _, l = source#getitem i in
2820 if l != curlevel then i else flow (i+incr)
2822 let active = flow m_active in
2823 let first = calcfirst m_first active in
2824 G.postRedisplay "outline updownlevel";
2825 {< m_active = active; m_first = first >}
2827 method private key1 key mask =
2828 let set1 active first qsearch =
2829 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
2831 let search active pattern incr =
2832 let dosearch re =
2833 let rec loop n =
2834 if n >= 0 && n < source#getitemcount
2835 then (
2836 let s, _ = source#getitem n in
2838 (try ignore (Str.search_forward re s 0); true
2839 with Not_found -> false)
2840 then Some n
2841 else loop (n + incr)
2843 else None
2845 loop active
2848 let re = Str.regexp_case_fold pattern in
2849 dosearch re
2850 with Failure s ->
2851 state.text <- s;
2852 None
2854 let itemcount = source#getitemcount in
2855 let find start incr =
2856 let rec find i =
2857 if i = -1 || i = itemcount
2858 then -1
2859 else (
2860 if source#hasaction i
2861 then i
2862 else find (i + incr)
2865 find start
2867 let set active first =
2868 let first = bound first 0 (itemcount - fstate.maxrows) in
2869 state.text <- "";
2870 coe {< m_active = active; m_first = first >}
2872 let navigate incr =
2873 let isvisible first n = n >= first && n - first <= fstate.maxrows in
2874 let active, first =
2875 let incr1 = if incr > 0 then 1 else -1 in
2876 if isvisible m_first m_active
2877 then
2878 let next =
2879 let next = m_active + incr in
2880 let next =
2881 if next < 0 || next >= itemcount
2882 then -1
2883 else find next incr1
2885 if next = -1 || abs (m_active - next) > fstate.maxrows
2886 then -1
2887 else next
2889 if next = -1
2890 then
2891 let first = m_first + incr in
2892 let first = bound first 0 (itemcount - 1) in
2893 let next =
2894 let next = m_active + incr in
2895 let next = bound next 0 (itemcount - 1) in
2896 find next ~-incr1
2898 let active = if next = -1 then m_active else next in
2899 active, first
2900 else
2901 let first = min next m_first in
2902 let first =
2903 if abs (next - first) > fstate.maxrows
2904 then first + incr
2905 else first
2907 next, first
2908 else
2909 let first = m_first + incr in
2910 let first = bound first 0 (itemcount - 1) in
2911 let active =
2912 let next = m_active + incr in
2913 let next = bound next 0 (itemcount - 1) in
2914 let next = find next incr1 in
2915 let active =
2916 if next = -1 || abs (m_active - first) > fstate.maxrows
2917 then (
2918 let active = if m_active = -1 then next else m_active in
2919 active
2921 else next
2923 if isvisible first active
2924 then active
2925 else -1
2927 active, first
2929 G.postRedisplay "listview navigate";
2930 set active first;
2932 match key with
2933 | (0x72|0x73) when Wsi.withctrl mask -> (* ctrl-r/ctlr-s *)
2934 let incr = if key = 0x72 then -1 else 1 in
2935 let active, first =
2936 match search (m_active + incr) m_qsearch incr with
2937 | None ->
2938 state.text <- m_qsearch ^ " [not found]";
2939 m_active, m_first
2940 | Some active ->
2941 state.text <- m_qsearch;
2942 active, firstof m_first active
2944 G.postRedisplay "listview ctrl-r/s";
2945 set1 active first m_qsearch;
2947 | 0xff08 -> (* backspace *)
2948 if String.length m_qsearch = 0
2949 then coe self
2950 else (
2951 let qsearch = withoutlastutf8 m_qsearch in
2952 let len = String.length qsearch in
2953 if len = 0
2954 then (
2955 state.text <- "";
2956 G.postRedisplay "listview empty qsearch";
2957 set1 m_active m_first "";
2959 else
2960 let active, first =
2961 match search m_active qsearch ~-1 with
2962 | None ->
2963 state.text <- qsearch ^ " [not found]";
2964 m_active, m_first
2965 | Some active ->
2966 state.text <- qsearch;
2967 active, firstof m_first active
2969 G.postRedisplay "listview backspace qsearch";
2970 set1 active first qsearch
2973 | key when (key != 0 && key land 0xff00 != 0xff00) ->
2974 let pattern = m_qsearch ^ Wsi.toutf8 key in
2975 let active, first =
2976 match search m_active pattern 1 with
2977 | None ->
2978 state.text <- pattern ^ " [not found]";
2979 m_active, m_first
2980 | Some active ->
2981 state.text <- pattern;
2982 active, firstof m_first active
2984 G.postRedisplay "listview qsearch add";
2985 set1 active first pattern;
2987 | 0xff1b -> (* escape *)
2988 state.text <- "";
2989 if String.length m_qsearch = 0
2990 then (
2991 G.postRedisplay "list view escape";
2992 begin
2993 match
2994 source#exit (coe self) true m_active m_first m_pan m_qsearch
2995 with
2996 | None -> m_prev_uioh
2997 | Some uioh -> uioh
3000 else (
3001 G.postRedisplay "list view kill qsearch";
3002 source#setqsearch "";
3003 coe {< m_qsearch = "" >}
3006 | 0xff0d -> (* return *)
3007 state.text <- "";
3008 let self = {< m_qsearch = "" >} in
3009 source#setqsearch "";
3010 let opt =
3011 G.postRedisplay "listview enter";
3012 if m_active >= 0 && m_active < source#getitemcount
3013 then (
3014 source#exit (coe self) false m_active m_first m_pan "";
3016 else (
3017 source#exit (coe self) true m_active m_first m_pan "";
3020 begin match opt with
3021 | None -> m_prev_uioh
3022 | Some uioh -> uioh
3025 | 0xff9f | 0xffff -> (* delete *)
3026 coe self
3028 | 0xff52 -> navigate ~-1 (* up *)
3029 | 0xff54 -> navigate 1 (* down *)
3030 | 0xff55 -> navigate ~-(fstate.maxrows) (* prior *)
3031 | 0xff56 -> navigate fstate.maxrows (* next *)
3033 | 0xff53 -> (* right *)
3034 state.text <- "";
3035 G.postRedisplay "listview right";
3036 coe {< m_pan = m_pan - 1 >}
3038 | 0xff51 -> (* left *)
3039 state.text <- "";
3040 G.postRedisplay "listview left";
3041 coe {< m_pan = m_pan + 1 >}
3043 | 0xff50 -> (* home *)
3044 let active = find 0 1 in
3045 G.postRedisplay "listview home";
3046 set active 0;
3048 | 0xff57 -> (* end *)
3049 let first = max 0 (itemcount - fstate.maxrows) in
3050 let active = find (itemcount - 1) ~-1 in
3051 G.postRedisplay "listview end";
3052 set active first;
3054 | key when (key = 0 || key land 0xff00 = 0xff00) ->
3055 coe self
3057 | _ ->
3058 dolog "listview unknown key %#x" key; coe self
3060 method key key mask =
3061 match state.mode with
3062 | Textentry te -> textentrykeyboard key mask te; coe self
3063 | _ -> self#key1 key mask
3065 method button button down x y _ =
3066 let opt =
3067 match button with
3068 | 1 when x > conf.winw - conf.scrollbw ->
3069 G.postRedisplay "listview scroll";
3070 if down
3071 then
3072 let _, position, sh = self#scrollph in
3073 if y > truncate position && y < truncate (position +. sh)
3074 then (
3075 state.mstate <- Mscrolly;
3076 Some (coe self)
3078 else
3079 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
3080 let first = truncate (s *. float source#getitemcount) in
3081 let first = min source#getitemcount first in
3082 Some (coe {< m_first = first; m_active = first >})
3083 else (
3084 state.mstate <- Mnone;
3085 Some (coe self);
3087 | 1 when not down ->
3088 begin match self#elemunder y with
3089 | Some n ->
3090 G.postRedisplay "listview click";
3091 source#exit
3092 (coe {< m_active = n >}) false n m_first m_pan m_qsearch
3093 | _ ->
3094 Some (coe self)
3096 | n when (n == 4 || n == 5) && not down ->
3097 let len = source#getitemcount in
3098 let first =
3099 if n = 5 && m_first + fstate.maxrows >= len
3100 then
3101 m_first
3102 else
3103 let first = m_first + (if n == 4 then -1 else 1) in
3104 bound first 0 (len - 1)
3106 G.postRedisplay "listview wheel";
3107 Some (coe {< m_first = first >})
3108 | _ ->
3109 Some (coe self)
3111 match opt with
3112 | None -> m_prev_uioh
3113 | Some uioh -> uioh
3115 method motion _ y =
3116 match state.mstate with
3117 | Mscrolly ->
3118 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
3119 let first = truncate (s *. float source#getitemcount) in
3120 let first = min source#getitemcount first in
3121 G.postRedisplay "listview motion";
3122 coe {< m_first = first; m_active = first >}
3123 | _ -> coe self
3125 method pmotion x y =
3126 if x < conf.winw - conf.scrollbw
3127 then
3128 let n =
3129 match self#elemunder y with
3130 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3131 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3133 let o =
3134 if n != m_active
3135 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3136 else self
3138 coe o
3139 else (
3140 Wsi.setcursor Wsi.CURSOR_INHERIT;
3141 coe self
3144 method infochanged _ = ()
3146 method scrollpw = (0, 0.0, 0.0)
3147 method scrollph =
3148 let nfs = fstate.fontsize + 1 in
3149 let y = m_first * nfs in
3150 let itemcount = source#getitemcount in
3151 let maxi = max 0 (itemcount - fstate.maxrows) in
3152 let maxy = maxi * nfs in
3153 let p, h = scrollph y maxy in
3154 conf.scrollbw, p, h
3156 method modehash = modehash
3157 end;;
3159 class outlinelistview ~source =
3160 object (self)
3161 inherit listview
3162 ~source:(source :> lvsource)
3163 ~trusted:false
3164 ~modehash:(findkeyhash conf "outline")
3165 as super
3167 method key key mask =
3168 let calcfirst first active =
3169 if active > first
3170 then
3171 let rows = active - first in
3172 if rows > fstate.maxrows then active - fstate.maxrows else first
3173 else active
3175 let navigate incr =
3176 let active = m_active + incr in
3177 let active = bound active 0 (source#getitemcount - 1) in
3178 let first = calcfirst m_first active in
3179 G.postRedisplay "outline navigate";
3180 coe {< m_active = active; m_first = first >}
3182 let ctrl = Wsi.withctrl mask in
3183 match key with
3184 | 110 when ctrl -> (* ctrl-n *)
3185 source#narrow m_qsearch;
3186 G.postRedisplay "outline ctrl-n";
3187 coe {< m_first = 0; m_active = 0 >}
3189 | 117 when ctrl -> (* ctrl-u *)
3190 source#denarrow;
3191 G.postRedisplay "outline ctrl-u";
3192 state.text <- "";
3193 coe {< m_first = 0; m_active = 0 >}
3195 | 108 when ctrl -> (* ctrl-l *)
3196 let first = m_active - (fstate.maxrows / 2) in
3197 G.postRedisplay "outline ctrl-l";
3198 coe {< m_first = first >}
3200 | 0xff9f | 0xffff -> (* delete *)
3201 source#remove m_active;
3202 G.postRedisplay "outline delete";
3203 let active = max 0 (m_active-1) in
3204 coe {< m_first = firstof m_first active;
3205 m_active = active >}
3207 | 0xff52 -> navigate ~-1 (* up *)
3208 | 0xff54 -> navigate 1 (* down *)
3209 | 0xff55 -> (* prior *)
3210 navigate ~-(fstate.maxrows)
3211 | 0xff56 -> (* next *)
3212 navigate fstate.maxrows
3214 | 0xff53 -> (* [ctrl-]right *)
3215 let o =
3216 if ctrl
3217 then (
3218 G.postRedisplay "outline ctrl right";
3219 {< m_pan = m_pan + 1 >}
3221 else self#updownlevel 1
3223 coe o
3225 | 0xff51 -> (* [ctrl-]left *)
3226 let o =
3227 if ctrl
3228 then (
3229 G.postRedisplay "outline ctrl left";
3230 {< m_pan = m_pan - 1 >}
3232 else self#updownlevel ~-1
3234 coe o
3236 | 0xff50 -> (* home *)
3237 G.postRedisplay "outline home";
3238 coe {< m_first = 0; m_active = 0 >}
3240 | 0xff57 -> (* end *)
3241 let active = source#getitemcount - 1 in
3242 let first = max 0 (active - fstate.maxrows) in
3243 G.postRedisplay "outline end";
3244 coe {< m_active = active; m_first = first >}
3246 | _ -> super#key key mask
3249 let outlinesource usebookmarks =
3250 let empty = [||] in
3251 (object
3252 inherit lvsourcebase
3253 val mutable m_items = empty
3254 val mutable m_orig_items = empty
3255 val mutable m_prev_items = empty
3256 val mutable m_narrow_pattern = ""
3257 val mutable m_hadremovals = false
3259 method getitemcount =
3260 Array.length m_items + (if m_hadremovals then 1 else 0)
3262 method getitem n =
3263 if n == Array.length m_items && m_hadremovals
3264 then
3265 ("[Confirm removal]", 0)
3266 else
3267 let s, n, _ = m_items.(n) in
3268 (s, n)
3270 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3271 ignore (uioh, first, qsearch);
3272 let confrimremoval = m_hadremovals && active = Array.length m_items in
3273 let items =
3274 if String.length m_narrow_pattern = 0
3275 then m_orig_items
3276 else m_items
3278 if not cancel
3279 then (
3280 if not confrimremoval
3281 then(
3282 let _, _, anchor = m_items.(active) in
3283 gotoanchor anchor;
3284 m_items <- items;
3286 else (
3287 state.bookmarks <- Array.to_list m_items;
3288 m_orig_items <- m_items;
3291 else m_items <- items;
3292 m_pan <- pan;
3293 None
3295 method hasaction _ = true
3297 method greetmsg =
3298 if Array.length m_items != Array.length m_orig_items
3299 then "Narrowed to " ^ m_narrow_pattern ^ " (ctrl-u to restore)"
3300 else ""
3302 method narrow pattern =
3303 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
3304 match reopt with
3305 | None -> ()
3306 | Some re ->
3307 let rec loop accu n =
3308 if n = -1
3309 then (
3310 m_narrow_pattern <- pattern;
3311 m_items <- Array.of_list accu
3313 else
3314 let (s, _, _) as o = m_items.(n) in
3315 let accu =
3316 if (try ignore (Str.search_forward re s 0); true
3317 with Not_found -> false)
3318 then o :: accu
3319 else accu
3321 loop accu (n-1)
3323 loop [] (Array.length m_items - 1)
3325 method denarrow =
3326 m_orig_items <- (
3327 if usebookmarks
3328 then Array.of_list state.bookmarks
3329 else state.outlines
3331 m_items <- m_orig_items
3333 method remove m =
3334 if usebookmarks
3335 then
3336 if m >= 0 && m < Array.length m_items
3337 then (
3338 m_hadremovals <- true;
3339 m_items <- Array.init (Array.length m_items - 1) (fun n ->
3340 let n = if n >= m then n+1 else n in
3341 m_items.(n)
3345 method reset anchor items =
3346 m_hadremovals <- false;
3347 if m_orig_items == empty || m_prev_items != items
3348 then (
3349 m_orig_items <- items;
3350 if String.length m_narrow_pattern = 0
3351 then m_items <- items;
3353 m_prev_items <- items;
3354 let rely = getanchory anchor in
3355 let active =
3356 let rec loop n best bestd =
3357 if n = Array.length m_items
3358 then best
3359 else
3360 let (_, _, anchor) = m_items.(n) in
3361 let orely = getanchory anchor in
3362 let d = abs (orely - rely) in
3363 if d < bestd
3364 then loop (n+1) n d
3365 else loop (n+1) best bestd
3367 loop 0 ~-1 max_int
3369 m_active <- active;
3370 m_first <- firstof m_first active
3371 end)
3374 let enterselector usebookmarks =
3375 let source = outlinesource usebookmarks in
3376 fun errmsg ->
3377 let outlines =
3378 if usebookmarks
3379 then Array.of_list state.bookmarks
3380 else state.outlines
3382 if Array.length outlines = 0
3383 then (
3384 showtext ' ' errmsg;
3386 else (
3387 state.text <- source#greetmsg;
3388 Wsi.setcursor Wsi.CURSOR_INHERIT;
3389 let anchor = getanchor () in
3390 source#reset anchor outlines;
3391 state.uioh <- coe (new outlinelistview ~source);
3392 G.postRedisplay "enter selector";
3396 let enteroutlinemode =
3397 let f = enterselector false in
3398 fun ()-> f "Document has no outline";
3401 let enterbookmarkmode =
3402 let f = enterselector true in
3403 fun () -> f "Document has no bookmarks (yet)";
3406 let color_of_string s =
3407 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
3408 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
3412 let color_to_string (r, g, b) =
3413 let r = truncate (r *. 256.0)
3414 and g = truncate (g *. 256.0)
3415 and b = truncate (b *. 256.0) in
3416 Printf.sprintf "%d/%d/%d" r g b
3419 let irect_of_string s =
3420 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
3423 let irect_to_string (x0,y0,x1,y1) =
3424 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
3427 let makecheckers () =
3428 (* Appropriated from lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
3429 following to say:
3430 converted by Issac Trotts. July 25, 2002 *)
3431 let image_height = 64
3432 and image_width = 64 in
3434 let make_image () =
3435 let image =
3436 GlPix.create `ubyte ~format:`rgb ~width:image_width ~height:image_height
3438 for i = 0 to image_width - 1 do
3439 for j = 0 to image_height - 1 do
3440 Raw.sets (GlPix.to_raw image) ~pos:(3*(i*image_height+j))
3441 (if (i land 8 ) lxor (j land 8) = 0
3442 then [|255;255;255|] else [|200;200;200|])
3443 done
3444 done;
3445 image
3447 let image = make_image () in
3448 let id = GlTex.gen_texture () in
3449 GlTex.bind_texture `texture_2d id;
3450 GlPix.store (`unpack_alignment 1);
3451 GlTex.image2d image;
3452 List.iter (GlTex.parameter ~target:`texture_2d)
3453 [ `wrap_s `repeat;
3454 `wrap_t `repeat;
3455 `mag_filter `nearest;
3456 `min_filter `nearest ];
3460 let setcheckers enabled =
3461 match state.texid with
3462 | None ->
3463 if enabled then state.texid <- Some (makecheckers ())
3465 | Some texid ->
3466 if not enabled
3467 then (
3468 GlTex.delete_texture texid;
3469 state.texid <- None;
3473 let int_of_string_with_suffix s =
3474 let l = String.length s in
3475 let s1, shift =
3476 if l > 1
3477 then
3478 let suffix = Char.lowercase s.[l-1] in
3479 match suffix with
3480 | 'k' -> String.sub s 0 (l-1), 10
3481 | 'm' -> String.sub s 0 (l-1), 20
3482 | 'g' -> String.sub s 0 (l-1), 30
3483 | _ -> s, 0
3484 else s, 0
3486 let n = int_of_string s1 in
3487 let m = n lsl shift in
3488 if m < 0 || m < n
3489 then raise (Failure "value too large")
3490 else m
3493 let string_with_suffix_of_int n =
3494 if n = 0
3495 then "0"
3496 else
3497 let n, s =
3498 if n = 0
3499 then 0, ""
3500 else (
3501 if n land ((1 lsl 20) - 1) = 0
3502 then n lsr 20, "M"
3503 else (
3504 if n land ((1 lsl 10) - 1) = 0
3505 then n lsr 10, "K"
3506 else n, ""
3510 let rec loop s n =
3511 let h = n mod 1000 in
3512 let n = n / 1000 in
3513 if n = 0
3514 then string_of_int h ^ s
3515 else (
3516 let s = Printf.sprintf "_%03d%s" h s in
3517 loop s n
3520 loop "" n ^ s;
3523 let defghyllscroll = (40, 8, 32);;
3524 let ghyllscroll_of_string s =
3525 let (n, a, b) as nab =
3526 if s = "default"
3527 then defghyllscroll
3528 else Scanf.sscanf s "%u,%u,%u" (fun n a b -> n, a, b)
3530 if n <= a || n <= b || a >= b
3531 then failwith "invalid ghyll N,A,B (N <= A, A < B, N <= B)";
3532 nab;
3535 let ghyllscroll_to_string ((n, a, b) as nab) =
3536 if nab = defghyllscroll
3537 then "default"
3538 else Printf.sprintf "%d,%d,%d" n a b;
3541 let describe_location () =
3542 let f (fn, _) l =
3543 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
3545 let fn, ln = List.fold_left f (-1, -1) state.layout in
3546 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
3547 let percent =
3548 if maxy <= 0
3549 then 100.
3550 else (100. *. (float state.y /. float maxy))
3552 if fn = ln
3553 then
3554 Printf.sprintf "page %d of %d [%.2f%%]"
3555 (fn+1) state.pagecount percent
3556 else
3557 Printf.sprintf
3558 "pages %d-%d of %d [%.2f%%]"
3559 (fn+1) (ln+1) state.pagecount percent
3562 let enterinfomode =
3563 let btos b = if b then "\xe2\x88\x9a" else "" in
3564 let showextended = ref false in
3565 let leave mode = function
3566 | Confirm -> state.mode <- mode
3567 | Cancel -> state.mode <- mode in
3568 let src =
3569 (object
3570 val mutable m_first_time = true
3571 val mutable m_l = []
3572 val mutable m_a = [||]
3573 val mutable m_prev_uioh = nouioh
3574 val mutable m_prev_mode = View
3576 inherit lvsourcebase
3578 method reset prev_mode prev_uioh =
3579 m_a <- Array.of_list (List.rev m_l);
3580 m_l <- [];
3581 m_prev_mode <- prev_mode;
3582 m_prev_uioh <- prev_uioh;
3583 if m_first_time
3584 then (
3585 let rec loop n =
3586 if n >= Array.length m_a
3587 then ()
3588 else
3589 match m_a.(n) with
3590 | _, _, _, Action _ -> m_active <- n
3591 | _ -> loop (n+1)
3593 loop 0;
3594 m_first_time <- false;
3597 method int name get set =
3598 m_l <-
3599 (name, `int get, 1, Action (
3600 fun u ->
3601 let ondone s =
3602 try set (int_of_string s)
3603 with exn ->
3604 state.text <- Printf.sprintf "bad integer `%s': %s"
3605 s (Printexc.to_string exn)
3607 state.text <- "";
3608 let te = name ^ ": ", "", None, intentry, ondone in
3609 state.mode <- Textentry (te, leave m_prev_mode);
3611 )) :: m_l
3613 method int_with_suffix name get set =
3614 m_l <-
3615 (name, `intws get, 1, Action (
3616 fun u ->
3617 let ondone s =
3618 try set (int_of_string_with_suffix s)
3619 with exn ->
3620 state.text <- Printf.sprintf "bad integer `%s': %s"
3621 s (Printexc.to_string exn)
3623 state.text <- "";
3624 let te =
3625 name ^ ": ", "", None, intentry_with_suffix, ondone
3627 state.mode <- Textentry (te, leave m_prev_mode);
3629 )) :: m_l
3631 method bool ?(offset=1) ?(btos=btos) name get set =
3632 m_l <-
3633 (name, `bool (btos, get), offset, Action (
3634 fun u ->
3635 let v = get () in
3636 set (not v);
3638 )) :: m_l
3640 method color name get set =
3641 m_l <-
3642 (name, `color get, 1, Action (
3643 fun u ->
3644 let invalid = (nan, nan, nan) in
3645 let ondone s =
3646 let c =
3647 try color_of_string s
3648 with exn ->
3649 state.text <- Printf.sprintf "bad color `%s': %s"
3650 s (Printexc.to_string exn);
3651 invalid
3653 if c <> invalid
3654 then set c;
3656 let te = name ^ ": ", "", None, textentry, ondone in
3657 state.text <- color_to_string (get ());
3658 state.mode <- Textentry (te, leave m_prev_mode);
3660 )) :: m_l
3662 method string name get set =
3663 m_l <-
3664 (name, `string get, 1, Action (
3665 fun u ->
3666 let ondone s = set s in
3667 let te = name ^ ": ", "", None, textentry, ondone in
3668 state.mode <- Textentry (te, leave m_prev_mode);
3670 )) :: m_l
3672 method colorspace name get set =
3673 m_l <-
3674 (name, `string get, 1, Action (
3675 fun _ ->
3676 let source =
3677 let vals = [| "rgb"; "bgr"; "gray" |] in
3678 (object
3679 inherit lvsourcebase
3681 initializer
3682 m_active <- int_of_colorspace conf.colorspace;
3683 m_first <- 0;
3685 method getitemcount = Array.length vals
3686 method getitem n = (vals.(n), 0)
3687 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3688 ignore (uioh, first, pan, qsearch);
3689 if not cancel then set active;
3690 None
3691 method hasaction _ = true
3692 end)
3694 state.text <- "";
3695 let modehash = findkeyhash conf "info" in
3696 coe (new listview ~source ~trusted:true ~modehash)
3697 )) :: m_l
3699 method caption s offset =
3700 m_l <- (s, `empty, offset, Noaction) :: m_l
3702 method caption2 s f offset =
3703 m_l <- (s, `string f, offset, Noaction) :: m_l
3705 method getitemcount = Array.length m_a
3707 method getitem n =
3708 let tostr = function
3709 | `int f -> string_of_int (f ())
3710 | `intws f -> string_with_suffix_of_int (f ())
3711 | `string f -> f ()
3712 | `color f -> color_to_string (f ())
3713 | `bool (btos, f) -> btos (f ())
3714 | `empty -> ""
3716 let name, t, offset, _ = m_a.(n) in
3717 ((let s = tostr t in
3718 if String.length s > 0
3719 then Printf.sprintf "%s\t%s" name s
3720 else name),
3721 offset)
3723 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3724 let uiohopt =
3725 if not cancel
3726 then (
3727 m_qsearch <- qsearch;
3728 let uioh =
3729 match m_a.(active) with
3730 | _, _, _, Action f -> f uioh
3731 | _ -> uioh
3733 Some uioh
3735 else None
3737 m_active <- active;
3738 m_first <- first;
3739 m_pan <- pan;
3740 uiohopt
3742 method hasaction n =
3743 match m_a.(n) with
3744 | _, _, _, Action _ -> true
3745 | _ -> false
3746 end)
3748 let rec fillsrc prevmode prevuioh =
3749 let sep () = src#caption "" 0 in
3750 let colorp name get set =
3751 src#string name
3752 (fun () -> color_to_string (get ()))
3753 (fun v ->
3755 let c = color_of_string v in
3756 set c
3757 with exn ->
3758 state.text <- Printf.sprintf "bad color `%s': %s"
3759 v (Printexc.to_string exn);
3762 let oldmode = state.mode in
3763 let birdseye = isbirdseye state.mode in
3765 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
3767 src#bool "presentation mode"
3768 (fun () -> conf.presentation)
3769 (fun v ->
3770 conf.presentation <- v;
3771 state.anchor <- getanchor ();
3772 represent ());
3774 src#bool "ignore case in searches"
3775 (fun () -> conf.icase)
3776 (fun v -> conf.icase <- v);
3778 src#bool "preload"
3779 (fun () -> conf.preload)
3780 (fun v -> conf.preload <- v);
3782 src#bool "highlight links"
3783 (fun () -> conf.hlinks)
3784 (fun v -> conf.hlinks <- v);
3786 src#bool "under info"
3787 (fun () -> conf.underinfo)
3788 (fun v -> conf.underinfo <- v);
3790 src#bool "persistent bookmarks"
3791 (fun () -> conf.savebmarks)
3792 (fun v -> conf.savebmarks <- v);
3794 src#bool "proportional display"
3795 (fun () -> conf.proportional)
3796 (fun v -> reqlayout conf.angle v);
3798 src#bool "trim margins"
3799 (fun () -> conf.trimmargins)
3800 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
3802 src#bool "persistent location"
3803 (fun () -> conf.jumpback)
3804 (fun v -> conf.jumpback <- v);
3806 sep ();
3807 src#int "inter-page space"
3808 (fun () -> conf.interpagespace)
3809 (fun n ->
3810 conf.interpagespace <- n;
3811 let pageno, py =
3812 match state.layout with
3813 | [] -> 0, 0
3814 | l :: _ ->
3815 l.pageno, l.pagey
3817 state.maxy <- calcheight ();
3818 let y = getpagey pageno in
3819 gotoy (y + py)
3822 src#int "page bias"
3823 (fun () -> conf.pagebias)
3824 (fun v -> conf.pagebias <- v);
3826 src#int "scroll step"
3827 (fun () -> conf.scrollstep)
3828 (fun n -> conf.scrollstep <- n);
3830 src#int "auto scroll step"
3831 (fun () ->
3832 match state.autoscroll with
3833 | Some step -> step
3834 | _ -> conf.autoscrollstep)
3835 (fun n ->
3836 if state.autoscroll <> None
3837 then state.autoscroll <- Some n;
3838 conf.autoscrollstep <- n);
3840 src#int "zoom"
3841 (fun () -> truncate (conf.zoom *. 100.))
3842 (fun v -> setzoom ((float v) /. 100.));
3844 src#int "rotation"
3845 (fun () -> conf.angle)
3846 (fun v -> reqlayout v conf.proportional);
3848 src#int "scroll bar width"
3849 (fun () -> state.scrollw)
3850 (fun v ->
3851 state.scrollw <- v;
3852 conf.scrollbw <- v;
3853 reshape conf.winw conf.winh;
3856 src#int "scroll handle height"
3857 (fun () -> conf.scrollh)
3858 (fun v -> conf.scrollh <- v;);
3860 src#int "thumbnail width"
3861 (fun () -> conf.thumbw)
3862 (fun v ->
3863 conf.thumbw <- min 4096 v;
3864 match oldmode with
3865 | Birdseye beye ->
3866 leavebirdseye beye false;
3867 enterbirdseye ()
3868 | _ -> ()
3871 src#string "columns"
3872 (fun () ->
3873 match conf.columns with
3874 | None -> "1"
3875 | Some (multicol, _) -> columns_to_string multicol)
3876 (fun v ->
3877 let n, a, b = columns_of_string v in
3878 setcolumns n a b);
3880 sep ();
3881 src#caption "Presentation mode" 0;
3882 src#bool "scrollbar visible"
3883 (fun () -> conf.scrollbarinpm)
3884 (fun v ->
3885 if v != conf.scrollbarinpm
3886 then (
3887 conf.scrollbarinpm <- v;
3888 if conf.presentation
3889 then (
3890 state.scrollw <- if v then conf.scrollbw else 0;
3891 reshape conf.winw conf.winh;
3896 sep ();
3897 src#caption "Pixmap cache" 0;
3898 src#int_with_suffix "size (advisory)"
3899 (fun () -> conf.memlimit)
3900 (fun v -> conf.memlimit <- v);
3902 src#caption2 "used"
3903 (fun () -> Printf.sprintf "%s bytes, %d tiles"
3904 (string_with_suffix_of_int state.memused)
3905 (Hashtbl.length state.tilemap)) 1;
3907 sep ();
3908 src#caption "Layout" 0;
3909 src#caption2 "Dimension"
3910 (fun () ->
3911 Printf.sprintf "%dx%d (virtual %dx%d)"
3912 conf.winw conf.winh
3913 state.w state.maxy)
3915 if conf.debug
3916 then
3917 src#caption2 "Position" (fun () ->
3918 Printf.sprintf "%dx%d" state.x state.y
3920 else
3921 src#caption2 "Visible" (fun () -> describe_location ()) 1
3924 sep ();
3925 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
3926 "Save these parameters as global defaults at exit"
3927 (fun () -> conf.bedefault)
3928 (fun v -> conf.bedefault <- v)
3931 sep ();
3932 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
3933 src#bool ~offset:0 ~btos "Extended parameters"
3934 (fun () -> !showextended)
3935 (fun v -> showextended := v; fillsrc prevmode prevuioh);
3936 if !showextended
3937 then (
3938 src#bool "checkers"
3939 (fun () -> conf.checkers)
3940 (fun v -> conf.checkers <- v; setcheckers v);
3941 src#bool "update cursor"
3942 (fun () -> conf.updatecurs)
3943 (fun v -> conf.updatecurs <- v);
3944 src#bool "verbose"
3945 (fun () -> conf.verbose)
3946 (fun v -> conf.verbose <- v);
3947 src#bool "invert colors"
3948 (fun () -> conf.invert)
3949 (fun v -> conf.invert <- v);
3950 src#bool "max fit"
3951 (fun () -> conf.maxhfit)
3952 (fun v -> conf.maxhfit <- v);
3953 src#bool "redirect stderr"
3954 (fun () -> conf.redirectstderr)
3955 (fun v -> conf.redirectstderr <- v; redirectstderr ());
3956 src#string "uri launcher"
3957 (fun () -> conf.urilauncher)
3958 (fun v -> conf.urilauncher <- v);
3959 src#string "path launcher"
3960 (fun () -> conf.pathlauncher)
3961 (fun v -> conf.pathlauncher <- v);
3962 src#string "tile size"
3963 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
3964 (fun v ->
3966 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
3967 conf.tileh <- max 64 w;
3968 conf.tilew <- max 64 h;
3969 flushtiles ();
3970 with exn ->
3971 state.text <- Printf.sprintf "bad tile size `%s': %s"
3972 v (Printexc.to_string exn));
3973 src#int "texture count"
3974 (fun () -> conf.texcount)
3975 (fun v ->
3976 if realloctexts v
3977 then conf.texcount <- v
3978 else showtext '!' " Failed to set texture count please retry later"
3980 src#int "slice height"
3981 (fun () -> conf.sliceheight)
3982 (fun v ->
3983 conf.sliceheight <- v;
3984 wcmd "sliceh %d" conf.sliceheight;
3986 src#int "anti-aliasing level"
3987 (fun () -> conf.aalevel)
3988 (fun v ->
3989 conf.aalevel <- bound v 0 8;
3990 state.anchor <- getanchor ();
3991 opendoc state.path state.password;
3993 src#int "ui font size"
3994 (fun () -> fstate.fontsize)
3995 (fun v -> setfontsize (bound v 5 100));
3996 colorp "background color"
3997 (fun () -> conf.bgcolor)
3998 (fun v -> conf.bgcolor <- v);
3999 src#bool "crop hack"
4000 (fun () -> conf.crophack)
4001 (fun v -> conf.crophack <- v);
4002 src#string "trim fuzz"
4003 (fun () -> irect_to_string conf.trimfuzz)
4004 (fun v ->
4006 conf.trimfuzz <- irect_of_string v;
4007 if conf.trimmargins
4008 then settrim true conf.trimfuzz;
4009 with exn ->
4010 state.text <- Printf.sprintf "bad irect `%s': %s"
4011 v (Printexc.to_string exn)
4013 src#string "throttle"
4014 (fun () ->
4015 match conf.maxwait with
4016 | None -> "show place holder if page is not ready"
4017 | Some time ->
4018 if time = infinity
4019 then "wait for page to fully render"
4020 else
4021 "wait " ^ string_of_float time
4022 ^ " seconds before showing placeholder"
4024 (fun v ->
4026 let f = float_of_string v in
4027 if f <= 0.0
4028 then conf.maxwait <- None
4029 else conf.maxwait <- Some f
4030 with exn ->
4031 state.text <- Printf.sprintf "bad time `%s': %s"
4032 v (Printexc.to_string exn)
4034 src#string "ghyll scroll"
4035 (fun () ->
4036 match conf.ghyllscroll with
4037 | None -> ""
4038 | Some nab -> ghyllscroll_to_string nab
4040 (fun v ->
4042 let gs =
4043 if String.length v = 0
4044 then None
4045 else Some (ghyllscroll_of_string v)
4047 conf.ghyllscroll <- gs
4048 with exn ->
4049 state.text <- Printf.sprintf "bad ghyll `%s': %s"
4050 v (Printexc.to_string exn)
4052 src#string "selection command"
4053 (fun () -> conf.selcmd)
4054 (fun v -> conf.selcmd <- v);
4055 src#colorspace "color space"
4056 (fun () -> colorspace_to_string conf.colorspace)
4057 (fun v ->
4058 conf.colorspace <- colorspace_of_int v;
4059 wcmd "cs %d" v;
4060 load state.layout;
4064 sep ();
4065 src#caption "Document" 0;
4066 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
4067 src#caption2 "Pages"
4068 (fun () -> string_of_int state.pagecount) 1;
4069 src#caption2 "Dimensions"
4070 (fun () -> string_of_int (List.length state.pdims)) 1;
4071 if conf.trimmargins
4072 then (
4073 sep ();
4074 src#caption "Trimmed margins" 0;
4075 src#caption2 "Dimensions"
4076 (fun () -> string_of_int (List.length state.pdims)) 1;
4079 src#reset prevmode prevuioh;
4081 fun () ->
4082 state.text <- "";
4083 let prevmode = state.mode
4084 and prevuioh = state.uioh in
4085 fillsrc prevmode prevuioh;
4086 let source = (src :> lvsource) in
4087 let modehash = findkeyhash conf "info" in
4088 state.uioh <- coe (object (self)
4089 inherit listview ~source ~trusted:true ~modehash as super
4090 val mutable m_prevmemused = 0
4091 method infochanged = function
4092 | Memused ->
4093 if m_prevmemused != state.memused
4094 then (
4095 m_prevmemused <- state.memused;
4096 G.postRedisplay "memusedchanged";
4098 | Pdim -> G.postRedisplay "pdimchanged"
4099 | Docinfo -> fillsrc prevmode prevuioh
4101 method key key mask =
4102 if not (Wsi.withctrl mask)
4103 then
4104 match key with
4105 | 0xff51 -> coe (self#updownlevel ~-1)
4106 | 0xff53 -> coe (self#updownlevel 1)
4107 | _ -> super#key key mask
4108 else super#key key mask
4109 end);
4110 G.postRedisplay "info";
4113 let enterhelpmode =
4114 let source =
4115 (object
4116 inherit lvsourcebase
4117 method getitemcount = Array.length state.help
4118 method getitem n =
4119 let s, n, _ = state.help.(n) in
4120 (s, n)
4122 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4123 let optuioh =
4124 if not cancel
4125 then (
4126 m_qsearch <- qsearch;
4127 match state.help.(active) with
4128 | _, _, Action f -> Some (f uioh)
4129 | _ -> Some (uioh)
4131 else None
4133 m_active <- active;
4134 m_first <- first;
4135 m_pan <- pan;
4136 optuioh
4138 method hasaction n =
4139 match state.help.(n) with
4140 | _, _, Action _ -> true
4141 | _ -> false
4143 initializer
4144 m_active <- -1
4145 end)
4146 in fun () ->
4147 let modehash = findkeyhash conf "help" in
4148 state.uioh <- coe (new listview ~source ~trusted:true ~modehash);
4149 G.postRedisplay "help";
4152 let entermsgsmode =
4153 let msgsource =
4154 let re = Str.regexp "[\r\n]" in
4155 (object
4156 inherit lvsourcebase
4157 val mutable m_items = [||]
4159 method getitemcount = 1 + Array.length m_items
4161 method getitem n =
4162 if n = 0
4163 then "[Clear]", 0
4164 else m_items.(n-1), 0
4166 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4167 ignore uioh;
4168 if not cancel
4169 then (
4170 if active = 0
4171 then Buffer.clear state.errmsgs;
4172 m_qsearch <- qsearch;
4174 m_active <- active;
4175 m_first <- first;
4176 m_pan <- pan;
4177 None
4179 method hasaction n =
4180 n = 0
4182 method reset =
4183 state.newerrmsgs <- false;
4184 let l = Str.split re (Buffer.contents state.errmsgs) in
4185 m_items <- Array.of_list l
4187 initializer
4188 m_active <- 0
4189 end)
4190 in fun () ->
4191 state.text <- "";
4192 msgsource#reset;
4193 let source = (msgsource :> lvsource) in
4194 let modehash = findkeyhash conf "listview" in
4195 state.uioh <- coe (object
4196 inherit listview ~source ~trusted:false ~modehash as super
4197 method display =
4198 if state.newerrmsgs
4199 then msgsource#reset;
4200 super#display
4201 end);
4202 G.postRedisplay "msgs";
4205 let quickbookmark ?title () =
4206 match state.layout with
4207 | [] -> ()
4208 | l :: _ ->
4209 let title =
4210 match title with
4211 | None ->
4212 let sec = Unix.gettimeofday () in
4213 let tm = Unix.localtime sec in
4214 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
4215 (l.pageno+1)
4216 tm.Unix.tm_mday
4217 tm.Unix.tm_mon
4218 (tm.Unix.tm_year + 1900)
4219 tm.Unix.tm_hour
4220 tm.Unix.tm_min
4221 | Some title -> title
4223 state.bookmarks <-
4224 (title, 0, (l.pageno, float l.pagey /. float l.pageh))
4225 :: state.bookmarks
4228 let doreshape w h =
4229 state.fullscreen <- None;
4230 Wsi.reshape w h;
4233 let setautoscrollspeed step goingdown =
4234 let incr = max 1 ((abs step) / 2) in
4235 let incr = if goingdown then incr else -incr in
4236 let astep = step + incr in
4237 state.autoscroll <- Some astep;
4240 let viewkeyboard key mask =
4241 let enttext te =
4242 let mode = state.mode in
4243 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
4244 state.text <- "";
4245 enttext ();
4246 G.postRedisplay "view:enttext"
4248 let ctrl = Wsi.withctrl mask in
4249 match key with
4250 | 81 -> (* Q *)
4251 exit 0
4253 | 0xff63 -> (* insert *)
4254 if conf.angle mod 360 = 0
4255 then (
4256 state.mode <- LinkNav (Ltgendir 0);
4257 gotoy state.y;
4259 else showtext '!' "Keyboard link naviagtion does not work under rotation"
4261 | 0xff1b | 113 -> (* escape / q *)
4262 begin match state.mstate with
4263 | Mzoomrect _ ->
4264 state.mstate <- Mnone;
4265 Wsi.setcursor Wsi.CURSOR_INHERIT;
4266 G.postRedisplay "kill zoom rect";
4267 | _ ->
4268 match state.ranchors with
4269 | [] -> raise Quit
4270 | (path, password, anchor) :: rest ->
4271 state.ranchors <- rest;
4272 state.anchor <- anchor;
4273 opendoc path password
4274 end;
4276 | 0xff08 -> (* backspace *)
4277 let y = getnav ~-1 in
4278 gotoy_and_clear_text y
4280 | 111 -> (* o *)
4281 enteroutlinemode ()
4283 | 117 -> (* u *)
4284 state.rects <- [];
4285 state.text <- "";
4286 G.postRedisplay "dehighlight";
4288 | 47 | 63 -> (* / ? *)
4289 let ondone isforw s =
4290 cbput state.hists.pat s;
4291 state.searchpattern <- s;
4292 search s isforw
4294 let s = String.create 1 in
4295 s.[0] <- Char.chr key;
4296 enttext (s, "", Some (onhist state.hists.pat),
4297 textentry, ondone (key = 47))
4299 | 43 | 0xffab when ctrl -> (* ctrl-+ *)
4300 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
4301 setzoom (conf.zoom +. incr)
4303 | 43 | 0xffab -> (* + *)
4304 let ondone s =
4305 let n =
4306 try int_of_string s with exc ->
4307 state.text <- Printf.sprintf "bad integer `%s': %s"
4308 s (Printexc.to_string exc);
4309 max_int
4311 if n != max_int
4312 then (
4313 conf.pagebias <- n;
4314 state.text <- "page bias is now " ^ string_of_int n;
4317 enttext ("page bias: ", "", None, intentry, ondone)
4319 | 45 | 0xffad when ctrl -> (* ctrl-- *)
4320 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
4321 setzoom (max 0.01 (conf.zoom -. decr))
4323 | 45 | 0xffad -> (* - *)
4324 let ondone msg = state.text <- msg in
4325 enttext (
4326 "option [acfhilpstvxACPRSZTIS]: ", "", None,
4327 optentry state.mode, ondone
4330 | 48 when ctrl -> (* ctrl-0 *)
4331 setzoom 1.0
4333 | 49 when ctrl -> (* 1 *)
4334 let zoom = zoomforh conf.winw conf.winh state.scrollw in
4335 if zoom < 1.0
4336 then setzoom zoom
4338 | 0xffc6 -> (* f9 *)
4339 togglebirdseye ()
4341 | 57 when ctrl -> (* ctrl-9 *)
4342 togglebirdseye ()
4344 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
4345 when not ctrl -> (* 0..9 *)
4346 let ondone s =
4347 let n =
4348 try int_of_string s with exc ->
4349 state.text <- Printf.sprintf "bad integer `%s': %s"
4350 s (Printexc.to_string exc);
4353 if n >= 0
4354 then (
4355 addnav ();
4356 cbput state.hists.pag (string_of_int n);
4357 gotopage1 (n + conf.pagebias - 1) 0;
4360 let pageentry text key =
4361 match Char.unsafe_chr key with
4362 | 'g' -> TEdone text
4363 | _ -> intentry text key
4365 let text = "x" in text.[0] <- Char.chr key;
4366 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone)
4368 | 98 -> (* b *)
4369 state.scrollw <- if state.scrollw > 0 then 0 else conf.scrollbw;
4370 reshape conf.winw conf.winh;
4372 | 108 -> (* l *)
4373 conf.hlinks <- not conf.hlinks;
4374 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
4375 G.postRedisplay "toggle highlightlinks";
4377 | 97 -> (* a *)
4378 begin match state.autoscroll with
4379 | Some step ->
4380 conf.autoscrollstep <- step;
4381 state.autoscroll <- None
4382 | None ->
4383 if conf.autoscrollstep = 0
4384 then state.autoscroll <- Some 1
4385 else state.autoscroll <- Some conf.autoscrollstep
4388 | 80 -> (* P *)
4389 conf.presentation <- not conf.presentation;
4390 if conf.presentation
4391 then (
4392 if not conf.scrollbarinpm
4393 then state.scrollw <- 0;
4395 else
4396 state.scrollw <- conf.scrollbw;
4398 showtext ' ' ("presentation mode " ^
4399 if conf.presentation then "on" else "off");
4400 state.anchor <- getanchor ();
4401 represent ()
4403 | 102 -> (* f *)
4404 begin match state.fullscreen with
4405 | None ->
4406 state.fullscreen <- Some (conf.winw, conf.winh);
4407 Wsi.fullscreen ()
4408 | Some (w, h) ->
4409 state.fullscreen <- None;
4410 doreshape w h
4413 | 103 -> (* g *)
4414 gotoy_and_clear_text 0
4416 | 71 -> (* G *)
4417 gotopage1 (state.pagecount - 1) 0
4419 | 112 | 78 -> (* p|N *)
4420 search state.searchpattern false
4422 | 110 | 0xffc0 -> (* n|F3 *)
4423 search state.searchpattern true
4425 | 116 -> (* t *)
4426 begin match state.layout with
4427 | [] -> ()
4428 | l :: _ ->
4429 gotoy_and_clear_text (getpagey l.pageno)
4432 | 32 -> (* ' ' *)
4433 begin match List.rev state.layout with
4434 | [] -> ()
4435 | l :: _ ->
4436 let pageno = min (l.pageno+1) (state.pagecount-1) in
4437 gotoy_and_clear_text (getpagey pageno)
4440 | 0xff9f | 0xffff -> (* delete *)
4441 begin match state.layout with
4442 | [] -> ()
4443 | l :: _ ->
4444 let pageno = max 0 (l.pageno-1) in
4445 gotoy_and_clear_text (getpagey pageno)
4448 | 61 -> (* = *)
4449 showtext ' ' (describe_location ());
4451 | 119 -> (* w *)
4452 begin match state.layout with
4453 | [] -> ()
4454 | l :: _ ->
4455 doreshape (l.pagew + state.scrollw) l.pageh;
4456 G.postRedisplay "w"
4459 | 39 -> (* ' *)
4460 enterbookmarkmode ()
4462 | 104 | 0xffbe -> (* h|F1 *)
4463 enterhelpmode ()
4465 | 105 -> (* i *)
4466 enterinfomode ()
4468 | 101 when conf.redirectstderr -> (* e *)
4469 entermsgsmode ()
4471 | 109 -> (* m *)
4472 let ondone s =
4473 match state.layout with
4474 | l :: _ ->
4475 state.bookmarks <-
4476 (s, 0, (l.pageno, float l.pagey /. float l.pageh))
4477 :: state.bookmarks
4478 | _ -> ()
4480 enttext ("bookmark: ", "", None, textentry, ondone)
4482 | 126 -> (* ~ *)
4483 quickbookmark ();
4484 showtext ' ' "Quick bookmark added";
4486 | 122 -> (* z *)
4487 begin match state.layout with
4488 | l :: _ ->
4489 let rect = getpdimrect l.pagedimno in
4490 let w, h =
4491 if conf.crophack
4492 then
4493 (truncate (1.8 *. (rect.(1) -. rect.(0))),
4494 truncate (1.2 *. (rect.(3) -. rect.(0))))
4495 else
4496 (truncate (rect.(1) -. rect.(0)),
4497 truncate (rect.(3) -. rect.(0)))
4499 let w = truncate ((float w)*.conf.zoom)
4500 and h = truncate ((float h)*.conf.zoom) in
4501 if w != 0 && h != 0
4502 then (
4503 state.anchor <- getanchor ();
4504 doreshape (w + state.scrollw) (h + conf.interpagespace)
4506 G.postRedisplay "z";
4508 | [] -> ()
4511 | 50 when ctrl -> (* ctrl-2 *)
4512 let maxw = getmaxw () in
4513 if maxw > 0.0
4514 then setzoom (maxw /. float conf.winw)
4516 | 60 | 62 -> (* < > *)
4517 reqlayout (conf.angle + (if key = 62 then 30 else -30)) conf.proportional
4519 | 91 | 93 -> (* [ ] *)
4520 conf.colorscale <-
4521 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
4523 G.postRedisplay "brightness";
4525 | 0xff54 | 0xff52 when ctrl && Wsi.withshift mask ->
4526 setzoom state.prevzoom
4528 | 107 | 0xff52 -> (* k up *)
4529 begin match state.autoscroll with
4530 | None ->
4531 begin match state.mode with
4532 | Birdseye beye -> upbirdseye 1 beye
4533 | _ ->
4534 if ctrl
4535 then gotoy_and_clear_text (clamp ~-(conf.winh/2))
4536 else gotoy_and_clear_text (clamp (-conf.scrollstep))
4538 | Some n ->
4539 setautoscrollspeed n false
4542 | 106 | 0xff54 -> (* j down *)
4543 begin match state.autoscroll with
4544 | None ->
4545 begin match state.mode with
4546 | Birdseye beye -> downbirdseye 1 beye
4547 | _ ->
4548 if ctrl
4549 then gotoy_and_clear_text (clamp (conf.winh/2))
4550 else gotoy_and_clear_text (clamp conf.scrollstep)
4552 | Some n ->
4553 setautoscrollspeed n true
4556 | 0xff51 | 0xff53 when Wsi.withnone mask -> (* left / right *)
4557 if conf.zoom > 1.0
4558 then
4559 let dx =
4560 if ctrl
4561 then conf.winw / 2
4562 else 10
4564 let dx = if key = 0xff51 then dx else -dx in
4565 state.x <- state.x + dx;
4566 gotoy_and_clear_text state.y
4567 else (
4568 state.text <- "";
4569 G.postRedisplay "lef/right"
4572 | 0xff55 -> (* prior *)
4573 let y =
4574 if ctrl
4575 then
4576 match state.layout with
4577 | [] -> state.y
4578 | l :: _ -> state.y - l.pagey
4579 else
4580 clamp (-conf.winh)
4582 gotoghyll y
4584 | 0xff56 -> (* next *)
4585 let y =
4586 if ctrl
4587 then
4588 match List.rev state.layout with
4589 | [] -> state.y
4590 | l :: _ -> getpagey l.pageno
4591 else
4592 clamp conf.winh
4594 gotoghyll y
4596 | 0xff50 -> gotoghyll 0
4597 | 0xff57 -> gotoghyll (clamp state.maxy)
4598 | 0xff53 when Wsi.withalt mask ->
4599 gotoghyll (getnav ~-1)
4600 | 0xff51 when Wsi.withalt mask ->
4601 gotoghyll (getnav 1)
4603 | 114 -> (* r *)
4604 state.anchor <- getanchor ();
4605 opendoc state.path state.password
4607 | 76 -> (* L *)
4608 launchpath ()
4610 | 118 when conf.debug -> (* v *)
4611 state.rects <- [];
4612 List.iter (fun l ->
4613 match getopaque l.pageno with
4614 | None -> ()
4615 | Some opaque ->
4616 let x0, y0, x1, y1 = pagebbox opaque in
4617 let a,b = float x0, float y0 in
4618 let c,d = float x1, float y0 in
4619 let e,f = float x1, float y1 in
4620 let h,j = float x0, float y1 in
4621 let rect = (a,b,c,d,e,f,h,j) in
4622 debugrect rect;
4623 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
4624 ) state.layout;
4625 G.postRedisplay "v";
4627 | _ ->
4628 vlog "huh? %s" (Wsi.keyname key)
4631 let gotounder = function
4632 | Ulinkgoto (pageno, top) ->
4633 if pageno >= 0
4634 then (
4635 addnav ();
4636 gotopage1 pageno top;
4639 | Ulinkuri s ->
4640 gotouri s
4642 | Uremote (filename, pageno) ->
4643 let path =
4644 if Sys.file_exists filename
4645 then filename
4646 else
4647 let dir = Filename.dirname state.path in
4648 let path = Filename.concat dir filename in
4649 if Sys.file_exists path
4650 then path
4651 else ""
4653 if String.length path > 0
4654 then (
4655 let anchor = getanchor () in
4656 let ranchor = state.path, state.password, anchor in
4657 state.anchor <- (pageno, 0.0);
4658 state.ranchors <- ranchor :: state.ranchors;
4659 opendoc path "";
4661 else showtext '!' ("Could not find " ^ filename)
4663 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
4666 let linknavkeyboard key mask linknav =
4667 if key = 0xff63
4668 then (
4669 state.mode <- View;
4670 G.postRedisplay "leave linknav"
4672 else
4673 begin match linknav with
4674 | Ltgendir _ -> viewkeyboard key mask
4675 | Ltexact ((l, opaque, n), _) ->
4676 if key = 0xff0d
4677 then
4678 let under = getlink opaque n in
4679 G.postRedisplay "link gotounder";
4680 gotounder under;
4681 state.mode <- View;
4682 else
4683 let opt, dir =
4684 match key with
4685 | 0xff50 -> (* home *)
4686 Some (findlink opaque LDfirst), -1
4688 | 0xff57 -> (* end *)
4689 Some (findlink opaque LDlast), 1
4691 | 0xff51 -> (* left *)
4692 Some (findlink opaque (LDleft n)), -1
4694 | 0xff53 -> (* right *)
4695 Some (findlink opaque (LDright n)), 1
4697 | 0xff52 -> (* up *)
4698 Some (findlink opaque (LDup n)), -1
4700 | 0xff54 -> (* down *)
4701 Some (findlink opaque (LDdown n)), 1
4703 | _ -> None, 0
4705 begin match opt with
4706 | Some Lnolock -> showtext '!' "failed to acquire lock";
4707 | Some Lnotfound ->
4708 begin match findpwl l.pageno dir with
4709 | Pwlnotfound -> ()
4710 | Pwl pageno ->
4711 state.mode <- LinkNav (Ltgendir dir);
4712 let y, h = getpageyh pageno in
4713 let y =
4714 if dir < 0
4715 then y + h - conf.winh
4716 else y
4718 gotoy y;
4719 end;
4721 | Some (Lfound (m, x0, y0, x1, y1)) ->
4722 if y0 < l.pagey
4723 || l.pagedispy + (y1 - l.pagey) > conf.winh - state.hscrollh
4724 then (
4725 state.mode <- LinkNav (Ltgendir 0);
4726 gotoy (state.y + (y0 - l.pagey))
4728 else (
4729 if m = n
4730 then (
4731 match findpwl l.pageno dir with
4732 | Pwlnotfound -> ()
4733 | Pwl pageno ->
4734 state.mode <- LinkNav (Ltgendir dir);
4735 let y, h = getpageyh pageno in
4736 let y =
4737 if dir < 0
4738 then y + h - conf.winh
4739 else y
4741 gotoy y;
4743 else
4744 let r = x0, y0, x1, y1 in
4745 state.mode <- LinkNav (Ltexact ((l, opaque, m), r));
4746 G.postRedisplay "linknav up"
4748 | None ->
4749 state.mode <- LinkNav (Ltgendir 0);
4750 viewkeyboard key mask
4751 end;
4752 end;
4755 let keyboard key mask =
4756 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
4757 then wcmd "interrupt"
4758 else state.uioh <- state.uioh#key key mask
4761 let birdseyekeyboard key mask
4762 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
4763 let incr =
4764 match conf.columns with
4765 | None -> 1
4766 | Some ((c, _, _), _) -> c
4768 match key with
4769 | 108 when Wsi.withctrl mask -> (* ctrl-l *)
4770 let y, h = getpageyh pageno in
4771 let top = (conf.winh - h) / 2 in
4772 gotoy (max 0 (y - top))
4773 | 0xff0d -> leavebirdseye beye false
4774 | 0xff1b -> leavebirdseye beye true (* escape *)
4775 | 0xff52 -> upbirdseye incr beye (* prior *)
4776 | 0xff54 -> downbirdseye incr beye (* next *)
4777 | 0xff51 -> upbirdseye 1 beye (* up *)
4778 | 0xff53 -> downbirdseye 1 beye (* down *)
4780 | 0xff55 ->
4781 begin match state.layout with
4782 | l :: _ ->
4783 if l.pagey != 0
4784 then (
4785 state.mode <- Birdseye (
4786 oconf, leftx, l.pageno, hooverpageno, anchor
4788 gotopage1 l.pageno 0;
4790 else (
4791 let layout = layout (state.y-conf.winh) conf.winh in
4792 match layout with
4793 | [] -> gotoy (clamp (-conf.winh))
4794 | l :: _ ->
4795 state.mode <- Birdseye (
4796 oconf, leftx, l.pageno, hooverpageno, anchor
4798 gotopage1 l.pageno 0
4801 | [] -> gotoy (clamp (-conf.winh))
4802 end;
4804 | 0xff56 ->
4805 begin match List.rev state.layout with
4806 | l :: _ ->
4807 let layout = layout (state.y + conf.winh) conf.winh in
4808 begin match layout with
4809 | [] ->
4810 let incr = l.pageh - l.pagevh in
4811 if incr = 0
4812 then (
4813 state.mode <-
4814 Birdseye (
4815 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
4817 G.postRedisplay "birdseye pagedown";
4819 else gotoy (clamp (incr + conf.interpagespace*2));
4821 | l :: _ ->
4822 state.mode <-
4823 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
4824 gotopage1 l.pageno 0;
4827 | [] -> gotoy (clamp conf.winh)
4828 end;
4830 | 0xff50 ->
4831 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
4832 gotopage1 0 0
4834 | 0xff57 ->
4835 let pageno = state.pagecount - 1 in
4836 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
4837 if not (pagevisible state.layout pageno)
4838 then
4839 let h =
4840 match List.rev state.pdims with
4841 | [] -> conf.winh
4842 | (_, _, h, _) :: _ -> h
4844 gotoy (max 0 (getpagey pageno - (conf.winh - h - conf.interpagespace)))
4845 else G.postRedisplay "birdseye end";
4846 | _ -> viewkeyboard key mask
4849 let drawpage l =
4850 let color =
4851 match state.mode with
4852 | Textentry _ -> scalecolor 0.4
4853 | LinkNav _
4854 | View -> scalecolor 1.0
4855 | Birdseye (_, _, pageno, hooverpageno, _) ->
4856 if l.pageno = hooverpageno
4857 then scalecolor 0.9
4858 else (
4859 if l.pageno = pageno
4860 then scalecolor 1.0
4861 else scalecolor 0.8
4864 drawtiles l color;
4865 begin match getopaque l.pageno with
4866 | Some opaque ->
4867 if tileready l l.pagex l.pagey
4868 then
4869 let x = l.pagedispx - l.pagex
4870 and y = l.pagedispy - l.pagey in
4871 postprocess opaque conf.hlinks x y;
4873 | _ -> ()
4874 end;
4877 let scrollindicator () =
4878 let sbw, ph, sh = state.uioh#scrollph in
4879 let sbh, pw, sw = state.uioh#scrollpw in
4881 GlDraw.color (0.64, 0.64, 0.64);
4882 GlDraw.rect
4883 (float (conf.winw - sbw), 0.)
4884 (float conf.winw, float conf.winh)
4886 GlDraw.rect
4887 (0., float (conf.winh - sbh))
4888 (float (conf.winw - state.scrollw - 1), float conf.winh)
4890 GlDraw.color (0.0, 0.0, 0.0);
4892 GlDraw.rect
4893 (float (conf.winw - sbw), ph)
4894 (float conf.winw, ph +. sh)
4896 GlDraw.rect
4897 (pw, float (conf.winh - sbh))
4898 (pw +. sw, float conf.winh)
4902 let showsel () =
4903 match state.mstate with
4904 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
4907 | Msel ((x0, y0), (x1, y1)) ->
4908 let rec loop = function
4909 | l :: ls ->
4910 if ((y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
4911 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh))))
4912 && ((x0 >= l.pagedispx && x0 <= (l.pagedispx + l.pagevw))
4913 || ((x1 >= l.pagedispx && x1 <= (l.pagedispx + l.pagevw))))
4914 then
4915 match getopaque l.pageno with
4916 | Some opaque ->
4917 let x0, y0 = pagetranslatepoint l x0 y0 in
4918 let x1, y1 = pagetranslatepoint l x1 y1 in
4919 seltext opaque (x0, y0, x1, y1);
4920 | _ -> ()
4921 else loop ls
4922 | [] -> ()
4924 loop state.layout
4927 let showrects rects =
4928 Gl.enable `blend;
4929 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
4930 GlDraw.polygon_mode `both `fill;
4931 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
4932 List.iter
4933 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
4934 List.iter (fun l ->
4935 if l.pageno = pageno
4936 then (
4937 let dx = float (l.pagedispx - l.pagex) in
4938 let dy = float (l.pagedispy - l.pagey) in
4939 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
4940 GlDraw.begins `quads;
4942 GlDraw.vertex2 (x0+.dx, y0+.dy);
4943 GlDraw.vertex2 (x1+.dx, y1+.dy);
4944 GlDraw.vertex2 (x2+.dx, y2+.dy);
4945 GlDraw.vertex2 (x3+.dx, y3+.dy);
4947 GlDraw.ends ();
4949 ) state.layout
4950 ) rects
4952 Gl.disable `blend;
4955 let display () =
4956 GlClear.color (scalecolor2 conf.bgcolor);
4957 GlClear.clear [`color];
4958 List.iter drawpage state.layout;
4959 let rects =
4960 match state.mode with
4961 | LinkNav (Ltexact ((page, _, _), (x0, y0, x1, y1))) ->
4962 (page.pageno, 5, (
4963 float x0, float y0,
4964 float x1, float y0,
4965 float x1, float y1,
4966 float x0, float y1)
4967 ) :: state.rects
4968 | _ -> state.rects
4970 showrects rects;
4971 showsel ();
4972 state.uioh#display;
4973 scrollindicator ();
4974 begin match state.mstate with
4975 | Mzoomrect ((x0, y0), (x1, y1)) ->
4976 Gl.enable `blend;
4977 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
4978 GlDraw.polygon_mode `both `fill;
4979 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
4980 GlDraw.rect (float x0, float y0)
4981 (float x1, float y1);
4982 Gl.disable `blend;
4983 | _ -> ()
4984 end;
4985 enttext ();
4986 if conf.updatecurs
4987 then (
4988 let mx, my = state.mpos in
4989 updateunder mx my;
4991 Wsi.swapb ();
4994 let zoomrect x y x1 y1 =
4995 let x0 = min x x1
4996 and x1 = max x x1
4997 and y0 = min y y1 in
4998 gotoy (state.y + y0);
4999 state.anchor <- getanchor ();
5000 let zoom = (float conf.winw *. conf.zoom) /. float (x1 - x0) in
5001 let margin =
5002 if state.w < conf.winw - state.scrollw
5003 then (conf.winw - state.scrollw - state.w) / 2
5004 else 0
5006 state.x <- (state.x + margin) - x0;
5007 setzoom zoom;
5008 Wsi.setcursor Wsi.CURSOR_INHERIT;
5009 state.mstate <- Mnone;
5012 let scrollx x =
5013 let winw = conf.winw - state.scrollw - 1 in
5014 let s = float x /. float winw in
5015 let destx = truncate (float (state.w + winw) *. s) in
5016 state.x <- winw - destx;
5017 gotoy_and_clear_text state.y;
5018 state.mstate <- Mscrollx;
5021 let scrolly y =
5022 let s = float y /. float conf.winh in
5023 let desty = truncate (float (state.maxy - conf.winh) *. s) in
5024 gotoy_and_clear_text desty;
5025 state.mstate <- Mscrolly;
5028 let viewmouse button down x y mask =
5029 match button with
5030 | n when (n == 4 || n == 5) && not down ->
5031 if Wsi.withctrl mask
5032 then (
5033 match state.mstate with
5034 | Mzoom (oldn, i) ->
5035 if oldn = n
5036 then (
5037 if i = 2
5038 then
5039 let incr =
5040 match n with
5041 | 5 ->
5042 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
5043 | _ ->
5044 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
5046 let zoom = conf.zoom -. incr in
5047 setzoom zoom;
5048 state.mstate <- Mzoom (n, 0);
5049 else
5050 state.mstate <- Mzoom (n, i+1);
5052 else state.mstate <- Mzoom (n, 0)
5054 | _ -> state.mstate <- Mzoom (n, 0)
5056 else (
5057 match state.autoscroll with
5058 | Some step -> setautoscrollspeed step (n=4)
5059 | None ->
5060 let incr =
5061 if n = 4
5062 then -conf.scrollstep
5063 else conf.scrollstep
5065 let incr = incr * 2 in
5066 let y = clamp incr in
5067 gotoy_and_clear_text y
5070 | 1 when Wsi.withctrl mask ->
5071 if down
5072 then (
5073 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5074 state.mstate <- Mpan (x, y)
5076 else
5077 state.mstate <- Mnone
5079 | 3 ->
5080 if down
5081 then (
5082 Wsi.setcursor Wsi.CURSOR_CYCLE;
5083 let p = (x, y) in
5084 state.mstate <- Mzoomrect (p, p)
5086 else (
5087 match state.mstate with
5088 | Mzoomrect ((x0, y0), _) ->
5089 if abs (x-x0) > 10 && abs (y - y0) > 10
5090 then zoomrect x0 y0 x y
5091 else (
5092 state.mstate <- Mnone;
5093 Wsi.setcursor Wsi.CURSOR_INHERIT;
5094 G.postRedisplay "kill accidental zoom rect";
5096 | _ ->
5097 Wsi.setcursor Wsi.CURSOR_INHERIT;
5098 state.mstate <- Mnone
5101 | 1 when x > conf.winw - state.scrollw ->
5102 if down
5103 then
5104 let _, position, sh = state.uioh#scrollph in
5105 if y > truncate position && y < truncate (position +. sh)
5106 then state.mstate <- Mscrolly
5107 else scrolly y
5108 else
5109 state.mstate <- Mnone
5111 | 1 when y > conf.winh - state.hscrollh ->
5112 if down
5113 then
5114 let _, position, sw = state.uioh#scrollpw in
5115 if x > truncate position && x < truncate (position +. sw)
5116 then state.mstate <- Mscrollx
5117 else scrollx x
5118 else
5119 state.mstate <- Mnone
5121 | 1 ->
5122 let dest = if down then getunder x y else Unone in
5123 begin match dest with
5124 | Ulinkgoto (pageno, top) ->
5125 if pageno >= 0
5126 then (
5127 addnav ();
5128 gotopage1 pageno top;
5131 | Ulinkuri s ->
5132 gotouri s
5134 | Uremote (filename, pageno) ->
5135 let path =
5136 if Sys.file_exists filename
5137 then filename
5138 else
5139 let dir = Filename.dirname state.path in
5140 let path = Filename.concat dir filename in
5141 if Sys.file_exists path
5142 then path
5143 else ""
5145 if String.length path > 0
5146 then (
5147 let anchor = getanchor () in
5148 let ranchor = state.path, state.password, anchor in
5149 state.anchor <- (pageno, 0.0);
5150 state.ranchors <- ranchor :: state.ranchors;
5151 opendoc path "";
5153 else showtext '!' ("Could not find " ^ filename)
5155 | Uunexpected _ | Ulaunch _ | Unamed _ -> ()
5157 | Unone when down ->
5158 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5159 state.mstate <- Mpan (x, y);
5161 | Unone | Utext _ ->
5162 if down
5163 then (
5164 if conf.angle mod 360 = 0
5165 then (
5166 state.mstate <- Msel ((x, y), (x, y));
5167 G.postRedisplay "mouse select";
5170 else (
5171 match state.mstate with
5172 | Mnone -> ()
5174 | Mzoom _ | Mscrollx | Mscrolly ->
5175 state.mstate <- Mnone
5177 | Mzoomrect ((x0, y0), _) ->
5178 zoomrect x0 y0 x y
5180 | Mpan _ ->
5181 Wsi.setcursor Wsi.CURSOR_INHERIT;
5182 state.mstate <- Mnone
5184 | Msel ((_, y0), (_, y1)) ->
5185 let rec loop = function
5186 | [] -> ()
5187 | l :: rest ->
5188 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
5189 || ((y1 >= l.pagedispy
5190 && y1 <= (l.pagedispy + l.pagevh)))
5191 then
5192 match getopaque l.pageno with
5193 | Some opaque ->
5194 copysel conf.selcmd opaque;
5195 G.postRedisplay "copysel"
5196 | _ -> ()
5197 else loop rest
5199 loop state.layout;
5200 Wsi.setcursor Wsi.CURSOR_INHERIT;
5201 state.mstate <- Mnone;
5205 | _ -> ()
5208 let birdseyemouse button down x y mask
5209 (conf, leftx, _, hooverpageno, anchor) =
5210 match button with
5211 | 1 when down ->
5212 let rec loop = function
5213 | [] -> ()
5214 | l :: rest ->
5215 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5216 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5217 then (
5218 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
5220 else loop rest
5222 loop state.layout
5223 | 3 -> ()
5224 | _ -> viewmouse button down x y mask
5227 let mouse button down x y mask =
5228 state.uioh <- state.uioh#button button down x y mask;
5231 let motion ~x ~y =
5232 state.uioh <- state.uioh#motion x y
5235 let pmotion ~x ~y =
5236 state.uioh <- state.uioh#pmotion x y;
5239 let uioh = object
5240 method display = ()
5242 method key key mask =
5243 begin match state.mode with
5244 | Textentry textentry -> textentrykeyboard key mask textentry
5245 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
5246 | View -> viewkeyboard key mask
5247 | LinkNav linknav -> linknavkeyboard key mask linknav
5248 end;
5249 state.uioh
5251 method button button bstate x y mask =
5252 begin match state.mode with
5253 | LinkNav _
5254 | View -> viewmouse button bstate x y mask
5255 | Birdseye beye -> birdseyemouse button bstate x y mask beye
5256 | Textentry _ -> ()
5257 end;
5258 state.uioh
5260 method motion x y =
5261 begin match state.mode with
5262 | Textentry _ -> ()
5263 | View | Birdseye _ | LinkNav _ ->
5264 match state.mstate with
5265 | Mzoom _ | Mnone -> ()
5267 | Mpan (x0, y0) ->
5268 let dx = x - x0
5269 and dy = y0 - y in
5270 state.mstate <- Mpan (x, y);
5271 if conf.zoom > 1.0 then state.x <- state.x + dx;
5272 let y = clamp dy in
5273 gotoy_and_clear_text y
5275 | Msel (a, _) ->
5276 state.mstate <- Msel (a, (x, y));
5277 G.postRedisplay "motion select";
5279 | Mscrolly ->
5280 let y = min conf.winh (max 0 y) in
5281 scrolly y
5283 | Mscrollx ->
5284 let x = min conf.winw (max 0 x) in
5285 scrollx x
5287 | Mzoomrect (p0, _) ->
5288 state.mstate <- Mzoomrect (p0, (x, y));
5289 G.postRedisplay "motion zoomrect";
5290 end;
5291 state.uioh
5293 method pmotion x y =
5294 begin match state.mode with
5295 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
5296 let rec loop = function
5297 | [] ->
5298 if hooverpageno != -1
5299 then (
5300 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
5301 G.postRedisplay "pmotion birdseye no hoover";
5303 | l :: rest ->
5304 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5305 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5306 then (
5307 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
5308 G.postRedisplay "pmotion birdseye hoover";
5310 else loop rest
5312 loop state.layout
5314 | Textentry _ -> ()
5316 | LinkNav _
5317 | View ->
5318 match state.mstate with
5319 | Mnone -> updateunder x y
5320 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
5322 end;
5323 state.uioh
5325 method infochanged _ = ()
5327 method scrollph =
5328 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
5329 let p, h = scrollph state.y maxy in
5330 state.scrollw, p, h
5332 method scrollpw =
5333 let winw = conf.winw - state.scrollw - 1 in
5334 let fwinw = float winw in
5335 let sw =
5336 let sw = fwinw /. float state.w in
5337 let sw = fwinw *. sw in
5338 max sw (float conf.scrollh)
5340 let position, sw =
5341 let f = state.w+winw in
5342 let r = float (winw-state.x) /. float f in
5343 let p = fwinw *. r in
5344 p-.sw/.2., sw
5346 let sw =
5347 if position +. sw > fwinw
5348 then fwinw -. position
5349 else sw
5351 state.hscrollh, position, sw
5353 method modehash =
5354 let modename =
5355 match state.mode with
5356 | LinkNav _ -> "links"
5357 | Textentry _ -> "textentry"
5358 | Birdseye _ -> "birdseye"
5359 | View -> "global"
5361 findkeyhash conf modename
5362 end;;
5364 module Config =
5365 struct
5366 open Parser
5368 let fontpath = ref "";;
5370 module KeyMap =
5371 Map.Make (struct type t = (int * int) let compare = compare end);;
5373 let unent s =
5374 let l = String.length s in
5375 let b = Buffer.create l in
5376 unent b s 0 l;
5377 Buffer.contents b;
5380 let home =
5381 try Sys.getenv "HOME"
5382 with exn ->
5383 prerr_endline
5384 ("Can not determine home directory location: " ^
5385 Printexc.to_string exn);
5389 let modifier_of_string = function
5390 | "alt" -> Wsi.altmask
5391 | "shift" -> Wsi.shiftmask
5392 | "ctrl" | "control" -> Wsi.ctrlmask
5393 | "meta" -> Wsi.metamask
5394 | _ -> 0
5397 let key_of_string =
5398 let r = Str.regexp "-" in
5399 fun s ->
5400 let elems = Str.full_split r s in
5401 let f n k m =
5402 let g s =
5403 let m1 = modifier_of_string s in
5404 if m1 = 0
5405 then (Wsi.namekey s, m)
5406 else (k, m lor m1)
5407 in function
5408 | Str.Delim s when n land 1 = 0 -> g s
5409 | Str.Text s -> g s
5410 | Str.Delim _ -> (k, m)
5412 let rec loop n k m = function
5413 | [] -> (k, m)
5414 | x :: xs ->
5415 let k, m = f n k m x in
5416 loop (n+1) k m xs
5418 loop 0 0 0 elems
5421 let keys_of_string =
5422 let r = Str.regexp "[ \t]" in
5423 fun s ->
5424 let elems = Str.split r s in
5425 List.map key_of_string elems
5428 let copykeyhashes c =
5429 List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
5432 let config_of c attrs =
5433 let apply c k v =
5435 match k with
5436 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
5437 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
5438 | "case-insensitive-search" -> { c with icase = bool_of_string v }
5439 | "preload" -> { c with preload = bool_of_string v }
5440 | "page-bias" -> { c with pagebias = int_of_string v }
5441 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
5442 | "auto-scroll-step" ->
5443 { c with autoscrollstep = max 0 (int_of_string v) }
5444 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
5445 | "crop-hack" -> { c with crophack = bool_of_string v }
5446 | "throttle" ->
5447 let mw =
5448 match String.lowercase v with
5449 | "true" -> Some infinity
5450 | "false" -> None
5451 | f -> Some (float_of_string f)
5453 { c with maxwait = mw}
5454 | "highlight-links" -> { c with hlinks = bool_of_string v }
5455 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
5456 | "vertical-margin" ->
5457 { c with interpagespace = max 0 (int_of_string v) }
5458 | "zoom" ->
5459 let zoom = float_of_string v /. 100. in
5460 let zoom = max zoom 0.0 in
5461 { c with zoom = zoom }
5462 | "presentation" -> { c with presentation = bool_of_string v }
5463 | "rotation-angle" -> { c with angle = int_of_string v }
5464 | "width" -> { c with winw = max 20 (int_of_string v) }
5465 | "height" -> { c with winh = max 20 (int_of_string v) }
5466 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
5467 | "proportional-display" -> { c with proportional = bool_of_string v }
5468 | "pixmap-cache-size" ->
5469 { c with memlimit = max 2 (int_of_string_with_suffix v) }
5470 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
5471 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
5472 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
5473 | "persistent-location" -> { c with jumpback = bool_of_string v }
5474 | "background-color" -> { c with bgcolor = color_of_string v }
5475 | "scrollbar-in-presentation" ->
5476 { c with scrollbarinpm = bool_of_string v }
5477 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
5478 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
5479 | "mupdf-store-size" ->
5480 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
5481 | "checkers" -> { c with checkers = bool_of_string v }
5482 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
5483 | "trim-margins" -> { c with trimmargins = bool_of_string v }
5484 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
5485 | "uri-launcher" -> { c with urilauncher = unent v }
5486 | "path-launcher" -> { c with pathlauncher = unent v }
5487 | "color-space" -> { c with colorspace = colorspace_of_string v }
5488 | "invert-colors" -> { c with invert = bool_of_string v }
5489 | "brightness" -> { c with colorscale = float_of_string v }
5490 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
5491 | "ghyllscroll" ->
5492 { c with ghyllscroll = Some (ghyllscroll_of_string v) }
5493 | "columns" ->
5494 let nab = columns_of_string v in
5495 { c with columns = Some (nab, [||]) }
5496 | "birds-eye-columns" ->
5497 { c with beyecolumns = Some (max (int_of_string v) 2) }
5498 | "selection-command" -> { c with selcmd = unent v }
5499 | "update-cursor" -> { c with updatecurs = bool_of_string v }
5500 | _ -> c
5501 with exn ->
5502 prerr_endline ("Error processing attribute (`" ^
5503 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
5506 let rec fold c = function
5507 | [] -> c
5508 | (k, v) :: rest ->
5509 let c = apply c k v in
5510 fold c rest
5512 fold { c with keyhashes = copykeyhashes c } attrs;
5515 let fromstring f pos n v d =
5516 try f v
5517 with exn ->
5518 dolog "Error processing attribute (%S=%S) at %d\n%s"
5519 n v pos (Printexc.to_string exn)
5524 let bookmark_of attrs =
5525 let rec fold title page rely = function
5526 | ("title", v) :: rest -> fold v page rely rest
5527 | ("page", v) :: rest -> fold title v rely rest
5528 | ("rely", v) :: rest -> fold title page v rest
5529 | _ :: rest -> fold title page rely rest
5530 | [] -> title, page, rely
5532 fold "invalid" "0" "0" attrs
5535 let doc_of attrs =
5536 let rec fold path page rely pan = function
5537 | ("path", v) :: rest -> fold v page rely pan rest
5538 | ("page", v) :: rest -> fold path v rely pan rest
5539 | ("rely", v) :: rest -> fold path page v pan rest
5540 | ("pan", v) :: rest -> fold path page rely v rest
5541 | _ :: rest -> fold path page rely pan rest
5542 | [] -> path, page, rely, pan
5544 fold "" "0" "0" "0" attrs
5547 let map_of attrs =
5548 let rec fold rs ls = function
5549 | ("out", v) :: rest -> fold v ls rest
5550 | ("in", v) :: rest -> fold rs v rest
5551 | _ :: rest -> fold ls rs rest
5552 | [] -> ls, rs
5554 fold "" "" attrs
5557 let setconf dst src =
5558 dst.scrollbw <- src.scrollbw;
5559 dst.scrollh <- src.scrollh;
5560 dst.icase <- src.icase;
5561 dst.preload <- src.preload;
5562 dst.pagebias <- src.pagebias;
5563 dst.verbose <- src.verbose;
5564 dst.scrollstep <- src.scrollstep;
5565 dst.maxhfit <- src.maxhfit;
5566 dst.crophack <- src.crophack;
5567 dst.autoscrollstep <- src.autoscrollstep;
5568 dst.maxwait <- src.maxwait;
5569 dst.hlinks <- src.hlinks;
5570 dst.underinfo <- src.underinfo;
5571 dst.interpagespace <- src.interpagespace;
5572 dst.zoom <- src.zoom;
5573 dst.presentation <- src.presentation;
5574 dst.angle <- src.angle;
5575 dst.winw <- src.winw;
5576 dst.winh <- src.winh;
5577 dst.savebmarks <- src.savebmarks;
5578 dst.memlimit <- src.memlimit;
5579 dst.proportional <- src.proportional;
5580 dst.texcount <- src.texcount;
5581 dst.sliceheight <- src.sliceheight;
5582 dst.thumbw <- src.thumbw;
5583 dst.jumpback <- src.jumpback;
5584 dst.bgcolor <- src.bgcolor;
5585 dst.scrollbarinpm <- src.scrollbarinpm;
5586 dst.tilew <- src.tilew;
5587 dst.tileh <- src.tileh;
5588 dst.mustoresize <- src.mustoresize;
5589 dst.checkers <- src.checkers;
5590 dst.aalevel <- src.aalevel;
5591 dst.trimmargins <- src.trimmargins;
5592 dst.trimfuzz <- src.trimfuzz;
5593 dst.urilauncher <- src.urilauncher;
5594 dst.colorspace <- src.colorspace;
5595 dst.invert <- src.invert;
5596 dst.colorscale <- src.colorscale;
5597 dst.redirectstderr <- src.redirectstderr;
5598 dst.ghyllscroll <- src.ghyllscroll;
5599 dst.columns <- src.columns;
5600 dst.beyecolumns <- src.beyecolumns;
5601 dst.selcmd <- src.selcmd;
5602 dst.updatecurs <- src.updatecurs;
5603 dst.pathlauncher <- src.pathlauncher;
5604 dst.keyhashes <- copykeyhashes src;
5607 let get s =
5608 let h = Hashtbl.create 10 in
5609 let dc = { defconf with angle = defconf.angle } in
5610 let rec toplevel v t spos _ =
5611 match t with
5612 | Vdata | Vcdata | Vend -> v
5613 | Vopen ("llppconfig", _, closed) ->
5614 if closed
5615 then v
5616 else { v with f = llppconfig }
5617 | Vopen _ ->
5618 error "unexpected subelement at top level" s spos
5619 | Vclose _ -> error "unexpected close at top level" s spos
5621 and llppconfig v t spos _ =
5622 match t with
5623 | Vdata | Vcdata -> v
5624 | Vend -> error "unexpected end of input in llppconfig" s spos
5625 | Vopen ("defaults", attrs, closed) ->
5626 let c = config_of dc attrs in
5627 setconf dc c;
5628 if closed
5629 then v
5630 else { v with f = defaults }
5632 | Vopen ("ui-font", attrs, closed) ->
5633 let rec getsize size = function
5634 | [] -> size
5635 | ("size", v) :: rest ->
5636 let size =
5637 fromstring int_of_string spos "size" v fstate.fontsize in
5638 getsize size rest
5639 | l -> getsize size l
5641 fstate.fontsize <- getsize fstate.fontsize attrs;
5642 if closed
5643 then v
5644 else { v with f = uifont (Buffer.create 10) }
5646 | Vopen ("doc", attrs, closed) ->
5647 let pathent, spage, srely, span = doc_of attrs in
5648 let path = unent pathent
5649 and pageno = fromstring int_of_string spos "page" spage 0
5650 and rely = fromstring float_of_string spos "rely" srely 0.0
5651 and pan = fromstring int_of_string spos "pan" span 0 in
5652 let c = config_of dc attrs in
5653 let anchor = (pageno, rely) in
5654 if closed
5655 then (Hashtbl.add h path (c, [], pan, anchor); v)
5656 else { v with f = doc path pan anchor c [] }
5658 | Vopen _ ->
5659 error "unexpected subelement in llppconfig" s spos
5661 | Vclose "llppconfig" -> { v with f = toplevel }
5662 | Vclose _ -> error "unexpected close in llppconfig" s spos
5664 and defaults v t spos _ =
5665 match t with
5666 | Vdata | Vcdata -> v
5667 | Vend -> error "unexpected end of input in defaults" s spos
5668 | Vopen ("keymap", attrs, closed) ->
5669 let modename =
5670 try List.assoc "mode" attrs
5671 with Not_found -> "global" in
5672 if closed
5673 then v
5674 else
5675 let ret keymap =
5676 let h = findkeyhash dc modename in
5677 KeyMap.iter (Hashtbl.replace h) keymap;
5678 defaults
5680 { v with f = pkeymap ret KeyMap.empty }
5682 | Vopen (_, _, _) ->
5683 error "unexpected subelement in defaults" s spos
5685 | Vclose "defaults" ->
5686 { v with f = llppconfig }
5688 | Vclose _ -> error "unexpected close in defaults" s spos
5690 and uifont b v t spos epos =
5691 match t with
5692 | Vdata | Vcdata ->
5693 Buffer.add_substring b s spos (epos - spos);
5695 | Vopen (_, _, _) ->
5696 error "unexpected subelement in ui-font" s spos
5697 | Vclose "ui-font" ->
5698 if String.length !fontpath = 0
5699 then fontpath := Buffer.contents b;
5700 { v with f = llppconfig }
5701 | Vclose _ -> error "unexpected close in ui-font" s spos
5702 | Vend -> error "unexpected end of input in ui-font" s spos
5704 and doc path pan anchor c bookmarks v t spos _ =
5705 match t with
5706 | Vdata | Vcdata -> v
5707 | Vend -> error "unexpected end of input in doc" s spos
5708 | Vopen ("bookmarks", _, closed) ->
5709 if closed
5710 then v
5711 else { v with f = pbookmarks path pan anchor c bookmarks }
5713 | Vopen ("keymap", attrs, closed) ->
5714 let modename =
5715 try List.assoc "mode" attrs
5716 with Not_found -> "global"
5718 if closed
5719 then v
5720 else
5721 let ret keymap =
5722 let h = findkeyhash c modename in
5723 KeyMap.iter (Hashtbl.replace h) keymap;
5724 doc path pan anchor c bookmarks
5726 { v with f = pkeymap ret KeyMap.empty }
5728 | Vopen (_, _, _) ->
5729 error "unexpected subelement in doc" s spos
5731 | Vclose "doc" ->
5732 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
5733 { v with f = llppconfig }
5735 | Vclose _ -> error "unexpected close in doc" s spos
5737 and pkeymap ret keymap v t spos _ =
5738 match t with
5739 | Vdata | Vcdata -> v
5740 | Vend -> error "unexpected end of input in keymap" s spos
5741 | Vopen ("map", attrs, closed) ->
5742 let r, l = map_of attrs in
5743 let kss = fromstring keys_of_string spos "in" r [] in
5744 let lss = fromstring keys_of_string spos "out" l [] in
5745 let keymap =
5746 match kss with
5747 | [] -> keymap
5748 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
5749 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
5751 if closed
5752 then { v with f = pkeymap ret keymap }
5753 else
5754 let f () = v in
5755 { v with f = skip "map" f }
5757 | Vopen _ ->
5758 error "unexpected subelement in keymap" s spos
5760 | Vclose "keymap" ->
5761 { v with f = ret keymap }
5763 | Vclose _ -> error "unexpected close in keymap" s spos
5765 and pbookmarks path pan anchor c bookmarks v t spos _ =
5766 match t with
5767 | Vdata | Vcdata -> v
5768 | Vend -> error "unexpected end of input in bookmarks" s spos
5769 | Vopen ("item", attrs, closed) ->
5770 let titleent, spage, srely = bookmark_of attrs in
5771 let page = fromstring int_of_string spos "page" spage 0
5772 and rely = fromstring float_of_string spos "rely" srely 0.0 in
5773 let bookmarks = (unent titleent, 0, (page, rely)) :: bookmarks in
5774 if closed
5775 then { v with f = pbookmarks path pan anchor c bookmarks }
5776 else
5777 let f () = v in
5778 { v with f = skip "item" f }
5780 | Vopen _ ->
5781 error "unexpected subelement in bookmarks" s spos
5783 | Vclose "bookmarks" ->
5784 { v with f = doc path pan anchor c bookmarks }
5786 | Vclose _ -> error "unexpected close in bookmarks" s spos
5788 and skip tag f v t spos _ =
5789 match t with
5790 | Vdata | Vcdata -> v
5791 | Vend ->
5792 error ("unexpected end of input in skipped " ^ tag) s spos
5793 | Vopen (tag', _, closed) ->
5794 if closed
5795 then v
5796 else
5797 let f' () = { v with f = skip tag f } in
5798 { v with f = skip tag' f' }
5799 | Vclose ctag ->
5800 if tag = ctag
5801 then f ()
5802 else error ("unexpected close in skipped " ^ tag) s spos
5805 parse { f = toplevel; accu = () } s;
5806 h, dc;
5809 let do_load f ic =
5811 let len = in_channel_length ic in
5812 let s = String.create len in
5813 really_input ic s 0 len;
5814 f s;
5815 with
5816 | Parse_error (msg, s, pos) ->
5817 let subs = subs s pos in
5818 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
5819 failwith ("parse error: " ^ s)
5821 | exn ->
5822 failwith ("config load error: " ^ Printexc.to_string exn)
5825 let defconfpath =
5826 let dir =
5828 let dir = Filename.concat home ".config" in
5829 if Sys.is_directory dir then dir else home
5830 with _ -> home
5832 Filename.concat dir "llpp.conf"
5835 let confpath = ref defconfpath;;
5837 let load1 f =
5838 if Sys.file_exists !confpath
5839 then
5840 match
5841 (try Some (open_in_bin !confpath)
5842 with exn ->
5843 prerr_endline
5844 ("Error opening configuation file `" ^ !confpath ^ "': " ^
5845 Printexc.to_string exn);
5846 None
5848 with
5849 | Some ic ->
5850 begin try
5851 f (do_load get ic)
5852 with exn ->
5853 prerr_endline
5854 ("Error loading configuation from `" ^ !confpath ^ "': " ^
5855 Printexc.to_string exn);
5856 end;
5857 close_in ic;
5859 | None -> ()
5860 else
5861 f (Hashtbl.create 0, defconf)
5864 let load () =
5865 let f (h, dc) =
5866 let pc, pb, px, pa =
5868 Hashtbl.find h (Filename.basename state.path)
5869 with Not_found -> dc, [], 0, (0, 0.0)
5871 setconf defconf dc;
5872 setconf conf pc;
5873 state.bookmarks <- pb;
5874 state.x <- px;
5875 state.scrollw <- conf.scrollbw;
5876 if conf.jumpback
5877 then state.anchor <- pa;
5878 cbput state.hists.nav pa;
5880 load1 f
5883 let add_attrs bb always dc c =
5884 let ob s a b =
5885 if always || a != b
5886 then Printf.bprintf bb "\n %s='%b'" s a
5887 and oi s a b =
5888 if always || a != b
5889 then Printf.bprintf bb "\n %s='%d'" s a
5890 and oI s a b =
5891 if always || a != b
5892 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
5893 and oz s a b =
5894 if always || a <> b
5895 then Printf.bprintf bb "\n %s='%d'" s (truncate (a*.100.))
5896 and oF s a b =
5897 if always || a <> b
5898 then Printf.bprintf bb "\n %s='%f'" s a
5899 and oc s a b =
5900 if always || a <> b
5901 then
5902 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
5903 and oC s a b =
5904 if always || a <> b
5905 then
5906 Printf.bprintf bb "\n %s='%s'" s (colorspace_to_string a)
5907 and oR s a b =
5908 if always || a <> b
5909 then
5910 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
5911 and os s a b =
5912 if always || a <> b
5913 then
5914 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
5915 and og s a b =
5916 if always || a <> b
5917 then
5918 match a with
5919 | None -> ()
5920 | Some (_N, _A, _B) ->
5921 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
5922 and oW s a b =
5923 if always || a <> b
5924 then
5925 let v =
5926 match a with
5927 | None -> "false"
5928 | Some f ->
5929 if f = infinity
5930 then "true"
5931 else string_of_float f
5933 Printf.bprintf bb "\n %s='%s'" s v
5934 and oco s a b =
5935 if always || a <> b
5936 then
5937 match a with
5938 | Some ((n, a, b), _) when n > 1 ->
5939 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
5940 | _ -> ()
5941 and obeco s a b =
5942 if always || a <> b
5943 then
5944 match a with
5945 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
5946 | _ -> ()
5948 let w, h =
5949 if always
5950 then dc.winw, dc.winh
5951 else
5952 match state.fullscreen with
5953 | Some wh -> wh
5954 | None -> c.winw, c.winh
5956 let zoom, presentation, interpagespace, maxwait =
5957 if always
5958 then dc.zoom, dc.presentation, dc.interpagespace, dc.maxwait
5959 else
5960 match state.mode with
5961 | Birdseye (bc, _, _, _, _) ->
5962 bc.zoom, bc.presentation, bc.interpagespace, bc.maxwait
5963 | _ -> c.zoom, c.presentation, c.interpagespace, c.maxwait
5965 oi "width" w dc.winw;
5966 oi "height" h dc.winh;
5967 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
5968 oi "scroll-handle-height" c.scrollh dc.scrollh;
5969 ob "case-insensitive-search" c.icase dc.icase;
5970 ob "preload" c.preload dc.preload;
5971 oi "page-bias" c.pagebias dc.pagebias;
5972 oi "scroll-step" c.scrollstep dc.scrollstep;
5973 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
5974 ob "max-height-fit" c.maxhfit dc.maxhfit;
5975 ob "crop-hack" c.crophack dc.crophack;
5976 oW "throttle" maxwait dc.maxwait;
5977 ob "highlight-links" c.hlinks dc.hlinks;
5978 ob "under-cursor-info" c.underinfo dc.underinfo;
5979 oi "vertical-margin" interpagespace dc.interpagespace;
5980 oz "zoom" zoom dc.zoom;
5981 ob "presentation" presentation dc.presentation;
5982 oi "rotation-angle" c.angle dc.angle;
5983 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
5984 ob "proportional-display" c.proportional dc.proportional;
5985 oI "pixmap-cache-size" c.memlimit dc.memlimit;
5986 oi "tex-count" c.texcount dc.texcount;
5987 oi "slice-height" c.sliceheight dc.sliceheight;
5988 oi "thumbnail-width" c.thumbw dc.thumbw;
5989 ob "persistent-location" c.jumpback dc.jumpback;
5990 oc "background-color" c.bgcolor dc.bgcolor;
5991 ob "scrollbar-in-presentation" c.scrollbarinpm dc.scrollbarinpm;
5992 oi "tile-width" c.tilew dc.tilew;
5993 oi "tile-height" c.tileh dc.tileh;
5994 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
5995 ob "checkers" c.checkers dc.checkers;
5996 oi "aalevel" c.aalevel dc.aalevel;
5997 ob "trim-margins" c.trimmargins dc.trimmargins;
5998 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
5999 os "uri-launcher" c.urilauncher dc.urilauncher;
6000 os "path-launcher" c.pathlauncher dc.pathlauncher;
6001 oC "color-space" c.colorspace dc.colorspace;
6002 ob "invert-colors" c.invert dc.invert;
6003 oF "brightness" c.colorscale dc.colorscale;
6004 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
6005 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
6006 oco "columns" c.columns dc.columns;
6007 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
6008 os "selection-command" c.selcmd dc.selcmd;
6009 ob "update-cursor" c.updatecurs dc.updatecurs;
6012 let keymapsbuf always dc c =
6013 let bb = Buffer.create 16 in
6014 let rec loop = function
6015 | [] -> ()
6016 | (modename, h) :: rest ->
6017 let dh = findkeyhash dc modename in
6018 if always || h <> dh
6019 then (
6020 if Hashtbl.length h > 0
6021 then (
6022 if Buffer.length bb > 0
6023 then Buffer.add_char bb '\n';
6024 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
6025 Hashtbl.iter (fun i o ->
6026 let isdifferent = always ||
6028 let dO = Hashtbl.find dh i in
6029 dO <> o
6030 with Not_found -> true
6032 if isdifferent
6033 then
6034 let addkm (k, m) =
6035 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
6036 if Wsi.withalt m then Buffer.add_string bb "alt-";
6037 if Wsi.withshift m then Buffer.add_string bb "shift-";
6038 if Wsi.withmeta m then Buffer.add_string bb "meta-";
6039 Buffer.add_string bb (Wsi.keyname k);
6041 let addkms l =
6042 let rec loop = function
6043 | [] -> ()
6044 | km :: [] -> addkm km
6045 | km :: rest -> addkm km; Buffer.add_char bb ' '; loop rest
6047 loop l
6049 Buffer.add_string bb "<map in='";
6050 addkm i;
6051 match o with
6052 | KMinsrt km ->
6053 Buffer.add_char bb '\'';
6054 Buffer.add_string bb " out='";
6055 addkm km;
6056 Buffer.add_string bb "'/>\n"
6058 | KMinsrl kms ->
6059 Buffer.add_char bb '\'';
6060 Buffer.add_string bb " out='";
6061 addkms kms;
6062 Buffer.add_string bb "'/>\n"
6064 | KMmulti (ins, kms) ->
6065 Buffer.add_char bb ' ';
6066 addkms ins;
6067 Buffer.add_char bb '\'';
6068 Buffer.add_string bb " out='";
6069 addkms kms;
6070 Buffer.add_string bb "'/>\n"
6071 ) h;
6072 Buffer.add_string bb "</keymap>";
6075 loop rest
6077 loop c.keyhashes;
6081 let save () =
6082 let uifontsize = fstate.fontsize in
6083 let bb = Buffer.create 32768 in
6084 let f (h, dc) =
6085 let dc = if conf.bedefault then conf else dc in
6086 Buffer.add_string bb "<llppconfig>\n";
6088 if String.length !fontpath > 0
6089 then
6090 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
6091 uifontsize
6092 !fontpath
6093 else (
6094 if uifontsize <> 14
6095 then
6096 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
6099 Buffer.add_string bb "<defaults ";
6100 add_attrs bb true dc dc;
6101 let kb = keymapsbuf true dc dc in
6102 if Buffer.length kb > 0
6103 then (
6104 Buffer.add_string bb ">\n";
6105 Buffer.add_buffer bb kb;
6106 Buffer.add_string bb "\n</defaults>\n";
6108 else Buffer.add_string bb "/>\n";
6110 let adddoc path pan anchor c bookmarks =
6111 if bookmarks == [] && c = dc && anchor = emptyanchor
6112 then ()
6113 else (
6114 Printf.bprintf bb "<doc path='%s'"
6115 (enent path 0 (String.length path));
6117 if anchor <> emptyanchor
6118 then (
6119 let n, y = anchor in
6120 Printf.bprintf bb " page='%d'" n;
6121 if y > 1e-6
6122 then
6123 Printf.bprintf bb " rely='%f'" y
6127 if pan != 0
6128 then Printf.bprintf bb " pan='%d'" pan;
6130 add_attrs bb false dc c;
6131 let kb = keymapsbuf false dc c in
6133 begin match bookmarks with
6134 | [] ->
6135 if Buffer.length kb > 0
6136 then (
6137 Buffer.add_string bb ">\n";
6138 Buffer.add_buffer bb kb;
6139 Buffer.add_string bb "</doc>\n";
6141 else Buffer.add_string bb "/>\n"
6142 | _ ->
6143 Buffer.add_string bb ">\n<bookmarks>\n";
6144 List.iter (fun (title, _level, (page, rely)) ->
6145 Printf.bprintf bb
6146 "<item title='%s' page='%d'"
6147 (enent title 0 (String.length title))
6148 page
6150 if rely > 1e-6
6151 then
6152 Printf.bprintf bb " rely='%f'" rely
6154 Buffer.add_string bb "/>\n";
6155 ) bookmarks;
6156 Buffer.add_string bb "</bookmarks>";
6157 if Buffer.length kb > 0
6158 then (
6159 Buffer.add_string bb "\n";
6160 Buffer.add_buffer bb kb;
6162 Buffer.add_string bb "\n</doc>\n";
6163 end;
6167 let pan, conf =
6168 match state.mode with
6169 | Birdseye (c, pan, _, _, _) ->
6170 let beyecolumns =
6171 match conf.columns with
6172 | Some ((c, _, _), _) -> Some c
6173 | None -> None
6174 and columns =
6175 match c.columns with
6176 | Some (c, _) -> Some (c, [||])
6177 | None -> None
6179 pan, { c with beyecolumns = beyecolumns; columns = columns }
6180 | _ -> state.x, conf
6182 let basename = Filename.basename state.path in
6183 adddoc basename pan (getanchor ())
6184 { conf with
6185 autoscrollstep =
6186 match state.autoscroll with
6187 | Some step -> step
6188 | None -> conf.autoscrollstep }
6189 (if conf.savebmarks then state.bookmarks else []);
6191 Hashtbl.iter (fun path (c, bookmarks, x, y) ->
6192 if basename <> path
6193 then adddoc path x y c bookmarks
6194 ) h;
6195 Buffer.add_string bb "</llppconfig>";
6197 load1 f;
6198 if Buffer.length bb > 0
6199 then
6201 let tmp = !confpath ^ ".tmp" in
6202 let oc = open_out_bin tmp in
6203 Buffer.output_buffer oc bb;
6204 close_out oc;
6205 Unix.rename tmp !confpath;
6206 with exn ->
6207 prerr_endline
6208 ("error while saving configuration: " ^ Printexc.to_string exn)
6210 end;;
6212 let () =
6213 Arg.parse
6214 (Arg.align
6215 [("-p", Arg.String (fun s -> state.password <- s) ,
6216 "<password> Set password");
6218 ("-f", Arg.String (fun s -> Config.fontpath := s),
6219 "<path> Set path to the user interface font");
6221 ("-c", Arg.String (fun s -> Config.confpath := s),
6222 "<path> Set path to the configuration file");
6224 ("-v", Arg.Unit (fun () ->
6225 Printf.printf
6226 "%s\nconfiguration path: %s\n"
6227 (version ())
6228 Config.defconfpath
6230 exit 0), " Print version and exit");
6233 (fun s -> state.path <- s)
6234 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
6236 if String.length state.path = 0
6237 then (prerr_endline "file name missing"; exit 1);
6239 Config.load ();
6241 let globalkeyhash = findkeyhash conf "global" in
6242 state.wsfd <- Wsi.init (object
6243 method display = display ()
6244 method reshape w h = reshape w h
6245 method mouse b d x y m = mouse b d x y m
6246 method motion x y = state.mpos <- (x, y); motion x y
6247 method pmotion x y = state.mpos <- (x, y); pmotion x y
6248 method key k m =
6249 match state.keystate with
6250 | KSnone ->
6251 let km = k, m in
6252 begin
6253 match
6254 try Hashtbl.find globalkeyhash km
6255 with Not_found ->
6256 let modehash = state.uioh#modehash in
6257 try Hashtbl.find modehash km
6258 with Not_found -> KMinsrt (k, m)
6259 with
6260 | KMinsrt (k, m) -> keyboard k m
6261 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
6262 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
6264 | KSinto ((k', m') :: [], insrt) when k'=k && m' land m = m' ->
6265 List.iter (fun (k, m) -> keyboard k m) insrt;
6266 state.keystate <- KSnone
6267 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land m = m' ->
6268 state.keystate <- KSinto (keys, insrt)
6269 | _ ->
6270 state.keystate <- KSnone
6272 method enter x y = state.mpos <- (x, y); pmotion x y
6273 method leave = state.mpos <- (-1, -1)
6274 method quit = raise Quit
6275 end) conf.winw conf.winh;
6277 if not (
6278 List.exists GlMisc.check_extension
6279 [ "GL_ARB_texture_rectangle"
6280 ; "GL_EXT_texture_recangle"
6281 ; "GL_NV_texture_rectangle" ]
6283 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
6285 let cr, sw = Unix.pipe ()
6286 and sr, cw = Unix.pipe () in
6288 cloexec cr;
6289 cloexec sw;
6290 cloexec sr;
6291 cloexec cw;
6293 setcheckers conf.checkers;
6294 redirectstderr ();
6296 init (cr, cw) (
6297 conf.angle, conf.proportional, (conf.trimmargins, conf.trimfuzz),
6298 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
6299 !Config.fontpath
6301 state.sr <- sr;
6302 state.sw <- sw;
6303 state.text <- "Opening " ^ state.path;
6304 setaalevel conf.aalevel;
6305 writeopen state.path state.password;
6306 state.uioh <- uioh;
6307 setfontsize fstate.fontsize;
6308 doreshape conf.winw conf.winh;
6310 let rec loop deadline =
6311 let r =
6312 match state.errfd with
6313 | None -> [state.sr; state.wsfd]
6314 | Some fd -> [state.sr; state.wsfd; fd]
6316 if state.redisplay
6317 then (
6318 state.redisplay <- false;
6319 display ();
6321 let timeout =
6322 let now = now () in
6323 if deadline > now
6324 then (
6325 if deadline = infinity
6326 then ~-.1.0
6327 else max 0.0 (deadline -. now)
6329 else 0.0
6331 let r, _, _ =
6332 try Unix.select r [] [] timeout
6333 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
6335 begin match r with
6336 | [] ->
6337 state.ghyll None;
6338 let newdeadline =
6339 if state.ghyll == noghyll
6340 then
6341 match state.autoscroll with
6342 | Some step when step != 0 ->
6343 let y = state.y + step in
6344 let y =
6345 if y < 0
6346 then state.maxy
6347 else if y >= state.maxy then 0 else y
6349 gotoy y;
6350 if state.mode = View
6351 then state.text <- "";
6352 deadline +. 0.01
6353 | _ -> infinity
6354 else deadline +. 0.01
6356 loop newdeadline
6358 | l ->
6359 let rec checkfds = function
6360 | [] -> ()
6361 | fd :: rest when fd = state.sr ->
6362 let cmd = readcmd state.sr in
6363 act cmd;
6364 checkfds rest
6366 | fd :: rest when fd = state.wsfd ->
6367 Wsi.readresp fd;
6368 checkfds rest
6370 | fd :: rest ->
6371 let s = String.create 80 in
6372 let n = Unix.read fd s 0 80 in
6373 if conf.redirectstderr
6374 then (
6375 Buffer.add_substring state.errmsgs s 0 n;
6376 state.newerrmsgs <- true;
6377 state.redisplay <- true;
6379 else (
6380 prerr_string (String.sub s 0 n);
6381 flush stderr;
6383 checkfds rest
6385 checkfds l;
6386 let newdeadline =
6387 let deadline1 =
6388 if deadline = infinity
6389 then now () +. 0.01
6390 else deadline
6392 match state.autoscroll with
6393 | Some step when step != 0 -> deadline1
6394 | _ -> if state.ghyll == noghyll then infinity else deadline1
6396 loop newdeadline
6397 end;
6400 loop infinity;
6401 with Quit ->
6402 wcmd "quit";
6403 Config.save ();
6404 exit 0;