Consistency
[llpp.git] / main.ml
blob3cda3653a26befa23cb7004b8bb3ab71b7e53974
1 type under =
2 | Unone
3 | Ulinkuri of string
4 | Ulinkgoto of (int * int)
5 | Utext of facename
6 and facename = string;;
8 let dolog fmt = Printf.kprintf prerr_endline fmt;;
9 let dolog2 fmt = Printf.kprintf print_endline fmt;;
10 let now = Unix.gettimeofday;;
12 exception Quit;;
14 type params = (angle * proportional * trimparams
15 * texcount * sliceheight * memsize
16 * colorspace * wmclasshack * fontpath)
17 and pageno = int
18 and width = int
19 and height = int
20 and leftx = int
21 and opaque = string
22 and recttype = int
23 and pixmapsize = int
24 and angle = int
25 and proportional = bool
26 and trimmargins = bool
27 and interpagespace = int
28 and texcount = int
29 and sliceheight = int
30 and gen = int
31 and top = float
32 and fontpath = string
33 and memsize = int
34 and aalevel = int
35 and wmclasshack = bool
36 and irect = (int * int * int * int)
37 and trimparams = (trimmargins * irect)
38 and colorspace = | Rgb | Bgr | Gray
41 type platform = | Punknown | Plinux | Pwindows | Posx | Psun
42 | Pfreebsd | Pdragonflybsd | Popenbsd | Pmingw | Pcygwin;;
44 external init : Unix.file_descr -> params -> unit = "ml_init";;
45 external seltext : string -> (int * int * int * int) -> unit = "ml_seltext";;
46 external copysel : string -> unit = "ml_copysel";;
47 external getpdimrect : int -> float array = "ml_getpdimrect";;
48 external whatsunder : string -> int -> int -> under = "ml_whatsunder";;
49 external zoomforh : int -> int -> int -> float = "ml_zoom_for_height";;
50 external drawstr : int -> int -> int -> string -> float = "ml_draw_string";;
51 external measurestr : int -> string -> float = "ml_measure_string";;
52 external getmaxw : unit -> float = "ml_getmaxw";;
53 external postprocess : opaque -> bool -> int -> int -> unit = "ml_postprocess";;
54 external pagebbox : opaque -> (int * int * int * int) = "ml_getpagebox";;
55 external platform : unit -> platform = "ml_platform";;
56 external setaalevel : int -> unit = "ml_setaalevel";;
58 let platform_to_string = function
59 | Punknown -> "unknown"
60 | Plinux -> "Linux"
61 | Pwindows -> "Windows"
62 | Posx -> "OSX"
63 | Psun -> "Sun"
64 | Pfreebsd -> "FreeBSD"
65 | Pdragonflybsd -> "DragonflyBSD"
66 | Popenbsd -> "OpenBSD"
67 | Pcygwin -> "Cygwin"
68 | Pmingw -> "MingW"
71 let platform = platform ();;
73 let is_windows =
74 match platform with
75 | Pwindows | Pmingw -> true
76 | _ -> false
79 type x = int
80 and y = int
81 and tilex = int
82 and tiley = int
83 and tileparams = (x * y * width * height * tilex * tiley)
86 external drawtile : tileparams -> string -> unit = "ml_drawtile";;
88 type mpos = int * int
89 and mstate =
90 | Msel of (mpos * mpos)
91 | Mpan of mpos
92 | Mscrolly | Mscrollx
93 | Mzoom of (int * int)
94 | Mzoomrect of (mpos * mpos)
95 | Mnone
98 type textentry = string * string * onhist option * onkey * ondone
99 and onkey = string -> int -> te
100 and ondone = string -> unit
101 and histcancel = unit -> unit
102 and onhist = ((histcmd -> string) * histcancel)
103 and histcmd = HCnext | HCprev | HCfirst | HClast
104 and te =
105 | TEstop
106 | TEdone of string
107 | TEcont of string
108 | TEswitch of textentry
111 type 'a circbuf =
112 { store : 'a array
113 ; mutable rc : int
114 ; mutable wc : int
115 ; mutable len : int
119 let bound v minv maxv =
120 max minv (min maxv v);
123 let cbnew n v =
124 { store = Array.create n v
125 ; rc = 0
126 ; wc = 0
127 ; len = 0
131 let drawstring size x y s =
132 Gl.enable `blend;
133 Gl.enable `texture_2d;
134 ignore (drawstr size x y s);
135 Gl.disable `blend;
136 Gl.disable `texture_2d;
139 let drawstring1 size x y s =
140 drawstr size x y s;
143 let drawstring2 size x y fmt =
144 Printf.kprintf (drawstring size (x+1) (y+size+1)) fmt
147 let cbcap b = Array.length b.store;;
149 let cbput b v =
150 let cap = cbcap b in
151 b.store.(b.wc) <- v;
152 b.wc <- (b.wc + 1) mod cap;
153 b.rc <- b.wc;
154 b.len <- min (b.len + 1) cap;
157 let cbempty b = b.len = 0;;
159 let cbgetg b circular dir =
160 if cbempty b
161 then b.store.(0)
162 else
163 let rc = b.rc + dir in
164 let rc =
165 if circular
166 then (
167 if rc = -1
168 then b.len-1
169 else (
170 if rc = b.len
171 then 0
172 else rc
175 else max 0 (min rc (b.len-1))
177 b.rc <- rc;
178 b.store.(rc);
181 let cbget b = cbgetg b false;;
182 let cbgetc b = cbgetg b true;;
184 type page =
185 { pageno : int
186 ; pagedimno : int
187 ; pagew : int
188 ; pageh : int
189 ; pagex : int
190 ; pagey : int
191 ; pagevw : int
192 ; pagevh : int
193 ; pagedispx : int
194 ; pagedispy : int
198 let debugl l =
199 dolog "l %d dim=%d {" l.pageno l.pagedimno;
200 dolog " WxH %dx%d" l.pagew l.pageh;
201 dolog " vWxH %dx%d" l.pagevw l.pagevh;
202 dolog " pagex,y %d,%d" l.pagex l.pagey;
203 dolog " dispx,y %d,%d" l.pagedispx l.pagedispy;
204 dolog "}";
207 let debugrect (x0, y0, x1, y1, x2, y2, x3, y3) =
208 dolog "rect {";
209 dolog " x0,y0=(% f, % f)" x0 y0;
210 dolog " x1,y1=(% f, % f)" x1 y1;
211 dolog " x2,y2=(% f, % f)" x2 y2;
212 dolog " x3,y3=(% f, % f)" x3 y3;
213 dolog "}";
216 type conf =
217 { mutable scrollbw : int
218 ; mutable scrollh : int
219 ; mutable icase : bool
220 ; mutable preload : bool
221 ; mutable pagebias : int
222 ; mutable verbose : bool
223 ; mutable debug : bool
224 ; mutable scrollstep : int
225 ; mutable maxhfit : bool
226 ; mutable crophack : bool
227 ; mutable autoscrollstep : int
228 ; mutable maxwait : float option
229 ; mutable hlinks : bool
230 ; mutable underinfo : bool
231 ; mutable interpagespace : interpagespace
232 ; mutable zoom : float
233 ; mutable presentation : bool
234 ; mutable angle : angle
235 ; mutable winw : int
236 ; mutable winh : int
237 ; mutable savebmarks : bool
238 ; mutable proportional : proportional
239 ; mutable trimmargins : trimmargins
240 ; mutable trimfuzz : irect
241 ; mutable memlimit : memsize
242 ; mutable texcount : texcount
243 ; mutable sliceheight : sliceheight
244 ; mutable thumbw : width
245 ; mutable jumpback : bool
246 ; mutable bgcolor : float * float * float
247 ; mutable bedefault : bool
248 ; mutable scrollbarinpm : bool
249 ; mutable tilew : int
250 ; mutable tileh : int
251 ; mutable mumemlimit : memsize
252 ; mutable checkers : bool
253 ; mutable aalevel : int
254 ; mutable urilauncher : string
255 ; mutable colorspace : colorspace
256 ; mutable invert : bool
257 ; mutable colorscale : float
258 ; mutable redirectstderr : bool
262 type anchor = pageno * top;;
264 type outline = string * int * anchor;;
266 type rect = float * float * float * float * float * float * float * float;;
268 type tile = opaque * pixmapsize * elapsed
269 and elapsed = float;;
270 type pagemapkey = pageno * gen;;
271 type tilemapkey = pageno * gen * colorspace * angle * width * height * col * row
272 and row = int
273 and col = int;;
275 let emptyanchor = (0, 0.0);;
277 type infochange = | Memused | Docinfo | Pdim;;
279 class type uioh = object
280 method display : unit
281 method key : int -> uioh
282 method special : Glut.special_key_t -> uioh
283 method button :
284 Glut.button_t -> Glut.mouse_button_state_t -> int -> int -> uioh
285 method motion : int -> int -> uioh
286 method pmotion : int -> int -> uioh
287 method infochanged : infochange -> unit
288 end;;
290 type mode =
291 | Birdseye of (conf * leftx * pageno * pageno * anchor)
292 | Textentry of (textentry * onleave)
293 | View
294 and onleave = leavetextentrystatus -> unit
295 and leavetextentrystatus = | Cancel | Confirm
296 and helpitem = string * int * action
297 and action =
298 | Noaction
299 | Action of (uioh -> uioh)
302 let isbirdseye = function Birdseye _ -> true | _ -> false;;
303 let istextentry = function Textentry _ -> true | _ -> false;;
305 type currently =
306 | Idle
307 | Loading of (page * gen)
308 | Tiling of (
309 page * opaque * colorspace * angle * gen * col * row * width * height
311 | Outlining of outline list
314 let nouioh : uioh = object (self)
315 method display = ()
316 method key _ = self
317 method special _ = self
318 method button _ _ _ _ = self
319 method motion _ _ = self
320 method pmotion _ _ = self
321 method infochanged _ = ()
322 end;;
324 type state =
325 { mutable csock : Unix.file_descr
326 ; mutable ssock : Unix.file_descr
327 ; mutable errfd : Unix.file_descr
328 ; mutable stderr : Unix.file_descr
329 ; mutable errmsgs : Buffer.t
330 ; mutable newerrmsgs : bool
331 ; mutable w : int
332 ; mutable x : int
333 ; mutable y : int
334 ; mutable scrollw : int
335 ; mutable hscrollh : int
336 ; mutable anchor : anchor
337 ; mutable maxy : int
338 ; mutable layout : page list
339 ; pagemap : (pagemapkey, opaque) Hashtbl.t
340 ; tilemap : (tilemapkey, tile) Hashtbl.t
341 ; tilelru : (tilemapkey * opaque * pixmapsize) Queue.t
342 ; mutable pdims : (pageno * width * height * leftx) list
343 ; mutable pagecount : int
344 ; mutable currently : currently
345 ; mutable mstate : mstate
346 ; mutable searchpattern : string
347 ; mutable rects : (pageno * recttype * rect) list
348 ; mutable rects1 : (pageno * recttype * rect) list
349 ; mutable text : string
350 ; mutable fullscreen : (width * height) option
351 ; mutable mode : mode
352 ; mutable uioh : uioh
353 ; mutable outlines : outline array
354 ; mutable bookmarks : outline list
355 ; mutable path : string
356 ; mutable password : string
357 ; mutable invalidated : int
358 ; mutable memused : memsize
359 ; mutable gen : gen
360 ; mutable throttle : (page list * int * float) option
361 ; mutable autoscroll : int option
362 ; mutable help : helpitem array
363 ; mutable docinfo : (int * string) list
364 ; mutable deadline : float
365 ; mutable texid : GlTex.texture_id option
366 ; hists : hists
367 ; mutable prevzoom : float
368 ; mutable progress : float
370 and hists =
371 { pat : string circbuf
372 ; pag : string circbuf
373 ; nav : anchor circbuf
377 let defconf =
378 { scrollbw = 7
379 ; scrollh = 12
380 ; icase = true
381 ; preload = true
382 ; pagebias = 0
383 ; verbose = false
384 ; debug = false
385 ; scrollstep = 24
386 ; maxhfit = true
387 ; crophack = false
388 ; autoscrollstep = 2
389 ; maxwait = None
390 ; hlinks = false
391 ; underinfo = false
392 ; interpagespace = 2
393 ; zoom = 1.0
394 ; presentation = false
395 ; angle = 0
396 ; winw = 900
397 ; winh = 900
398 ; savebmarks = true
399 ; proportional = true
400 ; trimmargins = false
401 ; trimfuzz = (0,0,0,0)
402 ; memlimit = 32 lsl 20
403 ; texcount = 256
404 ; sliceheight = 24
405 ; thumbw = 76
406 ; jumpback = true
407 ; bgcolor = (0.5, 0.5, 0.5)
408 ; bedefault = false
409 ; scrollbarinpm = true
410 ; tilew = 2048
411 ; tileh = 2048
412 ; mumemlimit = 128 lsl 20
413 ; checkers = true
414 ; aalevel = 8
415 ; urilauncher =
416 (match platform with
417 | Plinux | Pfreebsd | Pdragonflybsd | Popenbsd | Psun -> "xdg-open \"%s\""
418 | Posx -> "open \"%s\""
419 | Pwindows | Pcygwin | Pmingw -> "iexplore \"%s\""
420 | _ -> "")
421 ; colorspace = Rgb
422 ; invert = false
423 ; colorscale = 1.0
424 ; redirectstderr = false
428 let conf = { defconf with angle = defconf.angle };;
430 type fontstate =
431 { mutable fontsize : int
432 ; mutable wwidth : float
433 ; mutable maxrows : int
437 let fstate =
438 { fontsize = 14
439 ; wwidth = nan
440 ; maxrows = -1
444 let setfontsize n =
445 fstate.fontsize <- n;
446 fstate.wwidth <- measurestr fstate.fontsize "w";
447 fstate.maxrows <- (conf.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
450 let gotouri uri =
451 if String.length conf.urilauncher = 0
452 then print_endline uri
453 else
454 let re = Str.regexp "%s" in
455 let command = Str.global_replace re uri conf.urilauncher in
456 let optic =
457 try Some (Unix.open_process_in command)
458 with exn ->
459 Printf.eprintf
460 "failed to execute `%s': %s\n" command (Printexc.to_string exn);
461 flush stderr;
462 None
464 match optic with
465 | Some ic -> close_in ic
466 | None -> ()
469 let version () =
470 Printf.sprintf "llpp version %s (%s/%dbit, ocaml %s)" Help.version
471 (platform_to_string platform) Sys.word_size Sys.ocaml_version
474 let makehelp () =
475 let strings = version () :: "" :: Help.keys in
476 Array.of_list (
477 let r = Str.regexp "\\(http://[^ ]+\\)" in
478 List.map (fun s ->
479 if (try Str.search_forward r s 0 with Not_found -> -1) >= 0
480 then
481 let uri = Str.matched_string s in
482 (s, 0, Action (fun u -> gotouri uri; u))
483 else s, 0, Noaction) strings
487 let state =
488 { csock = Unix.stdin
489 ; ssock = Unix.stdin
490 ; errfd = Unix.stdin
491 ; stderr = Unix.stderr
492 ; errmsgs = Buffer.create 0
493 ; newerrmsgs = false
494 ; x = 0
495 ; y = 0
496 ; w = 0
497 ; scrollw = 0
498 ; hscrollh = 0
499 ; anchor = emptyanchor
500 ; layout = []
501 ; maxy = max_int
502 ; tilelru = Queue.create ()
503 ; pagemap = Hashtbl.create 10
504 ; tilemap = Hashtbl.create 10
505 ; pdims = []
506 ; pagecount = 0
507 ; currently = Idle
508 ; mstate = Mnone
509 ; rects = []
510 ; rects1 = []
511 ; text = ""
512 ; mode = View
513 ; fullscreen = None
514 ; searchpattern = ""
515 ; outlines = [||]
516 ; bookmarks = []
517 ; path = ""
518 ; password = ""
519 ; invalidated = 0
520 ; hists =
521 { nav = cbnew 10 (0, 0.0)
522 ; pat = cbnew 1 ""
523 ; pag = cbnew 1 ""
525 ; memused = 0
526 ; gen = 0
527 ; throttle = None
528 ; autoscroll = None
529 ; help = makehelp ()
530 ; docinfo = []
531 ; deadline = nan
532 ; texid = None
533 ; prevzoom = 1.0
534 ; progress = -1.0
535 ; uioh = nouioh
539 let vlog fmt =
540 if conf.verbose
541 then
542 Printf.kprintf prerr_endline fmt
543 else
544 Printf.kprintf ignore fmt
547 let redirectstderr () =
548 if conf.redirectstderr
549 then
550 let rfd, wfd = Unix.pipe () in
551 state.stderr <- Unix.dup Unix.stderr;
552 state.errfd <- rfd;
553 Unix.dup2 wfd Unix.stderr;
554 else (
555 state.newerrmsgs <- false;
556 Unix.dup2 state.stderr Unix.stderr;
557 prerr_string (Buffer.contents state.errmsgs);
558 flush stderr;
559 Buffer.clear state.errmsgs;
563 module G =
564 struct
565 let postRedisplay who =
566 if conf.verbose
567 then prerr_endline ("redisplay for " ^ who);
568 Glut.postRedisplay ();
570 end;;
572 let addchar s c =
573 let b = Buffer.create (String.length s + 1) in
574 Buffer.add_string b s;
575 Buffer.add_char b c;
576 Buffer.contents b;
579 let colorspace_of_string s =
580 match String.lowercase s with
581 | "rgb" -> Rgb
582 | "bgr" -> Bgr
583 | "gray" -> Gray
584 | _ -> failwith "invalid colorspace"
587 let int_of_colorspace = function
588 | Rgb -> 0
589 | Bgr -> 1
590 | Gray -> 2
593 let colorspace_of_int = function
594 | 0 -> Rgb
595 | 1 -> Bgr
596 | 2 -> Gray
597 | n -> failwith ("invalid colorspace index " ^ string_of_int n)
600 let colorspace_to_string = function
601 | Rgb -> "rgb"
602 | Bgr -> "bgr"
603 | Gray -> "gray"
606 let intentry_with_suffix text key =
607 let c = Char.unsafe_chr key in
608 match Char.lowercase c with
609 | '0' .. '9' ->
610 let text = addchar text c in
611 TEcont text
613 | 'k' | 'm' | 'g' ->
614 let text = addchar text c in
615 TEcont text
617 | _ ->
618 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
619 TEcont text
622 let writecmd fd s =
623 let len = String.length s in
624 let n = 4 + len in
625 let b = Buffer.create n in
626 Buffer.add_char b (Char.chr ((len lsr 24) land 0xff));
627 Buffer.add_char b (Char.chr ((len lsr 16) land 0xff));
628 Buffer.add_char b (Char.chr ((len lsr 8) land 0xff));
629 Buffer.add_char b (Char.chr ((len lsr 0) land 0xff));
630 Buffer.add_string b s;
631 let s' = Buffer.contents b in
632 let n' = Unix.write fd s' 0 n in
633 if n' != n then failwith "write failed";
636 let readcmd fd =
637 let s = "xxxx" in
638 let n = Unix.read fd s 0 4 in
639 if n != 4 then failwith "incomplete read(len)";
640 let len = 0
641 lor (Char.code s.[0] lsl 24)
642 lor (Char.code s.[1] lsl 16)
643 lor (Char.code s.[2] lsl 8)
644 lor (Char.code s.[3] lsl 0)
646 let s = String.create len in
647 let n = Unix.read fd s 0 len in
648 if n != len then failwith "incomplete read(data)";
652 let makecmd s l =
653 let b = Buffer.create 10 in
654 Buffer.add_string b s;
655 let rec combine = function
656 | [] -> b
657 | x :: xs ->
658 Buffer.add_char b ' ';
659 let s =
660 match x with
661 | `b b -> if b then "1" else "0"
662 | `s s -> s
663 | `i i -> string_of_int i
664 | `f f -> string_of_float f
665 | `I f -> string_of_int (truncate f)
667 Buffer.add_string b s;
668 combine xs;
670 combine l;
673 let wcmd s l =
674 let cmd = Buffer.contents (makecmd s l) in
675 writecmd state.csock cmd;
678 let calcips h =
679 if conf.presentation
680 then
681 let d = conf.winh - h in
682 max 0 ((d + 1) / 2)
683 else
684 conf.interpagespace
687 let calcheight () =
688 let rec f pn ph pi fh l =
689 match l with
690 | (n, _, h, _) :: rest ->
691 let ips = calcips h in
692 let fh =
693 if conf.presentation
694 then fh+ips
695 else (
696 if isbirdseye state.mode && pn = 0
697 then fh + ips
698 else fh
701 let fh = fh + ((n - pn) * (ph + pi)) in
702 f n h ips fh rest;
704 | [] ->
705 let inc =
706 if conf.presentation || (isbirdseye state.mode && pn = 0)
707 then 0
708 else -pi
710 let fh = fh + ((state.pagecount - pn) * (ph + pi)) + inc in
711 max 0 fh
713 let fh = f 0 0 0 0 state.pdims in
717 let getpageyh pageno =
718 let rec f pn ph pi y l =
719 match l with
720 | (n, _, h, _) :: rest ->
721 let ips = calcips h in
722 if n >= pageno
723 then
724 let h = if n = pageno then h else ph in
725 if conf.presentation && n = pageno
726 then
727 y + (pageno - pn) * (ph + pi) + pi, h
728 else
729 y + (pageno - pn) * (ph + pi), h
730 else
731 let y = y + (if conf.presentation then pi else 0) in
732 let y = y + (n - pn) * (ph + pi) in
733 f n h ips y rest
735 | [] ->
736 y + (pageno - pn) * (ph + pi), ph
738 f 0 0 0 0 state.pdims
741 let getpagedim pageno =
742 let rec f ppdim l =
743 match l with
744 | (n, _, _, _) as pdim :: rest ->
745 if n >= pageno
746 then (if n = pageno then pdim else ppdim)
747 else f pdim rest
749 | [] -> ppdim
751 f (-1, -1, -1, -1) state.pdims
754 let getpagey pageno = fst (getpageyh pageno);;
756 let layout y sh =
757 let sh = sh - state.hscrollh in
758 let rec f ~pageno ~pdimno ~prev ~py ~dy ~pdims ~accu =
759 let ((w, h, ips, xoff) as curr), rest, pdimno, yinc =
760 match pdims with
761 | (pageno', w, h, xoff) :: rest when pageno' = pageno ->
762 let ips = calcips h in
763 let yinc =
764 if conf.presentation || (isbirdseye state.mode && pageno = 0)
765 then ips
766 else 0
768 (w, h, ips, xoff), rest, pdimno + 1, yinc
769 | _ ->
770 prev, pdims, pdimno, 0
772 let dy = dy + yinc in
773 let py = py + yinc in
774 if pageno = state.pagecount || dy >= sh
775 then
776 accu
777 else
778 let vy = y + dy in
779 if py + h <= vy - yinc
780 then
781 let py = py + h + ips in
782 let dy = max 0 (py - y) in
783 f ~pageno:(pageno+1)
784 ~pdimno
785 ~prev:curr
788 ~pdims:rest
789 ~accu
790 else
791 let pagey = vy - py in
792 let pagevh = h - pagey in
793 let pagevh = min (sh - dy) pagevh in
794 let off = if yinc > 0 then py - vy else 0 in
795 let py = py + h + ips in
796 let pagex, dx =
797 let xoff = xoff +
798 if state.w < conf.winw - state.scrollw
799 then (conf.winw - state.scrollw - state.w) / 2
800 else 0
802 let dispx = xoff + state.x in
803 if dispx < 0
804 then (-dispx, 0)
805 else (0, dispx)
807 let pagevw =
808 let lw = w - pagex in
809 min lw (conf.winw - state.scrollw)
811 let e =
812 { pageno = pageno
813 ; pagedimno = pdimno
814 ; pagew = w
815 ; pageh = h
816 ; pagex = pagex
817 ; pagey = pagey + off
818 ; pagevw = pagevw
819 ; pagevh = pagevh - off
820 ; pagedispx = dx
821 ; pagedispy = dy + off
824 let accu = e :: accu in
825 f ~pageno:(pageno+1)
826 ~pdimno
827 ~prev:curr
829 ~dy:(dy+pagevh+ips)
830 ~pdims:rest
831 ~accu
833 if state.invalidated = 0
834 then (
835 let accu =
837 ~pageno:0
838 ~pdimno:~-1
839 ~prev:(0,0,0,0)
840 ~py:0
841 ~dy:0
842 ~pdims:state.pdims
843 ~accu:[]
845 List.rev accu
847 else
851 let clamp incr =
852 let y = state.y + incr in
853 let y = max 0 y in
854 let y = min y (state.maxy - (if conf.maxhfit then conf.winh else 0)) in
858 let getopaque pageno =
859 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
860 with Not_found -> None
863 let putopaque pageno opaque =
864 Hashtbl.replace state.pagemap (pageno, state.gen) opaque
867 let itertiles l f =
868 let tilex = l.pagex mod conf.tilew in
869 let tiley = l.pagey mod conf.tileh in
871 let col = l.pagex / conf.tilew in
872 let row = l.pagey / conf.tileh in
874 let vw =
875 let a = l.pagew - l.pagex in
876 let b = conf.winw - state.scrollw in
877 min a b
878 and vh = l.pagevh in
880 let rec rowloop row y0 dispy h =
881 if h = 0
882 then ()
883 else (
884 let dh = conf.tileh - y0 in
885 let dh = min h dh in
886 let rec colloop col x0 dispx w =
887 if w = 0
888 then ()
889 else (
890 let dw = conf.tilew - x0 in
891 let dw = min w dw in
893 f col row dispx dispy x0 y0 dw dh;
894 colloop (col+1) 0 (dispx+dw) (w-dw)
897 colloop col tilex l.pagedispx vw;
898 rowloop (row+1) 0 (dispy+dh) (h-dh)
901 if vw > 0 && vh > 0
902 then rowloop row tiley l.pagedispy vh;
905 let gettileopaque l col row =
906 let key =
907 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
909 try Some (Hashtbl.find state.tilemap key)
910 with Not_found -> None
913 let puttileopaque l col row gen colorspace angle opaque size elapsed =
914 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
915 Hashtbl.add state.tilemap key (opaque, size, elapsed)
918 let drawtiles l color =
919 GlDraw.color color;
920 let f col row x y tilex tiley w h =
921 match gettileopaque l col row with
922 | Some (opaque, _, t) ->
923 let params = x, y, w, h, tilex, tiley in
924 if conf.invert
925 then (
926 Gl.enable `blend;
927 GlFunc.blend_func `zero `one_minus_src_color;
929 drawtile params opaque;
930 if conf.invert
931 then Gl.disable `blend;
932 if conf.debug
933 then (
934 let s = Printf.sprintf
935 "%d[%d,%d] %f sec"
936 l.pageno col row t
938 let ww = fstate.wwidth in
939 GlMisc.push_attrib [`current];
940 GlDraw.color (0.0, 0.0, 0.0);
941 GlDraw.rect
942 (float (x-2), float (y-2))
943 (float (x+2) +. ww, float (y + fstate.fontsize + 2));
944 GlDraw.color (1.0, 1.0, 1.0);
945 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
946 GlMisc.pop_attrib ();
949 | _ ->
950 let w =
951 let lw = conf.winw - state.scrollw - x in
952 min lw w
953 and h =
954 let lh = conf.winh - y in
955 min lh h
957 Gl.enable `texture_2d;
958 begin match state.texid with
959 | Some id ->
960 GlTex.bind_texture `texture_2d id;
961 let x0 = float x
962 and y0 = float y
963 and x1 = float (x+w)
964 and y1 = float (y+h) in
966 let tw = float w /. 64.0
967 and th = float h /. 64.0 in
968 let tx0 = float tilex /. 64.0
969 and ty0 = float tiley /. 64.0 in
970 let tx1 = tx0 +. tw
971 and ty1 = ty0 +. th in
972 GlDraw.begins `quads;
973 GlTex.coord2 (tx0, ty0); GlDraw.vertex2 (x0, y0);
974 GlTex.coord2 (tx0, ty1); GlDraw.vertex2 (x0, y1);
975 GlTex.coord2 (tx1, ty1); GlDraw.vertex2 (x1, y1);
976 GlTex.coord2 (tx1, ty0); GlDraw.vertex2 (x1, y0);
977 GlDraw.ends ();
979 Gl.disable `texture_2d;
980 | None ->
981 GlDraw.color (1.0, 1.0, 1.0);
982 GlDraw.rect
983 (float x, float y)
984 (float (x+w), float (y+h));
985 end;
986 if w > 128 && h > fstate.fontsize + 10
987 then (
988 GlDraw.color (0.0, 0.0, 0.0);
989 let c, r =
990 if conf.verbose
991 then (col*conf.tilew, row*conf.tileh)
992 else col, row
994 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
996 GlDraw.color color;
998 itertiles l f
1001 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
1003 let tilevisible1 l x y =
1004 let ax0 = l.pagex
1005 and ax1 = l.pagex + l.pagevw
1006 and ay0 = l.pagey
1007 and ay1 = l.pagey + l.pagevh in
1009 let bx0 = x
1010 and by0 = y in
1011 let bx1 = min (bx0 + conf.tilew) l.pagew
1012 and by1 = min (by0 + conf.tileh) l.pageh in
1014 let rx0 = max ax0 bx0
1015 and ry0 = max ay0 by0
1016 and rx1 = min ax1 bx1
1017 and ry1 = min ay1 by1 in
1019 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
1020 nonemptyintersection
1023 let tilevisible layout n x y =
1024 let rec findpageinlayout = function
1025 | l :: _ when l.pageno = n -> tilevisible1 l x y
1026 | _ :: rest -> findpageinlayout rest
1027 | [] -> false
1029 findpageinlayout layout
1032 let tileready l x y =
1033 tilevisible1 l x y &&
1034 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
1037 let tilepage n p layout =
1038 let rec loop = function
1039 | l :: rest ->
1040 if l.pageno = n
1041 then
1042 let f col row _ _ _ _ _ _ =
1043 if state.currently = Idle
1044 then
1045 match gettileopaque l col row with
1046 | Some _ -> ()
1047 | None ->
1048 let x = col*conf.tilew
1049 and y = row*conf.tileh in
1050 let w =
1051 let w = l.pagew - x in
1052 min w conf.tilew
1054 let h =
1055 let h = l.pageh - y in
1056 min h conf.tileh
1058 wcmd "tile"
1059 [`s p
1060 ;`i x
1061 ;`i y
1062 ;`i w
1063 ;`i h
1065 state.currently <-
1066 Tiling (
1067 l, p, conf.colorspace, conf.angle, state.gen, col, row,
1068 conf.tilew, conf.tileh
1071 itertiles l f;
1072 else
1073 loop rest
1075 | [] -> ()
1077 if state.invalidated = 0 then loop layout;
1080 let preloadlayout visiblepages =
1081 let presentation = conf.presentation in
1082 let interpagespace = conf.interpagespace in
1083 let maxy = state.maxy in
1084 conf.presentation <- false;
1085 conf.interpagespace <- 0;
1086 state.maxy <- calcheight ();
1087 let y =
1088 match visiblepages with
1089 | [] -> 0
1090 | l :: _ -> getpagey l.pageno + l.pagey
1092 let y = if y < conf.winh then 0 else y - conf.winh in
1093 let h = state.y - y + conf.winh*3 in
1094 let pages = layout y h in
1095 conf.presentation <- presentation;
1096 conf.interpagespace <- interpagespace;
1097 state.maxy <- maxy;
1098 pages;
1101 let load pages =
1102 let rec loop pages =
1103 if state.currently != Idle
1104 then ()
1105 else
1106 match pages with
1107 | l :: rest ->
1108 begin match getopaque l.pageno with
1109 | None ->
1110 wcmd "page" [`i l.pageno; `i l.pagedimno];
1111 state.currently <- Loading (l, state.gen);
1112 | Some opaque ->
1113 tilepage l.pageno opaque pages;
1114 loop rest
1115 end;
1116 | _ -> ()
1118 if state.invalidated = 0 then loop pages
1121 let preload pages =
1122 load pages;
1123 if conf.preload && state.currently = Idle
1124 then load (preloadlayout pages);
1127 let layoutready layout =
1128 let rec fold all ls =
1129 all && match ls with
1130 | l :: rest ->
1131 let seen = ref false in
1132 let allvisible = ref true in
1133 let foo col row _ _ _ _ _ _ =
1134 seen := true;
1135 allvisible := !allvisible &&
1136 begin match gettileopaque l col row with
1137 | Some _ -> true
1138 | None -> false
1141 itertiles l foo;
1142 fold (!seen && !allvisible) rest
1143 | [] -> true
1145 let alltilesvisible = fold true layout in
1146 alltilesvisible;
1149 let gotoy y =
1150 let y = bound y 0 state.maxy in
1151 let y, layout, proceed =
1152 match conf.maxwait with
1153 | Some time ->
1154 begin match state.throttle with
1155 | None ->
1156 let layout = layout y conf.winh in
1157 let ready = layoutready layout in
1158 if not ready
1159 then (
1160 load layout;
1161 state.throttle <- Some (layout, y, now ());
1163 else G.postRedisplay "gotoy showall (None)";
1164 y, layout, ready
1165 | Some (_, _, started) ->
1166 let dt = now () -. started in
1167 if dt > time
1168 then (
1169 state.throttle <- None;
1170 let layout = layout y conf.winh in
1171 load layout;
1172 G.postRedisplay "maxwait";
1173 y, layout, true
1175 else -1, [], false
1178 | None ->
1179 let layout = layout y conf.winh in
1180 if true || layoutready layout
1181 then G.postRedisplay "gotoy ready";
1182 y, layout, true
1184 if proceed
1185 then (
1186 state.y <- y;
1187 state.layout <- layout;
1188 begin match state.mode with
1189 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
1190 if not (pagevisible layout pageno)
1191 then (
1192 match state.layout with
1193 | [] -> ()
1194 | l :: _ ->
1195 state.mode <- Birdseye (
1196 conf, leftx, l.pageno, hooverpageno, anchor
1199 | _ -> ()
1200 end;
1201 preload layout;
1205 let conttiling pageno opaque =
1206 tilepage pageno opaque
1207 (if conf.preload then preloadlayout state.layout else state.layout)
1210 let gotoy_and_clear_text y =
1211 gotoy y;
1212 if not conf.verbose then state.text <- "";
1215 let getanchor () =
1216 match state.layout with
1217 | [] -> emptyanchor
1218 | l :: _ -> (l.pageno, float l.pagey /. float l.pageh)
1221 let getanchory (n, top) =
1222 let y, h = getpageyh n in
1223 y + (truncate (top *. float h));
1226 let gotoanchor anchor =
1227 gotoy (getanchory anchor);
1230 let addnav () =
1231 cbput state.hists.nav (getanchor ());
1234 let getnav dir =
1235 let anchor = cbgetc state.hists.nav dir in
1236 getanchory anchor;
1239 let gotopage n top =
1240 let y, h = getpageyh n in
1241 gotoy_and_clear_text (y + (truncate (top *. float h)));
1244 let gotopage1 n top =
1245 let y = getpagey n in
1246 gotoy_and_clear_text (y + top);
1249 let invalidate () =
1250 state.layout <- [];
1251 state.pdims <- [];
1252 state.rects <- [];
1253 state.rects1 <- [];
1254 state.invalidated <- state.invalidated + 1;
1257 let writeopen path password =
1258 writecmd state.csock ("open " ^ path ^ "\000" ^ password ^ "\000");
1261 let opendoc path password =
1262 invalidate ();
1263 state.path <- path;
1264 state.password <- password;
1265 state.gen <- state.gen + 1;
1266 state.docinfo <- [];
1268 setaalevel conf.aalevel;
1269 writeopen path password;
1270 Glut.setWindowTitle ("llpp " ^ Filename.basename path);
1271 wcmd "geometry" [`i state.w; `i conf.winh];
1274 let scalecolor c =
1275 let c = c *. conf.colorscale in
1276 (c, c, c);
1279 let scalecolor2 (r, g, b) =
1280 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
1283 let represent () =
1284 state.maxy <- calcheight ();
1285 state.hscrollh <-
1286 if state.w <= conf.winw - state.scrollw
1287 then 0
1288 else state.scrollw
1290 match state.mode with
1291 | Birdseye (_, _, pageno, _, _) ->
1292 let y, h = getpageyh pageno in
1293 let top = (conf.winh - h) / 2 in
1294 gotoy (max 0 (y - top))
1295 | _ -> gotoanchor state.anchor
1298 let reshape =
1299 let firsttime = ref true in
1300 fun ~w ~h ->
1301 GlDraw.viewport 0 0 w h;
1302 if state.invalidated = 0 && not !firsttime
1303 then state.anchor <- getanchor ();
1305 firsttime := false;
1306 conf.winw <- w;
1307 let w = truncate (float w *. conf.zoom) - state.scrollw in
1308 let w = max w 2 in
1309 state.w <- w;
1310 conf.winh <- h;
1311 setfontsize fstate.fontsize;
1312 GlMat.mode `modelview;
1313 GlMat.load_identity ();
1315 GlMat.mode `projection;
1316 GlMat.load_identity ();
1317 GlMat.rotate ~x:1.0 ~angle:180.0 ();
1318 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
1319 GlMat.scale3 (2.0 /. float conf.winw, 2.0 /. float conf.winh, 1.0);
1321 invalidate ();
1322 wcmd "geometry" [`i w; `i h];
1325 let enttext () =
1326 let len = String.length state.text in
1327 let drawstring s =
1328 let hscrollh =
1329 match state.mode with
1330 | View -> state.hscrollh
1331 | _ -> 0
1333 let rect x w =
1334 GlDraw.rect
1335 (x, float (conf.winh - (fstate.fontsize + 4) - hscrollh))
1336 (x+.w, float (conf.winh - hscrollh))
1339 let w = float (conf.winw - state.scrollw - 1) in
1340 if state.progress >= 0.0 && state.progress < 1.0
1341 then (
1342 GlDraw.color (0.3, 0.3, 0.3);
1343 let w1 = w *. state.progress in
1344 rect 0.0 w1;
1345 GlDraw.color (0.0, 0.0, 0.0);
1346 rect w1 (w-.w1)
1348 else (
1349 GlDraw.color (0.0, 0.0, 0.0);
1350 rect 0.0 w;
1353 GlDraw.color (1.0, 1.0, 1.0);
1354 drawstring fstate.fontsize
1355 (if len > 0 then 8 else 2) (conf.winh - hscrollh - 5) s;
1357 let s =
1358 match state.mode with
1359 | Textentry ((prefix, text, _, _, _), _) ->
1360 let s =
1361 if len > 0
1362 then
1363 Printf.sprintf "%s%s_ [%s]" prefix text state.text
1364 else
1365 Printf.sprintf "%s%s_" prefix text
1369 | _ -> state.text
1371 let s =
1372 if state.newerrmsgs
1373 then (
1374 if not (istextentry state.mode)
1375 then
1376 let s1 = "(press 'e' to review error messasges)" in
1377 if String.length s > 0 then s ^ " " ^ s1 else s1
1378 else s
1380 else s
1382 if String.length s > 0
1383 then drawstring s
1386 let showtext c s =
1387 state.text <- Printf.sprintf "%c%s" c s;
1388 G.postRedisplay "showtext";
1391 let gctiles () =
1392 let len = Queue.length state.tilelru in
1393 let rec loop qpos =
1394 if state.memused <= conf.memlimit
1395 then ()
1396 else (
1397 if qpos < len
1398 then
1399 let (k, p, s) as lruitem = Queue.pop state.tilelru in
1400 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
1401 let (_, pw, ph, _) = getpagedim n in
1403 gen = state.gen
1404 && colorspace = conf.colorspace
1405 && angle = conf.angle
1406 && pagew = pw
1407 && pageh = ph
1408 && (
1409 let layout =
1410 match state.throttle with
1411 | None ->
1412 if conf.preload
1413 then preloadlayout state.layout
1414 else state.layout
1415 | Some (layout, _, _) ->
1416 layout
1418 let x = col*conf.tilew
1419 and y = row*conf.tileh in
1420 tilevisible layout n x y
1422 then Queue.push lruitem state.tilelru
1423 else (
1424 wcmd "freetile" [`s p];
1425 state.memused <- state.memused - s;
1426 state.uioh#infochanged Memused;
1427 Hashtbl.remove state.tilemap k;
1429 loop (qpos+1)
1432 loop 0
1435 let flushtiles () =
1436 Queue.iter (fun (k, p, s) ->
1437 wcmd "freetile" [`s p];
1438 state.memused <- state.memused - s;
1439 state.uioh#infochanged Memused;
1440 Hashtbl.remove state.tilemap k;
1441 ) state.tilelru;
1442 Queue.clear state.tilelru;
1443 load state.layout;
1446 let logcurrently = function
1447 | Idle -> dolog "Idle"
1448 | Loading (l, gen) ->
1449 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
1450 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
1451 dolog
1452 "Tiling %d[%d,%d] page=%s cs=%s angle"
1453 l.pageno col row pageopaque
1454 (colorspace_to_string colorspace)
1456 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
1457 angle gen conf.angle state.gen
1458 tilew tileh
1459 conf.tilew conf.tileh
1461 | Outlining _ ->
1462 dolog "outlining"
1465 let act cmds =
1466 (* dolog "%S" cmds; *)
1467 let op, args =
1468 let spacepos =
1469 try String.index cmds ' '
1470 with Not_found -> -1
1472 if spacepos = -1
1473 then cmds, ""
1474 else
1475 let l = String.length cmds in
1476 let op = String.sub cmds 0 spacepos in
1477 op, begin
1478 if l - spacepos < 2 then ""
1479 else String.sub cmds (spacepos+1) (l-spacepos-1)
1482 match op with
1483 | "clear" ->
1484 state.uioh#infochanged Pdim;
1485 state.pdims <- [];
1487 | "clearrects" ->
1488 state.rects <- state.rects1;
1489 G.postRedisplay "clearrects";
1491 | "continue" ->
1492 let n =
1493 try Scanf.sscanf args "%u" (fun n -> n)
1494 with exn ->
1495 dolog "error processing 'continue' %S: %s"
1496 cmds (Printexc.to_string exn);
1497 exit 1;
1499 state.pagecount <- n;
1500 state.invalidated <- state.invalidated - 1;
1501 begin match state.currently with
1502 | Outlining l ->
1503 state.currently <- Idle;
1504 state.outlines <- Array.of_list (List.rev l)
1505 | _ -> ()
1506 end;
1507 if state.invalidated = 0
1508 then represent ();
1509 if conf.maxwait = None
1510 then G.postRedisplay "continue";
1512 | "title" ->
1513 Glut.setWindowTitle args
1515 | "msg" ->
1516 showtext ' ' args
1518 | "vmsg" ->
1519 if conf.verbose
1520 then showtext ' ' args
1522 | "progress" ->
1523 let progress, text =
1525 Scanf.sscanf args "%f %n"
1526 (fun f pos ->
1527 f, String.sub args pos (String.length args - pos))
1528 with exn ->
1529 dolog "error processing 'progress' %S: %s"
1530 cmds (Printexc.to_string exn);
1531 exit 1;
1533 state.text <- text;
1534 state.progress <- progress;
1535 G.postRedisplay "progress"
1537 | "firstmatch" ->
1538 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1540 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
1541 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1542 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1543 with exn ->
1544 dolog "error processing 'firstmatch' %S: %s"
1545 cmds (Printexc.to_string exn);
1546 exit 1;
1548 let y = (getpagey pageno) + truncate y0 in
1549 addnav ();
1550 gotoy y;
1551 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
1553 | "match" ->
1554 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1556 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
1557 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1558 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1559 with exn ->
1560 dolog "error processing 'match' %S: %s"
1561 cmds (Printexc.to_string exn);
1562 exit 1;
1564 state.rects1 <-
1565 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
1567 | "page" ->
1568 let pageopaque, t =
1570 Scanf.sscanf args "%s %f" (fun p t -> p, t)
1571 with exn ->
1572 dolog "error processing 'page' %S: %s"
1573 cmds (Printexc.to_string exn);
1574 exit 1;
1576 begin match state.currently with
1577 | Loading (l, gen) ->
1578 vlog "page %d took %f sec" l.pageno t;
1579 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
1580 begin match state.throttle with
1581 | None ->
1582 let preloadedpages =
1583 if conf.preload
1584 then preloadlayout state.layout
1585 else state.layout
1587 let evict () =
1588 let module IntSet =
1589 Set.Make (struct type t = int let compare = (-) end) in
1590 let set =
1591 List.fold_left (fun s l -> IntSet.add l.pageno s)
1592 IntSet.empty preloadedpages
1594 let evictedpages =
1595 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
1596 if not (IntSet.mem pageno set)
1597 then (
1598 wcmd "freepage" [`s opaque];
1599 key :: accu
1601 else accu
1602 ) state.pagemap []
1604 List.iter (Hashtbl.remove state.pagemap) evictedpages;
1606 evict ();
1607 state.currently <- Idle;
1608 if gen = state.gen
1609 then (
1610 tilepage l.pageno pageopaque state.layout;
1611 load state.layout;
1612 load preloadedpages;
1613 if pagevisible state.layout l.pageno
1614 && layoutready state.layout
1615 then G.postRedisplay "page";
1618 | Some (layout, _, _) ->
1619 state.currently <- Idle;
1620 tilepage l.pageno pageopaque layout;
1621 load state.layout
1622 end;
1624 | _ ->
1625 dolog "Inconsistent loading state";
1626 logcurrently state.currently;
1627 raise Quit;
1630 | "tile" ->
1631 let (x, y, opaque, size, t) =
1633 Scanf.sscanf args "%u %u %s %u %f"
1634 (fun x y p size t -> (x, y, p, size, t))
1635 with exn ->
1636 dolog "error processing 'tile' %S: %s"
1637 cmds (Printexc.to_string exn);
1638 exit 1;
1640 begin match state.currently with
1641 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
1642 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
1644 if tilew != conf.tilew || tileh != conf.tileh
1645 then (
1646 wcmd "freetile" [`s opaque];
1647 state.currently <- Idle;
1648 load state.layout;
1650 else (
1651 puttileopaque l col row gen cs angle opaque size t;
1652 state.memused <- state.memused + size;
1653 state.uioh#infochanged Memused;
1654 gctiles ();
1655 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
1656 opaque, size) state.tilelru;
1658 let layout =
1659 match state.throttle with
1660 | None -> state.layout
1661 | Some (layout, _, _) -> layout
1664 state.currently <- Idle;
1665 if gen = state.gen
1666 && conf.colorspace = cs
1667 && conf.angle = angle
1668 && tilevisible layout l.pageno x y
1669 then conttiling l.pageno pageopaque;
1671 begin match state.throttle with
1672 | None ->
1673 preload state.layout;
1674 if gen = state.gen
1675 && conf.colorspace = cs
1676 && conf.angle = angle
1677 && tilevisible state.layout l.pageno x y
1678 then G.postRedisplay "tile nothrottle";
1680 | Some (layout, y, _) ->
1681 let ready = layoutready layout in
1682 if ready
1683 then (
1684 state.y <- y;
1685 state.layout <- layout;
1686 state.throttle <- None;
1687 G.postRedisplay "throttle";
1689 else load layout;
1690 end;
1693 | _ ->
1694 dolog "Inconsistent tiling state";
1695 logcurrently state.currently;
1696 raise Quit;
1699 | "pdim" ->
1700 let pdim =
1702 Scanf.sscanf args "%u %u %u %u" (fun n w h x -> n, w, h, x)
1703 with exn ->
1704 dolog "error processing 'pdim' %S: %s"
1705 cmds (Printexc.to_string exn);
1706 exit 1;
1708 state.uioh#infochanged Pdim;
1709 state.pdims <- pdim :: state.pdims
1711 | "o" ->
1712 let (l, n, t, h, pos) =
1714 Scanf.sscanf args "%u %u %d %u %n"
1715 (fun l n t h pos -> l, n, t, h, pos)
1716 with exn ->
1717 dolog "error processing 'o' %S: %s"
1718 cmds (Printexc.to_string exn);
1719 exit 1;
1721 let s = String.sub args pos (String.length args - pos) in
1722 let outline = (s, l, (n, float t /. float h)) in
1723 begin match state.currently with
1724 | Outlining outlines ->
1725 state.currently <- Outlining (outline :: outlines)
1726 | Idle ->
1727 state.currently <- Outlining [outline]
1728 | currently ->
1729 dolog "invalid outlining state";
1730 logcurrently currently
1733 | "info" ->
1734 state.docinfo <- (1, args) :: state.docinfo
1736 | "infoend" ->
1737 state.uioh#infochanged Docinfo;
1738 state.docinfo <- List.rev state.docinfo
1740 | _ ->
1741 dolog "unknown cmd `%S'" cmds
1744 let idle () =
1745 if state.deadline == nan then state.deadline <- now ();
1746 let rec loop delay =
1747 let timeout =
1748 if delay > 0.0
1749 then max 0.0 (state.deadline -. now ())
1750 else 0.0
1752 let r, _, _ = Unix.select [state.csock; state.errfd] [] [] timeout in
1753 begin match r with
1754 | [] ->
1755 begin match state.autoscroll with
1756 | Some step when step != 0 ->
1757 let y = state.y + step in
1758 let y =
1759 if y < 0
1760 then state.maxy
1761 else if y >= state.maxy then 0 else y
1763 gotoy y;
1764 if state.mode = View
1765 then state.text <- "";
1766 state.deadline <- state.deadline +. 0.005;
1768 | _ ->
1769 state.deadline <- state.deadline +. delay;
1770 end;
1772 | l ->
1773 let rec checkfds c = function
1774 | [] -> c
1775 | fd :: rest when fd = state.csock ->
1776 let cmd = readcmd state.csock in
1777 act cmd;
1778 checkfds true rest
1779 | fd :: rest when fd = state.errfd ->
1780 let s = String.create 80 in
1781 let n = Unix.read fd s 0 80 in
1782 if conf.redirectstderr
1783 then (
1784 Buffer.add_substring state.errmsgs s 0 n;
1785 state.newerrmsgs <- true;
1786 Glut.postRedisplay ();
1788 else (
1789 prerr_string (String.sub s 0 n);
1790 flush stderr;
1792 checkfds c rest
1794 | _ ->
1795 failwith "me? fail english? that's unpossible!"
1797 if checkfds false l
1798 then loop 0.0
1799 end;
1800 in loop 0.007
1803 let onhist cb =
1804 let rc = cb.rc in
1805 let action = function
1806 | HCprev -> cbget cb ~-1
1807 | HCnext -> cbget cb 1
1808 | HCfirst -> cbget cb ~-(cb.rc)
1809 | HClast -> cbget cb (cb.len - 1 - cb.rc)
1810 and cancel () = cb.rc <- rc
1811 in (action, cancel)
1814 let search pattern forward =
1815 if String.length pattern > 0
1816 then
1817 let pn, py =
1818 match state.layout with
1819 | [] -> 0, 0
1820 | l :: _ ->
1821 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
1823 let cmd =
1824 let b = makecmd "search"
1825 [`b conf.icase; `i pn; `i py; `i (if forward then 1 else 0)]
1827 Buffer.add_char b ',';
1828 Buffer.add_string b pattern;
1829 Buffer.add_char b '\000';
1830 Buffer.contents b;
1832 writecmd state.csock cmd;
1835 let intentry text key =
1836 let c = Char.unsafe_chr key in
1837 match c with
1838 | '0' .. '9' ->
1839 let text = addchar text c in
1840 TEcont text
1842 | _ ->
1843 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
1844 TEcont text
1847 let textentry text key =
1848 let c = Char.unsafe_chr key in
1849 match c with
1850 | _ when key >= 32 && key < 127 ->
1851 let text = addchar text c in
1852 TEcont text
1854 | _ ->
1855 dolog "unhandled key %d char `%c'" key (Char.unsafe_chr key);
1856 TEcont text
1859 let reqlayout angle proportional =
1860 match state.throttle with
1861 | None ->
1862 if state.invalidated = 0 then state.anchor <- getanchor ();
1863 conf.angle <- angle mod 360;
1864 conf.proportional <- proportional;
1865 invalidate ();
1866 wcmd "reqlayout" [`i conf.angle; `b proportional];
1867 | _ -> ()
1870 let settrim trimmargins trimfuzz =
1871 if state.invalidated = 0 then state.anchor <- getanchor ();
1872 conf.trimmargins <- trimmargins;
1873 conf.trimfuzz <- trimfuzz;
1874 let x0, y0, x1, y1 = trimfuzz in
1875 invalidate ();
1876 wcmd "settrim" [
1877 `b conf.trimmargins;
1878 `i x0;
1879 `i y0;
1880 `i x1;
1881 `i y1;
1883 Hashtbl.iter (fun _ opaque ->
1884 wcmd "freepage" [`s opaque];
1885 ) state.pagemap;
1886 Hashtbl.clear state.pagemap;
1889 let setzoom zoom =
1890 match state.throttle with
1891 | None ->
1892 let zoom = max 0.01 zoom in
1893 if zoom <> conf.zoom
1894 then (
1895 state.prevzoom <- conf.zoom;
1896 let relx =
1897 if zoom <= 1.0
1898 then (state.x <- 0; 0.0)
1899 else float state.x /. float state.w
1901 conf.zoom <- zoom;
1902 reshape conf.winw conf.winh;
1903 if zoom > 1.0
1904 then (
1905 let x = relx *. float state.w in
1906 state.x <- truncate x;
1908 state.text <- Printf.sprintf "zoom is now %-5.1f" (zoom *. 100.0);
1911 | Some (layout, y, started) ->
1912 let time =
1913 match conf.maxwait with
1914 | None -> 0.0
1915 | Some t -> t
1917 let dt = now () -. started in
1918 if dt > time
1919 then (
1920 state.y <- y;
1921 load layout;
1925 let enterbirdseye () =
1926 let zoom = float conf.thumbw /. float conf.winw in
1927 let birdseyepageno =
1928 let cy = conf.winh / 2 in
1929 let fold = function
1930 | [] -> 0
1931 | l :: rest ->
1932 let rec fold best = function
1933 | [] -> best.pageno
1934 | l :: rest ->
1935 let d = cy - (l.pagedispy + l.pagevh/2)
1936 and dbest = cy - (best.pagedispy + best.pagevh/2) in
1937 if abs d < abs dbest
1938 then fold l rest
1939 else best.pageno
1940 in fold l rest
1942 fold state.layout
1944 state.mode <- Birdseye (
1945 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
1947 conf.zoom <- zoom;
1948 conf.presentation <- false;
1949 conf.interpagespace <- 10;
1950 conf.hlinks <- false;
1951 state.x <- 0;
1952 state.mstate <- Mnone;
1953 conf.maxwait <- None;
1954 Glut.setCursor Glut.CURSOR_INHERIT;
1955 if conf.verbose
1956 then
1957 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
1958 (100.0*.zoom)
1959 else
1960 state.text <- ""
1962 reshape conf.winw conf.winh;
1965 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
1966 state.mode <- View;
1967 conf.zoom <- c.zoom;
1968 conf.presentation <- c.presentation;
1969 conf.interpagespace <- c.interpagespace;
1970 conf.maxwait <- c.maxwait;
1971 conf.hlinks <- c.hlinks;
1972 state.x <- leftx;
1973 if conf.verbose
1974 then
1975 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
1976 (100.0*.conf.zoom)
1978 reshape conf.winw conf.winh;
1979 state.anchor <- if goback then anchor else (pageno, 0.0);
1982 let togglebirdseye () =
1983 match state.mode with
1984 | Birdseye vals -> leavebirdseye vals true
1985 | View -> enterbirdseye ()
1986 | _ -> ()
1989 let upbirdseye (conf, leftx, pageno, hooverpageno, anchor) =
1990 let pageno = max 0 (pageno - 1) in
1991 let rec loop = function
1992 | [] -> gotopage1 pageno 0
1993 | l :: _ when l.pageno = pageno ->
1994 if l.pagedispy >= 0 && l.pagey = 0
1995 then G.postRedisplay "upbirdseye"
1996 else gotopage1 pageno 0
1997 | _ :: rest -> loop rest
1999 loop state.layout;
2000 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2003 let downbirdseye (conf, leftx, pageno, hooverpageno, anchor) =
2004 let pageno = min (state.pagecount - 1) (pageno + 1) in
2005 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2006 let rec loop = function
2007 | [] ->
2008 let y, h = getpageyh pageno in
2009 let dy = (y - state.y) - (conf.winh - h - conf.interpagespace) in
2010 gotoy (clamp dy)
2011 | l :: _ when l.pageno = pageno ->
2012 if l.pagevh != l.pageh
2013 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
2014 else G.postRedisplay "downbirdseye"
2015 | _ :: rest -> loop rest
2017 loop state.layout
2020 let optentry mode _ key =
2021 let btos b = if b then "on" else "off" in
2022 let c = Char.unsafe_chr key in
2023 match c with
2024 | 's' ->
2025 let ondone s =
2026 try conf.scrollstep <- int_of_string s with exc ->
2027 state.text <- Printf.sprintf "bad integer `%s': %s"
2028 s (Printexc.to_string exc)
2030 TEswitch ("scroll step: ", "", None, intentry, ondone)
2032 | 'A' ->
2033 let ondone s =
2035 conf.autoscrollstep <- int_of_string s;
2036 if state.autoscroll <> None
2037 then state.autoscroll <- Some conf.autoscrollstep
2038 with exc ->
2039 state.text <- Printf.sprintf "bad integer `%s': %s"
2040 s (Printexc.to_string exc)
2042 TEswitch ("auto scroll step: ", "", None, intentry, ondone)
2044 | 'Z' ->
2045 let ondone s =
2047 let zoom = float (int_of_string s) /. 100.0 in
2048 setzoom zoom
2049 with exc ->
2050 state.text <- Printf.sprintf "bad integer `%s': %s"
2051 s (Printexc.to_string exc)
2053 TEswitch ("zoom: ", "", None, intentry, ondone)
2055 | 't' ->
2056 let ondone s =
2058 conf.thumbw <- bound (int_of_string s) 2 4096;
2059 state.text <-
2060 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
2061 begin match mode with
2062 | Birdseye beye ->
2063 leavebirdseye beye false;
2064 enterbirdseye ();
2065 | _ -> ();
2067 with exc ->
2068 state.text <- Printf.sprintf "bad integer `%s': %s"
2069 s (Printexc.to_string exc)
2071 TEswitch ("thumbnail width: ", "", None, intentry, ondone)
2073 | 'R' ->
2074 let ondone s =
2075 match try
2076 Some (int_of_string s)
2077 with exc ->
2078 state.text <- Printf.sprintf "bad integer `%s': %s"
2079 s (Printexc.to_string exc);
2080 None
2081 with
2082 | Some angle -> reqlayout angle conf.proportional
2083 | None -> ()
2085 TEswitch ("rotation: ", "", None, intentry, ondone)
2087 | 'i' ->
2088 conf.icase <- not conf.icase;
2089 TEdone ("case insensitive search " ^ (btos conf.icase))
2091 | 'p' ->
2092 conf.preload <- not conf.preload;
2093 gotoy state.y;
2094 TEdone ("preload " ^ (btos conf.preload))
2096 | 'v' ->
2097 conf.verbose <- not conf.verbose;
2098 TEdone ("verbose " ^ (btos conf.verbose))
2100 | 'd' ->
2101 conf.debug <- not conf.debug;
2102 TEdone ("debug " ^ (btos conf.debug))
2104 | 'h' ->
2105 conf.maxhfit <- not conf.maxhfit;
2106 state.maxy <-
2107 state.maxy + (if conf.maxhfit then -conf.winh else conf.winh);
2108 TEdone ("maxhfit " ^ (btos conf.maxhfit))
2110 | 'c' ->
2111 conf.crophack <- not conf.crophack;
2112 TEdone ("crophack " ^ btos conf.crophack)
2114 | 'a' ->
2115 let s =
2116 match conf.maxwait with
2117 | None ->
2118 conf.maxwait <- Some infinity;
2119 "always wait for page to complete"
2120 | Some _ ->
2121 conf.maxwait <- None;
2122 "show placeholder if page is not ready"
2124 TEdone s
2126 | 'f' ->
2127 conf.underinfo <- not conf.underinfo;
2128 TEdone ("underinfo " ^ btos conf.underinfo)
2130 | 'P' ->
2131 conf.savebmarks <- not conf.savebmarks;
2132 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
2134 | 'S' ->
2135 let ondone s =
2137 let pageno, py =
2138 match state.layout with
2139 | [] -> 0, 0
2140 | l :: _ ->
2141 l.pageno, l.pagey
2143 conf.interpagespace <- int_of_string s;
2144 state.maxy <- calcheight ();
2145 let y = getpagey pageno in
2146 gotoy (y + py)
2147 with exc ->
2148 state.text <- Printf.sprintf "bad integer `%s': %s"
2149 s (Printexc.to_string exc)
2151 TEswitch ("vertical margin: ", "", None, intentry, ondone)
2153 | 'l' ->
2154 reqlayout conf.angle (not conf.proportional);
2155 TEdone ("proportional display " ^ btos conf.proportional)
2157 | 'T' ->
2158 settrim (not conf.trimmargins) conf.trimfuzz;
2159 TEdone ("trim margins " ^ btos conf.trimmargins)
2161 | 'I' ->
2162 conf.invert <- not conf.invert;
2163 TEdone ("invert colors " ^ btos conf.invert)
2165 | _ ->
2166 state.text <- Printf.sprintf "bad option %d `%c'" key c;
2167 TEstop
2170 class type lvsource = object
2171 method getitemcount : int
2172 method getitem : int -> (string * int)
2173 method hasaction : int -> bool
2174 method exit :
2175 uioh:uioh ->
2176 cancel:bool ->
2177 active:int ->
2178 first:int ->
2179 pan:int ->
2180 qsearch:string ->
2181 uioh option
2182 method getactive : int
2183 method getfirst : int
2184 method getqsearch : string
2185 method setqsearch : string -> unit
2186 method getpan : int
2187 end;;
2189 class virtual lvsourcebase = object
2190 val mutable m_active = 0
2191 val mutable m_first = 0
2192 val mutable m_qsearch = ""
2193 val mutable m_pan = 0
2194 method getactive = m_active
2195 method getfirst = m_first
2196 method getqsearch = m_qsearch
2197 method getpan = m_pan
2198 method setqsearch s = m_qsearch <- s
2199 end;;
2201 let textentryspecial key = function
2202 | ((c, _, (Some (action, _) as onhist), onkey, ondone), mode) ->
2203 let s =
2204 match key with
2205 | Glut.KEY_UP -> action HCprev
2206 | Glut.KEY_DOWN -> action HCnext
2207 | Glut.KEY_HOME -> action HCfirst
2208 | Glut.KEY_END -> action HClast
2209 | _ -> state.text
2211 state.mode <- Textentry ((c, s, onhist, onkey, ondone), mode);
2212 G.postRedisplay "special textentry";
2213 | _ -> ()
2216 let textentrykeyboard key ((c, text, opthist, onkey, ondone), onleave) =
2217 let enttext te =
2218 state.mode <- Textentry (te, onleave);
2219 state.text <- "";
2220 enttext ();
2221 G.postRedisplay "textentrykeyboard enttext";
2223 match Char.unsafe_chr key with
2224 | '\008' -> (* backspace *)
2225 let len = String.length text in
2226 if len = 0
2227 then (
2228 onleave Cancel;
2229 G.postRedisplay "textentrykeyboard after cancel";
2231 else (
2232 let s = String.sub text 0 (len - 1) in
2233 enttext (c, s, opthist, onkey, ondone)
2236 | '\r' | '\n' ->
2237 ondone text;
2238 onleave Confirm;
2239 G.postRedisplay "textentrykeyboard after confirm"
2241 | '\007' (* ctrl-g *)
2242 | '\027' -> (* escape *)
2243 if String.length text = 0
2244 then (
2245 begin match opthist with
2246 | None -> ()
2247 | Some (_, onhistcancel) -> onhistcancel ()
2248 end;
2249 onleave Cancel;
2250 state.text <- "";
2251 G.postRedisplay "textentrykeyboard after cancel2"
2253 else (
2254 enttext (c, "", opthist, onkey, ondone)
2257 | '\127' -> () (* delete *)
2259 | _ ->
2260 begin match onkey text key with
2261 | TEdone text ->
2262 ondone text;
2263 onleave Confirm;
2264 G.postRedisplay "textentrykeyboard after confirm2";
2266 | TEcont text ->
2267 enttext (c, text, opthist, onkey, ondone);
2269 | TEstop ->
2270 onleave Cancel;
2271 state.text <- "";
2272 G.postRedisplay "textentrykeyboard after cancel3"
2274 | TEswitch te ->
2275 state.mode <- Textentry (te, onleave);
2276 G.postRedisplay "textentrykeyboard switch";
2277 end;
2280 let firstof first active =
2281 if first > active || abs (first - active) > fstate.maxrows - 1
2282 then max 0 (active - (fstate.maxrows/2))
2283 else first
2286 let calcfirst first active =
2287 if active > first
2288 then
2289 let rows = active - first in
2290 if rows > fstate.maxrows then active - fstate.maxrows else first
2291 else active
2294 let coe s = (s :> uioh);;
2296 class listview ~(source:lvsource) ~trusted =
2297 object (self)
2298 val m_pan = source#getpan
2299 val m_first = source#getfirst
2300 val m_active = source#getactive
2301 val m_qsearch = source#getqsearch
2302 val m_prev_uioh = state.uioh
2304 method private elemunder y =
2305 let n = y / (fstate.fontsize+1) in
2306 if m_first + n < source#getitemcount
2307 then (
2308 if source#hasaction (m_first + n)
2309 then Some (m_first + n)
2310 else None
2312 else None
2314 method display =
2315 Gl.enable `blend;
2316 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2317 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2318 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
2319 GlDraw.color (1., 1., 1.);
2320 Gl.enable `texture_2d;
2321 let fs = fstate.fontsize in
2322 let nfs = fs + 1 in
2323 let ww = fstate.wwidth in
2324 let tabw = 30.0*.ww in
2325 let rec loop row =
2326 if (row - m_first) * nfs > conf.winh
2327 then ()
2328 else (
2329 if row >= 0 && row < source#getitemcount
2330 then (
2331 let (s, level) = source#getitem row in
2332 let y = (row - m_first) * nfs in
2333 let x = 5.0 +. float (level + m_pan) *. ww in
2334 if row = m_active
2335 then (
2336 Gl.disable `texture_2d;
2337 GlDraw.polygon_mode `both `line;
2338 GlDraw.color (1., 1., 1.) ~alpha:0.9;
2339 GlDraw.rect (1., float (y + 1))
2340 (float (conf.winw - 1), float (y + fs + 3));
2341 GlDraw.polygon_mode `both `fill;
2342 GlDraw.color (1., 1., 1.);
2343 Gl.enable `texture_2d;
2346 let drawtabularstring s =
2347 let drawstr x s = drawstring1 fs (truncate x) (y+nfs) s in
2348 if trusted
2349 then
2350 let tabpos = try String.index s '\t' with Not_found -> -1 in
2351 if tabpos > 0
2352 then
2353 let len = String.length s - tabpos - 1 in
2354 let s1 = String.sub s 0 tabpos
2355 and s2 = String.sub s (tabpos + 1) len in
2356 let nx = drawstr x s1 in
2357 let sw = nx -. x in
2358 let x = x +. (max tabw sw) in
2359 drawstr x s2
2360 else
2361 drawstr x s
2362 else
2363 drawstr x s
2365 let _ = drawtabularstring s in
2366 loop (row+1)
2370 loop 0;
2371 Gl.disable `blend;
2372 Gl.disable `texture_2d;
2374 method updownlevel incr =
2375 let len = source#getitemcount in
2376 let _, curlevel = source#getitem m_active in
2377 let rec flow i =
2378 if i = len then i-1 else if i = -1 then 0 else
2379 let _, l = source#getitem i in
2380 if l != curlevel then i else flow (i+incr)
2382 let active = flow m_active in
2383 let first = calcfirst m_first active in
2384 G.postRedisplay "special outline updownlevel";
2385 {< m_active = active; m_first = first >}
2387 method private key1 key =
2388 let set active first qsearch =
2389 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
2391 let search active pattern incr =
2392 let dosearch re =
2393 let rec loop n =
2394 if n >= 0 && n < source#getitemcount
2395 then (
2396 let s, _ = source#getitem n in
2398 (try ignore (Str.search_forward re s 0); true
2399 with Not_found -> false)
2400 then Some n
2401 else loop (n + incr)
2403 else None
2405 loop active
2408 let re = Str.regexp_case_fold pattern in
2409 dosearch re
2410 with Failure s ->
2411 state.text <- s;
2412 None
2414 match key with
2415 | 18 | 19 -> (* ctrl-r/ctlr-s *)
2416 let incr = if key = 18 then -1 else 1 in
2417 let active, first =
2418 match search (m_active + incr) m_qsearch incr with
2419 | None ->
2420 state.text <- m_qsearch ^ " [not found]";
2421 m_active, m_first
2422 | Some active ->
2423 state.text <- m_qsearch;
2424 active, firstof m_first active
2426 G.postRedisplay "listview ctrl-r/s";
2427 set active first m_qsearch;
2429 | 8 -> (* backspace *)
2430 let len = String.length m_qsearch in
2431 if len = 0
2432 then coe self
2433 else (
2434 if len = 1
2435 then (
2436 state.text <- "";
2437 G.postRedisplay "listview empty qsearch";
2438 set m_active m_first "";
2440 else
2441 let qsearch = String.sub m_qsearch 0 (len - 1) in
2442 let active, first =
2443 match search m_active qsearch ~-1 with
2444 | None ->
2445 state.text <- qsearch ^ " [not found]";
2446 m_active, m_first
2447 | Some active ->
2448 state.text <- qsearch;
2449 active, firstof m_first active
2451 G.postRedisplay "listview backspace qsearch";
2452 set active first qsearch
2455 | _ when key >= 32 && key < 127 ->
2456 let pattern = addchar m_qsearch (Char.chr key) in
2457 let active, first =
2458 match search m_active pattern 1 with
2459 | None ->
2460 state.text <- pattern ^ " [not found]";
2461 m_active, m_first
2462 | Some active ->
2463 state.text <- pattern;
2464 active, firstof m_first active
2466 G.postRedisplay "listview qsearch add";
2467 set active first pattern;
2469 | 27 -> (* escape *)
2470 state.text <- "";
2471 if String.length m_qsearch = 0
2472 then (
2473 G.postRedisplay "list view escape";
2474 begin
2475 match
2476 source#exit (coe self) true m_active m_first m_pan m_qsearch
2477 with
2478 | None -> m_prev_uioh
2479 | Some uioh -> uioh
2482 else (
2483 G.postRedisplay "list view kill qsearch";
2484 source#setqsearch "";
2485 coe {< m_qsearch = "" >}
2488 | 13 -> (* enter *)
2489 state.text <- "";
2490 let self = {< m_qsearch = "" >} in
2491 source#setqsearch "";
2492 let opt =
2493 G.postRedisplay "listview enter";
2494 if m_active >= 0 && m_active < source#getitemcount
2495 then (
2496 source#exit (coe self) false m_active m_first m_pan "";
2498 else (
2499 source#exit (coe self) true m_active m_first m_pan "";
2502 begin match opt with
2503 | None -> m_prev_uioh
2504 | Some uioh -> uioh
2507 | 127 -> (* delete *)
2508 coe self
2510 | _ -> dolog "unknown key %d" key; coe self
2512 method private special1 key =
2513 let itemcount = source#getitemcount in
2514 let find start incr =
2515 let rec find i =
2516 if i = -1 || i = itemcount
2517 then -1
2518 else (
2519 if source#hasaction i
2520 then i
2521 else find (i + incr)
2524 find start
2526 let set active first =
2527 let first = bound first 0 (itemcount - fstate.maxrows) in
2528 state.text <- "";
2529 coe {< m_active = active; m_first = first >}
2531 let navigate incr =
2532 let isvisible first n = n >= first && n - first <= fstate.maxrows in
2533 let active, first =
2534 let incr1 = if incr > 0 then 1 else -1 in
2535 if isvisible m_first m_active
2536 then
2537 let next =
2538 let next = m_active + incr in
2539 let next =
2540 if next < 0 || next >= itemcount
2541 then -1
2542 else find next incr1
2544 if next = -1 || abs (m_active - next) > fstate.maxrows
2545 then -1
2546 else next
2548 if next = -1
2549 then
2550 let first = m_first + incr in
2551 let first = bound first 0 (itemcount - 1) in
2552 let next =
2553 let next = m_active + incr in
2554 let next = bound next 0 (itemcount - 1) in
2555 find next ~-incr1
2557 let active = if next = -1 then m_active else next in
2558 active, first
2559 else
2560 let first = min next m_first in
2561 next, first
2562 else
2563 let first = m_first + incr in
2564 let first = bound first 0 (itemcount - 1) in
2565 let active =
2566 let next = m_active + incr in
2567 let next = bound next 0 (itemcount - 1) in
2568 let next = find next incr1 in
2569 if next = -1 || abs (m_active - first) > fstate.maxrows
2570 then m_active
2571 else next
2573 active, first
2575 G.postRedisplay "listview navigate";
2576 set active first;
2578 begin match key with
2579 | Glut.KEY_UP -> navigate ~-1
2580 | Glut.KEY_DOWN -> navigate 1
2581 | Glut.KEY_PAGE_UP -> navigate ~-(fstate.maxrows)
2582 | Glut.KEY_PAGE_DOWN -> navigate fstate.maxrows
2584 | Glut.KEY_RIGHT ->
2585 state.text <- "";
2586 G.postRedisplay "listview right";
2587 coe {< m_pan = m_pan - 1 >}
2589 | Glut.KEY_LEFT ->
2590 state.text <- "";
2591 G.postRedisplay "listview left";
2592 coe {< m_pan = m_pan + 1 >}
2594 | Glut.KEY_HOME ->
2595 let active = find 0 1 in
2596 G.postRedisplay "listview home";
2597 set active 0;
2599 | Glut.KEY_END ->
2600 let first = max 0 (itemcount - fstate.maxrows) in
2601 let active = find (itemcount - 1) ~-1 in
2602 G.postRedisplay "listview end";
2603 set active first;
2605 | _ -> coe self
2606 end;
2608 method key key =
2609 match state.mode with
2610 | Textentry te -> textentrykeyboard key te; coe self
2611 | _ -> self#key1 key
2613 method special key =
2614 match state.mode with
2615 | Textentry te -> textentryspecial key te; coe self
2616 | _ -> self#special1 key
2618 method button button bstate _ y =
2619 let opt =
2620 match button with
2621 | Glut.LEFT_BUTTON when bstate = Glut.UP ->
2622 begin match self#elemunder y with
2623 | Some n ->
2624 G.postRedisplay "listview click";
2625 source#exit
2626 (coe {< m_active = n >}) false n m_first m_pan m_qsearch
2627 | _ ->
2628 Some (coe self)
2630 | Glut.OTHER_BUTTON n when (n == 3 || n == 4) && bstate = Glut.UP ->
2631 let len = source#getitemcount in
2632 let first =
2633 if m_first + fstate.maxrows >= len
2634 then
2635 m_first
2636 else
2637 let first = m_first + (if n == 3 then -1 else 1) in
2638 bound first 0 (len - 1)
2640 G.postRedisplay "listview wheel";
2641 Some (coe {< m_first = first >})
2642 | _ ->
2643 Some (coe self)
2645 match opt with
2646 | None -> m_prev_uioh
2647 | Some uioh -> uioh
2649 method motion _ _ = coe self
2651 method pmotion _ y =
2652 let n =
2653 match self#elemunder y with
2654 | None -> Glut.setCursor Glut.CURSOR_INHERIT; m_active
2655 | Some n -> Glut.setCursor Glut.CURSOR_INFO; n
2657 let o =
2658 if n != m_active
2659 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
2660 else self
2662 coe o
2664 method infochanged _ = ()
2665 end;;
2667 class outlinelistview ~source =
2668 object (self)
2669 inherit listview ~source:(source :> lvsource) ~trusted:false as super
2671 method key key =
2672 match key with
2673 | 14 -> (* ctrl-n *)
2674 source#narrow m_qsearch;
2675 G.postRedisplay "outline ctrl-n";
2676 coe {< m_first = 0; m_active = 0 >}
2678 | 21 -> (* ctrl-u *)
2679 source#denarrow;
2680 G.postRedisplay "outline ctrl-u";
2681 coe {< m_first = 0; m_active = 0 >}
2683 | 12 -> (* ctrl-l *)
2684 let first = m_active - (fstate.maxrows / 2) in
2685 G.postRedisplay "outline ctrl-l";
2686 coe {< m_first = first >}
2688 | 127 -> (* delete *)
2689 source#remove m_active;
2690 G.postRedisplay "outline delete";
2691 let active = max 0 (m_active-1) in
2692 coe {< m_first = firstof m_first active;
2693 m_active = active >}
2695 | key -> super#key key
2697 method special key =
2698 let calcfirst first active =
2699 if active > first
2700 then
2701 let rows = active - first in
2702 if rows > fstate.maxrows then active - fstate.maxrows else first
2703 else active
2705 let navigate incr =
2706 let active = m_active + incr in
2707 let active = bound active 0 (source#getitemcount - 1) in
2708 let first = calcfirst m_first active in
2709 G.postRedisplay "special outline navigate";
2710 coe {< m_active = active; m_first = first >}
2712 match key with
2713 | Glut.KEY_UP -> navigate ~-1
2714 | Glut.KEY_DOWN -> navigate 1
2715 | Glut.KEY_PAGE_UP -> navigate ~-(fstate.maxrows)
2716 | Glut.KEY_PAGE_DOWN -> navigate fstate.maxrows
2718 | Glut.KEY_RIGHT ->
2719 let o =
2720 if Glut.getModifiers () land Glut.active_ctrl != 0
2721 then (
2722 G.postRedisplay "special outline right";
2723 {< m_pan = m_pan + 1 >}
2725 else self#updownlevel 1
2727 coe o
2729 | Glut.KEY_LEFT ->
2730 let o =
2731 if Glut.getModifiers () land Glut.active_ctrl != 0
2732 then (
2733 G.postRedisplay "special outline left";
2734 {< m_pan = m_pan - 1 >}
2736 else self#updownlevel ~-1
2738 coe o
2740 | Glut.KEY_HOME ->
2741 G.postRedisplay "special outline home";
2742 coe {< m_first = 0; m_active = 0 >}
2744 | Glut.KEY_END ->
2745 let active = source#getitemcount - 1 in
2746 let first = max 0 (active - fstate.maxrows) in
2747 G.postRedisplay "special outline end";
2748 coe {< m_active = active; m_first = first >}
2750 | _ -> super#special key
2753 let outlinesource usebookmarks =
2754 let empty = [||] in
2755 (object
2756 inherit lvsourcebase
2757 val mutable m_items = empty
2758 val mutable m_orig_items = empty
2759 val mutable m_prev_items = empty
2760 val mutable m_narrow_pattern = ""
2761 val mutable m_hadremovals = false
2763 method getitemcount =
2764 Array.length m_items + (if m_hadremovals then 1 else 0)
2766 method getitem n =
2767 if n == Array.length m_items && m_hadremovals
2768 then
2769 ("[Confirm removal]", 0)
2770 else
2771 let s, n, _ = m_items.(n) in
2772 (s, n)
2774 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
2775 ignore (uioh, first, pan, qsearch);
2776 let confrimremoval = m_hadremovals && active = Array.length m_items in
2777 let items =
2778 if String.length m_narrow_pattern = 0
2779 then m_orig_items
2780 else m_items
2782 if not cancel
2783 then (
2784 if not confrimremoval
2785 then(
2786 let _, _, anchor = m_items.(active) in
2787 gotoanchor anchor;
2788 m_items <- items;
2790 else (
2791 state.bookmarks <- Array.to_list m_items;
2792 m_orig_items <- m_items;
2795 else m_items <- items;
2796 None
2798 method hasaction _ = true
2800 method greetmsg =
2801 if Array.length m_items != Array.length m_orig_items
2802 then "Narrowed to " ^ m_narrow_pattern ^ " (ctrl-u to restore)"
2803 else ""
2805 method narrow pattern =
2806 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
2807 match reopt with
2808 | None -> ()
2809 | Some re ->
2810 let rec loop accu n =
2811 if n = -1
2812 then (
2813 m_narrow_pattern <- pattern;
2814 m_items <- Array.of_list accu
2816 else
2817 let (s, _, _) as o = m_items.(n) in
2818 let accu =
2819 if (try ignore (Str.search_forward re s 0); true
2820 with Not_found -> false)
2821 then o :: accu
2822 else accu
2824 loop accu (n-1)
2826 loop [] (Array.length m_items - 1)
2828 method denarrow =
2829 m_orig_items <- (
2830 if usebookmarks
2831 then Array.of_list state.bookmarks
2832 else state.outlines
2834 m_items <- m_orig_items
2836 method remove m =
2837 if usebookmarks
2838 then
2839 if m >= 0 && m < Array.length m_items
2840 then (
2841 m_hadremovals <- true;
2842 m_items <- Array.init (Array.length m_items - 1) (fun n ->
2843 let n = if n >= m then n+1 else n in
2844 m_items.(n)
2848 method reset pageno items =
2849 m_hadremovals <- false;
2850 if m_orig_items == empty || m_prev_items != items
2851 then (
2852 m_orig_items <- items;
2853 if String.length m_narrow_pattern = 0
2854 then m_items <- items;
2856 m_prev_items <- items;
2857 let active =
2858 let rec loop n best bestd =
2859 if n = Array.length m_items
2860 then best
2861 else
2862 let (_, _, (outlinepageno, _)) = m_items.(n) in
2863 let d = abs (outlinepageno - pageno) in
2864 if d < bestd
2865 then loop (n+1) n d
2866 else loop (n+1) best bestd
2868 loop 0 ~-1 max_int
2870 m_active <- active;
2871 m_first <- firstof m_first active
2872 end)
2875 let enterselector usebookmarks =
2876 let source = outlinesource usebookmarks in
2877 fun errmsg ->
2878 let outlines =
2879 if usebookmarks
2880 then Array.of_list state.bookmarks
2881 else state.outlines
2883 if Array.length outlines = 0
2884 then (
2885 showtext ' ' errmsg;
2887 else (
2888 state.text <- source#greetmsg;
2889 Glut.setCursor Glut.CURSOR_INHERIT;
2890 let pageno =
2891 match state.layout with
2892 | [] -> -1
2893 | {pageno=pageno} :: _ -> pageno
2895 source#reset pageno outlines;
2896 state.uioh <- coe (new outlinelistview ~source);
2897 G.postRedisplay "enter selector";
2901 let enteroutlinemode =
2902 let f = enterselector false in
2903 fun ()-> f "Document has no outline";
2906 let enterbookmarkmode =
2907 let f = enterselector true in
2908 fun () -> f "Document has no bookmarks (yet)";
2911 let color_of_string s =
2912 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
2913 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
2917 let color_to_string (r, g, b) =
2918 let r = truncate (r *. 256.0)
2919 and g = truncate (g *. 256.0)
2920 and b = truncate (b *. 256.0) in
2921 Printf.sprintf "%d/%d/%d" r g b
2924 let irect_of_string s =
2925 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
2928 let irect_to_string (x0,y0,x1,y1) =
2929 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
2932 let makecheckers () =
2933 (* Appropriated from lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
2934 following to say:
2935 converted by Issac Trotts. July 25, 2002 *)
2936 let image_height = 64
2937 and image_width = 64 in
2939 let make_image () =
2940 let image =
2941 GlPix.create `ubyte ~format:`rgb ~width:image_width ~height:image_height
2943 for i = 0 to image_width - 1 do
2944 for j = 0 to image_height - 1 do
2945 Raw.sets (GlPix.to_raw image) ~pos:(3*(i*image_height+j))
2946 (if (i land 8 ) lxor (j land 8) = 0
2947 then [|255;255;255|] else [|200;200;200|])
2948 done
2949 done;
2950 image
2952 let image = make_image () in
2953 let id = GlTex.gen_texture () in
2954 GlTex.bind_texture `texture_2d id;
2955 GlPix.store (`unpack_alignment 1);
2956 GlTex.image2d image;
2957 List.iter (GlTex.parameter ~target:`texture_2d)
2958 [ `wrap_s `repeat;
2959 `wrap_t `repeat;
2960 `mag_filter `nearest;
2961 `min_filter `nearest ];
2965 let setcheckers enabled =
2966 match state.texid with
2967 | None ->
2968 if enabled then state.texid <- Some (makecheckers ())
2970 | Some texid ->
2971 if not enabled
2972 then (
2973 GlTex.delete_texture texid;
2974 state.texid <- None;
2978 let int_of_string_with_suffix s =
2979 let l = String.length s in
2980 let s1, shift =
2981 if l > 1
2982 then
2983 let suffix = Char.lowercase s.[l-1] in
2984 match suffix with
2985 | 'k' -> String.sub s 0 (l-1), 10
2986 | 'm' -> String.sub s 0 (l-1), 20
2987 | 'g' -> String.sub s 0 (l-1), 30
2988 | _ -> s, 0
2989 else s, 0
2991 let n = int_of_string s1 in
2992 let m = n lsl shift in
2993 if m < 0 || m < n
2994 then raise (Failure "value too large")
2995 else m
2998 let string_with_suffix_of_int n =
2999 if n = 0
3000 then "0"
3001 else
3002 let n, s =
3003 if n = 0
3004 then 0, ""
3005 else (
3006 if n land ((1 lsl 20) - 1) = 0
3007 then n lsr 20, "M"
3008 else (
3009 if n land ((1 lsl 10) - 1) = 0
3010 then n lsr 10, "K"
3011 else n, ""
3015 let rec loop s n =
3016 let h = n mod 1000 in
3017 let n = n / 1000 in
3018 if n = 0
3019 then string_of_int h ^ s
3020 else (
3021 let s = Printf.sprintf "_%03d%s" h s in
3022 loop s n
3025 loop "" n ^ s;
3028 let describe_location () =
3029 let f (fn, _) l =
3030 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
3032 let fn, ln = List.fold_left f (-1, -1) state.layout in
3033 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
3034 let percent =
3035 if maxy <= 0
3036 then 100.
3037 else (100. *. (float state.y /. float maxy))
3039 if fn = ln
3040 then
3041 Printf.sprintf "page %d of %d [%.2f%%]"
3042 (fn+1) state.pagecount percent
3043 else
3044 Printf.sprintf
3045 "pages %d-%d of %d [%.2f%%]"
3046 (fn+1) (ln+1) state.pagecount percent
3049 let enterinfomode =
3050 let btos b = if b then "\xe2\x88\x9a" else "" in
3051 let showextended = ref false in
3052 let leave mode = function
3053 | Confirm -> state.mode <- mode
3054 | Cancel -> state.mode <- mode in
3055 let src =
3056 (object
3057 val mutable m_first_time = true
3058 val mutable m_l = []
3059 val mutable m_a = [||]
3060 val mutable m_prev_uioh = nouioh
3061 val mutable m_prev_mode = View
3063 inherit lvsourcebase
3065 method reset prev_mode prev_uioh =
3066 m_a <- Array.of_list (List.rev m_l);
3067 m_l <- [];
3068 m_prev_mode <- prev_mode;
3069 m_prev_uioh <- prev_uioh;
3070 if m_first_time
3071 then (
3072 let rec loop n =
3073 if n >= Array.length m_a
3074 then ()
3075 else
3076 match m_a.(n) with
3077 | _, _, _, Action _ -> m_active <- n
3078 | _ -> loop (n+1)
3080 loop 0;
3081 m_first_time <- false;
3084 method int name get set =
3085 m_l <-
3086 (name, `int get, 1, Action (
3087 fun u ->
3088 let ondone s =
3089 try set (int_of_string s)
3090 with exn ->
3091 state.text <- Printf.sprintf "bad integer `%s': %s"
3092 s (Printexc.to_string exn)
3094 state.text <- "";
3095 let te = name ^ ": ", "", None, intentry, ondone in
3096 state.mode <- Textentry (te, leave m_prev_mode);
3098 )) :: m_l
3100 method int_with_suffix name get set =
3101 m_l <-
3102 (name, `intws get, 1, Action (
3103 fun u ->
3104 let ondone s =
3105 try set (int_of_string_with_suffix s)
3106 with exn ->
3107 state.text <- Printf.sprintf "bad integer `%s': %s"
3108 s (Printexc.to_string exn)
3110 state.text <- "";
3111 let te =
3112 name ^ ": ", "", None, intentry_with_suffix, ondone
3114 state.mode <- Textentry (te, leave m_prev_mode);
3116 )) :: m_l
3118 method bool ?(offset=1) ?(btos=btos) name get set =
3119 m_l <-
3120 (name, `bool (btos, get), offset, Action (
3121 fun u ->
3122 let v = get () in
3123 set (not v);
3125 )) :: m_l
3127 method color name get set =
3128 m_l <-
3129 (name, `color get, 1, Action (
3130 fun u ->
3131 let invalid = (nan, nan, nan) in
3132 let ondone s =
3133 let c =
3134 try color_of_string s
3135 with exn ->
3136 state.text <- Printf.sprintf "bad color `%s': %s"
3137 s (Printexc.to_string exn);
3138 invalid
3140 if c <> invalid
3141 then set c;
3143 let te = name ^ ": ", "", None, textentry, ondone in
3144 state.text <- color_to_string (get ());
3145 state.mode <- Textentry (te, leave m_prev_mode);
3147 )) :: m_l
3149 method string name get set =
3150 m_l <-
3151 (name, `string get, 1, Action (
3152 fun u ->
3153 let ondone s = set s in
3154 let te = name ^ ": ", "", None, textentry, ondone in
3155 state.mode <- Textentry (te, leave m_prev_mode);
3157 )) :: m_l
3159 method colorspace name get set =
3160 m_l <-
3161 (name, `string get, 1, Action (
3162 fun _ ->
3163 let source =
3164 let vals = [| "rgb"; "bgr"; "gray" |] in
3165 (object
3166 inherit lvsourcebase
3168 initializer
3169 m_active <- int_of_colorspace conf.colorspace;
3170 m_first <- 0;
3172 method getitemcount = Array.length vals
3173 method getitem n = (vals.(n), 0)
3174 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3175 ignore (uioh, first, pan, qsearch);
3176 if not cancel then set active;
3177 None
3178 method hasaction _ = true
3179 end)
3181 state.text <- "";
3182 coe (new listview ~source ~trusted:true)
3183 )) :: m_l
3185 method caption s offset =
3186 m_l <- (s, `empty, offset, Noaction) :: m_l
3188 method caption2 s f offset =
3189 m_l <- (s, `string f, offset, Noaction) :: m_l
3191 method getitemcount = Array.length m_a
3193 method getitem n =
3194 let tostr = function
3195 | `int f -> string_of_int (f ())
3196 | `intws f -> string_with_suffix_of_int (f ())
3197 | `string f -> f ()
3198 | `color f -> color_to_string (f ())
3199 | `bool (btos, f) -> btos (f ())
3200 | `empty -> ""
3202 let name, t, offset, _ = m_a.(n) in
3203 ((let s = tostr t in
3204 if String.length s > 0
3205 then Printf.sprintf "%s\t%s" name s
3206 else name),
3207 offset)
3209 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3210 let uiohopt =
3211 if not cancel
3212 then (
3213 m_qsearch <- qsearch;
3214 let uioh =
3215 match m_a.(active) with
3216 | _, _, _, Action f -> f uioh
3217 | _ -> uioh
3219 Some uioh
3221 else None
3223 m_active <- active;
3224 m_first <- first;
3225 m_pan <- pan;
3226 uiohopt
3228 method hasaction n =
3229 match m_a.(n) with
3230 | _, _, _, Action _ -> true
3231 | _ -> false
3232 end)
3234 let rec fillsrc prevmode prevuioh =
3235 let sep () = src#caption "" 0 in
3236 let colorp name get set =
3237 src#string name
3238 (fun () -> color_to_string (get ()))
3239 (fun v ->
3241 let c = color_of_string v in
3242 set c
3243 with exn ->
3244 state.text <- Printf.sprintf "bad color `%s': %s"
3245 v (Printexc.to_string exn);
3248 let oldmode = state.mode in
3249 let birdseye = isbirdseye state.mode in
3251 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
3253 src#bool "presentation mode"
3254 (fun () -> conf.presentation)
3255 (fun v ->
3256 conf.presentation <- v;
3257 state.anchor <- getanchor ();
3258 represent ());
3260 src#bool "ignore case in searches"
3261 (fun () -> conf.icase)
3262 (fun v -> conf.icase <- v);
3264 src#bool "preload"
3265 (fun () -> conf.preload)
3266 (fun v -> conf.preload <- v);
3268 src#bool "highlight links"
3269 (fun () -> conf.hlinks)
3270 (fun v -> conf.hlinks <- v);
3272 src#bool "under info"
3273 (fun () -> conf.underinfo)
3274 (fun v -> conf.underinfo <- v);
3276 src#bool "persistent bookmarks"
3277 (fun () -> conf.savebmarks)
3278 (fun v -> conf.savebmarks <- v);
3280 src#bool "proportional display"
3281 (fun () -> conf.proportional)
3282 (fun v -> reqlayout conf.angle v);
3284 src#bool "trim margins"
3285 (fun () -> conf.trimmargins)
3286 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
3288 src#bool "persistent location"
3289 (fun () -> conf.jumpback)
3290 (fun v -> conf.jumpback <- v);
3292 sep ();
3293 src#int "vertical margin"
3294 (fun () -> conf.interpagespace)
3295 (fun n ->
3296 conf.interpagespace <- n;
3297 let pageno, py =
3298 match state.layout with
3299 | [] -> 0, 0
3300 | l :: _ ->
3301 l.pageno, l.pagey
3303 state.maxy <- calcheight ();
3304 let y = getpagey pageno in
3305 gotoy (y + py)
3308 src#int "page bias"
3309 (fun () -> conf.pagebias)
3310 (fun v -> conf.pagebias <- v);
3312 src#int "scroll step"
3313 (fun () -> conf.scrollstep)
3314 (fun n -> conf.scrollstep <- n);
3316 src#int "auto scroll step"
3317 (fun () ->
3318 match state.autoscroll with
3319 | Some step -> step
3320 | _ -> conf.autoscrollstep)
3321 (fun n ->
3322 if state.autoscroll <> None
3323 then state.autoscroll <- Some n;
3324 conf.autoscrollstep <- n);
3326 src#int "zoom"
3327 (fun () -> truncate (conf.zoom *. 100.))
3328 (fun v -> setzoom ((float v) /. 100.));
3330 src#int "rotation"
3331 (fun () -> conf.angle)
3332 (fun v -> reqlayout v conf.proportional);
3334 src#int "scroll bar width"
3335 (fun () -> state.scrollw)
3336 (fun v ->
3337 state.scrollw <- v;
3338 conf.scrollbw <- v;
3339 reshape conf.winw conf.winh;
3342 src#int "scroll handle height"
3343 (fun () -> conf.scrollh)
3344 (fun v -> conf.scrollh <- v;);
3346 src#int "thumbnail width"
3347 (fun () -> conf.thumbw)
3348 (fun v ->
3349 conf.thumbw <- min 4096 v;
3350 match oldmode with
3351 | Birdseye beye ->
3352 leavebirdseye beye false;
3353 enterbirdseye ()
3354 | _ -> ()
3357 sep ();
3358 src#caption "Presentation mode" 0;
3359 src#bool "scrollbar visible"
3360 (fun () -> conf.scrollbarinpm)
3361 (fun v ->
3362 if v != conf.scrollbarinpm
3363 then (
3364 conf.scrollbarinpm <- v;
3365 if conf.presentation
3366 then (
3367 state.scrollw <- if v then conf.scrollbw else 0;
3368 reshape conf.winw conf.winh;
3373 sep ();
3374 src#caption "Pixmap cache" 0;
3375 src#int_with_suffix "size (advisory)"
3376 (fun () -> conf.memlimit)
3377 (fun v -> conf.memlimit <- v);
3379 src#caption2 "used"
3380 (fun () -> Printf.sprintf "%s bytes, %d tiles"
3381 (string_with_suffix_of_int state.memused)
3382 (Hashtbl.length state.tilemap)) 1;
3384 sep ();
3385 src#caption "Layout" 0;
3386 src#caption2 "Dimension"
3387 (fun () ->
3388 Printf.sprintf "%dx%d (virtual %dx%d)"
3389 conf.winw conf.winh
3390 state.w state.maxy)
3392 if conf.debug
3393 then
3394 src#caption2 "Position" (fun () ->
3395 Printf.sprintf "%dx%d" state.x state.y
3397 else
3398 src#caption2 "Visible" (fun () -> describe_location ()) 1
3401 sep ();
3402 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
3403 "Save these parameters as global defaults at exit"
3404 (fun () -> conf.bedefault)
3405 (fun v -> conf.bedefault <- v)
3408 sep ();
3409 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
3410 src#bool ~offset:0 ~btos "Extended parameters"
3411 (fun () -> !showextended)
3412 (fun v -> showextended := v; fillsrc prevmode prevuioh);
3413 if !showextended
3414 then (
3415 src#bool "checkers"
3416 (fun () -> conf.checkers)
3417 (fun v -> conf.checkers <- v; setcheckers v);
3418 src#bool "verbose"
3419 (fun () -> conf.verbose)
3420 (fun v -> conf.verbose <- v);
3421 src#bool "invert colors"
3422 (fun () -> conf.invert)
3423 (fun v -> conf.invert <- v);
3424 src#bool "max fit"
3425 (fun () -> conf.maxhfit)
3426 (fun v -> conf.maxhfit <- v);
3427 src#bool "redirect stderr"
3428 (fun () -> conf.redirectstderr)
3429 (fun v -> conf.redirectstderr <- v; redirectstderr ());
3430 src#string "uri launcher"
3431 (fun () -> conf.urilauncher)
3432 (fun v -> conf.urilauncher <- v);
3433 src#string "tile size"
3434 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
3435 (fun v ->
3437 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
3438 conf.tileh <- max 64 w;
3439 conf.tilew <- max 64 h;
3440 flushtiles ();
3441 with exn ->
3442 state.text <- Printf.sprintf "bad tile size `%s': %s"
3443 v (Printexc.to_string exn));
3444 src#int "texture count"
3445 (fun () -> conf.texcount)
3446 (fun v ->
3447 conf.texcount <- v;
3448 wcmd "texcount" [`i conf.texcount];
3450 src#int "slice height"
3451 (fun () -> conf.sliceheight)
3452 (fun v ->
3453 conf.sliceheight <- v;
3454 wcmd "sliceh" [`i conf.sliceheight];
3456 src#int "anti-aliasing level"
3457 (fun () -> conf.aalevel)
3458 (fun v ->
3459 conf.aalevel <- bound v 0 8;
3460 state.anchor <- getanchor ();
3461 opendoc state.path state.password;
3463 src#int "ui font size"
3464 (fun () -> fstate.fontsize)
3465 (fun v -> setfontsize (bound v 5 100));
3466 colorp "background color"
3467 (fun () -> conf.bgcolor)
3468 (fun v -> conf.bgcolor <- v);
3469 src#bool "crop hack"
3470 (fun () -> conf.crophack)
3471 (fun v -> conf.crophack <- v);
3472 src#string "trim fuzz"
3473 (fun () -> irect_to_string conf.trimfuzz)
3474 (fun v ->
3476 conf.trimfuzz <- irect_of_string v;
3477 if conf.trimmargins
3478 then settrim true conf.trimfuzz;
3479 with exn ->
3480 state.text <- Printf.sprintf "bad irect `%s': %s"
3481 v (Printexc.to_string exn)
3483 src#string "throttle"
3484 (fun () ->
3485 match conf.maxwait with
3486 | None -> "show place holder if page is not ready"
3487 | Some time ->
3488 if time = infinity
3489 then "wait for page to fully render"
3490 else
3491 "wait " ^ string_of_float time
3492 ^ " seconds before showing placeholder"
3494 (fun v ->
3496 let f = float_of_string v in
3497 if f <= 0.0
3498 then conf.maxwait <- None
3499 else conf.maxwait <- Some f
3500 with exn ->
3501 state.text <- Printf.sprintf "bad time `%s': %s"
3502 v (Printexc.to_string exn)
3504 src#colorspace "color space"
3505 (fun () -> colorspace_to_string conf.colorspace)
3506 (fun v ->
3507 conf.colorspace <- colorspace_of_int v;
3508 wcmd "cs" [`i v];
3509 load state.layout;
3513 sep ();
3514 src#caption "Document" 0;
3515 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
3516 if conf.trimmargins
3517 then (
3518 sep ();
3519 src#caption "Trimmed margins" 0;
3520 src#caption2 "Dimensions"
3521 (fun () -> string_of_int (List.length state.pdims)) 1;
3524 src#reset prevmode prevuioh;
3526 fun () ->
3527 state.text <- "";
3528 let prevmode = state.mode
3529 and prevuioh = state.uioh in
3530 fillsrc prevmode prevuioh;
3531 let source = (src :> lvsource) in
3532 state.uioh <- coe (object (self)
3533 inherit listview ~source ~trusted:true as super
3534 val mutable m_prevmemused = 0
3535 method infochanged = function
3536 | Memused ->
3537 if m_prevmemused != state.memused
3538 then (
3539 m_prevmemused <- state.memused;
3540 G.postRedisplay "memusedchanged";
3542 | Pdim -> G.postRedisplay "pdimchanged"
3543 | Docinfo -> fillsrc prevmode prevuioh
3545 method special key =
3546 if Glut.getModifiers () land Glut.active_ctrl = 0
3547 then
3548 match key with
3549 | Glut.KEY_LEFT -> coe (self#updownlevel ~-1)
3550 | Glut.KEY_RIGHT -> coe (self#updownlevel 1)
3551 | _ -> super#special key
3552 else super#special key
3553 end);
3554 G.postRedisplay "info";
3557 let enterhelpmode =
3558 let source =
3559 (object
3560 inherit lvsourcebase
3561 method getitemcount = Array.length state.help
3562 method getitem n =
3563 let s, n, _ = state.help.(n) in
3564 (s, n)
3566 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3567 let optuioh =
3568 if not cancel
3569 then (
3570 m_qsearch <- qsearch;
3571 match state.help.(active) with
3572 | _, _, Action f -> Some (f uioh)
3573 | _ -> Some (uioh)
3575 else None
3577 m_active <- active;
3578 m_first <- first;
3579 m_pan <- pan;
3580 optuioh
3582 method hasaction n =
3583 match state.help.(n) with
3584 | _, _, Action _ -> true
3585 | _ -> false
3587 initializer
3588 m_active <- -1
3589 end)
3590 in fun () ->
3591 state.uioh <- coe (new listview ~source ~trusted:true);
3592 G.postRedisplay "help";
3595 let entermsgsmode =
3596 let msgsource =
3597 let re = Str.regexp "[\r\n]" in
3598 (object
3599 inherit lvsourcebase
3600 val mutable m_items = [||]
3602 method getitemcount = 1 + Array.length m_items
3604 method getitem n =
3605 if n = 0
3606 then "[Clear]", 0
3607 else m_items.(n-1), 0
3609 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3610 ignore uioh;
3611 if not cancel
3612 then (
3613 if active = 0
3614 then Buffer.clear state.errmsgs;
3615 m_qsearch <- qsearch;
3617 m_active <- active;
3618 m_first <- first;
3619 m_pan <- pan;
3620 None
3622 method hasaction n =
3623 n = 0
3625 method reset =
3626 state.newerrmsgs <- false;
3627 let l = Str.split re (Buffer.contents state.errmsgs) in
3628 m_items <- Array.of_list l
3630 initializer
3631 m_active <- 0
3632 end)
3633 in fun () ->
3634 state.text <- "";
3635 msgsource#reset;
3636 let source = (msgsource :> lvsource) in
3637 state.uioh <- coe (object
3638 inherit listview ~source ~trusted:false as super
3639 method display =
3640 if state.newerrmsgs
3641 then msgsource#reset;
3642 super#display
3643 end);
3644 G.postRedisplay "msgs";
3647 let quickbookmark ?title () =
3648 match state.layout with
3649 | [] -> ()
3650 | l :: _ ->
3651 let title =
3652 match title with
3653 | None ->
3654 let sec = Unix.gettimeofday () in
3655 let tm = Unix.localtime sec in
3656 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
3657 (l.pageno+1)
3658 tm.Unix.tm_mday
3659 tm.Unix.tm_mon
3660 (tm.Unix.tm_year + 1900)
3661 tm.Unix.tm_hour
3662 tm.Unix.tm_min
3663 | Some title -> title
3665 state.bookmarks <-
3666 (title, 0, (l.pageno, float l.pagey /. float l.pageh))
3667 :: state.bookmarks
3670 let doreshape w h =
3671 state.fullscreen <- None;
3672 Glut.reshapeWindow w h;
3675 let viewkeyboard key =
3676 let enttext te =
3677 let mode = state.mode in
3678 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
3679 state.text <- "";
3680 enttext ();
3681 G.postRedisplay "view:enttext"
3683 let c = Char.chr key in
3684 match c with
3685 | '\027' | 'q' -> (* escape *)
3686 begin match state.mstate with
3687 | Mzoomrect _ ->
3688 state.mstate <- Mnone;
3689 Glut.setCursor Glut.CURSOR_INHERIT;
3690 G.postRedisplay "kill zoom rect";
3691 | _ ->
3692 raise Quit
3693 end;
3695 | '\008' -> (* backspace *)
3696 let y = getnav ~-1 in
3697 gotoy_and_clear_text y
3699 | 'o' ->
3700 enteroutlinemode ()
3702 | 'u' ->
3703 state.rects <- [];
3704 state.text <- "";
3705 G.postRedisplay "dehighlight";
3707 | '/' | '?' ->
3708 let ondone isforw s =
3709 cbput state.hists.pat s;
3710 state.searchpattern <- s;
3711 search s isforw
3713 let s = String.create 1 in
3714 s.[0] <- c;
3715 enttext (s, "", Some (onhist state.hists.pat),
3716 textentry, ondone (c ='/'))
3718 | '+' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
3719 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
3720 setzoom (conf.zoom +. incr)
3722 | '+' ->
3723 let ondone s =
3724 let n =
3725 try int_of_string s with exc ->
3726 state.text <- Printf.sprintf "bad integer `%s': %s"
3727 s (Printexc.to_string exc);
3728 max_int
3730 if n != max_int
3731 then (
3732 conf.pagebias <- n;
3733 state.text <- "page bias is now " ^ string_of_int n;
3736 enttext ("page bias: ", "", None, intentry, ondone)
3738 | '-' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
3739 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
3740 setzoom (max 0.01 (conf.zoom -. decr))
3742 | '-' ->
3743 let ondone msg = state.text <- msg in
3744 enttext (
3745 "option [acfhilpstvAPRSZTI]: ", "", None,
3746 optentry state.mode, ondone
3749 | '0' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
3750 setzoom 1.0
3752 | '1' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
3753 let zoom = zoomforh conf.winw conf.winh state.scrollw in
3754 if zoom < 1.0
3755 then setzoom zoom
3757 | '9' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
3758 togglebirdseye ()
3760 | '0' .. '9' ->
3761 let ondone s =
3762 let n =
3763 try int_of_string s with exc ->
3764 state.text <- Printf.sprintf "bad integer `%s': %s"
3765 s (Printexc.to_string exc);
3768 if n >= 0
3769 then (
3770 addnav ();
3771 cbput state.hists.pag (string_of_int n);
3772 gotoy_and_clear_text (getpagey (n + conf.pagebias - 1))
3775 let pageentry text key =
3776 match Char.unsafe_chr key with
3777 | 'g' -> TEdone text
3778 | _ -> intentry text key
3780 let text = "x" in text.[0] <- c;
3781 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone)
3783 | 'b' ->
3784 state.scrollw <- if state.scrollw > 0 then 0 else conf.scrollbw;
3785 reshape conf.winw conf.winh;
3787 | 'l' ->
3788 conf.hlinks <- not conf.hlinks;
3789 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
3790 G.postRedisplay "toggle highlightlinks";
3792 | 'a' ->
3793 begin match state.autoscroll with
3794 | Some step ->
3795 conf.autoscrollstep <- step;
3796 state.autoscroll <- None
3797 | None ->
3798 if conf.autoscrollstep = 0
3799 then state.autoscroll <- Some 1
3800 else state.autoscroll <- Some conf.autoscrollstep
3803 | 'P' ->
3804 conf.presentation <- not conf.presentation;
3805 if conf.presentation
3806 then (
3807 if not conf.scrollbarinpm
3808 then state.scrollw <- 0;
3810 else
3811 state.scrollw <- conf.scrollbw;
3813 showtext ' ' ("presentation mode " ^
3814 if conf.presentation then "on" else "off");
3815 state.anchor <- getanchor ();
3816 represent ()
3818 | 'f' ->
3819 begin match state.fullscreen with
3820 | None ->
3821 state.fullscreen <- Some (conf.winw, conf.winh);
3822 Glut.fullScreen ()
3823 | Some (w, h) ->
3824 state.fullscreen <- None;
3825 doreshape w h
3828 | 'g' ->
3829 gotoy_and_clear_text 0
3831 | 'G' ->
3832 gotopage1 (state.pagecount - 1) 0
3834 | 'n' ->
3835 search state.searchpattern true
3837 | 'p' | 'N' ->
3838 search state.searchpattern false
3840 | 't' ->
3841 begin match state.layout with
3842 | [] -> ()
3843 | l :: _ ->
3844 gotoy_and_clear_text (getpagey l.pageno)
3847 | ' ' ->
3848 begin match List.rev state.layout with
3849 | [] -> ()
3850 | l :: _ ->
3851 let pageno = min (l.pageno+1) (state.pagecount-1) in
3852 gotoy_and_clear_text (getpagey pageno)
3855 | '\127' -> (* del *)
3856 begin match state.layout with
3857 | [] -> ()
3858 | l :: _ ->
3859 let pageno = max 0 (l.pageno-1) in
3860 gotoy_and_clear_text (getpagey pageno)
3863 | '=' ->
3864 showtext ' ' (describe_location ());
3866 | 'w' ->
3867 begin match state.layout with
3868 | [] -> ()
3869 | l :: _ ->
3870 doreshape (l.pagew + state.scrollw) l.pageh;
3871 G.postRedisplay "w"
3874 | '\'' ->
3875 enterbookmarkmode ()
3877 | 'h' ->
3878 enterhelpmode ()
3880 | 'i' ->
3881 enterinfomode ()
3883 | 'e' when conf.redirectstderr ->
3884 entermsgsmode ()
3886 | 'm' ->
3887 let ondone s =
3888 match state.layout with
3889 | l :: _ ->
3890 state.bookmarks <-
3891 (s, 0, (l.pageno, float l.pagey /. float l.pageh))
3892 :: state.bookmarks
3893 | _ -> ()
3895 enttext ("bookmark: ", "", None, textentry, ondone)
3897 | '~' ->
3898 quickbookmark ();
3899 showtext ' ' "Quick bookmark added";
3901 | 'z' ->
3902 begin match state.layout with
3903 | l :: _ ->
3904 let rect = getpdimrect l.pagedimno in
3905 let w, h =
3906 if conf.crophack
3907 then
3908 (truncate (1.8 *. (rect.(1) -. rect.(0))),
3909 truncate (1.2 *. (rect.(3) -. rect.(0))))
3910 else
3911 (truncate (rect.(1) -. rect.(0)),
3912 truncate (rect.(3) -. rect.(0)))
3914 let w = truncate ((float w)*.conf.zoom)
3915 and h = truncate ((float h)*.conf.zoom) in
3916 if w != 0 && h != 0
3917 then (
3918 state.anchor <- getanchor ();
3919 doreshape (w + state.scrollw) (h + conf.interpagespace)
3921 G.postRedisplay "z";
3923 | [] -> ()
3926 | '\000' -> (* ctrl-2 *)
3927 let maxw = getmaxw () in
3928 if maxw > 0.0
3929 then setzoom (maxw /. float conf.winw)
3931 | '<' | '>' ->
3932 reqlayout (conf.angle + (if c = '>' then 30 else -30)) conf.proportional
3934 | '[' | ']' ->
3935 conf.colorscale <-
3936 bound (conf.colorscale +. (if c = ']' then 0.1 else -0.1)) 0.0 1.0
3938 G.postRedisplay "brightness";
3940 | 'k' ->
3941 begin match state.mode with
3942 | Birdseye beye -> upbirdseye beye
3943 | _ -> gotoy (clamp (-conf.scrollstep))
3946 | 'j' ->
3947 begin match state.mode with
3948 | Birdseye beye -> downbirdseye beye
3949 | _ -> gotoy (clamp conf.scrollstep)
3952 | 'r' ->
3953 state.anchor <- getanchor ();
3954 opendoc state.path state.password
3956 | 'v' when conf.debug ->
3957 state.rects <- [];
3958 List.iter (fun l ->
3959 match getopaque l.pageno with
3960 | None -> ()
3961 | Some opaque ->
3962 let x0, y0, x1, y1 = pagebbox opaque in
3963 let a,b = float x0, float y0 in
3964 let c,d = float x1, float y0 in
3965 let e,f = float x1, float y1 in
3966 let h,j = float x0, float y1 in
3967 let rect = (a,b,c,d,e,f,h,j) in
3968 debugrect rect;
3969 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
3970 ) state.layout;
3971 G.postRedisplay "v";
3973 | _ ->
3974 vlog "huh? %d %c" key (Char.chr key);
3977 let birdseyekeyboard key ((_, _, pageno, _, _) as beye) =
3978 match key with
3979 | 27 -> (* escape *)
3980 leavebirdseye beye true
3982 | 12 -> (* ctrl-l *)
3983 let y, h = getpageyh pageno in
3984 let top = (conf.winh - h) / 2 in
3985 gotoy (max 0 (y - top))
3987 | 13 -> (* enter *)
3988 leavebirdseye beye false
3990 | _ ->
3991 viewkeyboard key
3994 let keyboard ~key ~x ~y =
3995 ignore x;
3996 ignore y;
3997 if key = 7 && not (istextentry state.mode) (* ctrl-g *)
3998 then wcmd "interrupt" []
3999 else state.uioh <- state.uioh#key key
4002 let birdseyespecial key ((conf, leftx, _, hooverpageno, anchor) as beye) =
4003 match key with
4004 | Glut.KEY_UP -> upbirdseye beye
4005 | Glut.KEY_DOWN -> downbirdseye beye
4007 | Glut.KEY_PAGE_UP ->
4008 begin match state.layout with
4009 | l :: _ ->
4010 if l.pagey != 0
4011 then (
4012 state.mode <- Birdseye (
4013 conf, leftx, l.pageno, hooverpageno, anchor
4015 gotopage1 l.pageno 0;
4017 else (
4018 let layout = layout (state.y-conf.winh) conf.winh in
4019 match layout with
4020 | [] -> gotoy (clamp (-conf.winh))
4021 | l :: _ ->
4022 state.mode <- Birdseye (
4023 conf, leftx, l.pageno, hooverpageno, anchor
4025 gotopage1 l.pageno 0
4028 | [] -> gotoy (clamp (-conf.winh))
4029 end;
4031 | Glut.KEY_PAGE_DOWN ->
4032 begin match List.rev state.layout with
4033 | l :: _ ->
4034 let layout = layout (state.y + conf.winh) conf.winh in
4035 begin match layout with
4036 | [] ->
4037 let incr = l.pageh - l.pagevh in
4038 if incr = 0
4039 then (
4040 state.mode <-
4041 Birdseye (
4042 conf, leftx, state.pagecount - 1, hooverpageno, anchor
4044 G.postRedisplay "birdseye pagedown";
4046 else gotoy (clamp (incr + conf.interpagespace*2));
4048 | l :: _ ->
4049 state.mode <-
4050 Birdseye (conf, leftx, l.pageno, hooverpageno, anchor);
4051 gotopage1 l.pageno 0;
4054 | [] -> gotoy (clamp conf.winh)
4055 end;
4057 | Glut.KEY_HOME ->
4058 state.mode <- Birdseye (conf, leftx, 0, hooverpageno, anchor);
4059 gotopage1 0 0
4061 | Glut.KEY_END ->
4062 let pageno = state.pagecount - 1 in
4063 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
4064 if not (pagevisible state.layout pageno)
4065 then
4066 let h =
4067 match List.rev state.pdims with
4068 | [] -> conf.winh
4069 | (_, _, h, _) :: _ -> h
4071 gotoy (max 0 (getpagey pageno - (conf.winh - h - conf.interpagespace)))
4072 else G.postRedisplay "birdseye end";
4073 | _ -> ()
4076 let setautoscrollspeed step goingdown =
4077 let incr = max 1 ((abs step) / 2) in
4078 let incr = if goingdown then incr else -incr in
4079 let astep = step + incr in
4080 state.autoscroll <- Some astep;
4083 let special ~key ~x ~y =
4084 ignore x;
4085 ignore y;
4086 state.uioh <- state.uioh#special key
4089 let drawpage l =
4090 let color =
4091 match state.mode with
4092 | Textentry _ -> scalecolor 0.4
4093 | View -> scalecolor 1.0
4094 | Birdseye (_, _, pageno, hooverpageno, _) ->
4095 if l.pageno = hooverpageno
4096 then scalecolor 0.9
4097 else (
4098 if l.pageno = pageno
4099 then scalecolor 1.0
4100 else scalecolor 0.8
4103 drawtiles l color;
4104 begin match getopaque l.pageno with
4105 | Some opaque ->
4106 if tileready l l.pagex l.pagey
4107 then
4108 let x = l.pagedispx - l.pagex
4109 and y = l.pagedispy - l.pagey in
4110 postprocess opaque conf.hlinks x y;
4112 | _ -> ()
4113 end;
4116 let scrollph y =
4117 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
4118 let sh = (float (maxy + conf.winh) /. float conf.winh) in
4119 let sh = float conf.winh /. sh in
4120 let sh = max sh (float conf.scrollh) in
4122 let percent =
4123 if y = state.maxy
4124 then 1.0
4125 else float y /. float maxy
4127 let position = (float conf.winh -. sh) *. percent in
4129 let position =
4130 if position +. sh > float conf.winh
4131 then float conf.winh -. sh
4132 else position
4134 position, sh;
4137 let scrollpw x =
4138 let winw = conf.winw - state.scrollw - 1 in
4139 let fwinw = float winw in
4140 let sw =
4141 let sw = fwinw /. float state.w in
4142 let sw = fwinw *. sw in
4143 max sw (float conf.scrollh)
4145 let position, sw =
4146 let f = state.w+winw in
4147 let r = float (winw-x) /. float f in
4148 let p = fwinw *. r in
4149 p-.sw/.2., sw
4151 let sw =
4152 if position +. sw > fwinw
4153 then fwinw -. position
4154 else sw
4156 position, sw;
4159 let scrollindicator () =
4160 GlDraw.color (0.64 , 0.64, 0.64);
4161 GlDraw.rect
4162 (float (conf.winw - state.scrollw), 0.)
4163 (float conf.winw, float conf.winh)
4165 GlDraw.rect
4166 (0., float (conf.winh - state.hscrollh))
4167 (float (conf.winw - state.scrollw - 1), float conf.winh)
4169 GlDraw.color (0.0, 0.0, 0.0);
4171 let position, sh = scrollph state.y in
4172 GlDraw.rect
4173 (float (conf.winw - state.scrollw), position)
4174 (float conf.winw, position +. sh)
4176 let position, sw = scrollpw state.x in
4177 GlDraw.rect
4178 (position, float (conf.winh - state.hscrollh))
4179 (position +. sw, float conf.winh)
4183 let pagetranslatepoint l x y =
4184 let dy = y - l.pagedispy in
4185 let y = dy + l.pagey in
4186 let dx = x - l.pagedispx in
4187 let x = dx + l.pagex in
4188 (x, y);
4191 let showsel () =
4192 match state.mstate with
4193 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
4196 | Msel ((x0, y0), (x1, y1)) ->
4197 let rec loop = function
4198 | l :: ls ->
4199 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
4200 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
4201 then
4202 match getopaque l.pageno with
4203 | Some opaque ->
4204 let dx, dy = pagetranslatepoint l 0 0 in
4205 let x0 = x0 + dx
4206 and y0 = y0 + dy
4207 and x1 = x1 + dx
4208 and y1 = y1 + dy in
4209 GlMat.mode `modelview;
4210 GlMat.push ();
4211 GlMat.translate ~x:(float ~-dx) ~y:(float ~-dy) ();
4212 seltext opaque (x0, y0, x1, y1);
4213 GlMat.pop ();
4214 | _ -> ()
4215 else loop ls
4216 | [] -> ()
4218 loop state.layout
4221 let showrects () =
4222 Gl.enable `blend;
4223 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
4224 GlDraw.polygon_mode `both `fill;
4225 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
4226 List.iter
4227 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
4228 List.iter (fun l ->
4229 if l.pageno = pageno
4230 then (
4231 let dx = float (l.pagedispx - l.pagex) in
4232 let dy = float (l.pagedispy - l.pagey) in
4233 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
4234 GlDraw.begins `quads;
4236 GlDraw.vertex2 (x0+.dx, y0+.dy);
4237 GlDraw.vertex2 (x1+.dx, y1+.dy);
4238 GlDraw.vertex2 (x2+.dx, y2+.dy);
4239 GlDraw.vertex2 (x3+.dx, y3+.dy);
4241 GlDraw.ends ();
4243 ) state.layout
4244 ) state.rects
4246 Gl.disable `blend;
4249 let display () =
4250 GlClear.color (scalecolor2 conf.bgcolor);
4251 GlClear.clear [`color];
4252 List.iter drawpage state.layout;
4253 showrects ();
4254 showsel ();
4255 scrollindicator ();
4256 state.uioh#display;
4257 begin match state.mstate with
4258 | Mzoomrect ((x0, y0), (x1, y1)) ->
4259 Gl.enable `blend;
4260 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
4261 GlDraw.polygon_mode `both `fill;
4262 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
4263 GlDraw.rect (float x0, float y0)
4264 (float x1, float y1);
4265 Gl.disable `blend;
4266 | _ -> ()
4267 end;
4268 enttext ();
4269 Glut.swapBuffers ();
4272 let getunder x y =
4273 let rec f = function
4274 | l :: rest ->
4275 begin match getopaque l.pageno with
4276 | Some opaque ->
4277 let x0 = l.pagedispx in
4278 let x1 = x0 + l.pagevw in
4279 let y0 = l.pagedispy in
4280 let y1 = y0 + l.pagevh in
4281 if y >= y0 && y <= y1 && x >= x0 && x <= x1
4282 then
4283 let px, py = pagetranslatepoint l x y in
4284 match whatsunder opaque px py with
4285 | Unone -> f rest
4286 | under -> under
4287 else f rest
4288 | _ ->
4289 f rest
4291 | [] -> Unone
4293 f state.layout
4296 let zoomrect x y x1 y1 =
4297 let x0 = min x x1
4298 and x1 = max x x1
4299 and y0 = min y y1 in
4300 gotoy (state.y + y0);
4301 state.anchor <- getanchor ();
4302 let zoom = (float conf.winw *. conf.zoom) /. float (x1 - x0) in
4303 let margin =
4304 if state.w < conf.winw - state.scrollw
4305 then (conf.winw - state.scrollw - state.w) / 2
4306 else 0
4308 state.x <- (state.x + margin) - x0;
4309 setzoom zoom;
4310 Glut.setCursor Glut.CURSOR_INHERIT;
4311 state.mstate <- Mnone;
4314 let scrollx x =
4315 let winw = conf.winw - state.scrollw - 1 in
4316 let s = float x /. float winw in
4317 let destx = truncate (float (state.w + winw) *. s) in
4318 state.x <- winw - destx;
4319 gotoy_and_clear_text state.y;
4320 state.mstate <- Mscrollx;
4323 let scrolly y =
4324 let s = float y /. float conf.winh in
4325 let desty = truncate (float (state.maxy - conf.winh) *. s) in
4326 gotoy_and_clear_text desty;
4327 state.mstate <- Mscrolly;
4330 let viewmouse button bstate x y =
4331 match button with
4332 | Glut.OTHER_BUTTON n when (n == 3 || n == 4) && bstate = Glut.UP ->
4333 if Glut.getModifiers () land Glut.active_ctrl != 0
4334 then (
4335 match state.mstate with
4336 | Mzoom (oldn, i) ->
4337 if oldn = n
4338 then (
4339 if i = 2
4340 then
4341 let incr =
4342 match n with
4343 | 4 ->
4344 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
4345 | _ ->
4346 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
4348 let zoom = conf.zoom -. incr in
4349 setzoom zoom;
4350 state.mstate <- Mzoom (n, 0);
4351 else
4352 state.mstate <- Mzoom (n, i+1);
4354 else state.mstate <- Mzoom (n, 0)
4356 | _ -> state.mstate <- Mzoom (n, 0)
4358 else (
4359 match state.autoscroll with
4360 | Some step -> setautoscrollspeed step (n=4)
4361 | None ->
4362 let incr =
4363 if n = 3
4364 then -conf.scrollstep
4365 else conf.scrollstep
4367 let incr = incr * 2 in
4368 let y = clamp incr in
4369 gotoy_and_clear_text y
4372 | Glut.LEFT_BUTTON when Glut.getModifiers () land Glut.active_ctrl != 0 ->
4373 if bstate = Glut.DOWN
4374 then (
4375 Glut.setCursor Glut.CURSOR_CROSSHAIR;
4376 state.mstate <- Mpan (x, y)
4378 else
4379 state.mstate <- Mnone
4381 | Glut.RIGHT_BUTTON ->
4382 if bstate = Glut.DOWN
4383 then (
4384 Glut.setCursor Glut.CURSOR_CYCLE;
4385 let p = (x, y) in
4386 state.mstate <- Mzoomrect (p, p)
4388 else (
4389 match state.mstate with
4390 | Mzoomrect ((x0, y0), _) -> zoomrect x0 y0 x y
4391 | _ ->
4392 Glut.setCursor Glut.CURSOR_INHERIT;
4393 state.mstate <- Mnone
4396 | Glut.LEFT_BUTTON when x > conf.winw - state.scrollw ->
4397 if bstate = Glut.DOWN
4398 then
4399 let position, sh = scrollph state.y in
4400 if y > truncate position && y < truncate (position +. sh)
4401 then state.mstate <- Mscrolly
4402 else scrolly y
4403 else
4404 state.mstate <- Mnone
4406 | Glut.LEFT_BUTTON when y > conf.winh - state.hscrollh ->
4407 if bstate = Glut.DOWN
4408 then
4409 let position, sw = scrollpw state.x in
4410 if x > truncate position && x < truncate (position +. sw)
4411 then state.mstate <- Mscrollx
4412 else scrollx x
4413 else
4414 state.mstate <- Mnone
4416 | Glut.LEFT_BUTTON ->
4417 let dest = if bstate = Glut.DOWN then getunder x y else Unone in
4418 begin match dest with
4419 | Ulinkgoto (pageno, top) ->
4420 if pageno >= 0
4421 then (
4422 addnav ();
4423 gotopage1 pageno top;
4426 | Ulinkuri s ->
4427 gotouri s
4429 | Unone when bstate = Glut.DOWN ->
4430 Glut.setCursor Glut.CURSOR_CROSSHAIR;
4431 state.mstate <- Mpan (x, y);
4433 | Unone | Utext _ ->
4434 if bstate = Glut.DOWN
4435 then (
4436 if conf.angle mod 360 = 0
4437 then (
4438 state.mstate <- Msel ((x, y), (x, y));
4439 G.postRedisplay "mouse select";
4442 else (
4443 match state.mstate with
4444 | Mnone -> ()
4446 | Mzoom _ | Mscrollx | Mscrolly ->
4447 state.mstate <- Mnone
4449 | Mzoomrect ((x0, y0), _) ->
4450 zoomrect x0 y0 x y
4452 | Mpan _ ->
4453 Glut.setCursor Glut.CURSOR_INHERIT;
4454 state.mstate <- Mnone
4456 | Msel ((_, y0), (_, y1)) ->
4457 let f l =
4458 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
4459 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
4460 then
4461 match getopaque l.pageno with
4462 | Some opaque ->
4463 copysel opaque
4464 | _ -> ()
4466 List.iter f state.layout;
4467 copysel ""; (* ugly *)
4468 Glut.setCursor Glut.CURSOR_INHERIT;
4469 state.mstate <- Mnone;
4473 | _ -> ()
4476 let birdseyemouse button bstate x y
4477 (conf, leftx, _, hooverpageno, anchor) =
4478 match button with
4479 | Glut.LEFT_BUTTON when bstate = Glut.UP ->
4480 let margin = (conf.winw - (state.w + state.scrollw)) / 2 in
4481 let rec loop = function
4482 | [] -> ()
4483 | l :: rest ->
4484 if y > l.pagedispy && y < l.pagedispy + l.pagevh
4485 && x > margin && x < margin + l.pagew
4486 then (
4487 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
4489 else loop rest
4491 loop state.layout
4492 | Glut.OTHER_BUTTON _ -> viewmouse button bstate x y
4493 | _ -> ()
4496 let mouse bstate button x y =
4497 state.uioh <- state.uioh#button button bstate x y;
4500 let mouse ~button ~state ~x ~y = mouse state button x y;;
4502 let motion ~x ~y =
4503 state.uioh <- state.uioh#motion x y
4506 let pmotion ~x ~y =
4507 state.uioh <- state.uioh#pmotion x y;
4510 let uioh = object
4511 method display = ()
4513 method key key =
4514 begin match state.mode with
4515 | Textentry textentry -> textentrykeyboard key textentry
4516 | Birdseye birdseye -> birdseyekeyboard key birdseye
4517 | View -> viewkeyboard key
4518 end;
4519 state.uioh
4521 method special key =
4522 begin match state.mode with
4523 | View | (Birdseye _) when key = Glut.KEY_F9 ->
4524 togglebirdseye ()
4526 | Birdseye vals ->
4527 birdseyespecial key vals
4529 | View when key = Glut.KEY_F1 ->
4530 enterhelpmode ()
4532 | View ->
4533 begin match state.autoscroll with
4534 | Some step when key = Glut.KEY_DOWN || key = Glut.KEY_UP ->
4535 setautoscrollspeed step (key = Glut.KEY_DOWN)
4537 | _ ->
4538 let y =
4539 match key with
4540 | Glut.KEY_F3 -> search state.searchpattern true; state.y
4541 | Glut.KEY_UP ->
4542 if Glut.getModifiers () land Glut.active_ctrl != 0
4543 then
4544 if Glut.getModifiers () land Glut.active_shift != 0
4545 then (setzoom state.prevzoom; state.y)
4546 else clamp (-conf.winh/2)
4547 else clamp (-conf.scrollstep)
4548 | Glut.KEY_DOWN ->
4549 if Glut.getModifiers () land Glut.active_ctrl != 0
4550 then
4551 if Glut.getModifiers () land Glut.active_shift != 0
4552 then (setzoom state.prevzoom; state.y)
4553 else clamp (conf.winh/2)
4554 else clamp (conf.scrollstep)
4555 | Glut.KEY_PAGE_UP ->
4556 if Glut.getModifiers () land Glut.active_ctrl != 0
4557 then
4558 match state.layout with
4559 | [] -> state.y
4560 | l :: _ -> state.y - l.pagey
4561 else
4562 clamp (-conf.winh)
4563 | Glut.KEY_PAGE_DOWN ->
4564 if Glut.getModifiers () land Glut.active_ctrl != 0
4565 then
4566 match List.rev state.layout with
4567 | [] -> state.y
4568 | l :: _ -> getpagey l.pageno
4569 else
4570 clamp conf.winh
4571 | Glut.KEY_HOME ->
4572 addnav ();
4574 | Glut.KEY_END ->
4575 addnav ();
4576 state.maxy - (if conf.maxhfit then conf.winh else 0)
4578 | (Glut.KEY_RIGHT | Glut.KEY_LEFT) when
4579 Glut.getModifiers () land Glut.active_alt != 0 ->
4580 getnav (if key = Glut.KEY_LEFT then 1 else -1)
4582 | Glut.KEY_RIGHT when conf.zoom > 1.0 ->
4583 let dx =
4584 if Glut.getModifiers () land Glut.active_ctrl != 0
4585 then (conf.winw / 2)
4586 else 10
4588 state.x <- state.x - dx;
4589 state.y
4590 | Glut.KEY_LEFT when conf.zoom > 1.0 ->
4591 let dx =
4592 if Glut.getModifiers () land Glut.active_ctrl != 0
4593 then (conf.winw / 2)
4594 else 10
4596 state.x <- state.x + dx;
4597 state.y
4599 | _ -> state.y
4601 gotoy_and_clear_text y
4604 | Textentry te -> textentryspecial key te
4605 end;
4606 state.uioh
4608 method button button bstate x y =
4609 begin match state.mode with
4610 | View -> viewmouse button bstate x y
4611 | Birdseye beye -> birdseyemouse button bstate x y beye
4612 | Textentry _ -> ()
4613 end;
4614 state.uioh
4616 method motion x y =
4617 begin match state.mode with
4618 | Textentry _ -> ()
4619 | View | Birdseye _ ->
4620 match state.mstate with
4621 | Mzoom _ | Mnone -> ()
4623 | Mpan (x0, y0) ->
4624 let dx = x - x0
4625 and dy = y0 - y in
4626 state.mstate <- Mpan (x, y);
4627 if conf.zoom > 1.0 then state.x <- state.x + dx;
4628 let y = clamp dy in
4629 gotoy_and_clear_text y
4631 | Msel (a, _) ->
4632 state.mstate <- Msel (a, (x, y));
4633 G.postRedisplay "motion select";
4635 | Mscrolly ->
4636 let y = min conf.winh (max 0 y) in
4637 scrolly y
4639 | Mscrollx ->
4640 let x = min conf.winw (max 0 x) in
4641 scrollx x
4643 | Mzoomrect (p0, _) ->
4644 state.mstate <- Mzoomrect (p0, (x, y));
4645 G.postRedisplay "motion zoomrect";
4646 end;
4647 state.uioh
4649 method pmotion x y =
4650 begin match state.mode with
4651 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
4652 let margin = (conf.winw - (state.w + state.scrollw)) / 2 in
4653 let rec loop = function
4654 | [] ->
4655 if hooverpageno != -1
4656 then (
4657 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
4658 G.postRedisplay "pmotion birdseye no hoover";
4660 | l :: rest ->
4661 if y > l.pagedispy && y < l.pagedispy + l.pagevh
4662 && x > margin && x < margin + l.pagew
4663 then (
4664 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
4665 G.postRedisplay "pmotion birdseye hoover";
4667 else loop rest
4669 loop state.layout
4671 | Textentry _ -> ()
4673 | View ->
4674 match state.mstate with
4675 | Mnone ->
4676 begin match getunder x y with
4677 | Unone -> Glut.setCursor Glut.CURSOR_INHERIT
4678 | Ulinkuri uri ->
4679 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
4680 Glut.setCursor Glut.CURSOR_INFO
4681 | Ulinkgoto (page, _) ->
4682 if conf.underinfo
4683 then showtext 'p' ("age: " ^ string_of_int (page+1));
4684 Glut.setCursor Glut.CURSOR_INFO
4685 | Utext s ->
4686 if conf.underinfo then showtext 'f' ("ont: " ^ s);
4687 Glut.setCursor Glut.CURSOR_TEXT
4690 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
4692 end;
4693 state.uioh
4695 method infochanged _ = ()
4696 end;;
4698 module Config =
4699 struct
4700 open Parser
4702 let fontpath = ref "";;
4703 let wmclasshack = ref false;;
4705 let unent s =
4706 let l = String.length s in
4707 let b = Buffer.create l in
4708 unent b s 0 l;
4709 Buffer.contents b;
4712 let home =
4714 match platform with
4715 | Pwindows | Pmingw -> Sys.getenv "HOMEPATH"
4716 | _ -> Sys.getenv "HOME"
4717 with exn ->
4718 prerr_endline
4719 ("Can not determine home directory location: " ^
4720 Printexc.to_string exn);
4724 let config_of c attrs =
4725 let apply c k v =
4727 match k with
4728 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
4729 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
4730 | "case-insensitive-search" -> { c with icase = bool_of_string v }
4731 | "preload" -> { c with preload = bool_of_string v }
4732 | "page-bias" -> { c with pagebias = int_of_string v }
4733 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
4734 | "auto-scroll-step" ->
4735 { c with autoscrollstep = max 0 (int_of_string v) }
4736 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
4737 | "crop-hack" -> { c with crophack = bool_of_string v }
4738 | "throttle" ->
4739 let mw =
4740 match String.lowercase v with
4741 | "true" -> Some infinity
4742 | "false" -> None
4743 | f -> Some (float_of_string f)
4745 { c with maxwait = mw}
4746 | "highlight-links" -> { c with hlinks = bool_of_string v }
4747 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
4748 | "vertical-margin" ->
4749 { c with interpagespace = max 0 (int_of_string v) }
4750 | "zoom" ->
4751 let zoom = float_of_string v /. 100. in
4752 let zoom = max zoom 0.0 in
4753 { c with zoom = zoom }
4754 | "presentation" -> { c with presentation = bool_of_string v }
4755 | "rotation-angle" -> { c with angle = int_of_string v }
4756 | "width" -> { c with winw = max 20 (int_of_string v) }
4757 | "height" -> { c with winh = max 20 (int_of_string v) }
4758 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
4759 | "proportional-display" -> { c with proportional = bool_of_string v }
4760 | "pixmap-cache-size" ->
4761 { c with memlimit = max 2 (int_of_string_with_suffix v) }
4762 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
4763 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
4764 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
4765 | "persistent-location" -> { c with jumpback = bool_of_string v }
4766 | "background-color" -> { c with bgcolor = color_of_string v }
4767 | "scrollbar-in-presentation" ->
4768 { c with scrollbarinpm = bool_of_string v }
4769 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
4770 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
4771 | "mupdf-memlimit" ->
4772 { c with mumemlimit = max 1024 (int_of_string_with_suffix v) }
4773 | "checkers" -> { c with checkers = bool_of_string v }
4774 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
4775 | "trim-margins" -> { c with trimmargins = bool_of_string v }
4776 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
4777 | "wmclass-hack" -> wmclasshack := bool_of_string v; c
4778 | "uri-launcher" -> { c with urilauncher = unent v }
4779 | "color-space" -> { c with colorspace = colorspace_of_string v }
4780 | "invert-colors" -> { c with invert = bool_of_string v }
4781 | "brightness" -> { c with colorscale = float_of_string v }
4782 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
4783 | _ -> c
4784 with exn ->
4785 prerr_endline ("Error processing attribute (`" ^
4786 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
4789 let rec fold c = function
4790 | [] -> c
4791 | (k, v) :: rest ->
4792 let c = apply c k v in
4793 fold c rest
4795 fold c attrs;
4798 let fromstring f pos n v d =
4799 try f v
4800 with exn ->
4801 dolog "Error processing attribute (%S=%S) at %d\n%s"
4802 n v pos (Printexc.to_string exn)
4807 let bookmark_of attrs =
4808 let rec fold title page rely = function
4809 | ("title", v) :: rest -> fold v page rely rest
4810 | ("page", v) :: rest -> fold title v rely rest
4811 | ("rely", v) :: rest -> fold title page v rest
4812 | _ :: rest -> fold title page rely rest
4813 | [] -> title, page, rely
4815 fold "invalid" "0" "0" attrs
4818 let doc_of attrs =
4819 let rec fold path page rely pan = function
4820 | ("path", v) :: rest -> fold v page rely pan rest
4821 | ("page", v) :: rest -> fold path v rely pan rest
4822 | ("rely", v) :: rest -> fold path page v pan rest
4823 | ("pan", v) :: rest -> fold path page rely v rest
4824 | _ :: rest -> fold path page rely pan rest
4825 | [] -> path, page, rely, pan
4827 fold "" "0" "0" "0" attrs
4830 let setconf dst src =
4831 dst.scrollbw <- src.scrollbw;
4832 dst.scrollh <- src.scrollh;
4833 dst.icase <- src.icase;
4834 dst.preload <- src.preload;
4835 dst.pagebias <- src.pagebias;
4836 dst.verbose <- src.verbose;
4837 dst.scrollstep <- src.scrollstep;
4838 dst.maxhfit <- src.maxhfit;
4839 dst.crophack <- src.crophack;
4840 dst.autoscrollstep <- src.autoscrollstep;
4841 dst.maxwait <- src.maxwait;
4842 dst.hlinks <- src.hlinks;
4843 dst.underinfo <- src.underinfo;
4844 dst.interpagespace <- src.interpagespace;
4845 dst.zoom <- src.zoom;
4846 dst.presentation <- src.presentation;
4847 dst.angle <- src.angle;
4848 dst.winw <- src.winw;
4849 dst.winh <- src.winh;
4850 dst.savebmarks <- src.savebmarks;
4851 dst.memlimit <- src.memlimit;
4852 dst.proportional <- src.proportional;
4853 dst.texcount <- src.texcount;
4854 dst.sliceheight <- src.sliceheight;
4855 dst.thumbw <- src.thumbw;
4856 dst.jumpback <- src.jumpback;
4857 dst.bgcolor <- src.bgcolor;
4858 dst.scrollbarinpm <- src.scrollbarinpm;
4859 dst.tilew <- src.tilew;
4860 dst.tileh <- src.tileh;
4861 dst.mumemlimit <- src.mumemlimit;
4862 dst.checkers <- src.checkers;
4863 dst.aalevel <- src.aalevel;
4864 dst.trimmargins <- src.trimmargins;
4865 dst.trimfuzz <- src.trimfuzz;
4866 dst.urilauncher <- src.urilauncher;
4867 dst.colorspace <- src.colorspace;
4868 dst.invert <- src.invert;
4869 dst.colorscale <- src.colorscale;
4870 dst.redirectstderr <- src.redirectstderr;
4873 let get s =
4874 let h = Hashtbl.create 10 in
4875 let dc = { defconf with angle = defconf.angle } in
4876 let rec toplevel v t spos _ =
4877 match t with
4878 | Vdata | Vcdata | Vend -> v
4879 | Vopen ("llppconfig", _, closed) ->
4880 if closed
4881 then v
4882 else { v with f = llppconfig }
4883 | Vopen _ ->
4884 error "unexpected subelement at top level" s spos
4885 | Vclose _ -> error "unexpected close at top level" s spos
4887 and llppconfig v t spos _ =
4888 match t with
4889 | Vdata | Vcdata -> v
4890 | Vend -> error "unexpected end of input in llppconfig" s spos
4891 | Vopen ("defaults", attrs, closed) ->
4892 let c = config_of dc attrs in
4893 setconf dc c;
4894 if closed
4895 then v
4896 else { v with f = skip "defaults" (fun () -> v) }
4898 | Vopen ("ui-font", attrs, closed) ->
4899 let rec getsize size = function
4900 | [] -> size
4901 | ("size", v) :: rest ->
4902 let size =
4903 fromstring int_of_string spos "size" v fstate.fontsize in
4904 getsize size rest
4905 | l -> getsize size l
4907 fstate.fontsize <- getsize fstate.fontsize attrs;
4908 if closed
4909 then v
4910 else { v with f = uifont (Buffer.create 10) }
4912 | Vopen ("doc", attrs, closed) ->
4913 let pathent, spage, srely, span = doc_of attrs in
4914 let path = unent pathent
4915 and pageno = fromstring int_of_string spos "page" spage 0
4916 and rely = fromstring float_of_string spos "rely" srely 0.0
4917 and pan = fromstring int_of_string spos "pan" span 0 in
4918 let c = config_of dc attrs in
4919 let anchor = (pageno, rely) in
4920 if closed
4921 then (Hashtbl.add h path (c, [], pan, anchor); v)
4922 else { v with f = doc path pan anchor c [] }
4924 | Vopen _ ->
4925 error "unexpected subelement in llppconfig" s spos
4927 | Vclose "llppconfig" -> { v with f = toplevel }
4928 | Vclose _ -> error "unexpected close in llppconfig" s spos
4930 and uifont b v t spos epos =
4931 match t with
4932 | Vdata | Vcdata ->
4933 Buffer.add_substring b s spos (epos - spos);
4935 | Vopen (_, _, _) ->
4936 error "unexpected subelement in ui-font" s spos
4937 | Vclose "ui-font" ->
4938 if String.length !fontpath = 0
4939 then fontpath := Buffer.contents b;
4940 { v with f = llppconfig }
4941 | Vclose _ -> error "unexpected close in ui-font" s spos
4942 | Vend -> error "unexpected end of input in ui-font" s spos
4944 and doc path pan anchor c bookmarks v t spos _ =
4945 match t with
4946 | Vdata | Vcdata -> v
4947 | Vend -> error "unexpected end of input in doc" s spos
4948 | Vopen ("bookmarks", _, closed) ->
4949 if closed
4950 then v
4951 else { v with f = pbookmarks path pan anchor c bookmarks }
4953 | Vopen (_, _, _) ->
4954 error "unexpected subelement in doc" s spos
4956 | Vclose "doc" ->
4957 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
4958 { v with f = llppconfig }
4960 | Vclose _ -> error "unexpected close in doc" s spos
4962 and pbookmarks path pan anchor c bookmarks v t spos _ =
4963 match t with
4964 | Vdata | Vcdata -> v
4965 | Vend -> error "unexpected end of input in bookmarks" s spos
4966 | Vopen ("item", attrs, closed) ->
4967 let titleent, spage, srely = bookmark_of attrs in
4968 let page = fromstring int_of_string spos "page" spage 0
4969 and rely = fromstring float_of_string spos "rely" srely 0.0 in
4970 let bookmarks = (unent titleent, 0, (page, rely)) :: bookmarks in
4971 if closed
4972 then { v with f = pbookmarks path pan anchor c bookmarks }
4973 else
4974 let f () = v in
4975 { v with f = skip "item" f }
4977 | Vopen _ ->
4978 error "unexpected subelement in bookmarks" s spos
4980 | Vclose "bookmarks" ->
4981 { v with f = doc path pan anchor c bookmarks }
4983 | Vclose _ -> error "unexpected close in bookmarks" s spos
4985 and skip tag f v t spos _ =
4986 match t with
4987 | Vdata | Vcdata -> v
4988 | Vend ->
4989 error ("unexpected end of input in skipped " ^ tag) s spos
4990 | Vopen (tag', _, closed) ->
4991 if closed
4992 then v
4993 else
4994 let f' () = { v with f = skip tag f } in
4995 { v with f = skip tag' f' }
4996 | Vclose ctag ->
4997 if tag = ctag
4998 then f ()
4999 else error ("unexpected close in skipped " ^ tag) s spos
5002 parse { f = toplevel; accu = () } s;
5003 h, dc;
5006 let do_load f ic =
5008 let len = in_channel_length ic in
5009 let s = String.create len in
5010 really_input ic s 0 len;
5011 f s;
5012 with
5013 | Parse_error (msg, s, pos) ->
5014 let subs = subs s pos in
5015 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
5016 failwith ("parse error: " ^ s)
5018 | exn ->
5019 failwith ("config load error: " ^ Printexc.to_string exn)
5022 let defconfpath =
5023 let dir =
5025 let dir = Filename.concat home ".config" in
5026 if Sys.is_directory dir then dir else home
5027 with _ -> home
5029 Filename.concat dir "llpp.conf"
5032 let confpath = ref defconfpath;;
5034 let load1 f =
5035 if Sys.file_exists !confpath
5036 then
5037 match
5038 (try Some (open_in_bin !confpath)
5039 with exn ->
5040 prerr_endline
5041 ("Error opening configuation file `" ^ !confpath ^ "': " ^
5042 Printexc.to_string exn);
5043 None
5045 with
5046 | Some ic ->
5047 begin try
5048 f (do_load get ic)
5049 with exn ->
5050 prerr_endline
5051 ("Error loading configuation from `" ^ !confpath ^ "': " ^
5052 Printexc.to_string exn);
5053 end;
5054 close_in ic;
5056 | None -> ()
5057 else
5058 f (Hashtbl.create 0, defconf)
5061 let load () =
5062 let f (h, dc) =
5063 let pc, pb, px, pa =
5065 Hashtbl.find h (Filename.basename state.path)
5066 with Not_found -> dc, [], 0, (0, 0.0)
5068 setconf defconf dc;
5069 setconf conf pc;
5070 state.bookmarks <- pb;
5071 state.x <- px;
5072 state.scrollw <- conf.scrollbw;
5073 if conf.jumpback
5074 then state.anchor <- pa;
5075 cbput state.hists.nav pa;
5077 load1 f
5080 let add_attrs bb always dc c =
5081 let ob s a b =
5082 if always || a != b
5083 then Printf.bprintf bb "\n %s='%b'" s a
5084 and oi s a b =
5085 if always || a != b
5086 then Printf.bprintf bb "\n %s='%d'" s a
5087 and oI s a b =
5088 if always || a != b
5089 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
5090 and oz s a b =
5091 if always || a <> b
5092 then Printf.bprintf bb "\n %s='%d'" s (truncate (a*.100.))
5093 and oF s a b =
5094 if always || a <> b
5095 then Printf.bprintf bb "\n %s='%f'" s a
5096 and oc s a b =
5097 if always || a <> b
5098 then
5099 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
5100 and oC s a b =
5101 if always || a <> b
5102 then
5103 Printf.bprintf bb "\n %s='%s'" s (colorspace_to_string a)
5104 and oR s a b =
5105 if always || a <> b
5106 then
5107 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
5108 and os s a b =
5109 if always || a <> b
5110 then
5111 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
5112 and oW s a b =
5113 if always || a <> b
5114 then
5115 let v =
5116 match a with
5117 | None -> "false"
5118 | Some f ->
5119 if f = infinity
5120 then "true"
5121 else string_of_float f
5123 Printf.bprintf bb "\n %s='%s'" s v
5125 let w, h =
5126 if always
5127 then dc.winw, dc.winh
5128 else
5129 match state.fullscreen with
5130 | Some wh -> wh
5131 | None -> c.winw, c.winh
5133 let zoom, presentation, interpagespace, maxwait =
5134 if always
5135 then dc.zoom, dc.presentation, dc.interpagespace, dc.maxwait
5136 else
5137 match state.mode with
5138 | Birdseye (bc, _, _, _, _) ->
5139 bc.zoom, bc.presentation, bc.interpagespace, bc.maxwait
5140 | _ -> c.zoom, c.presentation, c.interpagespace, c.maxwait
5142 oi "width" w dc.winw;
5143 oi "height" h dc.winh;
5144 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
5145 oi "scroll-handle-height" c.scrollh dc.scrollh;
5146 ob "case-insensitive-search" c.icase dc.icase;
5147 ob "preload" c.preload dc.preload;
5148 oi "page-bias" c.pagebias dc.pagebias;
5149 oi "scroll-step" c.scrollstep dc.scrollstep;
5150 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
5151 ob "max-height-fit" c.maxhfit dc.maxhfit;
5152 ob "crop-hack" c.crophack dc.crophack;
5153 oW "throttle" maxwait dc.maxwait;
5154 ob "highlight-links" c.hlinks dc.hlinks;
5155 ob "under-cursor-info" c.underinfo dc.underinfo;
5156 oi "vertical-margin" interpagespace dc.interpagespace;
5157 oz "zoom" zoom dc.zoom;
5158 ob "presentation" presentation dc.presentation;
5159 oi "rotation-angle" c.angle dc.angle;
5160 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
5161 ob "proportional-display" c.proportional dc.proportional;
5162 oI "pixmap-cache-size" c.memlimit dc.memlimit;
5163 oi "tex-count" c.texcount dc.texcount;
5164 oi "slice-height" c.sliceheight dc.sliceheight;
5165 oi "thumbnail-width" c.thumbw dc.thumbw;
5166 ob "persistent-location" c.jumpback dc.jumpback;
5167 oc "background-color" c.bgcolor dc.bgcolor;
5168 ob "scrollbar-in-presentation" c.scrollbarinpm dc.scrollbarinpm;
5169 oi "tile-width" c.tilew dc.tilew;
5170 oi "tile-height" c.tileh dc.tileh;
5171 oI "mupdf-memlimit" c.mumemlimit dc.mumemlimit;
5172 ob "checkers" c.checkers dc.checkers;
5173 oi "aalevel" c.aalevel dc.aalevel;
5174 ob "trim-margins" c.trimmargins dc.trimmargins;
5175 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
5176 os "uri-launcher" c.urilauncher dc.urilauncher;
5177 oC "color-space" c.colorspace dc.colorspace;
5178 ob "invert-colors" c.invert dc.invert;
5179 oF "brightness" c.colorscale dc.colorscale;
5180 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
5181 if always
5182 then ob "wmclass-hack" !wmclasshack false;
5185 let save () =
5186 let uifontsize = fstate.fontsize in
5187 let bb = Buffer.create 32768 in
5188 let f (h, dc) =
5189 let dc = if conf.bedefault then conf else dc in
5190 Buffer.add_string bb "<llppconfig>\n";
5192 if String.length !fontpath > 0
5193 then
5194 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
5195 uifontsize
5196 !fontpath
5197 else (
5198 if uifontsize <> 14
5199 then
5200 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
5203 Buffer.add_string bb "<defaults ";
5204 add_attrs bb true dc dc;
5205 Buffer.add_string bb "/>\n";
5207 let adddoc path pan anchor c bookmarks =
5208 if bookmarks == [] && c = dc && anchor = emptyanchor
5209 then ()
5210 else (
5211 Printf.bprintf bb "<doc path='%s'"
5212 (enent path 0 (String.length path));
5214 if anchor <> emptyanchor
5215 then (
5216 let n, y = anchor in
5217 Printf.bprintf bb " page='%d'" n;
5218 if y > 1e-6
5219 then
5220 Printf.bprintf bb " rely='%f'" y
5224 if pan != 0
5225 then Printf.bprintf bb " pan='%d'" pan;
5227 add_attrs bb false dc c;
5229 begin match bookmarks with
5230 | [] -> Buffer.add_string bb "/>\n"
5231 | _ ->
5232 Buffer.add_string bb ">\n<bookmarks>\n";
5233 List.iter (fun (title, _level, (page, rely)) ->
5234 Printf.bprintf bb
5235 "<item title='%s' page='%d'"
5236 (enent title 0 (String.length title))
5237 page
5239 if rely > 1e-6
5240 then
5241 Printf.bprintf bb " rely='%f'" rely
5243 Buffer.add_string bb "/>\n";
5244 ) bookmarks;
5245 Buffer.add_string bb "</bookmarks>\n</doc>\n";
5246 end;
5250 let pan =
5251 match state.mode with
5252 | Birdseye (_, pan, _, _, _) -> pan
5253 | _ -> state.x
5255 let basename = Filename.basename state.path in
5256 adddoc basename pan (getanchor ())
5257 { conf with
5258 autoscrollstep =
5259 match state.autoscroll with
5260 | Some step -> step
5261 | None -> conf.autoscrollstep }
5262 (if conf.savebmarks then state.bookmarks else []);
5264 Hashtbl.iter (fun path (c, bookmarks, x, y) ->
5265 if basename <> path
5266 then adddoc path x y c bookmarks
5267 ) h;
5268 Buffer.add_string bb "</llppconfig>";
5270 load1 f;
5271 if Buffer.length bb > 0
5272 then
5274 let tmp = !confpath ^ ".tmp" in
5275 let oc = open_out_bin tmp in
5276 Buffer.output_buffer oc bb;
5277 close_out oc;
5278 Unix.rename tmp !confpath;
5279 with exn ->
5280 prerr_endline
5281 ("error while saving configuration: " ^ Printexc.to_string exn)
5283 end;;
5285 let () =
5286 Arg.parse
5287 (Arg.align
5288 [("-p", Arg.String (fun s -> state.password <- s) ,
5289 "<password> Set password");
5291 ("-f", Arg.String (fun s -> Config.fontpath := s),
5292 "<path> Set path to the user interface font");
5294 ("-c", Arg.String (fun s -> Config.confpath := s),
5295 "<path> Set path to the configuration file");
5297 ("-v", Arg.Unit (fun () ->
5298 Printf.printf
5299 "%s\nconfiguration path: %s\n"
5300 (version ())
5301 Config.defconfpath
5303 exit 0), " Print version and exit");
5306 (fun s -> state.path <- s)
5307 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
5309 if String.length state.path = 0
5310 then (prerr_endline "file name missing"; exit 1);
5312 Config.load ();
5314 let _ = Glut.init Sys.argv in
5315 let () = Glut.initDisplayMode ~depth:false ~double_buffer:true () in
5316 let () = Glut.initWindowSize conf.winw conf.winh in
5317 let _ = Glut.createWindow ("llpp " ^ Filename.basename state.path) in
5319 if not (Glut.extensionSupported "GL_ARB_texture_rectangle"
5320 || Glut.extensionSupported "GL_EXT_texture_rectangle")
5321 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
5323 let csock, ssock =
5324 if not is_windows
5325 then
5326 Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0
5327 else
5328 let addr = Unix.ADDR_INET (Unix.inet_addr_loopback, 1337) in
5329 let sock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
5330 Unix.setsockopt sock Unix.SO_REUSEADDR true;
5331 Unix.bind sock addr;
5332 Unix.listen sock 1;
5333 let csock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
5334 Unix.connect csock addr;
5335 let ssock, _ = Unix.accept sock in
5336 Unix.close sock;
5337 let opts sock =
5338 Unix.setsockopt sock Unix.TCP_NODELAY true;
5339 Unix.setsockopt_optint sock Unix.SO_LINGER None;
5341 opts ssock;
5342 opts csock;
5343 ssock, csock
5346 let () = Glut.displayFunc display in
5347 let () = Glut.reshapeFunc reshape in
5348 let () = Glut.keyboardFunc keyboard in
5349 let () = Glut.specialFunc special in
5350 let () = Glut.idleFunc (Some idle) in
5351 let () = Glut.mouseFunc mouse in
5352 let () = Glut.motionFunc motion in
5353 let () = Glut.passiveMotionFunc pmotion in
5355 setcheckers conf.checkers;
5356 init ssock (
5357 conf.angle, conf.proportional, (conf.trimmargins, conf.trimfuzz),
5358 conf.texcount, conf.sliceheight, conf.mumemlimit, conf.colorspace,
5359 !Config.wmclasshack, !Config.fontpath
5361 state.csock <- csock;
5362 state.ssock <- ssock;
5363 state.text <- "Opening " ^ state.path;
5364 setaalevel conf.aalevel;
5365 writeopen state.path state.password;
5366 state.uioh <- uioh;
5367 setfontsize fstate.fontsize;
5369 redirectstderr ();
5371 while true do
5373 Glut.mainLoop ();
5374 with
5375 | Glut.BadEnum "key in special_of_int" ->
5376 showtext '!' " LablGlut bug: special key not recognized";
5378 | Quit ->
5379 wcmd "quit" [];
5380 Config.save ();
5381 exit 0
5383 | exn when conf.redirectstderr ->
5384 let s =
5385 Printf.sprintf "exception %s\n%s"
5386 (Printexc.to_string exn)
5387 (Printexc.get_backtrace ())
5389 ignore (try
5390 Unix.single_write state.stderr s 0 (String.length s);
5391 with _ -> 0);
5392 exit 1
5393 done;