Usability
[llpp.git] / main.ml
blob41855a982506cf07265ca5d712b699bff92018ee
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 * string)
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, pattern)) ->
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, pattern)
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 conf.proportional <- proportional;
2204 invalidate ();
2205 wcmd "reqlayout %d %d" conf.angle (btod proportional);
2206 | _ -> ()
2209 let settrim trimmargins trimfuzz =
2210 if state.invalidated = 0 then state.anchor <- getanchor ();
2211 conf.trimmargins <- trimmargins;
2212 conf.trimfuzz <- trimfuzz;
2213 let x0, y0, x1, y1 = trimfuzz in
2214 invalidate ();
2215 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1;
2216 Hashtbl.iter (fun _ opaque ->
2217 wcmd "freepage %s" opaque;
2218 ) state.pagemap;
2219 Hashtbl.clear state.pagemap;
2222 let setzoom zoom =
2223 match state.throttle with
2224 | None ->
2225 let zoom = max 0.01 zoom in
2226 if zoom <> conf.zoom
2227 then (
2228 state.prevzoom <- conf.zoom;
2229 let relx =
2230 if zoom <= 1.0
2231 then (state.x <- 0; 0.0)
2232 else float state.x /. float state.w
2234 conf.zoom <- zoom;
2235 reshape conf.winw conf.winh;
2236 if zoom > 1.0
2237 then (
2238 let x = relx *. float state.w in
2239 state.x <- truncate x;
2241 state.text <- Printf.sprintf "zoom is now %-5.1f" (zoom *. 100.0);
2244 | Some (layout, y, started) ->
2245 let time =
2246 match conf.maxwait with
2247 | None -> 0.0
2248 | Some t -> t
2250 let dt = now () -. started in
2251 if dt > time
2252 then (
2253 state.y <- y;
2254 load layout;
2258 let setcolumns columns coverA coverB =
2259 if columns < 2
2260 then (
2261 conf.columns <- None;
2262 state.x <- 0;
2263 setzoom 1.0;
2265 else (
2266 conf.columns <- Some ((columns, coverA, coverB), [||]);
2267 conf.zoom <- 1.0;
2269 reshape conf.winw conf.winh;
2272 let enterbirdseye () =
2273 let zoom = float conf.thumbw /. float conf.winw in
2274 let birdseyepageno =
2275 let cy = conf.winh / 2 in
2276 let fold = function
2277 | [] -> 0
2278 | l :: rest ->
2279 let rec fold best = function
2280 | [] -> best.pageno
2281 | l :: rest ->
2282 let d = cy - (l.pagedispy + l.pagevh/2)
2283 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2284 if abs d < abs dbest
2285 then fold l rest
2286 else best.pageno
2287 in fold l rest
2289 fold state.layout
2291 state.mode <- Birdseye (
2292 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2294 conf.zoom <- zoom;
2295 conf.presentation <- false;
2296 conf.interpagespace <- 10;
2297 conf.hlinks <- false;
2298 state.x <- 0;
2299 state.mstate <- Mnone;
2300 conf.maxwait <- None;
2301 conf.columns <- (
2302 match conf.beyecolumns with
2303 | Some c ->
2304 conf.zoom <- 1.0;
2305 Some ((c, 0, 0), [||])
2306 | None -> None
2308 Wsi.setcursor Wsi.CURSOR_INHERIT;
2309 if conf.verbose
2310 then
2311 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2312 (100.0*.zoom)
2313 else
2314 state.text <- ""
2316 reshape conf.winw conf.winh;
2319 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2320 state.mode <- View;
2321 conf.zoom <- c.zoom;
2322 conf.presentation <- c.presentation;
2323 conf.interpagespace <- c.interpagespace;
2324 conf.maxwait <- c.maxwait;
2325 conf.hlinks <- c.hlinks;
2326 conf.beyecolumns <- (
2327 match conf.columns with
2328 | Some ((c, _, _), _) -> Some c
2329 | None -> None
2331 conf.columns <- (
2332 match c.columns with
2333 | Some (c, _) -> Some (c, [||])
2334 | None -> None
2336 state.x <- leftx;
2337 if conf.verbose
2338 then
2339 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2340 (100.0*.conf.zoom)
2342 reshape conf.winw conf.winh;
2343 state.anchor <- if goback then anchor else (pageno, 0.0);
2346 let togglebirdseye () =
2347 match state.mode with
2348 | Birdseye vals -> leavebirdseye vals true
2349 | View -> enterbirdseye ()
2350 | _ -> ()
2353 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2354 let pageno = max 0 (pageno - incr) in
2355 let rec loop = function
2356 | [] -> gotopage1 pageno 0
2357 | l :: _ when l.pageno = pageno ->
2358 if l.pagedispy >= 0 && l.pagey = 0
2359 then G.postRedisplay "upbirdseye"
2360 else gotopage1 pageno 0
2361 | _ :: rest -> loop rest
2363 loop state.layout;
2364 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2367 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2368 let pageno = min (state.pagecount - 1) (pageno + incr) in
2369 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2370 let rec loop = function
2371 | [] ->
2372 let y, h = getpageyh pageno in
2373 let dy = (y - state.y) - (conf.winh - h - conf.interpagespace) in
2374 gotoy (clamp dy)
2375 | l :: _ when l.pageno = pageno ->
2376 if l.pagevh != l.pageh
2377 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
2378 else G.postRedisplay "downbirdseye"
2379 | _ :: rest -> loop rest
2381 loop state.layout
2384 let optentry mode _ key =
2385 let btos b = if b then "on" else "off" in
2386 if key >= 32 && key < 127
2387 then
2388 let c = Char.chr key in
2389 match c with
2390 | 's' ->
2391 let ondone s =
2392 try conf.scrollstep <- int_of_string s with exc ->
2393 state.text <- Printf.sprintf "bad integer `%s': %s"
2394 s (Printexc.to_string exc)
2396 TEswitch ("scroll step: ", "", None, intentry, ondone)
2398 | 'A' ->
2399 let ondone s =
2401 conf.autoscrollstep <- int_of_string s;
2402 if state.autoscroll <> None
2403 then state.autoscroll <- Some conf.autoscrollstep
2404 with exc ->
2405 state.text <- Printf.sprintf "bad integer `%s': %s"
2406 s (Printexc.to_string exc)
2408 TEswitch ("auto scroll step: ", "", None, intentry, ondone)
2410 | 'C' ->
2411 let ondone s =
2413 let n, a, b = columns_of_string s in
2414 setcolumns n a b;
2415 with exc ->
2416 state.text <- Printf.sprintf "bad columns `%s': %s"
2417 s (Printexc.to_string exc)
2419 TEswitch ("columns: ", "", None, textentry, ondone)
2421 | 'Z' ->
2422 let ondone s =
2424 let zoom = float (int_of_string s) /. 100.0 in
2425 setzoom zoom
2426 with exc ->
2427 state.text <- Printf.sprintf "bad integer `%s': %s"
2428 s (Printexc.to_string exc)
2430 TEswitch ("zoom: ", "", None, intentry, ondone)
2432 | 't' ->
2433 let ondone s =
2435 conf.thumbw <- bound (int_of_string s) 2 4096;
2436 state.text <-
2437 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
2438 begin match mode with
2439 | Birdseye beye ->
2440 leavebirdseye beye false;
2441 enterbirdseye ();
2442 | _ -> ();
2444 with exc ->
2445 state.text <- Printf.sprintf "bad integer `%s': %s"
2446 s (Printexc.to_string exc)
2448 TEswitch ("thumbnail width: ", "", None, intentry, ondone)
2450 | 'R' ->
2451 let ondone s =
2452 match try
2453 Some (int_of_string s)
2454 with exc ->
2455 state.text <- Printf.sprintf "bad integer `%s': %s"
2456 s (Printexc.to_string exc);
2457 None
2458 with
2459 | Some angle -> reqlayout angle conf.proportional
2460 | None -> ()
2462 TEswitch ("rotation: ", "", None, intentry, ondone)
2464 | 'i' ->
2465 conf.icase <- not conf.icase;
2466 TEdone ("case insensitive search " ^ (btos conf.icase))
2468 | 'p' ->
2469 conf.preload <- not conf.preload;
2470 gotoy state.y;
2471 TEdone ("preload " ^ (btos conf.preload))
2473 | 'v' ->
2474 conf.verbose <- not conf.verbose;
2475 TEdone ("verbose " ^ (btos conf.verbose))
2477 | 'd' ->
2478 conf.debug <- not conf.debug;
2479 TEdone ("debug " ^ (btos conf.debug))
2481 | 'h' ->
2482 conf.maxhfit <- not conf.maxhfit;
2483 state.maxy <-
2484 state.maxy + (if conf.maxhfit then -conf.winh else conf.winh);
2485 TEdone ("maxhfit " ^ (btos conf.maxhfit))
2487 | 'c' ->
2488 conf.crophack <- not conf.crophack;
2489 TEdone ("crophack " ^ btos conf.crophack)
2491 | 'a' ->
2492 let s =
2493 match conf.maxwait with
2494 | None ->
2495 conf.maxwait <- Some infinity;
2496 "always wait for page to complete"
2497 | Some _ ->
2498 conf.maxwait <- None;
2499 "show placeholder if page is not ready"
2501 TEdone s
2503 | 'f' ->
2504 conf.underinfo <- not conf.underinfo;
2505 TEdone ("underinfo " ^ btos conf.underinfo)
2507 | 'P' ->
2508 conf.savebmarks <- not conf.savebmarks;
2509 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
2511 | 'S' ->
2512 let ondone s =
2514 let pageno, py =
2515 match state.layout with
2516 | [] -> 0, 0
2517 | l :: _ ->
2518 l.pageno, l.pagey
2520 conf.interpagespace <- int_of_string s;
2521 state.maxy <- calcheight ();
2522 let y = getpagey pageno in
2523 gotoy (y + py)
2524 with exc ->
2525 state.text <- Printf.sprintf "bad integer `%s': %s"
2526 s (Printexc.to_string exc)
2528 TEswitch ("vertical margin: ", "", None, intentry, ondone)
2530 | 'l' ->
2531 reqlayout conf.angle (not conf.proportional);
2532 TEdone ("proportional display " ^ btos conf.proportional)
2534 | 'T' ->
2535 settrim (not conf.trimmargins) conf.trimfuzz;
2536 TEdone ("trim margins " ^ btos conf.trimmargins)
2538 | 'I' ->
2539 conf.invert <- not conf.invert;
2540 TEdone ("invert colors " ^ btos conf.invert)
2542 | 'x' ->
2543 let ondone s =
2544 cbput state.hists.sel s;
2545 conf.selcmd <- s;
2547 TEswitch ("selection command: ", "", Some (onhist state.hists.sel),
2548 textentry, ondone)
2550 | _ ->
2551 state.text <- Printf.sprintf "bad option %d `%c'" key c;
2552 TEstop
2553 else
2554 TEcont state.text
2557 class type lvsource = object
2558 method getitemcount : int
2559 method getitem : int -> (string * int)
2560 method hasaction : int -> bool
2561 method exit :
2562 uioh:uioh ->
2563 cancel:bool ->
2564 active:int ->
2565 first:int ->
2566 pan:int ->
2567 qsearch:string ->
2568 uioh option
2569 method getactive : int
2570 method getfirst : int
2571 method getqsearch : string
2572 method setqsearch : string -> unit
2573 method getpan : int
2574 end;;
2576 class virtual lvsourcebase = object
2577 val mutable m_active = 0
2578 val mutable m_first = 0
2579 val mutable m_qsearch = ""
2580 val mutable m_pan = 0
2581 method getactive = m_active
2582 method getfirst = m_first
2583 method getqsearch = m_qsearch
2584 method getpan = m_pan
2585 method setqsearch s = m_qsearch <- s
2586 end;;
2588 let withoutlastutf8 s =
2589 let len = String.length s in
2590 if len = 0
2591 then s
2592 else
2593 let rec find pos =
2594 if pos = 0
2595 then pos
2596 else
2597 let b = Char.code s.[pos] in
2598 if b land 0b110000 = 0b11000000
2599 then find (pos-1)
2600 else pos-1
2602 let first =
2603 if Char.code s.[len-1] land 0x80 = 0
2604 then len-1
2605 else find (len-1)
2607 String.sub s 0 first;
2610 let textentrykeyboard key _mask ((c, text, opthist, onkey, ondone), onleave) =
2611 let enttext te =
2612 state.mode <- Textentry (te, onleave);
2613 state.text <- "";
2614 enttext ();
2615 G.postRedisplay "textentrykeyboard enttext";
2617 let histaction cmd =
2618 match opthist with
2619 | None -> ()
2620 | Some (action, _) ->
2621 state.mode <- Textentry (
2622 (c, action cmd, opthist, onkey, ondone), onleave
2624 G.postRedisplay "textentry histaction"
2626 match key with
2627 | 0xff08 -> (* backspace *)
2628 let s = withoutlastutf8 text in
2629 let len = String.length s in
2630 if len = 0
2631 then (
2632 onleave Cancel;
2633 G.postRedisplay "textentrykeyboard after cancel";
2635 else (
2636 enttext (c, s, opthist, onkey, ondone)
2639 | 0xff0d ->
2640 ondone text;
2641 onleave Confirm;
2642 G.postRedisplay "textentrykeyboard after confirm"
2644 | 0xff52 -> histaction HCprev
2645 | 0xff54 -> histaction HCnext
2646 | 0xff50 -> histaction HCfirst
2647 | 0xff57 -> histaction HClast
2649 | 0xff1b -> (* escape*)
2650 if String.length text = 0
2651 then (
2652 begin match opthist with
2653 | None -> ()
2654 | Some (_, onhistcancel) -> onhistcancel ()
2655 end;
2656 onleave Cancel;
2657 state.text <- "";
2658 G.postRedisplay "textentrykeyboard after cancel2"
2660 else (
2661 enttext (c, "", opthist, onkey, ondone)
2664 | 0xff9f | 0xffff -> () (* delete *)
2666 | _ when key != 0 && key land 0xff00 != 0xff00 ->
2667 begin match onkey text key with
2668 | TEdone text ->
2669 ondone text;
2670 onleave Confirm;
2671 G.postRedisplay "textentrykeyboard after confirm2";
2673 | TEcont text ->
2674 enttext (c, text, opthist, onkey, ondone);
2676 | TEstop ->
2677 onleave Cancel;
2678 G.postRedisplay "textentrykeyboard after cancel3"
2680 | TEswitch te ->
2681 state.mode <- Textentry (te, onleave);
2682 G.postRedisplay "textentrykeyboard switch";
2683 end;
2685 | _ ->
2686 vlog "unhandled key %s" (Wsi.keyname key)
2689 let firstof first active =
2690 if first > active || abs (first - active) > fstate.maxrows - 1
2691 then max 0 (active - (fstate.maxrows/2))
2692 else first
2695 let calcfirst first active =
2696 if active > first
2697 then
2698 let rows = active - first in
2699 if rows > fstate.maxrows then active - fstate.maxrows else first
2700 else active
2703 let scrollph y maxy =
2704 let sh = (float (maxy + conf.winh) /. float conf.winh) in
2705 let sh = float conf.winh /. sh in
2706 let sh = max sh (float conf.scrollh) in
2708 let percent =
2709 if y = state.maxy
2710 then 1.0
2711 else float y /. float maxy
2713 let position = (float conf.winh -. sh) *. percent in
2715 let position =
2716 if position +. sh > float conf.winh
2717 then float conf.winh -. sh
2718 else position
2720 position, sh;
2723 let coe s = (s :> uioh);;
2725 class listview ~(source:lvsource) ~trusted ~modehash =
2726 object (self)
2727 val m_pan = source#getpan
2728 val m_first = source#getfirst
2729 val m_active = source#getactive
2730 val m_qsearch = source#getqsearch
2731 val m_prev_uioh = state.uioh
2733 method private elemunder y =
2734 let n = y / (fstate.fontsize+1) in
2735 if m_first + n < source#getitemcount
2736 then (
2737 if source#hasaction (m_first + n)
2738 then Some (m_first + n)
2739 else None
2741 else None
2743 method display =
2744 Gl.enable `blend;
2745 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2746 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2747 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
2748 GlDraw.color (1., 1., 1.);
2749 Gl.enable `texture_2d;
2750 let fs = fstate.fontsize in
2751 let nfs = fs + 1 in
2752 let ww = fstate.wwidth in
2753 let tabw = 30.0*.ww in
2754 let itemcount = source#getitemcount in
2755 let rec loop row =
2756 if (row - m_first) * nfs > conf.winh
2757 then ()
2758 else (
2759 if row >= 0 && row < itemcount
2760 then (
2761 let (s, level) = source#getitem row in
2762 let y = (row - m_first) * nfs in
2763 let x = 5.0 +. float (level + m_pan) *. ww in
2764 if row = m_active
2765 then (
2766 Gl.disable `texture_2d;
2767 GlDraw.polygon_mode `both `line;
2768 GlDraw.color (1., 1., 1.) ~alpha:0.9;
2769 GlDraw.rect (1., float (y + 1))
2770 (float (conf.winw - conf.scrollbw - 1), float (y + fs + 3));
2771 GlDraw.polygon_mode `both `fill;
2772 GlDraw.color (1., 1., 1.);
2773 Gl.enable `texture_2d;
2776 let drawtabularstring s =
2777 let drawstr x s = drawstring1 fs (truncate x) (y+nfs) s in
2778 if trusted
2779 then
2780 let tabpos = try String.index s '\t' with Not_found -> -1 in
2781 if tabpos > 0
2782 then
2783 let len = String.length s - tabpos - 1 in
2784 let s1 = String.sub s 0 tabpos
2785 and s2 = String.sub s (tabpos + 1) len in
2786 let nx = drawstr x s1 in
2787 let sw = nx -. x in
2788 let x = x +. (max tabw sw) in
2789 drawstr x s2
2790 else
2791 drawstr x s
2792 else
2793 drawstr x s
2795 let _ = drawtabularstring s in
2796 loop (row+1)
2800 loop m_first;
2801 Gl.disable `blend;
2802 Gl.disable `texture_2d;
2804 method updownlevel incr =
2805 let len = source#getitemcount in
2806 let curlevel =
2807 if m_active >= 0 && m_active < len
2808 then snd (source#getitem m_active)
2809 else -1
2811 let rec flow i =
2812 if i = len then i-1 else if i = -1 then 0 else
2813 let _, l = source#getitem i in
2814 if l != curlevel then i else flow (i+incr)
2816 let active = flow m_active in
2817 let first = calcfirst m_first active in
2818 G.postRedisplay "outline updownlevel";
2819 {< m_active = active; m_first = first >}
2821 method private key1 key mask =
2822 let set1 active first qsearch =
2823 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
2825 let search active pattern incr =
2826 let dosearch re =
2827 let rec loop n =
2828 if n >= 0 && n < source#getitemcount
2829 then (
2830 let s, _ = source#getitem n in
2832 (try ignore (Str.search_forward re s 0); true
2833 with Not_found -> false)
2834 then Some n
2835 else loop (n + incr)
2837 else None
2839 loop active
2842 let re = Str.regexp_case_fold pattern in
2843 dosearch re
2844 with Failure s ->
2845 state.text <- s;
2846 None
2848 let itemcount = source#getitemcount in
2849 let find start incr =
2850 let rec find i =
2851 if i = -1 || i = itemcount
2852 then -1
2853 else (
2854 if source#hasaction i
2855 then i
2856 else find (i + incr)
2859 find start
2861 let set active first =
2862 let first = bound first 0 (itemcount - fstate.maxrows) in
2863 state.text <- "";
2864 coe {< m_active = active; m_first = first >}
2866 let navigate incr =
2867 let isvisible first n = n >= first && n - first <= fstate.maxrows in
2868 let active, first =
2869 let incr1 = if incr > 0 then 1 else -1 in
2870 if isvisible m_first m_active
2871 then
2872 let next =
2873 let next = m_active + incr in
2874 let next =
2875 if next < 0 || next >= itemcount
2876 then -1
2877 else find next incr1
2879 if next = -1 || abs (m_active - next) > fstate.maxrows
2880 then -1
2881 else next
2883 if next = -1
2884 then
2885 let first = m_first + incr in
2886 let first = bound first 0 (itemcount - 1) in
2887 let next =
2888 let next = m_active + incr in
2889 let next = bound next 0 (itemcount - 1) in
2890 find next ~-incr1
2892 let active = if next = -1 then m_active else next in
2893 active, first
2894 else
2895 let first = min next m_first in
2896 let first =
2897 if abs (next - first) > fstate.maxrows
2898 then first + incr
2899 else first
2901 next, first
2902 else
2903 let first = m_first + incr in
2904 let first = bound first 0 (itemcount - 1) in
2905 let active =
2906 let next = m_active + incr in
2907 let next = bound next 0 (itemcount - 1) in
2908 let next = find next incr1 in
2909 let active =
2910 if next = -1 || abs (m_active - first) > fstate.maxrows
2911 then (
2912 let active = if m_active = -1 then next else m_active in
2913 active
2915 else next
2917 if isvisible first active
2918 then active
2919 else -1
2921 active, first
2923 G.postRedisplay "listview navigate";
2924 set active first;
2926 match key with
2927 | (0x72|0x73) when Wsi.withctrl mask -> (* ctrl-r/ctlr-s *)
2928 let incr = if key = 0x72 then -1 else 1 in
2929 let active, first =
2930 match search (m_active + incr) m_qsearch incr with
2931 | None ->
2932 state.text <- m_qsearch ^ " [not found]";
2933 m_active, m_first
2934 | Some active ->
2935 state.text <- m_qsearch;
2936 active, firstof m_first active
2938 G.postRedisplay "listview ctrl-r/s";
2939 set1 active first m_qsearch;
2941 | 0xff08 -> (* backspace *)
2942 if String.length m_qsearch = 0
2943 then coe self
2944 else (
2945 let qsearch = withoutlastutf8 m_qsearch in
2946 let len = String.length qsearch in
2947 if len = 0
2948 then (
2949 state.text <- "";
2950 G.postRedisplay "listview empty qsearch";
2951 set1 m_active m_first "";
2953 else
2954 let active, first =
2955 match search m_active qsearch ~-1 with
2956 | None ->
2957 state.text <- qsearch ^ " [not found]";
2958 m_active, m_first
2959 | Some active ->
2960 state.text <- qsearch;
2961 active, firstof m_first active
2963 G.postRedisplay "listview backspace qsearch";
2964 set1 active first qsearch
2967 | key when (key != 0 && key land 0xff00 != 0xff00) ->
2968 let pattern = m_qsearch ^ Wsi.toutf8 key in
2969 let active, first =
2970 match search m_active pattern 1 with
2971 | None ->
2972 state.text <- pattern ^ " [not found]";
2973 m_active, m_first
2974 | Some active ->
2975 state.text <- pattern;
2976 active, firstof m_first active
2978 G.postRedisplay "listview qsearch add";
2979 set1 active first pattern;
2981 | 0xff1b -> (* escape *)
2982 state.text <- "";
2983 if String.length m_qsearch = 0
2984 then (
2985 G.postRedisplay "list view escape";
2986 begin
2987 match
2988 source#exit (coe self) true m_active m_first m_pan m_qsearch
2989 with
2990 | None -> m_prev_uioh
2991 | Some uioh -> uioh
2994 else (
2995 G.postRedisplay "list view kill qsearch";
2996 source#setqsearch "";
2997 coe {< m_qsearch = "" >}
3000 | 0xff0d -> (* return *)
3001 state.text <- "";
3002 let self = {< m_qsearch = "" >} in
3003 source#setqsearch "";
3004 let opt =
3005 G.postRedisplay "listview enter";
3006 if m_active >= 0 && m_active < source#getitemcount
3007 then (
3008 source#exit (coe self) false m_active m_first m_pan "";
3010 else (
3011 source#exit (coe self) true m_active m_first m_pan "";
3014 begin match opt with
3015 | None -> m_prev_uioh
3016 | Some uioh -> uioh
3019 | 0xff9f | 0xffff -> (* delete *)
3020 coe self
3022 | 0xff52 -> navigate ~-1 (* up *)
3023 | 0xff54 -> navigate 1 (* down *)
3024 | 0xff55 -> navigate ~-(fstate.maxrows) (* prior *)
3025 | 0xff56 -> navigate fstate.maxrows (* next *)
3027 | 0xff53 -> (* right *)
3028 state.text <- "";
3029 G.postRedisplay "listview right";
3030 coe {< m_pan = m_pan - 1 >}
3032 | 0xff51 -> (* left *)
3033 state.text <- "";
3034 G.postRedisplay "listview left";
3035 coe {< m_pan = m_pan + 1 >}
3037 | 0xff50 -> (* home *)
3038 let active = find 0 1 in
3039 G.postRedisplay "listview home";
3040 set active 0;
3042 | 0xff57 -> (* end *)
3043 let first = max 0 (itemcount - fstate.maxrows) in
3044 let active = find (itemcount - 1) ~-1 in
3045 G.postRedisplay "listview end";
3046 set active first;
3048 | key when (key = 0 || key land 0xff00 = 0xff00) ->
3049 coe self
3051 | _ ->
3052 dolog "listview unknown key %#x" key; coe self
3054 method key key mask =
3055 match state.mode with
3056 | Textentry te -> textentrykeyboard key mask te; coe self
3057 | _ -> self#key1 key mask
3059 method button button down x y _ =
3060 let opt =
3061 match button with
3062 | 1 when x > conf.winw - conf.scrollbw ->
3063 G.postRedisplay "listview scroll";
3064 if down
3065 then
3066 let _, position, sh = self#scrollph in
3067 if y > truncate position && y < truncate (position +. sh)
3068 then (
3069 state.mstate <- Mscrolly;
3070 Some (coe self)
3072 else
3073 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
3074 let first = truncate (s *. float source#getitemcount) in
3075 let first = min source#getitemcount first in
3076 Some (coe {< m_first = first; m_active = first >})
3077 else (
3078 state.mstate <- Mnone;
3079 Some (coe self);
3081 | 1 when not down ->
3082 begin match self#elemunder y with
3083 | Some n ->
3084 G.postRedisplay "listview click";
3085 source#exit
3086 (coe {< m_active = n >}) false n m_first m_pan m_qsearch
3087 | _ ->
3088 Some (coe self)
3090 | n when (n == 4 || n == 5) && not down ->
3091 let len = source#getitemcount in
3092 let first =
3093 if n = 5 && m_first + fstate.maxrows >= len
3094 then
3095 m_first
3096 else
3097 let first = m_first + (if n == 4 then -1 else 1) in
3098 bound first 0 (len - 1)
3100 G.postRedisplay "listview wheel";
3101 Some (coe {< m_first = first >})
3102 | _ ->
3103 Some (coe self)
3105 match opt with
3106 | None -> m_prev_uioh
3107 | Some uioh -> uioh
3109 method motion _ y =
3110 match state.mstate with
3111 | Mscrolly ->
3112 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
3113 let first = truncate (s *. float source#getitemcount) in
3114 let first = min source#getitemcount first in
3115 G.postRedisplay "listview motion";
3116 coe {< m_first = first; m_active = first >}
3117 | _ -> coe self
3119 method pmotion x y =
3120 if x < conf.winw - conf.scrollbw
3121 then
3122 let n =
3123 match self#elemunder y with
3124 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3125 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3127 let o =
3128 if n != m_active
3129 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3130 else self
3132 coe o
3133 else (
3134 Wsi.setcursor Wsi.CURSOR_INHERIT;
3135 coe self
3138 method infochanged _ = ()
3140 method scrollpw = (0, 0.0, 0.0)
3141 method scrollph =
3142 let nfs = fstate.fontsize + 1 in
3143 let y = m_first * nfs in
3144 let itemcount = source#getitemcount in
3145 let maxi = max 0 (itemcount - fstate.maxrows) in
3146 let maxy = maxi * nfs in
3147 let p, h = scrollph y maxy in
3148 conf.scrollbw, p, h
3150 method modehash = modehash
3151 end;;
3153 class outlinelistview ~source =
3154 object (self)
3155 inherit listview
3156 ~source:(source :> lvsource)
3157 ~trusted:false
3158 ~modehash:(findkeyhash conf "outline")
3159 as super
3161 method key key mask =
3162 let calcfirst first active =
3163 if active > first
3164 then
3165 let rows = active - first in
3166 if rows > fstate.maxrows then active - fstate.maxrows else first
3167 else active
3169 let navigate incr =
3170 let active = m_active + incr in
3171 let active = bound active 0 (source#getitemcount - 1) in
3172 let first = calcfirst m_first active in
3173 G.postRedisplay "outline navigate";
3174 coe {< m_active = active; m_first = first >}
3176 let ctrl = Wsi.withctrl mask in
3177 match key with
3178 | 110 when ctrl -> (* ctrl-n *)
3179 source#narrow m_qsearch;
3180 G.postRedisplay "outline ctrl-n";
3181 coe {< m_first = 0; m_active = 0 >}
3183 | 117 when ctrl -> (* ctrl-u *)
3184 source#denarrow;
3185 G.postRedisplay "outline ctrl-u";
3186 state.text <- "";
3187 coe {< m_first = 0; m_active = 0 >}
3189 | 108 when ctrl -> (* ctrl-l *)
3190 let first = m_active - (fstate.maxrows / 2) in
3191 G.postRedisplay "outline ctrl-l";
3192 coe {< m_first = first >}
3194 | 0xff9f | 0xffff -> (* delete *)
3195 source#remove m_active;
3196 G.postRedisplay "outline delete";
3197 let active = max 0 (m_active-1) in
3198 coe {< m_first = firstof m_first active;
3199 m_active = active >}
3201 | 0xff52 -> navigate ~-1 (* up *)
3202 | 0xff54 -> navigate 1 (* down *)
3203 | 0xff55 -> (* prior *)
3204 navigate ~-(fstate.maxrows)
3205 | 0xff56 -> (* next *)
3206 navigate fstate.maxrows
3208 | 0xff53 -> (* [ctrl-]right *)
3209 let o =
3210 if ctrl
3211 then (
3212 G.postRedisplay "outline ctrl right";
3213 {< m_pan = m_pan + 1 >}
3215 else self#updownlevel 1
3217 coe o
3219 | 0xff51 -> (* [ctrl-]left *)
3220 let o =
3221 if ctrl
3222 then (
3223 G.postRedisplay "outline ctrl left";
3224 {< m_pan = m_pan - 1 >}
3226 else self#updownlevel ~-1
3228 coe o
3230 | 0xff50 -> (* home *)
3231 G.postRedisplay "outline home";
3232 coe {< m_first = 0; m_active = 0 >}
3234 | 0xff57 -> (* end *)
3235 let active = source#getitemcount - 1 in
3236 let first = max 0 (active - fstate.maxrows) in
3237 G.postRedisplay "outline end";
3238 coe {< m_active = active; m_first = first >}
3240 | _ -> super#key key mask
3243 let outlinesource usebookmarks =
3244 let empty = [||] in
3245 (object
3246 inherit lvsourcebase
3247 val mutable m_items = empty
3248 val mutable m_orig_items = empty
3249 val mutable m_prev_items = empty
3250 val mutable m_narrow_pattern = ""
3251 val mutable m_hadremovals = false
3253 method getitemcount =
3254 Array.length m_items + (if m_hadremovals then 1 else 0)
3256 method getitem n =
3257 if n == Array.length m_items && m_hadremovals
3258 then
3259 ("[Confirm removal]", 0)
3260 else
3261 let s, n, _ = m_items.(n) in
3262 (s, n)
3264 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3265 ignore (uioh, first, qsearch);
3266 let confrimremoval = m_hadremovals && active = Array.length m_items in
3267 let items =
3268 if String.length m_narrow_pattern = 0
3269 then m_orig_items
3270 else m_items
3272 if not cancel
3273 then (
3274 if not confrimremoval
3275 then(
3276 let _, _, anchor = m_items.(active) in
3277 gotoanchor anchor;
3278 m_items <- items;
3280 else (
3281 state.bookmarks <- Array.to_list m_items;
3282 m_orig_items <- m_items;
3285 else m_items <- items;
3286 m_pan <- pan;
3287 None
3289 method hasaction _ = true
3291 method greetmsg =
3292 if Array.length m_items != Array.length m_orig_items
3293 then "Narrowed to " ^ m_narrow_pattern ^ " (ctrl-u to restore)"
3294 else ""
3296 method narrow pattern =
3297 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
3298 match reopt with
3299 | None -> ()
3300 | Some re ->
3301 let rec loop accu n =
3302 if n = -1
3303 then (
3304 m_narrow_pattern <- pattern;
3305 m_items <- Array.of_list accu
3307 else
3308 let (s, _, _) as o = m_items.(n) in
3309 let accu =
3310 if (try ignore (Str.search_forward re s 0); true
3311 with Not_found -> false)
3312 then o :: accu
3313 else accu
3315 loop accu (n-1)
3317 loop [] (Array.length m_items - 1)
3319 method denarrow =
3320 m_orig_items <- (
3321 if usebookmarks
3322 then Array.of_list state.bookmarks
3323 else state.outlines
3325 m_items <- m_orig_items
3327 method remove m =
3328 if usebookmarks
3329 then
3330 if m >= 0 && m < Array.length m_items
3331 then (
3332 m_hadremovals <- true;
3333 m_items <- Array.init (Array.length m_items - 1) (fun n ->
3334 let n = if n >= m then n+1 else n in
3335 m_items.(n)
3339 method reset anchor items =
3340 m_hadremovals <- false;
3341 if m_orig_items == empty || m_prev_items != items
3342 then (
3343 m_orig_items <- items;
3344 if String.length m_narrow_pattern = 0
3345 then m_items <- items;
3347 m_prev_items <- items;
3348 let rely = getanchory anchor in
3349 let active =
3350 let rec loop n best bestd =
3351 if n = Array.length m_items
3352 then best
3353 else
3354 let (_, _, anchor) = m_items.(n) in
3355 let orely = getanchory anchor in
3356 let d = abs (orely - rely) in
3357 if d < bestd
3358 then loop (n+1) n d
3359 else loop (n+1) best bestd
3361 loop 0 ~-1 max_int
3363 m_active <- active;
3364 m_first <- firstof m_first active
3365 end)
3368 let enterselector usebookmarks =
3369 let source = outlinesource usebookmarks in
3370 fun errmsg ->
3371 let outlines =
3372 if usebookmarks
3373 then Array.of_list state.bookmarks
3374 else state.outlines
3376 if Array.length outlines = 0
3377 then (
3378 showtext ' ' errmsg;
3380 else (
3381 state.text <- source#greetmsg;
3382 Wsi.setcursor Wsi.CURSOR_INHERIT;
3383 let anchor = getanchor () in
3384 source#reset anchor outlines;
3385 state.uioh <- coe (new outlinelistview ~source);
3386 G.postRedisplay "enter selector";
3390 let enteroutlinemode =
3391 let f = enterselector false in
3392 fun ()-> f "Document has no outline";
3395 let enterbookmarkmode =
3396 let f = enterselector true in
3397 fun () -> f "Document has no bookmarks (yet)";
3400 let color_of_string s =
3401 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
3402 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
3406 let color_to_string (r, g, b) =
3407 let r = truncate (r *. 256.0)
3408 and g = truncate (g *. 256.0)
3409 and b = truncate (b *. 256.0) in
3410 Printf.sprintf "%d/%d/%d" r g b
3413 let irect_of_string s =
3414 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
3417 let irect_to_string (x0,y0,x1,y1) =
3418 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
3421 let makecheckers () =
3422 (* Appropriated from lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
3423 following to say:
3424 converted by Issac Trotts. July 25, 2002 *)
3425 let image_height = 64
3426 and image_width = 64 in
3428 let make_image () =
3429 let image =
3430 GlPix.create `ubyte ~format:`rgb ~width:image_width ~height:image_height
3432 for i = 0 to image_width - 1 do
3433 for j = 0 to image_height - 1 do
3434 Raw.sets (GlPix.to_raw image) ~pos:(3*(i*image_height+j))
3435 (if (i land 8 ) lxor (j land 8) = 0
3436 then [|255;255;255|] else [|200;200;200|])
3437 done
3438 done;
3439 image
3441 let image = make_image () in
3442 let id = GlTex.gen_texture () in
3443 GlTex.bind_texture `texture_2d id;
3444 GlPix.store (`unpack_alignment 1);
3445 GlTex.image2d image;
3446 List.iter (GlTex.parameter ~target:`texture_2d)
3447 [ `wrap_s `repeat;
3448 `wrap_t `repeat;
3449 `mag_filter `nearest;
3450 `min_filter `nearest ];
3454 let setcheckers enabled =
3455 match state.texid with
3456 | None ->
3457 if enabled then state.texid <- Some (makecheckers ())
3459 | Some texid ->
3460 if not enabled
3461 then (
3462 GlTex.delete_texture texid;
3463 state.texid <- None;
3467 let int_of_string_with_suffix s =
3468 let l = String.length s in
3469 let s1, shift =
3470 if l > 1
3471 then
3472 let suffix = Char.lowercase s.[l-1] in
3473 match suffix with
3474 | 'k' -> String.sub s 0 (l-1), 10
3475 | 'm' -> String.sub s 0 (l-1), 20
3476 | 'g' -> String.sub s 0 (l-1), 30
3477 | _ -> s, 0
3478 else s, 0
3480 let n = int_of_string s1 in
3481 let m = n lsl shift in
3482 if m < 0 || m < n
3483 then raise (Failure "value too large")
3484 else m
3487 let string_with_suffix_of_int n =
3488 if n = 0
3489 then "0"
3490 else
3491 let n, s =
3492 if n = 0
3493 then 0, ""
3494 else (
3495 if n land ((1 lsl 20) - 1) = 0
3496 then n lsr 20, "M"
3497 else (
3498 if n land ((1 lsl 10) - 1) = 0
3499 then n lsr 10, "K"
3500 else n, ""
3504 let rec loop s n =
3505 let h = n mod 1000 in
3506 let n = n / 1000 in
3507 if n = 0
3508 then string_of_int h ^ s
3509 else (
3510 let s = Printf.sprintf "_%03d%s" h s in
3511 loop s n
3514 loop "" n ^ s;
3517 let defghyllscroll = (40, 8, 32);;
3518 let ghyllscroll_of_string s =
3519 let (n, a, b) as nab =
3520 if s = "default"
3521 then defghyllscroll
3522 else Scanf.sscanf s "%u,%u,%u" (fun n a b -> n, a, b)
3524 if n <= a || n <= b || a >= b
3525 then failwith "invalid ghyll N,A,B (N <= A, A < B, N <= B)";
3526 nab;
3529 let ghyllscroll_to_string ((n, a, b) as nab) =
3530 if nab = defghyllscroll
3531 then "default"
3532 else Printf.sprintf "%d,%d,%d" n a b;
3535 let describe_location () =
3536 let f (fn, _) l =
3537 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
3539 let fn, ln = List.fold_left f (-1, -1) state.layout in
3540 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
3541 let percent =
3542 if maxy <= 0
3543 then 100.
3544 else (100. *. (float state.y /. float maxy))
3546 if fn = ln
3547 then
3548 Printf.sprintf "page %d of %d [%.2f%%]"
3549 (fn+1) state.pagecount percent
3550 else
3551 Printf.sprintf
3552 "pages %d-%d of %d [%.2f%%]"
3553 (fn+1) (ln+1) state.pagecount percent
3556 let enterinfomode =
3557 let btos b = if b then "\xe2\x88\x9a" else "" in
3558 let showextended = ref false in
3559 let leave mode = function
3560 | Confirm -> state.mode <- mode
3561 | Cancel -> state.mode <- mode in
3562 let src =
3563 (object
3564 val mutable m_first_time = true
3565 val mutable m_l = []
3566 val mutable m_a = [||]
3567 val mutable m_prev_uioh = nouioh
3568 val mutable m_prev_mode = View
3570 inherit lvsourcebase
3572 method reset prev_mode prev_uioh =
3573 m_a <- Array.of_list (List.rev m_l);
3574 m_l <- [];
3575 m_prev_mode <- prev_mode;
3576 m_prev_uioh <- prev_uioh;
3577 if m_first_time
3578 then (
3579 let rec loop n =
3580 if n >= Array.length m_a
3581 then ()
3582 else
3583 match m_a.(n) with
3584 | _, _, _, Action _ -> m_active <- n
3585 | _ -> loop (n+1)
3587 loop 0;
3588 m_first_time <- false;
3591 method int name get set =
3592 m_l <-
3593 (name, `int get, 1, Action (
3594 fun u ->
3595 let ondone s =
3596 try set (int_of_string s)
3597 with exn ->
3598 state.text <- Printf.sprintf "bad integer `%s': %s"
3599 s (Printexc.to_string exn)
3601 state.text <- "";
3602 let te = name ^ ": ", "", None, intentry, ondone in
3603 state.mode <- Textentry (te, leave m_prev_mode);
3605 )) :: m_l
3607 method int_with_suffix name get set =
3608 m_l <-
3609 (name, `intws get, 1, Action (
3610 fun u ->
3611 let ondone s =
3612 try set (int_of_string_with_suffix s)
3613 with exn ->
3614 state.text <- Printf.sprintf "bad integer `%s': %s"
3615 s (Printexc.to_string exn)
3617 state.text <- "";
3618 let te =
3619 name ^ ": ", "", None, intentry_with_suffix, ondone
3621 state.mode <- Textentry (te, leave m_prev_mode);
3623 )) :: m_l
3625 method bool ?(offset=1) ?(btos=btos) name get set =
3626 m_l <-
3627 (name, `bool (btos, get), offset, Action (
3628 fun u ->
3629 let v = get () in
3630 set (not v);
3632 )) :: m_l
3634 method color name get set =
3635 m_l <-
3636 (name, `color get, 1, Action (
3637 fun u ->
3638 let invalid = (nan, nan, nan) in
3639 let ondone s =
3640 let c =
3641 try color_of_string s
3642 with exn ->
3643 state.text <- Printf.sprintf "bad color `%s': %s"
3644 s (Printexc.to_string exn);
3645 invalid
3647 if c <> invalid
3648 then set c;
3650 let te = name ^ ": ", "", None, textentry, ondone in
3651 state.text <- color_to_string (get ());
3652 state.mode <- Textentry (te, leave m_prev_mode);
3654 )) :: m_l
3656 method string name get set =
3657 m_l <-
3658 (name, `string get, 1, Action (
3659 fun u ->
3660 let ondone s = set s in
3661 let te = name ^ ": ", "", None, textentry, ondone in
3662 state.mode <- Textentry (te, leave m_prev_mode);
3664 )) :: m_l
3666 method colorspace name get set =
3667 m_l <-
3668 (name, `string get, 1, Action (
3669 fun _ ->
3670 let source =
3671 let vals = [| "rgb"; "bgr"; "gray" |] in
3672 (object
3673 inherit lvsourcebase
3675 initializer
3676 m_active <- int_of_colorspace conf.colorspace;
3677 m_first <- 0;
3679 method getitemcount = Array.length vals
3680 method getitem n = (vals.(n), 0)
3681 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3682 ignore (uioh, first, pan, qsearch);
3683 if not cancel then set active;
3684 None
3685 method hasaction _ = true
3686 end)
3688 state.text <- "";
3689 let modehash = findkeyhash conf "info" in
3690 coe (new listview ~source ~trusted:true ~modehash)
3691 )) :: m_l
3693 method caption s offset =
3694 m_l <- (s, `empty, offset, Noaction) :: m_l
3696 method caption2 s f offset =
3697 m_l <- (s, `string f, offset, Noaction) :: m_l
3699 method getitemcount = Array.length m_a
3701 method getitem n =
3702 let tostr = function
3703 | `int f -> string_of_int (f ())
3704 | `intws f -> string_with_suffix_of_int (f ())
3705 | `string f -> f ()
3706 | `color f -> color_to_string (f ())
3707 | `bool (btos, f) -> btos (f ())
3708 | `empty -> ""
3710 let name, t, offset, _ = m_a.(n) in
3711 ((let s = tostr t in
3712 if String.length s > 0
3713 then Printf.sprintf "%s\t%s" name s
3714 else name),
3715 offset)
3717 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3718 let uiohopt =
3719 if not cancel
3720 then (
3721 m_qsearch <- qsearch;
3722 let uioh =
3723 match m_a.(active) with
3724 | _, _, _, Action f -> f uioh
3725 | _ -> uioh
3727 Some uioh
3729 else None
3731 m_active <- active;
3732 m_first <- first;
3733 m_pan <- pan;
3734 uiohopt
3736 method hasaction n =
3737 match m_a.(n) with
3738 | _, _, _, Action _ -> true
3739 | _ -> false
3740 end)
3742 let rec fillsrc prevmode prevuioh =
3743 let sep () = src#caption "" 0 in
3744 let colorp name get set =
3745 src#string name
3746 (fun () -> color_to_string (get ()))
3747 (fun v ->
3749 let c = color_of_string v in
3750 set c
3751 with exn ->
3752 state.text <- Printf.sprintf "bad color `%s': %s"
3753 v (Printexc.to_string exn);
3756 let oldmode = state.mode in
3757 let birdseye = isbirdseye state.mode in
3759 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
3761 src#bool "presentation mode"
3762 (fun () -> conf.presentation)
3763 (fun v ->
3764 conf.presentation <- v;
3765 state.anchor <- getanchor ();
3766 represent ());
3768 src#bool "ignore case in searches"
3769 (fun () -> conf.icase)
3770 (fun v -> conf.icase <- v);
3772 src#bool "preload"
3773 (fun () -> conf.preload)
3774 (fun v -> conf.preload <- v);
3776 src#bool "highlight links"
3777 (fun () -> conf.hlinks)
3778 (fun v -> conf.hlinks <- v);
3780 src#bool "under info"
3781 (fun () -> conf.underinfo)
3782 (fun v -> conf.underinfo <- v);
3784 src#bool "persistent bookmarks"
3785 (fun () -> conf.savebmarks)
3786 (fun v -> conf.savebmarks <- v);
3788 src#bool "proportional display"
3789 (fun () -> conf.proportional)
3790 (fun v -> reqlayout conf.angle v);
3792 src#bool "trim margins"
3793 (fun () -> conf.trimmargins)
3794 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
3796 src#bool "persistent location"
3797 (fun () -> conf.jumpback)
3798 (fun v -> conf.jumpback <- v);
3800 sep ();
3801 src#int "inter-page space"
3802 (fun () -> conf.interpagespace)
3803 (fun n ->
3804 conf.interpagespace <- n;
3805 let pageno, py =
3806 match state.layout with
3807 | [] -> 0, 0
3808 | l :: _ ->
3809 l.pageno, l.pagey
3811 state.maxy <- calcheight ();
3812 let y = getpagey pageno in
3813 gotoy (y + py)
3816 src#int "page bias"
3817 (fun () -> conf.pagebias)
3818 (fun v -> conf.pagebias <- v);
3820 src#int "scroll step"
3821 (fun () -> conf.scrollstep)
3822 (fun n -> conf.scrollstep <- n);
3824 src#int "auto scroll step"
3825 (fun () ->
3826 match state.autoscroll with
3827 | Some step -> step
3828 | _ -> conf.autoscrollstep)
3829 (fun n ->
3830 if state.autoscroll <> None
3831 then state.autoscroll <- Some n;
3832 conf.autoscrollstep <- n);
3834 src#int "zoom"
3835 (fun () -> truncate (conf.zoom *. 100.))
3836 (fun v -> setzoom ((float v) /. 100.));
3838 src#int "rotation"
3839 (fun () -> conf.angle)
3840 (fun v -> reqlayout v conf.proportional);
3842 src#int "scroll bar width"
3843 (fun () -> state.scrollw)
3844 (fun v ->
3845 state.scrollw <- v;
3846 conf.scrollbw <- v;
3847 reshape conf.winw conf.winh;
3850 src#int "scroll handle height"
3851 (fun () -> conf.scrollh)
3852 (fun v -> conf.scrollh <- v;);
3854 src#int "thumbnail width"
3855 (fun () -> conf.thumbw)
3856 (fun v ->
3857 conf.thumbw <- min 4096 v;
3858 match oldmode with
3859 | Birdseye beye ->
3860 leavebirdseye beye false;
3861 enterbirdseye ()
3862 | _ -> ()
3865 src#string "columns"
3866 (fun () ->
3867 match conf.columns with
3868 | None -> "1"
3869 | Some (multicol, _) -> columns_to_string multicol)
3870 (fun v ->
3871 let n, a, b = columns_of_string v in
3872 setcolumns n a b);
3874 sep ();
3875 src#caption "Presentation mode" 0;
3876 src#bool "scrollbar visible"
3877 (fun () -> conf.scrollbarinpm)
3878 (fun v ->
3879 if v != conf.scrollbarinpm
3880 then (
3881 conf.scrollbarinpm <- v;
3882 if conf.presentation
3883 then (
3884 state.scrollw <- if v then conf.scrollbw else 0;
3885 reshape conf.winw conf.winh;
3890 sep ();
3891 src#caption "Pixmap cache" 0;
3892 src#int_with_suffix "size (advisory)"
3893 (fun () -> conf.memlimit)
3894 (fun v -> conf.memlimit <- v);
3896 src#caption2 "used"
3897 (fun () -> Printf.sprintf "%s bytes, %d tiles"
3898 (string_with_suffix_of_int state.memused)
3899 (Hashtbl.length state.tilemap)) 1;
3901 sep ();
3902 src#caption "Layout" 0;
3903 src#caption2 "Dimension"
3904 (fun () ->
3905 Printf.sprintf "%dx%d (virtual %dx%d)"
3906 conf.winw conf.winh
3907 state.w state.maxy)
3909 if conf.debug
3910 then
3911 src#caption2 "Position" (fun () ->
3912 Printf.sprintf "%dx%d" state.x state.y
3914 else
3915 src#caption2 "Visible" (fun () -> describe_location ()) 1
3918 sep ();
3919 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
3920 "Save these parameters as global defaults at exit"
3921 (fun () -> conf.bedefault)
3922 (fun v -> conf.bedefault <- v)
3925 sep ();
3926 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
3927 src#bool ~offset:0 ~btos "Extended parameters"
3928 (fun () -> !showextended)
3929 (fun v -> showextended := v; fillsrc prevmode prevuioh);
3930 if !showextended
3931 then (
3932 src#bool "checkers"
3933 (fun () -> conf.checkers)
3934 (fun v -> conf.checkers <- v; setcheckers v);
3935 src#bool "update cursor"
3936 (fun () -> conf.updatecurs)
3937 (fun v -> conf.updatecurs <- v);
3938 src#bool "verbose"
3939 (fun () -> conf.verbose)
3940 (fun v -> conf.verbose <- v);
3941 src#bool "invert colors"
3942 (fun () -> conf.invert)
3943 (fun v -> conf.invert <- v);
3944 src#bool "max fit"
3945 (fun () -> conf.maxhfit)
3946 (fun v -> conf.maxhfit <- v);
3947 src#bool "redirect stderr"
3948 (fun () -> conf.redirectstderr)
3949 (fun v -> conf.redirectstderr <- v; redirectstderr ());
3950 src#string "uri launcher"
3951 (fun () -> conf.urilauncher)
3952 (fun v -> conf.urilauncher <- v);
3953 src#string "path launcher"
3954 (fun () -> conf.pathlauncher)
3955 (fun v -> conf.pathlauncher <- v);
3956 src#string "tile size"
3957 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
3958 (fun v ->
3960 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
3961 conf.tileh <- max 64 w;
3962 conf.tilew <- max 64 h;
3963 flushtiles ();
3964 with exn ->
3965 state.text <- Printf.sprintf "bad tile size `%s': %s"
3966 v (Printexc.to_string exn));
3967 src#int "texture count"
3968 (fun () -> conf.texcount)
3969 (fun v ->
3970 if realloctexts v
3971 then conf.texcount <- v
3972 else showtext '!' " Failed to set texture count please retry later"
3974 src#int "slice height"
3975 (fun () -> conf.sliceheight)
3976 (fun v ->
3977 conf.sliceheight <- v;
3978 wcmd "sliceh %d" conf.sliceheight;
3980 src#int "anti-aliasing level"
3981 (fun () -> conf.aalevel)
3982 (fun v ->
3983 conf.aalevel <- bound v 0 8;
3984 state.anchor <- getanchor ();
3985 opendoc state.path state.password;
3987 src#int "ui font size"
3988 (fun () -> fstate.fontsize)
3989 (fun v -> setfontsize (bound v 5 100));
3990 colorp "background color"
3991 (fun () -> conf.bgcolor)
3992 (fun v -> conf.bgcolor <- v);
3993 src#bool "crop hack"
3994 (fun () -> conf.crophack)
3995 (fun v -> conf.crophack <- v);
3996 src#string "trim fuzz"
3997 (fun () -> irect_to_string conf.trimfuzz)
3998 (fun v ->
4000 conf.trimfuzz <- irect_of_string v;
4001 if conf.trimmargins
4002 then settrim true conf.trimfuzz;
4003 with exn ->
4004 state.text <- Printf.sprintf "bad irect `%s': %s"
4005 v (Printexc.to_string exn)
4007 src#string "throttle"
4008 (fun () ->
4009 match conf.maxwait with
4010 | None -> "show place holder if page is not ready"
4011 | Some time ->
4012 if time = infinity
4013 then "wait for page to fully render"
4014 else
4015 "wait " ^ string_of_float time
4016 ^ " seconds before showing placeholder"
4018 (fun v ->
4020 let f = float_of_string v in
4021 if f <= 0.0
4022 then conf.maxwait <- None
4023 else conf.maxwait <- Some f
4024 with exn ->
4025 state.text <- Printf.sprintf "bad time `%s': %s"
4026 v (Printexc.to_string exn)
4028 src#string "ghyll scroll"
4029 (fun () ->
4030 match conf.ghyllscroll with
4031 | None -> ""
4032 | Some nab -> ghyllscroll_to_string nab
4034 (fun v ->
4036 let gs =
4037 if String.length v = 0
4038 then None
4039 else Some (ghyllscroll_of_string v)
4041 conf.ghyllscroll <- gs
4042 with exn ->
4043 state.text <- Printf.sprintf "bad ghyll `%s': %s"
4044 v (Printexc.to_string exn)
4046 src#string "selection command"
4047 (fun () -> conf.selcmd)
4048 (fun v -> conf.selcmd <- v);
4049 src#colorspace "color space"
4050 (fun () -> colorspace_to_string conf.colorspace)
4051 (fun v ->
4052 conf.colorspace <- colorspace_of_int v;
4053 wcmd "cs %d" v;
4054 load state.layout;
4058 sep ();
4059 src#caption "Document" 0;
4060 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
4061 src#caption2 "Pages"
4062 (fun () -> string_of_int state.pagecount) 1;
4063 src#caption2 "Dimensions"
4064 (fun () -> string_of_int (List.length state.pdims)) 1;
4065 if conf.trimmargins
4066 then (
4067 sep ();
4068 src#caption "Trimmed margins" 0;
4069 src#caption2 "Dimensions"
4070 (fun () -> string_of_int (List.length state.pdims)) 1;
4073 src#reset prevmode prevuioh;
4075 fun () ->
4076 state.text <- "";
4077 let prevmode = state.mode
4078 and prevuioh = state.uioh in
4079 fillsrc prevmode prevuioh;
4080 let source = (src :> lvsource) in
4081 let modehash = findkeyhash conf "info" in
4082 state.uioh <- coe (object (self)
4083 inherit listview ~source ~trusted:true ~modehash as super
4084 val mutable m_prevmemused = 0
4085 method infochanged = function
4086 | Memused ->
4087 if m_prevmemused != state.memused
4088 then (
4089 m_prevmemused <- state.memused;
4090 G.postRedisplay "memusedchanged";
4092 | Pdim -> G.postRedisplay "pdimchanged"
4093 | Docinfo -> fillsrc prevmode prevuioh
4095 method key key mask =
4096 if not (Wsi.withctrl mask)
4097 then
4098 match key with
4099 | 0xff51 -> coe (self#updownlevel ~-1)
4100 | 0xff53 -> coe (self#updownlevel 1)
4101 | _ -> super#key key mask
4102 else super#key key mask
4103 end);
4104 G.postRedisplay "info";
4107 let enterhelpmode =
4108 let source =
4109 (object
4110 inherit lvsourcebase
4111 method getitemcount = Array.length state.help
4112 method getitem n =
4113 let s, n, _ = state.help.(n) in
4114 (s, n)
4116 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4117 let optuioh =
4118 if not cancel
4119 then (
4120 m_qsearch <- qsearch;
4121 match state.help.(active) with
4122 | _, _, Action f -> Some (f uioh)
4123 | _ -> Some (uioh)
4125 else None
4127 m_active <- active;
4128 m_first <- first;
4129 m_pan <- pan;
4130 optuioh
4132 method hasaction n =
4133 match state.help.(n) with
4134 | _, _, Action _ -> true
4135 | _ -> false
4137 initializer
4138 m_active <- -1
4139 end)
4140 in fun () ->
4141 let modehash = findkeyhash conf "help" in
4142 state.uioh <- coe (new listview ~source ~trusted:true ~modehash);
4143 G.postRedisplay "help";
4146 let entermsgsmode =
4147 let msgsource =
4148 let re = Str.regexp "[\r\n]" in
4149 (object
4150 inherit lvsourcebase
4151 val mutable m_items = [||]
4153 method getitemcount = 1 + Array.length m_items
4155 method getitem n =
4156 if n = 0
4157 then "[Clear]", 0
4158 else m_items.(n-1), 0
4160 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4161 ignore uioh;
4162 if not cancel
4163 then (
4164 if active = 0
4165 then Buffer.clear state.errmsgs;
4166 m_qsearch <- qsearch;
4168 m_active <- active;
4169 m_first <- first;
4170 m_pan <- pan;
4171 None
4173 method hasaction n =
4174 n = 0
4176 method reset =
4177 state.newerrmsgs <- false;
4178 let l = Str.split re (Buffer.contents state.errmsgs) in
4179 m_items <- Array.of_list l
4181 initializer
4182 m_active <- 0
4183 end)
4184 in fun () ->
4185 state.text <- "";
4186 msgsource#reset;
4187 let source = (msgsource :> lvsource) in
4188 let modehash = findkeyhash conf "listview" in
4189 state.uioh <- coe (object
4190 inherit listview ~source ~trusted:false ~modehash as super
4191 method display =
4192 if state.newerrmsgs
4193 then msgsource#reset;
4194 super#display
4195 end);
4196 G.postRedisplay "msgs";
4199 let quickbookmark ?title () =
4200 match state.layout with
4201 | [] -> ()
4202 | l :: _ ->
4203 let title =
4204 match title with
4205 | None ->
4206 let sec = Unix.gettimeofday () in
4207 let tm = Unix.localtime sec in
4208 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
4209 (l.pageno+1)
4210 tm.Unix.tm_mday
4211 tm.Unix.tm_mon
4212 (tm.Unix.tm_year + 1900)
4213 tm.Unix.tm_hour
4214 tm.Unix.tm_min
4215 | Some title -> title
4217 state.bookmarks <-
4218 (title, 0, (l.pageno, float l.pagey /. float l.pageh))
4219 :: state.bookmarks
4222 let doreshape w h =
4223 state.fullscreen <- None;
4224 Wsi.reshape w h;
4227 let setautoscrollspeed step goingdown =
4228 let incr = max 1 ((abs step) / 2) in
4229 let incr = if goingdown then incr else -incr in
4230 let astep = step + incr in
4231 state.autoscroll <- Some astep;
4234 let viewkeyboard key mask =
4235 let enttext te =
4236 let mode = state.mode in
4237 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
4238 state.text <- "";
4239 enttext ();
4240 G.postRedisplay "view:enttext"
4242 let ctrl = Wsi.withctrl mask in
4243 match key with
4244 | 81 -> (* Q *)
4245 exit 0
4247 | 0xff63 -> (* insert *)
4248 state.mode <- LinkNav (Ltgendir 0, "");
4249 gotoy state.y;
4251 | 0xff1b | 113 -> (* escape / q *)
4252 begin match state.mstate with
4253 | Mzoomrect _ ->
4254 state.mstate <- Mnone;
4255 Wsi.setcursor Wsi.CURSOR_INHERIT;
4256 G.postRedisplay "kill zoom rect";
4257 | _ ->
4258 match state.ranchors with
4259 | [] -> raise Quit
4260 | (path, password, anchor) :: rest ->
4261 state.ranchors <- rest;
4262 state.anchor <- anchor;
4263 opendoc path password
4264 end;
4266 | 0xff08 -> (* backspace *)
4267 let y = getnav ~-1 in
4268 gotoy_and_clear_text y
4270 | 111 -> (* o *)
4271 enteroutlinemode ()
4273 | 117 -> (* u *)
4274 state.rects <- [];
4275 state.text <- "";
4276 G.postRedisplay "dehighlight";
4278 | 47 | 63 -> (* / ? *)
4279 let ondone isforw s =
4280 cbput state.hists.pat s;
4281 state.searchpattern <- s;
4282 search s isforw
4284 let s = String.create 1 in
4285 s.[0] <- Char.chr key;
4286 enttext (s, "", Some (onhist state.hists.pat),
4287 textentry, ondone (key = 47))
4289 | 43 | 0xffab when ctrl -> (* ctrl-+ *)
4290 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
4291 setzoom (conf.zoom +. incr)
4293 | 43 | 0xffab -> (* + *)
4294 let ondone s =
4295 let n =
4296 try int_of_string s with exc ->
4297 state.text <- Printf.sprintf "bad integer `%s': %s"
4298 s (Printexc.to_string exc);
4299 max_int
4301 if n != max_int
4302 then (
4303 conf.pagebias <- n;
4304 state.text <- "page bias is now " ^ string_of_int n;
4307 enttext ("page bias: ", "", None, intentry, ondone)
4309 | 45 | 0xffad when ctrl -> (* ctrl-- *)
4310 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
4311 setzoom (max 0.01 (conf.zoom -. decr))
4313 | 45 | 0xffad -> (* - *)
4314 let ondone msg = state.text <- msg in
4315 enttext (
4316 "option [acfhilpstvxACPRSZTIS]: ", "", None,
4317 optentry state.mode, ondone
4320 | 48 when ctrl -> (* ctrl-0 *)
4321 setzoom 1.0
4323 | 49 when ctrl -> (* 1 *)
4324 let zoom = zoomforh conf.winw conf.winh state.scrollw in
4325 if zoom < 1.0
4326 then setzoom zoom
4328 | 0xffc6 -> (* f9 *)
4329 togglebirdseye ()
4331 | 57 when ctrl -> (* ctrl-9 *)
4332 togglebirdseye ()
4334 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
4335 when not ctrl -> (* 0..9 *)
4336 let ondone s =
4337 let n =
4338 try int_of_string s with exc ->
4339 state.text <- Printf.sprintf "bad integer `%s': %s"
4340 s (Printexc.to_string exc);
4343 if n >= 0
4344 then (
4345 addnav ();
4346 cbput state.hists.pag (string_of_int n);
4347 gotopage1 (n + conf.pagebias - 1) 0;
4350 let pageentry text key =
4351 match Char.unsafe_chr key with
4352 | 'g' -> TEdone text
4353 | _ -> intentry text key
4355 let text = "x" in text.[0] <- Char.chr key;
4356 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone)
4358 | 98 -> (* b *)
4359 state.scrollw <- if state.scrollw > 0 then 0 else conf.scrollbw;
4360 reshape conf.winw conf.winh;
4362 | 108 -> (* l *)
4363 conf.hlinks <- not conf.hlinks;
4364 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
4365 G.postRedisplay "toggle highlightlinks";
4367 | 97 -> (* a *)
4368 begin match state.autoscroll with
4369 | Some step ->
4370 conf.autoscrollstep <- step;
4371 state.autoscroll <- None
4372 | None ->
4373 if conf.autoscrollstep = 0
4374 then state.autoscroll <- Some 1
4375 else state.autoscroll <- Some conf.autoscrollstep
4378 | 80 -> (* P *)
4379 conf.presentation <- not conf.presentation;
4380 if conf.presentation
4381 then (
4382 if not conf.scrollbarinpm
4383 then state.scrollw <- 0;
4385 else
4386 state.scrollw <- conf.scrollbw;
4388 showtext ' ' ("presentation mode " ^
4389 if conf.presentation then "on" else "off");
4390 state.anchor <- getanchor ();
4391 represent ()
4393 | 102 -> (* f *)
4394 begin match state.fullscreen with
4395 | None ->
4396 state.fullscreen <- Some (conf.winw, conf.winh);
4397 Wsi.fullscreen ()
4398 | Some (w, h) ->
4399 state.fullscreen <- None;
4400 doreshape w h
4403 | 103 -> (* g *)
4404 gotoy_and_clear_text 0
4406 | 71 -> (* G *)
4407 gotopage1 (state.pagecount - 1) 0
4409 | 112 | 78 -> (* p|N *)
4410 search state.searchpattern false
4412 | 110 | 0xffc0 -> (* n|F3 *)
4413 search state.searchpattern true
4415 | 116 -> (* t *)
4416 begin match state.layout with
4417 | [] -> ()
4418 | l :: _ ->
4419 gotoy_and_clear_text (getpagey l.pageno)
4422 | 32 -> (* ' ' *)
4423 begin match List.rev state.layout with
4424 | [] -> ()
4425 | l :: _ ->
4426 let pageno = min (l.pageno+1) (state.pagecount-1) in
4427 gotoy_and_clear_text (getpagey pageno)
4430 | 0xff9f | 0xffff -> (* delete *)
4431 begin match state.layout with
4432 | [] -> ()
4433 | l :: _ ->
4434 let pageno = max 0 (l.pageno-1) in
4435 gotoy_and_clear_text (getpagey pageno)
4438 | 61 -> (* = *)
4439 showtext ' ' (describe_location ());
4441 | 119 -> (* w *)
4442 begin match state.layout with
4443 | [] -> ()
4444 | l :: _ ->
4445 doreshape (l.pagew + state.scrollw) l.pageh;
4446 G.postRedisplay "w"
4449 | 39 -> (* ' *)
4450 enterbookmarkmode ()
4452 | 104 | 0xffbe -> (* h|F1 *)
4453 enterhelpmode ()
4455 | 105 -> (* i *)
4456 enterinfomode ()
4458 | 101 when conf.redirectstderr -> (* e *)
4459 entermsgsmode ()
4461 | 109 -> (* m *)
4462 let ondone s =
4463 match state.layout with
4464 | l :: _ ->
4465 state.bookmarks <-
4466 (s, 0, (l.pageno, float l.pagey /. float l.pageh))
4467 :: state.bookmarks
4468 | _ -> ()
4470 enttext ("bookmark: ", "", None, textentry, ondone)
4472 | 126 -> (* ~ *)
4473 quickbookmark ();
4474 showtext ' ' "Quick bookmark added";
4476 | 122 -> (* z *)
4477 begin match state.layout with
4478 | l :: _ ->
4479 let rect = getpdimrect l.pagedimno in
4480 let w, h =
4481 if conf.crophack
4482 then
4483 (truncate (1.8 *. (rect.(1) -. rect.(0))),
4484 truncate (1.2 *. (rect.(3) -. rect.(0))))
4485 else
4486 (truncate (rect.(1) -. rect.(0)),
4487 truncate (rect.(3) -. rect.(0)))
4489 let w = truncate ((float w)*.conf.zoom)
4490 and h = truncate ((float h)*.conf.zoom) in
4491 if w != 0 && h != 0
4492 then (
4493 state.anchor <- getanchor ();
4494 doreshape (w + state.scrollw) (h + conf.interpagespace)
4496 G.postRedisplay "z";
4498 | [] -> ()
4501 | 50 when ctrl -> (* ctrl-2 *)
4502 let maxw = getmaxw () in
4503 if maxw > 0.0
4504 then setzoom (maxw /. float conf.winw)
4506 | 60 | 62 -> (* < > *)
4507 reqlayout (conf.angle + (if key = 60 then 30 else -30)) conf.proportional
4509 | 91 | 93 -> (* [ ] *)
4510 conf.colorscale <-
4511 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
4513 G.postRedisplay "brightness";
4515 | 0xff54 | 0xff52 when ctrl && Wsi.withshift mask ->
4516 setzoom state.prevzoom
4518 | 107 | 0xff52 -> (* k up *)
4519 begin match state.autoscroll with
4520 | None ->
4521 begin match state.mode with
4522 | Birdseye beye -> upbirdseye 1 beye
4523 | _ ->
4524 if ctrl
4525 then gotoy_and_clear_text (clamp ~-(conf.winh/2))
4526 else gotoy_and_clear_text (clamp (-conf.scrollstep))
4528 | Some n ->
4529 setautoscrollspeed n false
4532 | 106 | 0xff54 -> (* j down *)
4533 begin match state.autoscroll with
4534 | None ->
4535 begin match state.mode with
4536 | Birdseye beye -> downbirdseye 1 beye
4537 | _ ->
4538 if ctrl
4539 then gotoy_and_clear_text (clamp (conf.winh/2))
4540 else gotoy_and_clear_text (clamp conf.scrollstep)
4542 | Some n ->
4543 setautoscrollspeed n true
4546 | 0xff51 | 0xff53 when Wsi.withnone mask -> (* left / right *)
4547 if conf.zoom > 1.0
4548 then
4549 let dx =
4550 if ctrl
4551 then conf.winw / 2
4552 else 10
4554 let dx = if key = 0xff51 then dx else -dx in
4555 state.x <- state.x + dx;
4556 gotoy_and_clear_text state.y
4557 else (
4558 state.text <- "";
4559 G.postRedisplay "lef/right"
4562 | 0xff55 -> (* prior *)
4563 let y =
4564 if ctrl
4565 then
4566 match state.layout with
4567 | [] -> state.y
4568 | l :: _ -> state.y - l.pagey
4569 else
4570 clamp (-conf.winh)
4572 gotoghyll y
4574 | 0xff56 -> (* next *)
4575 let y =
4576 if ctrl
4577 then
4578 match List.rev state.layout with
4579 | [] -> state.y
4580 | l :: _ -> getpagey l.pageno
4581 else
4582 clamp conf.winh
4584 gotoghyll y
4586 | 0xff50 -> gotoghyll 0
4587 | 0xff57 -> gotoghyll (clamp state.maxy)
4588 | 0xff53 when Wsi.withalt mask ->
4589 gotoghyll (getnav ~-1)
4590 | 0xff51 when Wsi.withalt mask ->
4591 gotoghyll (getnav 1)
4593 | 114 -> (* r *)
4594 state.anchor <- getanchor ();
4595 opendoc state.path state.password
4597 | 76 -> (* L *)
4598 launchpath ()
4600 | 118 when conf.debug -> (* v *)
4601 state.rects <- [];
4602 List.iter (fun l ->
4603 match getopaque l.pageno with
4604 | None -> ()
4605 | Some opaque ->
4606 let x0, y0, x1, y1 = pagebbox opaque in
4607 let a,b = float x0, float y0 in
4608 let c,d = float x1, float y0 in
4609 let e,f = float x1, float y1 in
4610 let h,j = float x0, float y1 in
4611 let rect = (a,b,c,d,e,f,h,j) in
4612 debugrect rect;
4613 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
4614 ) state.layout;
4615 G.postRedisplay "v";
4617 | _ ->
4618 vlog "huh? %s" (Wsi.keyname key)
4621 let gotounder = function
4622 | Ulinkgoto (pageno, top) ->
4623 if pageno >= 0
4624 then (
4625 addnav ();
4626 gotopage1 pageno top;
4629 | Ulinkuri s ->
4630 gotouri s
4632 | Uremote (filename, pageno) ->
4633 let path =
4634 if Sys.file_exists filename
4635 then filename
4636 else
4637 let dir = Filename.dirname state.path in
4638 let path = Filename.concat dir filename in
4639 if Sys.file_exists path
4640 then path
4641 else ""
4643 if String.length path > 0
4644 then (
4645 let anchor = getanchor () in
4646 let ranchor = state.path, state.password, anchor in
4647 state.anchor <- (pageno, 0.0);
4648 state.ranchors <- ranchor :: state.ranchors;
4649 opendoc path "";
4651 else showtext '!' ("Could not find " ^ filename)
4653 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
4656 let linknavkeyboard key mask (linknav, pattern) =
4657 if key = 0xff63
4658 then (
4659 state.mode <- View;
4660 G.postRedisplay "leave linknav"
4662 else
4663 begin match linknav with
4664 | Ltgendir _ -> viewkeyboard key mask
4665 | Ltexact ((l, opaque, n), _) ->
4666 if key = 0xff0d
4667 then
4668 let under = getlink opaque n in
4669 gotounder under;
4670 state.mode <- View;
4671 else
4672 let opt, dir =
4673 match key with
4674 | 0xff50 -> (* home *)
4675 Some (findlink opaque LDfirst), -1
4677 | 0xff57 -> (* end *)
4678 Some (findlink opaque LDlast), 1
4680 | 0xff51 -> (* left *)
4681 Some (findlink opaque (LDleft n)), -1
4683 | 0xff53 -> (* right *)
4684 Some (findlink opaque (LDright n)), 1
4686 | 0xff52 -> (* up *)
4687 Some (findlink opaque (LDup n)), -1
4689 | 0xff54 -> (* down *)
4690 Some (findlink opaque (LDdown n)), 1
4692 | _ -> None, 0
4694 begin match opt with
4695 | Some Lnolock -> showtext '!' "failed to acquire lock";
4696 | Some Lnotfound ->
4697 begin match findpwl l.pageno dir with
4698 | Pwlnotfound -> ()
4699 | Pwl pageno ->
4700 state.mode <- LinkNav (Ltgendir dir, pattern);
4701 let y, h = getpageyh pageno in
4702 let y =
4703 if dir < 0
4704 then y + h - conf.winh
4705 else y
4707 gotoy y;
4708 end;
4710 | Some (Lfound (m, x0, y0, x1, y1)) ->
4711 if y0 < l.pagey
4712 || l.pagedispy + (y1 - l.pagey) > conf.winh - state.hscrollh
4713 then (
4714 state.mode <- LinkNav ((Ltgendir 0), pattern);
4715 gotoy (state.y + (y0 - l.pagey))
4717 else (
4718 if m = n
4719 then (
4720 match findpwl l.pageno dir with
4721 | Pwlnotfound -> ()
4722 | Pwl pageno ->
4723 state.mode <- LinkNav ((Ltgendir dir), pattern);
4724 let y, h = getpageyh pageno in
4725 let y =
4726 if dir < 0
4727 then y + h - conf.winh
4728 else y
4730 gotoy y;
4732 else
4733 let r = x0, y0, x1, y1 in
4734 state.mode <- LinkNav (
4735 (Ltexact ((l, opaque, m), r)), pattern
4737 G.postRedisplay "linknav up"
4739 | None ->
4740 viewkeyboard key mask
4741 end;
4742 end;
4745 let keyboard key mask =
4746 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
4747 then wcmd "interrupt"
4748 else state.uioh <- state.uioh#key key mask
4751 let birdseyekeyboard key mask
4752 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
4753 let incr =
4754 match conf.columns with
4755 | None -> 1
4756 | Some ((c, _, _), _) -> c
4758 match key with
4759 | 108 when Wsi.withctrl mask -> (* ctrl-l *)
4760 let y, h = getpageyh pageno in
4761 let top = (conf.winh - h) / 2 in
4762 gotoy (max 0 (y - top))
4763 | 0xff0d -> leavebirdseye beye false
4764 | 0xff1b -> leavebirdseye beye true (* escape *)
4765 | 0xff52 -> upbirdseye incr beye (* prior *)
4766 | 0xff54 -> downbirdseye incr beye (* next *)
4767 | 0xff51 -> upbirdseye 1 beye (* up *)
4768 | 0xff53 -> downbirdseye 1 beye (* down *)
4770 | 0xff55 ->
4771 begin match state.layout with
4772 | l :: _ ->
4773 if l.pagey != 0
4774 then (
4775 state.mode <- Birdseye (
4776 oconf, leftx, l.pageno, hooverpageno, anchor
4778 gotopage1 l.pageno 0;
4780 else (
4781 let layout = layout (state.y-conf.winh) conf.winh in
4782 match layout with
4783 | [] -> gotoy (clamp (-conf.winh))
4784 | l :: _ ->
4785 state.mode <- Birdseye (
4786 oconf, leftx, l.pageno, hooverpageno, anchor
4788 gotopage1 l.pageno 0
4791 | [] -> gotoy (clamp (-conf.winh))
4792 end;
4794 | 0xff56 ->
4795 begin match List.rev state.layout with
4796 | l :: _ ->
4797 let layout = layout (state.y + conf.winh) conf.winh in
4798 begin match layout with
4799 | [] ->
4800 let incr = l.pageh - l.pagevh in
4801 if incr = 0
4802 then (
4803 state.mode <-
4804 Birdseye (
4805 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
4807 G.postRedisplay "birdseye pagedown";
4809 else gotoy (clamp (incr + conf.interpagespace*2));
4811 | l :: _ ->
4812 state.mode <-
4813 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
4814 gotopage1 l.pageno 0;
4817 | [] -> gotoy (clamp conf.winh)
4818 end;
4820 | 0xff50 ->
4821 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
4822 gotopage1 0 0
4824 | 0xff57 ->
4825 let pageno = state.pagecount - 1 in
4826 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
4827 if not (pagevisible state.layout pageno)
4828 then
4829 let h =
4830 match List.rev state.pdims with
4831 | [] -> conf.winh
4832 | (_, _, h, _) :: _ -> h
4834 gotoy (max 0 (getpagey pageno - (conf.winh - h - conf.interpagespace)))
4835 else G.postRedisplay "birdseye end";
4836 | _ -> viewkeyboard key mask
4839 let drawpage l =
4840 let color =
4841 match state.mode with
4842 | Textentry _ -> scalecolor 0.4
4843 | LinkNav _
4844 | View -> scalecolor 1.0
4845 | Birdseye (_, _, pageno, hooverpageno, _) ->
4846 if l.pageno = hooverpageno
4847 then scalecolor 0.9
4848 else (
4849 if l.pageno = pageno
4850 then scalecolor 1.0
4851 else scalecolor 0.8
4854 drawtiles l color;
4855 begin match getopaque l.pageno with
4856 | Some opaque ->
4857 if tileready l l.pagex l.pagey
4858 then
4859 let x = l.pagedispx - l.pagex
4860 and y = l.pagedispy - l.pagey in
4861 postprocess opaque conf.hlinks x y;
4863 | _ -> ()
4864 end;
4867 let scrollindicator () =
4868 let sbw, ph, sh = state.uioh#scrollph in
4869 let sbh, pw, sw = state.uioh#scrollpw in
4871 GlDraw.color (0.64, 0.64, 0.64);
4872 GlDraw.rect
4873 (float (conf.winw - sbw), 0.)
4874 (float conf.winw, float conf.winh)
4876 GlDraw.rect
4877 (0., float (conf.winh - sbh))
4878 (float (conf.winw - state.scrollw - 1), float conf.winh)
4880 GlDraw.color (0.0, 0.0, 0.0);
4882 GlDraw.rect
4883 (float (conf.winw - sbw), ph)
4884 (float conf.winw, ph +. sh)
4886 GlDraw.rect
4887 (pw, float (conf.winh - sbh))
4888 (pw +. sw, float conf.winh)
4892 let showsel () =
4893 match state.mstate with
4894 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
4897 | Msel ((x0, y0), (x1, y1)) ->
4898 let rec loop = function
4899 | l :: ls ->
4900 if ((y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
4901 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh))))
4902 && ((x0 >= l.pagedispx && x0 <= (l.pagedispx + l.pagevw))
4903 || ((x1 >= l.pagedispx && x1 <= (l.pagedispx + l.pagevw))))
4904 then
4905 match getopaque l.pageno with
4906 | Some opaque ->
4907 let dx, dy = pagetranslatepoint l 0 0 in
4908 let x0 = x0 + dx
4909 and y0 = y0 + dy
4910 and x1 = x1 + dx
4911 and y1 = y1 + dy in
4912 GlMat.mode `modelview;
4913 GlMat.push ();
4914 GlMat.translate ~x:(float ~-dx) ~y:(float ~-dy) ();
4915 seltext opaque (x0, y0, x1, y1);
4916 GlMat.pop ();
4917 | _ -> ()
4918 else loop ls
4919 | [] -> ()
4921 loop state.layout
4924 let showrects rects =
4925 Gl.enable `blend;
4926 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
4927 GlDraw.polygon_mode `both `fill;
4928 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
4929 List.iter
4930 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
4931 List.iter (fun l ->
4932 if l.pageno = pageno
4933 then (
4934 let dx = float (l.pagedispx - l.pagex) in
4935 let dy = float (l.pagedispy - l.pagey) in
4936 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
4937 GlDraw.begins `quads;
4939 GlDraw.vertex2 (x0+.dx, y0+.dy);
4940 GlDraw.vertex2 (x1+.dx, y1+.dy);
4941 GlDraw.vertex2 (x2+.dx, y2+.dy);
4942 GlDraw.vertex2 (x3+.dx, y3+.dy);
4944 GlDraw.ends ();
4946 ) state.layout
4947 ) rects
4949 Gl.disable `blend;
4952 let display () =
4953 GlClear.color (scalecolor2 conf.bgcolor);
4954 GlClear.clear [`color];
4955 List.iter drawpage state.layout;
4956 let rects =
4957 match state.mode with
4958 | LinkNav ((Ltexact ((page, _, _), (x0, y0, x1, y1))), _) ->
4959 (page.pageno, 5, (
4960 float x0, float y0,
4961 float x1, float y0,
4962 float x1, float y1,
4963 float x0, float y1)
4964 ) :: state.rects
4965 | _ -> state.rects
4967 showrects rects;
4968 showsel ();
4969 state.uioh#display;
4970 scrollindicator ();
4971 begin match state.mstate with
4972 | Mzoomrect ((x0, y0), (x1, y1)) ->
4973 Gl.enable `blend;
4974 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
4975 GlDraw.polygon_mode `both `fill;
4976 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
4977 GlDraw.rect (float x0, float y0)
4978 (float x1, float y1);
4979 Gl.disable `blend;
4980 | _ -> ()
4981 end;
4982 enttext ();
4983 if conf.updatecurs
4984 then (
4985 let mx, my = state.mpos in
4986 updateunder mx my;
4988 Wsi.swapb ();
4991 let zoomrect x y x1 y1 =
4992 let x0 = min x x1
4993 and x1 = max x x1
4994 and y0 = min y y1 in
4995 gotoy (state.y + y0);
4996 state.anchor <- getanchor ();
4997 let zoom = (float conf.winw *. conf.zoom) /. float (x1 - x0) in
4998 let margin =
4999 if state.w < conf.winw - state.scrollw
5000 then (conf.winw - state.scrollw - state.w) / 2
5001 else 0
5003 state.x <- (state.x + margin) - x0;
5004 setzoom zoom;
5005 Wsi.setcursor Wsi.CURSOR_INHERIT;
5006 state.mstate <- Mnone;
5009 let scrollx x =
5010 let winw = conf.winw - state.scrollw - 1 in
5011 let s = float x /. float winw in
5012 let destx = truncate (float (state.w + winw) *. s) in
5013 state.x <- winw - destx;
5014 gotoy_and_clear_text state.y;
5015 state.mstate <- Mscrollx;
5018 let scrolly y =
5019 let s = float y /. float conf.winh in
5020 let desty = truncate (float (state.maxy - conf.winh) *. s) in
5021 gotoy_and_clear_text desty;
5022 state.mstate <- Mscrolly;
5025 let viewmouse button down x y mask =
5026 match button with
5027 | n when (n == 4 || n == 5) && not down ->
5028 if Wsi.withctrl mask
5029 then (
5030 match state.mstate with
5031 | Mzoom (oldn, i) ->
5032 if oldn = n
5033 then (
5034 if i = 2
5035 then
5036 let incr =
5037 match n with
5038 | 5 ->
5039 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
5040 | _ ->
5041 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
5043 let zoom = conf.zoom -. incr in
5044 setzoom zoom;
5045 state.mstate <- Mzoom (n, 0);
5046 else
5047 state.mstate <- Mzoom (n, i+1);
5049 else state.mstate <- Mzoom (n, 0)
5051 | _ -> state.mstate <- Mzoom (n, 0)
5053 else (
5054 match state.autoscroll with
5055 | Some step -> setautoscrollspeed step (n=4)
5056 | None ->
5057 let incr =
5058 if n = 4
5059 then -conf.scrollstep
5060 else conf.scrollstep
5062 let incr = incr * 2 in
5063 let y = clamp incr in
5064 gotoy_and_clear_text y
5067 | 1 when Wsi.withctrl mask ->
5068 if down
5069 then (
5070 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5071 state.mstate <- Mpan (x, y)
5073 else
5074 state.mstate <- Mnone
5076 | 3 ->
5077 if down
5078 then (
5079 Wsi.setcursor Wsi.CURSOR_CYCLE;
5080 let p = (x, y) in
5081 state.mstate <- Mzoomrect (p, p)
5083 else (
5084 match state.mstate with
5085 | Mzoomrect ((x0, y0), _) ->
5086 if abs (x-x0) > 10 && abs (y - y0) > 10
5087 then zoomrect x0 y0 x y
5088 else (
5089 state.mstate <- Mnone;
5090 Wsi.setcursor Wsi.CURSOR_INHERIT;
5091 G.postRedisplay "kill accidental zoom rect";
5093 | _ ->
5094 Wsi.setcursor Wsi.CURSOR_INHERIT;
5095 state.mstate <- Mnone
5098 | 1 when x > conf.winw - state.scrollw ->
5099 if down
5100 then
5101 let _, position, sh = state.uioh#scrollph in
5102 if y > truncate position && y < truncate (position +. sh)
5103 then state.mstate <- Mscrolly
5104 else scrolly y
5105 else
5106 state.mstate <- Mnone
5108 | 1 when y > conf.winh - state.hscrollh ->
5109 if down
5110 then
5111 let _, position, sw = state.uioh#scrollpw in
5112 if x > truncate position && x < truncate (position +. sw)
5113 then state.mstate <- Mscrollx
5114 else scrollx x
5115 else
5116 state.mstate <- Mnone
5118 | 1 ->
5119 let dest = if down then getunder x y else Unone in
5120 begin match dest with
5121 | Ulinkgoto (pageno, top) ->
5122 if pageno >= 0
5123 then (
5124 addnav ();
5125 gotopage1 pageno top;
5128 | Ulinkuri s ->
5129 gotouri s
5131 | Uremote (filename, pageno) ->
5132 let path =
5133 if Sys.file_exists filename
5134 then filename
5135 else
5136 let dir = Filename.dirname state.path in
5137 let path = Filename.concat dir filename in
5138 if Sys.file_exists path
5139 then path
5140 else ""
5142 if String.length path > 0
5143 then (
5144 let anchor = getanchor () in
5145 let ranchor = state.path, state.password, anchor in
5146 state.anchor <- (pageno, 0.0);
5147 state.ranchors <- ranchor :: state.ranchors;
5148 opendoc path "";
5150 else showtext '!' ("Could not find " ^ filename)
5152 | Uunexpected _ | Ulaunch _ | Unamed _ -> ()
5154 | Unone when down ->
5155 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5156 state.mstate <- Mpan (x, y);
5158 | Unone | Utext _ ->
5159 if down
5160 then (
5161 if conf.angle mod 360 = 0
5162 then (
5163 state.mstate <- Msel ((x, y), (x, y));
5164 G.postRedisplay "mouse select";
5167 else (
5168 match state.mstate with
5169 | Mnone -> ()
5171 | Mzoom _ | Mscrollx | Mscrolly ->
5172 state.mstate <- Mnone
5174 | Mzoomrect ((x0, y0), _) ->
5175 zoomrect x0 y0 x y
5177 | Mpan _ ->
5178 Wsi.setcursor Wsi.CURSOR_INHERIT;
5179 state.mstate <- Mnone
5181 | Msel ((_, y0), (_, y1)) ->
5182 let rec loop = function
5183 | [] -> ()
5184 | l :: rest ->
5185 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
5186 || ((y1 >= l.pagedispy
5187 && y1 <= (l.pagedispy + l.pagevh)))
5188 then
5189 match getopaque l.pageno with
5190 | Some opaque ->
5191 copysel conf.selcmd opaque;
5192 G.postRedisplay "copysel"
5193 | _ -> ()
5194 else loop rest
5196 loop state.layout;
5197 Wsi.setcursor Wsi.CURSOR_INHERIT;
5198 state.mstate <- Mnone;
5202 | _ -> ()
5205 let birdseyemouse button down x y mask
5206 (conf, leftx, _, hooverpageno, anchor) =
5207 match button with
5208 | 1 when down ->
5209 let rec loop = function
5210 | [] -> ()
5211 | l :: rest ->
5212 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5213 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5214 then (
5215 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
5217 else loop rest
5219 loop state.layout
5220 | 3 -> ()
5221 | _ -> viewmouse button down x y mask
5224 let mouse button down x y mask =
5225 state.uioh <- state.uioh#button button down x y mask;
5228 let motion ~x ~y =
5229 state.uioh <- state.uioh#motion x y
5232 let pmotion ~x ~y =
5233 state.uioh <- state.uioh#pmotion x y;
5236 let uioh = object
5237 method display = ()
5239 method key key mask =
5240 begin match state.mode with
5241 | Textentry textentry -> textentrykeyboard key mask textentry
5242 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
5243 | View -> viewkeyboard key mask
5244 | LinkNav linknav -> linknavkeyboard key mask linknav
5245 end;
5246 state.uioh
5248 method button button bstate x y mask =
5249 begin match state.mode with
5250 | LinkNav _
5251 | View -> viewmouse button bstate x y mask
5252 | Birdseye beye -> birdseyemouse button bstate x y mask beye
5253 | Textentry _ -> ()
5254 end;
5255 state.uioh
5257 method motion x y =
5258 begin match state.mode with
5259 | Textentry _ -> ()
5260 | View | Birdseye _ | LinkNav _ ->
5261 match state.mstate with
5262 | Mzoom _ | Mnone -> ()
5264 | Mpan (x0, y0) ->
5265 let dx = x - x0
5266 and dy = y0 - y in
5267 state.mstate <- Mpan (x, y);
5268 if conf.zoom > 1.0 then state.x <- state.x + dx;
5269 let y = clamp dy in
5270 gotoy_and_clear_text y
5272 | Msel (a, _) ->
5273 state.mstate <- Msel (a, (x, y));
5274 G.postRedisplay "motion select";
5276 | Mscrolly ->
5277 let y = min conf.winh (max 0 y) in
5278 scrolly y
5280 | Mscrollx ->
5281 let x = min conf.winw (max 0 x) in
5282 scrollx x
5284 | Mzoomrect (p0, _) ->
5285 state.mstate <- Mzoomrect (p0, (x, y));
5286 G.postRedisplay "motion zoomrect";
5287 end;
5288 state.uioh
5290 method pmotion x y =
5291 begin match state.mode with
5292 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
5293 let rec loop = function
5294 | [] ->
5295 if hooverpageno != -1
5296 then (
5297 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
5298 G.postRedisplay "pmotion birdseye no hoover";
5300 | l :: rest ->
5301 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5302 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5303 then (
5304 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
5305 G.postRedisplay "pmotion birdseye hoover";
5307 else loop rest
5309 loop state.layout
5311 | Textentry _ -> ()
5313 | LinkNav _
5314 | View ->
5315 match state.mstate with
5316 | Mnone -> updateunder x y
5317 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
5319 end;
5320 state.uioh
5322 method infochanged _ = ()
5324 method scrollph =
5325 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
5326 let p, h = scrollph state.y maxy in
5327 state.scrollw, p, h
5329 method scrollpw =
5330 let winw = conf.winw - state.scrollw - 1 in
5331 let fwinw = float winw in
5332 let sw =
5333 let sw = fwinw /. float state.w in
5334 let sw = fwinw *. sw in
5335 max sw (float conf.scrollh)
5337 let position, sw =
5338 let f = state.w+winw in
5339 let r = float (winw-state.x) /. float f in
5340 let p = fwinw *. r in
5341 p-.sw/.2., sw
5343 let sw =
5344 if position +. sw > fwinw
5345 then fwinw -. position
5346 else sw
5348 state.hscrollh, position, sw
5350 method modehash =
5351 let modename =
5352 match state.mode with
5353 | LinkNav _ -> "links"
5354 | Textentry _ -> "textentry"
5355 | Birdseye _ -> "birdseye"
5356 | View -> "global"
5358 findkeyhash conf modename
5359 end;;
5361 module Config =
5362 struct
5363 open Parser
5365 let fontpath = ref "";;
5367 module KeyMap =
5368 Map.Make (struct type t = (int * int) let compare = compare end);;
5370 let unent s =
5371 let l = String.length s in
5372 let b = Buffer.create l in
5373 unent b s 0 l;
5374 Buffer.contents b;
5377 let home =
5378 try Sys.getenv "HOME"
5379 with exn ->
5380 prerr_endline
5381 ("Can not determine home directory location: " ^
5382 Printexc.to_string exn);
5386 let modifier_of_string = function
5387 | "alt" -> Wsi.altmask
5388 | "shift" -> Wsi.shiftmask
5389 | "ctrl" | "control" -> Wsi.ctrlmask
5390 | "meta" -> Wsi.metamask
5391 | _ -> 0
5394 let key_of_string =
5395 let r = Str.regexp "-" in
5396 fun s ->
5397 let elems = Str.full_split r s in
5398 let f n k m =
5399 let g s =
5400 let m1 = modifier_of_string s in
5401 if m1 = 0
5402 then (Wsi.namekey s, m)
5403 else (k, m lor m1)
5404 in function
5405 | Str.Delim s when n land 1 = 0 -> g s
5406 | Str.Text s -> g s
5407 | Str.Delim _ -> (k, m)
5409 let rec loop n k m = function
5410 | [] -> (k, m)
5411 | x :: xs ->
5412 let k, m = f n k m x in
5413 loop (n+1) k m xs
5415 loop 0 0 0 elems
5418 let keys_of_string =
5419 let r = Str.regexp "[ \t]" in
5420 fun s ->
5421 let elems = Str.split r s in
5422 List.map key_of_string elems
5425 let copykeyhashes c =
5426 List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
5429 let config_of c attrs =
5430 let apply c k v =
5432 match k with
5433 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
5434 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
5435 | "case-insensitive-search" -> { c with icase = bool_of_string v }
5436 | "preload" -> { c with preload = bool_of_string v }
5437 | "page-bias" -> { c with pagebias = int_of_string v }
5438 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
5439 | "auto-scroll-step" ->
5440 { c with autoscrollstep = max 0 (int_of_string v) }
5441 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
5442 | "crop-hack" -> { c with crophack = bool_of_string v }
5443 | "throttle" ->
5444 let mw =
5445 match String.lowercase v with
5446 | "true" -> Some infinity
5447 | "false" -> None
5448 | f -> Some (float_of_string f)
5450 { c with maxwait = mw}
5451 | "highlight-links" -> { c with hlinks = bool_of_string v }
5452 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
5453 | "vertical-margin" ->
5454 { c with interpagespace = max 0 (int_of_string v) }
5455 | "zoom" ->
5456 let zoom = float_of_string v /. 100. in
5457 let zoom = max zoom 0.0 in
5458 { c with zoom = zoom }
5459 | "presentation" -> { c with presentation = bool_of_string v }
5460 | "rotation-angle" -> { c with angle = int_of_string v }
5461 | "width" -> { c with winw = max 20 (int_of_string v) }
5462 | "height" -> { c with winh = max 20 (int_of_string v) }
5463 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
5464 | "proportional-display" -> { c with proportional = bool_of_string v }
5465 | "pixmap-cache-size" ->
5466 { c with memlimit = max 2 (int_of_string_with_suffix v) }
5467 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
5468 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
5469 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
5470 | "persistent-location" -> { c with jumpback = bool_of_string v }
5471 | "background-color" -> { c with bgcolor = color_of_string v }
5472 | "scrollbar-in-presentation" ->
5473 { c with scrollbarinpm = bool_of_string v }
5474 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
5475 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
5476 | "mupdf-store-size" ->
5477 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
5478 | "checkers" -> { c with checkers = bool_of_string v }
5479 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
5480 | "trim-margins" -> { c with trimmargins = bool_of_string v }
5481 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
5482 | "uri-launcher" -> { c with urilauncher = unent v }
5483 | "path-launcher" -> { c with pathlauncher = unent v }
5484 | "color-space" -> { c with colorspace = colorspace_of_string v }
5485 | "invert-colors" -> { c with invert = bool_of_string v }
5486 | "brightness" -> { c with colorscale = float_of_string v }
5487 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
5488 | "ghyllscroll" ->
5489 { c with ghyllscroll = Some (ghyllscroll_of_string v) }
5490 | "columns" ->
5491 let nab = columns_of_string v in
5492 { c with columns = Some (nab, [||]) }
5493 | "birds-eye-columns" ->
5494 { c with beyecolumns = Some (max (int_of_string v) 2) }
5495 | "selection-command" -> { c with selcmd = unent v }
5496 | "update-cursor" -> { c with updatecurs = bool_of_string v }
5497 | _ -> c
5498 with exn ->
5499 prerr_endline ("Error processing attribute (`" ^
5500 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
5503 let rec fold c = function
5504 | [] -> c
5505 | (k, v) :: rest ->
5506 let c = apply c k v in
5507 fold c rest
5509 fold { c with keyhashes = copykeyhashes c } attrs;
5512 let fromstring f pos n v d =
5513 try f v
5514 with exn ->
5515 dolog "Error processing attribute (%S=%S) at %d\n%s"
5516 n v pos (Printexc.to_string exn)
5521 let bookmark_of attrs =
5522 let rec fold title page rely = function
5523 | ("title", v) :: rest -> fold v page rely rest
5524 | ("page", v) :: rest -> fold title v rely rest
5525 | ("rely", v) :: rest -> fold title page v rest
5526 | _ :: rest -> fold title page rely rest
5527 | [] -> title, page, rely
5529 fold "invalid" "0" "0" attrs
5532 let doc_of attrs =
5533 let rec fold path page rely pan = function
5534 | ("path", v) :: rest -> fold v page rely pan rest
5535 | ("page", v) :: rest -> fold path v rely pan rest
5536 | ("rely", v) :: rest -> fold path page v pan rest
5537 | ("pan", v) :: rest -> fold path page rely v rest
5538 | _ :: rest -> fold path page rely pan rest
5539 | [] -> path, page, rely, pan
5541 fold "" "0" "0" "0" attrs
5544 let map_of attrs =
5545 let rec fold rs ls = function
5546 | ("out", v) :: rest -> fold v ls rest
5547 | ("in", v) :: rest -> fold rs v rest
5548 | _ :: rest -> fold ls rs rest
5549 | [] -> ls, rs
5551 fold "" "" attrs
5554 let setconf dst src =
5555 dst.scrollbw <- src.scrollbw;
5556 dst.scrollh <- src.scrollh;
5557 dst.icase <- src.icase;
5558 dst.preload <- src.preload;
5559 dst.pagebias <- src.pagebias;
5560 dst.verbose <- src.verbose;
5561 dst.scrollstep <- src.scrollstep;
5562 dst.maxhfit <- src.maxhfit;
5563 dst.crophack <- src.crophack;
5564 dst.autoscrollstep <- src.autoscrollstep;
5565 dst.maxwait <- src.maxwait;
5566 dst.hlinks <- src.hlinks;
5567 dst.underinfo <- src.underinfo;
5568 dst.interpagespace <- src.interpagespace;
5569 dst.zoom <- src.zoom;
5570 dst.presentation <- src.presentation;
5571 dst.angle <- src.angle;
5572 dst.winw <- src.winw;
5573 dst.winh <- src.winh;
5574 dst.savebmarks <- src.savebmarks;
5575 dst.memlimit <- src.memlimit;
5576 dst.proportional <- src.proportional;
5577 dst.texcount <- src.texcount;
5578 dst.sliceheight <- src.sliceheight;
5579 dst.thumbw <- src.thumbw;
5580 dst.jumpback <- src.jumpback;
5581 dst.bgcolor <- src.bgcolor;
5582 dst.scrollbarinpm <- src.scrollbarinpm;
5583 dst.tilew <- src.tilew;
5584 dst.tileh <- src.tileh;
5585 dst.mustoresize <- src.mustoresize;
5586 dst.checkers <- src.checkers;
5587 dst.aalevel <- src.aalevel;
5588 dst.trimmargins <- src.trimmargins;
5589 dst.trimfuzz <- src.trimfuzz;
5590 dst.urilauncher <- src.urilauncher;
5591 dst.colorspace <- src.colorspace;
5592 dst.invert <- src.invert;
5593 dst.colorscale <- src.colorscale;
5594 dst.redirectstderr <- src.redirectstderr;
5595 dst.ghyllscroll <- src.ghyllscroll;
5596 dst.columns <- src.columns;
5597 dst.beyecolumns <- src.beyecolumns;
5598 dst.selcmd <- src.selcmd;
5599 dst.updatecurs <- src.updatecurs;
5600 dst.pathlauncher <- src.pathlauncher;
5601 dst.keyhashes <- copykeyhashes src;
5604 let get s =
5605 let h = Hashtbl.create 10 in
5606 let dc = { defconf with angle = defconf.angle } in
5607 let rec toplevel v t spos _ =
5608 match t with
5609 | Vdata | Vcdata | Vend -> v
5610 | Vopen ("llppconfig", _, closed) ->
5611 if closed
5612 then v
5613 else { v with f = llppconfig }
5614 | Vopen _ ->
5615 error "unexpected subelement at top level" s spos
5616 | Vclose _ -> error "unexpected close at top level" s spos
5618 and llppconfig v t spos _ =
5619 match t with
5620 | Vdata | Vcdata -> v
5621 | Vend -> error "unexpected end of input in llppconfig" s spos
5622 | Vopen ("defaults", attrs, closed) ->
5623 let c = config_of dc attrs in
5624 setconf dc c;
5625 if closed
5626 then v
5627 else { v with f = defaults }
5629 | Vopen ("ui-font", attrs, closed) ->
5630 let rec getsize size = function
5631 | [] -> size
5632 | ("size", v) :: rest ->
5633 let size =
5634 fromstring int_of_string spos "size" v fstate.fontsize in
5635 getsize size rest
5636 | l -> getsize size l
5638 fstate.fontsize <- getsize fstate.fontsize attrs;
5639 if closed
5640 then v
5641 else { v with f = uifont (Buffer.create 10) }
5643 | Vopen ("doc", attrs, closed) ->
5644 let pathent, spage, srely, span = doc_of attrs in
5645 let path = unent pathent
5646 and pageno = fromstring int_of_string spos "page" spage 0
5647 and rely = fromstring float_of_string spos "rely" srely 0.0
5648 and pan = fromstring int_of_string spos "pan" span 0 in
5649 let c = config_of dc attrs in
5650 let anchor = (pageno, rely) in
5651 if closed
5652 then (Hashtbl.add h path (c, [], pan, anchor); v)
5653 else { v with f = doc path pan anchor c [] }
5655 | Vopen _ ->
5656 error "unexpected subelement in llppconfig" s spos
5658 | Vclose "llppconfig" -> { v with f = toplevel }
5659 | Vclose _ -> error "unexpected close in llppconfig" s spos
5661 and defaults v t spos _ =
5662 match t with
5663 | Vdata | Vcdata -> v
5664 | Vend -> error "unexpected end of input in defaults" s spos
5665 | Vopen ("keymap", attrs, closed) ->
5666 let modename =
5667 try List.assoc "mode" attrs
5668 with Not_found -> "global" in
5669 if closed
5670 then v
5671 else
5672 let ret keymap =
5673 let h = findkeyhash dc modename in
5674 KeyMap.iter (Hashtbl.replace h) keymap;
5675 defaults
5677 { v with f = pkeymap ret KeyMap.empty }
5679 | Vopen (_, _, _) ->
5680 error "unexpected subelement in defaults" s spos
5682 | Vclose "defaults" ->
5683 { v with f = llppconfig }
5685 | Vclose _ -> error "unexpected close in defaults" s spos
5687 and uifont b v t spos epos =
5688 match t with
5689 | Vdata | Vcdata ->
5690 Buffer.add_substring b s spos (epos - spos);
5692 | Vopen (_, _, _) ->
5693 error "unexpected subelement in ui-font" s spos
5694 | Vclose "ui-font" ->
5695 if String.length !fontpath = 0
5696 then fontpath := Buffer.contents b;
5697 { v with f = llppconfig }
5698 | Vclose _ -> error "unexpected close in ui-font" s spos
5699 | Vend -> error "unexpected end of input in ui-font" s spos
5701 and doc path pan anchor c bookmarks v t spos _ =
5702 match t with
5703 | Vdata | Vcdata -> v
5704 | Vend -> error "unexpected end of input in doc" s spos
5705 | Vopen ("bookmarks", _, closed) ->
5706 if closed
5707 then v
5708 else { v with f = pbookmarks path pan anchor c bookmarks }
5710 | Vopen ("keymap", attrs, closed) ->
5711 let modename =
5712 try List.assoc "mode" attrs
5713 with Not_found -> "global"
5715 if closed
5716 then v
5717 else
5718 let ret keymap =
5719 let h = findkeyhash c modename in
5720 KeyMap.iter (Hashtbl.replace h) keymap;
5721 doc path pan anchor c bookmarks
5723 { v with f = pkeymap ret KeyMap.empty }
5725 | Vopen (_, _, _) ->
5726 error "unexpected subelement in doc" s spos
5728 | Vclose "doc" ->
5729 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
5730 { v with f = llppconfig }
5732 | Vclose _ -> error "unexpected close in doc" s spos
5734 and pkeymap ret keymap v t spos _ =
5735 match t with
5736 | Vdata | Vcdata -> v
5737 | Vend -> error "unexpected end of input in keymap" s spos
5738 | Vopen ("map", attrs, closed) ->
5739 let r, l = map_of attrs in
5740 let kss = fromstring keys_of_string spos "in" r [] in
5741 let lss = fromstring keys_of_string spos "out" l [] in
5742 let keymap =
5743 match kss with
5744 | [] -> keymap
5745 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
5746 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
5748 if closed
5749 then { v with f = pkeymap ret keymap }
5750 else
5751 let f () = v in
5752 { v with f = skip "map" f }
5754 | Vopen _ ->
5755 error "unexpected subelement in keymap" s spos
5757 | Vclose "keymap" ->
5758 { v with f = ret keymap }
5760 | Vclose _ -> error "unexpected close in keymap" s spos
5762 and pbookmarks path pan anchor c bookmarks v t spos _ =
5763 match t with
5764 | Vdata | Vcdata -> v
5765 | Vend -> error "unexpected end of input in bookmarks" s spos
5766 | Vopen ("item", attrs, closed) ->
5767 let titleent, spage, srely = bookmark_of attrs in
5768 let page = fromstring int_of_string spos "page" spage 0
5769 and rely = fromstring float_of_string spos "rely" srely 0.0 in
5770 let bookmarks = (unent titleent, 0, (page, rely)) :: bookmarks in
5771 if closed
5772 then { v with f = pbookmarks path pan anchor c bookmarks }
5773 else
5774 let f () = v in
5775 { v with f = skip "item" f }
5777 | Vopen _ ->
5778 error "unexpected subelement in bookmarks" s spos
5780 | Vclose "bookmarks" ->
5781 { v with f = doc path pan anchor c bookmarks }
5783 | Vclose _ -> error "unexpected close in bookmarks" s spos
5785 and skip tag f v t spos _ =
5786 match t with
5787 | Vdata | Vcdata -> v
5788 | Vend ->
5789 error ("unexpected end of input in skipped " ^ tag) s spos
5790 | Vopen (tag', _, closed) ->
5791 if closed
5792 then v
5793 else
5794 let f' () = { v with f = skip tag f } in
5795 { v with f = skip tag' f' }
5796 | Vclose ctag ->
5797 if tag = ctag
5798 then f ()
5799 else error ("unexpected close in skipped " ^ tag) s spos
5802 parse { f = toplevel; accu = () } s;
5803 h, dc;
5806 let do_load f ic =
5808 let len = in_channel_length ic in
5809 let s = String.create len in
5810 really_input ic s 0 len;
5811 f s;
5812 with
5813 | Parse_error (msg, s, pos) ->
5814 let subs = subs s pos in
5815 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
5816 failwith ("parse error: " ^ s)
5818 | exn ->
5819 failwith ("config load error: " ^ Printexc.to_string exn)
5822 let defconfpath =
5823 let dir =
5825 let dir = Filename.concat home ".config" in
5826 if Sys.is_directory dir then dir else home
5827 with _ -> home
5829 Filename.concat dir "llpp.conf"
5832 let confpath = ref defconfpath;;
5834 let load1 f =
5835 if Sys.file_exists !confpath
5836 then
5837 match
5838 (try Some (open_in_bin !confpath)
5839 with exn ->
5840 prerr_endline
5841 ("Error opening configuation file `" ^ !confpath ^ "': " ^
5842 Printexc.to_string exn);
5843 None
5845 with
5846 | Some ic ->
5847 begin try
5848 f (do_load get ic)
5849 with exn ->
5850 prerr_endline
5851 ("Error loading configuation from `" ^ !confpath ^ "': " ^
5852 Printexc.to_string exn);
5853 end;
5854 close_in ic;
5856 | None -> ()
5857 else
5858 f (Hashtbl.create 0, defconf)
5861 let load () =
5862 let f (h, dc) =
5863 let pc, pb, px, pa =
5865 Hashtbl.find h (Filename.basename state.path)
5866 with Not_found -> dc, [], 0, (0, 0.0)
5868 setconf defconf dc;
5869 setconf conf pc;
5870 state.bookmarks <- pb;
5871 state.x <- px;
5872 state.scrollw <- conf.scrollbw;
5873 if conf.jumpback
5874 then state.anchor <- pa;
5875 cbput state.hists.nav pa;
5877 load1 f
5880 let add_attrs bb always dc c =
5881 let ob s a b =
5882 if always || a != b
5883 then Printf.bprintf bb "\n %s='%b'" s a
5884 and oi s a b =
5885 if always || a != b
5886 then Printf.bprintf bb "\n %s='%d'" s a
5887 and oI s a b =
5888 if always || a != b
5889 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
5890 and oz s a b =
5891 if always || a <> b
5892 then Printf.bprintf bb "\n %s='%d'" s (truncate (a*.100.))
5893 and oF s a b =
5894 if always || a <> b
5895 then Printf.bprintf bb "\n %s='%f'" s a
5896 and oc s a b =
5897 if always || a <> b
5898 then
5899 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
5900 and oC s a b =
5901 if always || a <> b
5902 then
5903 Printf.bprintf bb "\n %s='%s'" s (colorspace_to_string a)
5904 and oR s a b =
5905 if always || a <> b
5906 then
5907 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
5908 and os s a b =
5909 if always || a <> b
5910 then
5911 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
5912 and og s a b =
5913 if always || a <> b
5914 then
5915 match a with
5916 | None -> ()
5917 | Some (_N, _A, _B) ->
5918 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
5919 and oW s a b =
5920 if always || a <> b
5921 then
5922 let v =
5923 match a with
5924 | None -> "false"
5925 | Some f ->
5926 if f = infinity
5927 then "true"
5928 else string_of_float f
5930 Printf.bprintf bb "\n %s='%s'" s v
5931 and oco s a b =
5932 if always || a <> b
5933 then
5934 match a with
5935 | Some ((n, a, b), _) when n > 1 ->
5936 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
5937 | _ -> ()
5938 and obeco s a b =
5939 if always || a <> b
5940 then
5941 match a with
5942 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
5943 | _ -> ()
5945 let w, h =
5946 if always
5947 then dc.winw, dc.winh
5948 else
5949 match state.fullscreen with
5950 | Some wh -> wh
5951 | None -> c.winw, c.winh
5953 let zoom, presentation, interpagespace, maxwait =
5954 if always
5955 then dc.zoom, dc.presentation, dc.interpagespace, dc.maxwait
5956 else
5957 match state.mode with
5958 | Birdseye (bc, _, _, _, _) ->
5959 bc.zoom, bc.presentation, bc.interpagespace, bc.maxwait
5960 | _ -> c.zoom, c.presentation, c.interpagespace, c.maxwait
5962 oi "width" w dc.winw;
5963 oi "height" h dc.winh;
5964 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
5965 oi "scroll-handle-height" c.scrollh dc.scrollh;
5966 ob "case-insensitive-search" c.icase dc.icase;
5967 ob "preload" c.preload dc.preload;
5968 oi "page-bias" c.pagebias dc.pagebias;
5969 oi "scroll-step" c.scrollstep dc.scrollstep;
5970 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
5971 ob "max-height-fit" c.maxhfit dc.maxhfit;
5972 ob "crop-hack" c.crophack dc.crophack;
5973 oW "throttle" maxwait dc.maxwait;
5974 ob "highlight-links" c.hlinks dc.hlinks;
5975 ob "under-cursor-info" c.underinfo dc.underinfo;
5976 oi "vertical-margin" interpagespace dc.interpagespace;
5977 oz "zoom" zoom dc.zoom;
5978 ob "presentation" presentation dc.presentation;
5979 oi "rotation-angle" c.angle dc.angle;
5980 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
5981 ob "proportional-display" c.proportional dc.proportional;
5982 oI "pixmap-cache-size" c.memlimit dc.memlimit;
5983 oi "tex-count" c.texcount dc.texcount;
5984 oi "slice-height" c.sliceheight dc.sliceheight;
5985 oi "thumbnail-width" c.thumbw dc.thumbw;
5986 ob "persistent-location" c.jumpback dc.jumpback;
5987 oc "background-color" c.bgcolor dc.bgcolor;
5988 ob "scrollbar-in-presentation" c.scrollbarinpm dc.scrollbarinpm;
5989 oi "tile-width" c.tilew dc.tilew;
5990 oi "tile-height" c.tileh dc.tileh;
5991 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
5992 ob "checkers" c.checkers dc.checkers;
5993 oi "aalevel" c.aalevel dc.aalevel;
5994 ob "trim-margins" c.trimmargins dc.trimmargins;
5995 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
5996 os "uri-launcher" c.urilauncher dc.urilauncher;
5997 os "path-launcher" c.pathlauncher dc.pathlauncher;
5998 oC "color-space" c.colorspace dc.colorspace;
5999 ob "invert-colors" c.invert dc.invert;
6000 oF "brightness" c.colorscale dc.colorscale;
6001 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
6002 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
6003 oco "columns" c.columns dc.columns;
6004 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
6005 os "selection-command" c.selcmd dc.selcmd;
6006 ob "update-cursor" c.updatecurs dc.updatecurs;
6009 let keymapsbuf always dc c =
6010 let bb = Buffer.create 16 in
6011 let rec loop = function
6012 | [] -> ()
6013 | (modename, h) :: rest ->
6014 let dh = findkeyhash dc modename in
6015 if always || h <> dh
6016 then (
6017 if Hashtbl.length h > 0
6018 then (
6019 if Buffer.length bb > 0
6020 then Buffer.add_char bb '\n';
6021 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
6022 Hashtbl.iter (fun i o ->
6023 let isdifferent = always ||
6025 let dO = Hashtbl.find dh i in
6026 dO <> o
6027 with Not_found -> true
6029 if isdifferent
6030 then
6031 let addkm (k, m) =
6032 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
6033 if Wsi.withalt m then Buffer.add_string bb "alt-";
6034 if Wsi.withshift m then Buffer.add_string bb "shift-";
6035 if Wsi.withmeta m then Buffer.add_string bb "meta-";
6036 Buffer.add_string bb (Wsi.keyname k);
6038 let addkms l =
6039 let rec loop = function
6040 | [] -> ()
6041 | km :: [] -> addkm km
6042 | km :: rest -> addkm km; Buffer.add_char bb ' '; loop rest
6044 loop l
6046 Buffer.add_string bb "<map in='";
6047 addkm i;
6048 match o with
6049 | KMinsrt km ->
6050 Buffer.add_char bb '\'';
6051 Buffer.add_string bb " out='";
6052 addkm km;
6053 Buffer.add_string bb "'/>\n"
6055 | KMinsrl kms ->
6056 Buffer.add_char bb '\'';
6057 Buffer.add_string bb " out='";
6058 addkms kms;
6059 Buffer.add_string bb "'/>\n"
6061 | KMmulti (ins, kms) ->
6062 Buffer.add_char bb ' ';
6063 addkms ins;
6064 Buffer.add_char bb '\'';
6065 Buffer.add_string bb " out='";
6066 addkms kms;
6067 Buffer.add_string bb "'/>\n"
6068 ) h;
6069 Buffer.add_string bb "</keymap>";
6072 loop rest
6074 loop c.keyhashes;
6078 let save () =
6079 let uifontsize = fstate.fontsize in
6080 let bb = Buffer.create 32768 in
6081 let f (h, dc) =
6082 let dc = if conf.bedefault then conf else dc in
6083 Buffer.add_string bb "<llppconfig>\n";
6085 if String.length !fontpath > 0
6086 then
6087 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
6088 uifontsize
6089 !fontpath
6090 else (
6091 if uifontsize <> 14
6092 then
6093 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
6096 Buffer.add_string bb "<defaults ";
6097 add_attrs bb true dc dc;
6098 let kb = keymapsbuf true dc dc in
6099 if Buffer.length kb > 0
6100 then (
6101 Buffer.add_string bb ">\n";
6102 Buffer.add_buffer bb kb;
6103 Buffer.add_string bb "\n</defaults>\n";
6105 else Buffer.add_string bb "/>\n";
6107 let adddoc path pan anchor c bookmarks =
6108 if bookmarks == [] && c = dc && anchor = emptyanchor
6109 then ()
6110 else (
6111 Printf.bprintf bb "<doc path='%s'"
6112 (enent path 0 (String.length path));
6114 if anchor <> emptyanchor
6115 then (
6116 let n, y = anchor in
6117 Printf.bprintf bb " page='%d'" n;
6118 if y > 1e-6
6119 then
6120 Printf.bprintf bb " rely='%f'" y
6124 if pan != 0
6125 then Printf.bprintf bb " pan='%d'" pan;
6127 add_attrs bb false dc c;
6128 let kb = keymapsbuf false dc c in
6130 begin match bookmarks with
6131 | [] ->
6132 if Buffer.length kb > 0
6133 then (
6134 Buffer.add_string bb ">\n";
6135 Buffer.add_buffer bb kb;
6136 Buffer.add_string bb "</doc>\n";
6138 else Buffer.add_string bb "/>\n"
6139 | _ ->
6140 Buffer.add_string bb ">\n<bookmarks>\n";
6141 List.iter (fun (title, _level, (page, rely)) ->
6142 Printf.bprintf bb
6143 "<item title='%s' page='%d'"
6144 (enent title 0 (String.length title))
6145 page
6147 if rely > 1e-6
6148 then
6149 Printf.bprintf bb " rely='%f'" rely
6151 Buffer.add_string bb "/>\n";
6152 ) bookmarks;
6153 Buffer.add_string bb "</bookmarks>";
6154 if Buffer.length kb > 0
6155 then (
6156 Buffer.add_string bb "\n";
6157 Buffer.add_buffer bb kb;
6159 Buffer.add_string bb "\n</doc>\n";
6160 end;
6164 let pan, conf =
6165 match state.mode with
6166 | Birdseye (c, pan, _, _, _) ->
6167 let beyecolumns =
6168 match conf.columns with
6169 | Some ((c, _, _), _) -> Some c
6170 | None -> None
6171 and columns =
6172 match c.columns with
6173 | Some (c, _) -> Some (c, [||])
6174 | None -> None
6176 pan, { c with beyecolumns = beyecolumns; columns = columns }
6177 | _ -> state.x, conf
6179 let basename = Filename.basename state.path in
6180 adddoc basename pan (getanchor ())
6181 { conf with
6182 autoscrollstep =
6183 match state.autoscroll with
6184 | Some step -> step
6185 | None -> conf.autoscrollstep }
6186 (if conf.savebmarks then state.bookmarks else []);
6188 Hashtbl.iter (fun path (c, bookmarks, x, y) ->
6189 if basename <> path
6190 then adddoc path x y c bookmarks
6191 ) h;
6192 Buffer.add_string bb "</llppconfig>";
6194 load1 f;
6195 if Buffer.length bb > 0
6196 then
6198 let tmp = !confpath ^ ".tmp" in
6199 let oc = open_out_bin tmp in
6200 Buffer.output_buffer oc bb;
6201 close_out oc;
6202 Unix.rename tmp !confpath;
6203 with exn ->
6204 prerr_endline
6205 ("error while saving configuration: " ^ Printexc.to_string exn)
6207 end;;
6209 let () =
6210 Arg.parse
6211 (Arg.align
6212 [("-p", Arg.String (fun s -> state.password <- s) ,
6213 "<password> Set password");
6215 ("-f", Arg.String (fun s -> Config.fontpath := s),
6216 "<path> Set path to the user interface font");
6218 ("-c", Arg.String (fun s -> Config.confpath := s),
6219 "<path> Set path to the configuration file");
6221 ("-v", Arg.Unit (fun () ->
6222 Printf.printf
6223 "%s\nconfiguration path: %s\n"
6224 (version ())
6225 Config.defconfpath
6227 exit 0), " Print version and exit");
6230 (fun s -> state.path <- s)
6231 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
6233 if String.length state.path = 0
6234 then (prerr_endline "file name missing"; exit 1);
6236 Config.load ();
6238 let globalkeyhash = findkeyhash conf "global" in
6239 state.wsfd <- Wsi.init (object
6240 method display = display ()
6241 method reshape w h = reshape w h
6242 method mouse b d x y m = mouse b d x y m
6243 method motion x y = state.mpos <- (x, y); motion x y
6244 method pmotion x y = state.mpos <- (x, y); pmotion x y
6245 method key k m =
6246 match state.keystate with
6247 | KSnone ->
6248 let km = k, m in
6249 begin
6250 match
6251 try Hashtbl.find globalkeyhash km
6252 with Not_found ->
6253 let modehash = state.uioh#modehash in
6254 try Hashtbl.find modehash km
6255 with Not_found -> KMinsrt (k, m)
6256 with
6257 | KMinsrt (k, m) -> keyboard k m
6258 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
6259 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
6261 | KSinto ((k', m') :: [], insrt) when k'=k && m' land m = m' ->
6262 List.iter (fun (k, m) -> keyboard k m) insrt;
6263 state.keystate <- KSnone
6264 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land m = m' ->
6265 state.keystate <- KSinto (keys, insrt)
6266 | _ ->
6267 state.keystate <- KSnone
6269 method enter x y = state.mpos <- (x, y); pmotion x y
6270 method leave = state.mpos <- (-1, -1)
6271 method quit = raise Quit
6272 end) conf.winw conf.winh;
6274 if not (
6275 List.exists GlMisc.check_extension
6276 [ "GL_ARB_texture_rectangle"
6277 ; "GL_EXT_texture_recangle"
6278 ; "GL_NV_texture_rectangle" ]
6280 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
6282 let cr, sw = Unix.pipe ()
6283 and sr, cw = Unix.pipe () in
6285 cloexec cr;
6286 cloexec sw;
6287 cloexec sr;
6288 cloexec cw;
6290 setcheckers conf.checkers;
6291 redirectstderr ();
6293 init (cr, cw) (
6294 conf.angle, conf.proportional, (conf.trimmargins, conf.trimfuzz),
6295 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
6296 !Config.fontpath
6298 state.sr <- sr;
6299 state.sw <- sw;
6300 state.text <- "Opening " ^ state.path;
6301 setaalevel conf.aalevel;
6302 writeopen state.path state.password;
6303 state.uioh <- uioh;
6304 setfontsize fstate.fontsize;
6305 doreshape conf.winw conf.winh;
6307 let rec loop deadline =
6308 let r =
6309 match state.errfd with
6310 | None -> [state.sr; state.wsfd]
6311 | Some fd -> [state.sr; state.wsfd; fd]
6313 if state.redisplay
6314 then (
6315 state.redisplay <- false;
6316 display ();
6318 let timeout =
6319 let now = now () in
6320 if deadline > now
6321 then (
6322 if deadline = infinity
6323 then ~-.1.0
6324 else max 0.0 (deadline -. now)
6326 else 0.0
6328 let r, _, _ =
6329 try Unix.select r [] [] timeout
6330 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
6332 begin match r with
6333 | [] ->
6334 state.ghyll None;
6335 let newdeadline =
6336 if state.ghyll == noghyll
6337 then
6338 match state.autoscroll with
6339 | Some step when step != 0 ->
6340 let y = state.y + step in
6341 let y =
6342 if y < 0
6343 then state.maxy
6344 else if y >= state.maxy then 0 else y
6346 gotoy y;
6347 if state.mode = View
6348 then state.text <- "";
6349 deadline +. 0.01
6350 | _ -> infinity
6351 else deadline +. 0.01
6353 loop newdeadline
6355 | l ->
6356 let rec checkfds = function
6357 | [] -> ()
6358 | fd :: rest when fd = state.sr ->
6359 let cmd = readcmd state.sr in
6360 act cmd;
6361 checkfds rest
6363 | fd :: rest when fd = state.wsfd ->
6364 Wsi.readresp fd;
6365 checkfds rest
6367 | fd :: rest ->
6368 let s = String.create 80 in
6369 let n = Unix.read fd s 0 80 in
6370 if conf.redirectstderr
6371 then (
6372 Buffer.add_substring state.errmsgs s 0 n;
6373 state.newerrmsgs <- true;
6374 state.redisplay <- true;
6376 else (
6377 prerr_string (String.sub s 0 n);
6378 flush stderr;
6380 checkfds rest
6382 checkfds l;
6383 let newdeadline =
6384 let deadline1 =
6385 if deadline = infinity
6386 then now () +. 0.01
6387 else deadline
6389 match state.autoscroll with
6390 | Some step when step != 0 -> deadline1
6391 | _ -> if state.ghyll == noghyll then infinity else deadline1
6393 loop newdeadline
6394 end;
6397 loop infinity;
6398 with Quit ->
6399 wcmd "quit";
6400 Config.save ();
6401 exit 0;