NetBSD
[llpp.git] / main.ml
blob53599d37258008d10e106abb042ec0e430c51344
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 now = Unix.gettimeofday;;
11 exception Quit;;
13 type params = (angle * proportional * trimparams
14 * texcount * sliceheight * memsize
15 * colorspace * wmclasshack * fontpath)
16 and pageno = int
17 and width = int
18 and height = int
19 and leftx = int
20 and opaque = string
21 and recttype = int
22 and pixmapsize = int
23 and angle = int
24 and proportional = bool
25 and trimmargins = bool
26 and interpagespace = int
27 and texcount = int
28 and sliceheight = int
29 and gen = int
30 and top = float
31 and fontpath = string
32 and memsize = int
33 and aalevel = int
34 and wmclasshack = bool
35 and irect = (int * int * int * int)
36 and trimparams = (trimmargins * irect)
37 and colorspace = | Rgb | Bgr | Gray
40 type platform = | Punknown | Plinux | Pwindows | Posx | Psun
41 | Pfreebsd | Pdragonflybsd | Popenbsd | Pnetbsd
42 | 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";;
57 external realloctexts : int -> bool = "ml_realloctexts";;
59 let platform_to_string = function
60 | Punknown -> "unknown"
61 | Plinux -> "Linux"
62 | Pwindows -> "Windows"
63 | Posx -> "OSX"
64 | Psun -> "Sun"
65 | Pfreebsd -> "FreeBSD"
66 | Pdragonflybsd -> "DragonflyBSD"
67 | Popenbsd -> "OpenBSD"
68 | Pnetbsd -> "NetBSD"
69 | Pcygwin -> "Cygwin"
70 | Pmingw -> "MingW"
73 let platform = platform ();;
75 let is_windows =
76 match platform with
77 | Pwindows | Pmingw -> true
78 | _ -> false
81 type x = int
82 and y = int
83 and tilex = int
84 and tiley = int
85 and tileparams = (x * y * width * height * tilex * tiley)
88 external drawtile : tileparams -> opaque -> unit = "ml_drawtile";;
90 type mpos = int * int
91 and mstate =
92 | Msel of (mpos * mpos)
93 | Mpan of mpos
94 | Mscrolly | Mscrollx
95 | Mzoom of (int * int)
96 | Mzoomrect of (mpos * mpos)
97 | Mnone
100 type textentry = string * string * onhist option * onkey * ondone
101 and onkey = string -> int -> te
102 and ondone = string -> unit
103 and histcancel = unit -> unit
104 and onhist = ((histcmd -> string) * histcancel)
105 and histcmd = HCnext | HCprev | HCfirst | HClast
106 and te =
107 | TEstop
108 | TEdone of string
109 | TEcont of string
110 | TEswitch of textentry
113 type 'a circbuf =
114 { store : 'a array
115 ; mutable rc : int
116 ; mutable wc : int
117 ; mutable len : int
121 let bound v minv maxv =
122 max minv (min maxv v);
125 let cbnew n v =
126 { store = Array.create n v
127 ; rc = 0
128 ; wc = 0
129 ; len = 0
133 let drawstring size x y s =
134 Gl.enable `blend;
135 Gl.enable `texture_2d;
136 ignore (drawstr size x y s);
137 Gl.disable `blend;
138 Gl.disable `texture_2d;
141 let drawstring1 size x y s =
142 drawstr size x y s;
145 let drawstring2 size x y fmt =
146 Printf.kprintf (drawstring size (x+1) (y+size+1)) fmt
149 let cbcap b = Array.length b.store;;
151 let cbput b v =
152 let cap = cbcap b in
153 b.store.(b.wc) <- v;
154 b.wc <- (b.wc + 1) mod cap;
155 b.rc <- b.wc;
156 b.len <- min (b.len + 1) cap;
159 let cbempty b = b.len = 0;;
161 let cbgetg b circular dir =
162 if cbempty b
163 then b.store.(0)
164 else
165 let rc = b.rc + dir in
166 let rc =
167 if circular
168 then (
169 if rc = -1
170 then b.len-1
171 else (
172 if rc = b.len
173 then 0
174 else rc
177 else max 0 (min rc (b.len-1))
179 b.rc <- rc;
180 b.store.(rc);
183 let cbget b = cbgetg b false;;
184 let cbgetc b = cbgetg b true;;
186 type page =
187 { pageno : int
188 ; pagedimno : int
189 ; pagew : int
190 ; pageh : int
191 ; pagex : int
192 ; pagey : int
193 ; pagevw : int
194 ; pagevh : int
195 ; pagedispx : int
196 ; pagedispy : int
200 let debugl l =
201 dolog "l %d dim=%d {" l.pageno l.pagedimno;
202 dolog " WxH %dx%d" l.pagew l.pageh;
203 dolog " vWxH %dx%d" l.pagevw l.pagevh;
204 dolog " pagex,y %d,%d" l.pagex l.pagey;
205 dolog " dispx,y %d,%d" l.pagedispx l.pagedispy;
206 dolog "}";
209 let debugrect (x0, y0, x1, y1, x2, y2, x3, y3) =
210 dolog "rect {";
211 dolog " x0,y0=(% f, % f)" x0 y0;
212 dolog " x1,y1=(% f, % f)" x1 y1;
213 dolog " x2,y2=(% f, % f)" x2 y2;
214 dolog " x3,y3=(% f, % f)" x3 y3;
215 dolog "}";
218 type columns =
219 multicol * ((pdimno * x * y * (pageno * width * height * leftx)) array)
220 and multicol = columncount * covercount * covercount
221 and pdimno = int
222 and columncount = int
223 and covercount = int;;
225 type conf =
226 { mutable scrollbw : int
227 ; mutable scrollh : int
228 ; mutable icase : bool
229 ; mutable preload : bool
230 ; mutable pagebias : int
231 ; mutable verbose : bool
232 ; mutable debug : bool
233 ; mutable scrollstep : int
234 ; mutable maxhfit : bool
235 ; mutable crophack : bool
236 ; mutable autoscrollstep : int
237 ; mutable maxwait : float option
238 ; mutable hlinks : bool
239 ; mutable underinfo : bool
240 ; mutable interpagespace : interpagespace
241 ; mutable zoom : float
242 ; mutable presentation : bool
243 ; mutable angle : angle
244 ; mutable winw : int
245 ; mutable winh : int
246 ; mutable savebmarks : bool
247 ; mutable proportional : proportional
248 ; mutable trimmargins : trimmargins
249 ; mutable trimfuzz : irect
250 ; mutable memlimit : memsize
251 ; mutable texcount : texcount
252 ; mutable sliceheight : sliceheight
253 ; mutable thumbw : width
254 ; mutable jumpback : bool
255 ; mutable bgcolor : float * float * float
256 ; mutable bedefault : bool
257 ; mutable scrollbarinpm : bool
258 ; mutable tilew : int
259 ; mutable tileh : int
260 ; mutable mustoresize : memsize
261 ; mutable checkers : bool
262 ; mutable aalevel : int
263 ; mutable urilauncher : string
264 ; mutable colorspace : colorspace
265 ; mutable invert : bool
266 ; mutable colorscale : float
267 ; mutable redirectstderr : bool
268 ; mutable ghyllscroll : (int * int * int) option
269 ; mutable columns : columns option
270 ; mutable beyecolumns : columncount option
274 type anchor = pageno * top;;
276 type outline = string * int * anchor;;
278 type rect = float * float * float * float * float * float * float * float;;
280 type tile = opaque * pixmapsize * elapsed
281 and elapsed = float;;
282 type pagemapkey = pageno * gen;;
283 type tilemapkey = pageno * gen * colorspace * angle * width * height * col * row
284 and row = int
285 and col = int;;
287 let emptyanchor = (0, 0.0);;
289 type infochange = | Memused | Docinfo | Pdim;;
291 class type uioh = object
292 method display : unit
293 method key : int -> uioh
294 method special : Glut.special_key_t -> uioh
295 method button :
296 Glut.button_t -> Glut.mouse_button_state_t -> int -> int -> uioh
297 method motion : int -> int -> uioh
298 method pmotion : int -> int -> uioh
299 method infochanged : infochange -> unit
300 method scrollpw : (int * float * float)
301 method scrollph : (int * float * float)
302 end;;
304 type mode =
305 | Birdseye of (conf * leftx * pageno * pageno * anchor)
306 | Textentry of (textentry * onleave)
307 | View
308 and onleave = leavetextentrystatus -> unit
309 and leavetextentrystatus = | Cancel | Confirm
310 and helpitem = string * int * action
311 and action =
312 | Noaction
313 | Action of (uioh -> uioh)
316 let isbirdseye = function Birdseye _ -> true | _ -> false;;
317 let istextentry = function Textentry _ -> true | _ -> false;;
319 type currently =
320 | Idle
321 | Loading of (page * gen)
322 | Tiling of (
323 page * opaque * colorspace * angle * gen * col * row * width * height
325 | Outlining of outline list
328 let nouioh : uioh = object (self)
329 method display = ()
330 method key _ = self
331 method special _ = self
332 method button _ _ _ _ = self
333 method motion _ _ = self
334 method pmotion _ _ = self
335 method infochanged _ = ()
336 method scrollpw = (0, nan, nan)
337 method scrollph = (0, nan, nan)
338 end;;
340 type state =
341 { mutable csock : Unix.file_descr
342 ; mutable ssock : Unix.file_descr
343 ; mutable errfd : Unix.file_descr option
344 ; mutable stderr : Unix.file_descr
345 ; mutable errmsgs : Buffer.t
346 ; mutable newerrmsgs : bool
347 ; mutable w : int
348 ; mutable x : int
349 ; mutable y : int
350 ; mutable scrollw : int
351 ; mutable hscrollh : int
352 ; mutable anchor : anchor
353 ; mutable maxy : int
354 ; mutable layout : page list
355 ; pagemap : (pagemapkey, opaque) Hashtbl.t
356 ; tilemap : (tilemapkey, tile) Hashtbl.t
357 ; tilelru : (tilemapkey * opaque * pixmapsize) Queue.t
358 ; mutable pdims : (pageno * width * height * leftx) list
359 ; mutable pagecount : int
360 ; mutable currently : currently
361 ; mutable mstate : mstate
362 ; mutable searchpattern : string
363 ; mutable rects : (pageno * recttype * rect) list
364 ; mutable rects1 : (pageno * recttype * rect) list
365 ; mutable text : string
366 ; mutable fullscreen : (width * height) option
367 ; mutable mode : mode
368 ; mutable uioh : uioh
369 ; mutable outlines : outline array
370 ; mutable bookmarks : outline list
371 ; mutable path : string
372 ; mutable password : string
373 ; mutable invalidated : int
374 ; mutable memused : memsize
375 ; mutable gen : gen
376 ; mutable throttle : (page list * int * float) option
377 ; mutable autoscroll : int option
378 ; mutable ghyll : int option -> unit
379 ; mutable help : helpitem array
380 ; mutable docinfo : (int * string) list
381 ; mutable deadline : float
382 ; mutable texid : GlTex.texture_id option
383 ; hists : hists
384 ; mutable prevzoom : float
385 ; mutable progress : float
387 and hists =
388 { pat : string circbuf
389 ; pag : string circbuf
390 ; nav : anchor circbuf
394 let defconf =
395 { scrollbw = 7
396 ; scrollh = 12
397 ; icase = true
398 ; preload = true
399 ; pagebias = 0
400 ; verbose = false
401 ; debug = false
402 ; scrollstep = 24
403 ; maxhfit = true
404 ; crophack = false
405 ; autoscrollstep = 2
406 ; maxwait = None
407 ; hlinks = false
408 ; underinfo = false
409 ; interpagespace = 2
410 ; zoom = 1.0
411 ; presentation = false
412 ; angle = 0
413 ; winw = 900
414 ; winh = 900
415 ; savebmarks = true
416 ; proportional = true
417 ; trimmargins = false
418 ; trimfuzz = (0,0,0,0)
419 ; memlimit = 32 lsl 20
420 ; texcount = 256
421 ; sliceheight = 24
422 ; thumbw = 76
423 ; jumpback = true
424 ; bgcolor = (0.5, 0.5, 0.5)
425 ; bedefault = false
426 ; scrollbarinpm = true
427 ; tilew = 2048
428 ; tileh = 2048
429 ; mustoresize = 128 lsl 20
430 ; checkers = true
431 ; aalevel = 8
432 ; urilauncher =
433 (match platform with
434 | Plinux | Pfreebsd | Pdragonflybsd | Popenbsd | Psun -> "xdg-open \"%s\""
435 | Posx -> "open \"%s\""
436 | Pwindows | Pcygwin | Pmingw -> "iexplore \"%s\""
437 | _ -> "")
438 ; colorspace = Rgb
439 ; invert = false
440 ; colorscale = 1.0
441 ; redirectstderr = false
442 ; ghyllscroll = None
443 ; columns = None
444 ; beyecolumns = None
448 let conf = { defconf with angle = defconf.angle };;
450 type fontstate =
451 { mutable fontsize : int
452 ; mutable wwidth : float
453 ; mutable maxrows : int
457 let fstate =
458 { fontsize = 14
459 ; wwidth = nan
460 ; maxrows = -1
464 let setfontsize n =
465 fstate.fontsize <- n;
466 fstate.wwidth <- measurestr fstate.fontsize "w";
467 fstate.maxrows <- (conf.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
470 let gotouri uri =
471 if String.length conf.urilauncher = 0
472 then print_endline uri
473 else
474 let re = Str.regexp "%s" in
475 let command = Str.global_replace re uri conf.urilauncher in
476 let optic =
477 try Some (Unix.open_process_in command)
478 with exn ->
479 Printf.eprintf
480 "failed to execute `%s': %s\n" command (Printexc.to_string exn);
481 flush stderr;
482 None
484 match optic with
485 | Some ic -> close_in ic
486 | None -> ()
489 let version () =
490 Printf.sprintf "llpp version %s (%s/%dbit, ocaml %s)" Help.version
491 (platform_to_string platform) Sys.word_size Sys.ocaml_version
494 let makehelp () =
495 let strings = version () :: "" :: Help.keys in
496 Array.of_list (
497 let r = Str.regexp "\\(http://[^ ]+\\)" in
498 List.map (fun s ->
499 if (try Str.search_forward r s 0 with Not_found -> -1) >= 0
500 then
501 let uri = Str.matched_string s in
502 (s, 0, Action (fun u -> gotouri uri; u))
503 else s, 0, Noaction) strings
507 let noghyll _ = ();;
509 let state =
510 { csock = Unix.stdin
511 ; ssock = Unix.stdin
512 ; errfd = None
513 ; stderr = Unix.stderr
514 ; errmsgs = Buffer.create 0
515 ; newerrmsgs = false
516 ; x = 0
517 ; y = 0
518 ; w = 0
519 ; scrollw = 0
520 ; hscrollh = 0
521 ; anchor = emptyanchor
522 ; layout = []
523 ; maxy = max_int
524 ; tilelru = Queue.create ()
525 ; pagemap = Hashtbl.create 10
526 ; tilemap = Hashtbl.create 10
527 ; pdims = []
528 ; pagecount = 0
529 ; currently = Idle
530 ; mstate = Mnone
531 ; rects = []
532 ; rects1 = []
533 ; text = ""
534 ; mode = View
535 ; fullscreen = None
536 ; searchpattern = ""
537 ; outlines = [||]
538 ; bookmarks = []
539 ; path = ""
540 ; password = ""
541 ; invalidated = 0
542 ; hists =
543 { nav = cbnew 10 (0, 0.0)
544 ; pat = cbnew 1 ""
545 ; pag = cbnew 1 ""
547 ; memused = 0
548 ; gen = 0
549 ; throttle = None
550 ; autoscroll = None
551 ; ghyll = noghyll
552 ; help = makehelp ()
553 ; docinfo = []
554 ; deadline = nan
555 ; texid = None
556 ; prevzoom = 1.0
557 ; progress = -1.0
558 ; uioh = nouioh
562 let vlog fmt =
563 if conf.verbose
564 then
565 Printf.kprintf prerr_endline fmt
566 else
567 Printf.kprintf ignore fmt
570 let redirectstderr () =
571 if conf.redirectstderr
572 then
573 let rfd, wfd = Unix.pipe () in
574 state.stderr <- Unix.dup Unix.stderr;
575 state.errfd <- Some rfd;
576 Unix.dup2 wfd Unix.stderr;
577 else (
578 state.newerrmsgs <- false;
579 begin match state.errfd with
580 | Some fd ->
581 Unix.close fd;
582 Unix.dup2 state.stderr Unix.stderr;
583 state.errfd <- None;
584 | None -> ()
585 end;
586 prerr_string (Buffer.contents state.errmsgs);
587 flush stderr;
588 Buffer.clear state.errmsgs;
592 module G =
593 struct
594 let postRedisplay who =
595 if conf.verbose
596 then prerr_endline ("redisplay for " ^ who);
597 Glut.postRedisplay ();
599 end;;
601 let addchar s c =
602 let b = Buffer.create (String.length s + 1) in
603 Buffer.add_string b s;
604 Buffer.add_char b c;
605 Buffer.contents b;
608 let colorspace_of_string s =
609 match String.lowercase s with
610 | "rgb" -> Rgb
611 | "bgr" -> Bgr
612 | "gray" -> Gray
613 | _ -> failwith "invalid colorspace"
616 let int_of_colorspace = function
617 | Rgb -> 0
618 | Bgr -> 1
619 | Gray -> 2
622 let colorspace_of_int = function
623 | 0 -> Rgb
624 | 1 -> Bgr
625 | 2 -> Gray
626 | n -> failwith ("invalid colorspace index " ^ string_of_int n)
629 let colorspace_to_string = function
630 | Rgb -> "rgb"
631 | Bgr -> "bgr"
632 | Gray -> "gray"
635 let intentry_with_suffix text key =
636 let c = Char.unsafe_chr key in
637 match Char.lowercase c with
638 | '0' .. '9' ->
639 let text = addchar text c in
640 TEcont text
642 | 'k' | 'm' | 'g' ->
643 let text = addchar text c in
644 TEcont text
646 | _ ->
647 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
648 TEcont text
651 let columns_to_string (n, a, b) =
652 if a = 0 && b = 0
653 then Printf.sprintf "%d" n
654 else Printf.sprintf "%d,%d,%d" n a b;
657 let columns_of_string s =
659 (int_of_string s, 0, 0)
660 with _ ->
661 Scanf.sscanf s "%u,%u,%u" (fun n a b -> (n, a, b));
664 let writecmd fd s =
665 let len = String.length s in
666 let n = 4 + len in
667 let b = Buffer.create n in
668 Buffer.add_char b (Char.chr ((len lsr 24) land 0xff));
669 Buffer.add_char b (Char.chr ((len lsr 16) land 0xff));
670 Buffer.add_char b (Char.chr ((len lsr 8) land 0xff));
671 Buffer.add_char b (Char.chr ((len lsr 0) land 0xff));
672 Buffer.add_string b s;
673 let s' = Buffer.contents b in
674 let n' = Unix.write fd s' 0 n in
675 if n' != n then failwith "write failed";
678 let readcmd fd =
679 let s = "xxxx" in
680 let n = Unix.read fd s 0 4 in
681 if n != 4 then failwith "incomplete read(len)";
682 let len = 0
683 lor (Char.code s.[0] lsl 24)
684 lor (Char.code s.[1] lsl 16)
685 lor (Char.code s.[2] lsl 8)
686 lor (Char.code s.[3] lsl 0)
688 let s = String.create len in
689 let n = Unix.read fd s 0 len in
690 if n != len then failwith "incomplete read(data)";
694 let makecmd s l =
695 let b = Buffer.create 10 in
696 Buffer.add_string b s;
697 let rec combine = function
698 | [] -> b
699 | x :: xs ->
700 Buffer.add_char b ' ';
701 let s =
702 match x with
703 | `b b -> if b then "1" else "0"
704 | `s s -> s
705 | `i i -> string_of_int i
706 | `f f -> string_of_float f
707 | `I f -> string_of_int (truncate f)
709 Buffer.add_string b s;
710 combine xs;
712 combine l;
715 let wcmd s l =
716 let cmd = Buffer.contents (makecmd s l) in
717 writecmd state.csock cmd;
720 let calcips h =
721 if conf.presentation
722 then
723 let d = conf.winh - h in
724 max 0 ((d + 1) / 2)
725 else
726 conf.interpagespace
729 let calcheight () =
730 let rec f pn ph pi fh l =
731 match l with
732 | (n, _, h, _) :: rest ->
733 let ips = calcips h in
734 let fh =
735 if conf.presentation
736 then fh+ips
737 else (
738 if isbirdseye state.mode && pn = 0
739 then fh + ips
740 else fh
743 let fh = fh + ((n - pn) * (ph + pi)) in
744 f n h ips fh rest;
746 | [] ->
747 let inc =
748 if conf.presentation || (isbirdseye state.mode && pn = 0)
749 then 0
750 else -pi
752 let fh = fh + ((state.pagecount - pn) * (ph + pi)) + inc in
753 max 0 fh
755 let fh = f 0 0 0 0 state.pdims in
759 let calcheight () =
760 match conf.columns with
761 | None -> calcheight ()
762 | Some (_, b) ->
763 if Array.length b > 0
764 then
765 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
766 y + h
767 else 0
770 let getpageyh pageno =
771 let rec f pn ph pi y l =
772 match l with
773 | (n, _, h, _) :: rest ->
774 let ips = calcips h in
775 if n >= pageno
776 then
777 let h = if n = pageno then h else ph in
778 if conf.presentation && n = pageno
779 then
780 y + (pageno - pn) * (ph + pi) + pi, h
781 else
782 y + (pageno - pn) * (ph + pi), h
783 else
784 let y = y + (if conf.presentation then pi else 0) in
785 let y = y + (n - pn) * (ph + pi) in
786 f n h ips y rest
788 | [] ->
789 y + (pageno - pn) * (ph + pi), ph
791 f 0 0 0 0 state.pdims
794 let getpageyh pageno =
795 match conf.columns with
796 | None -> getpageyh pageno
797 | Some (_, b) ->
798 let (_, _, y, (_, _, h, _)) = b.(pageno) in
799 y, h
802 let getpagedim pageno =
803 let rec f ppdim l =
804 match l with
805 | (n, _, _, _) as pdim :: rest ->
806 if n >= pageno
807 then (if n = pageno then pdim else ppdim)
808 else f pdim rest
810 | [] -> ppdim
812 f (-1, -1, -1, -1) state.pdims
815 let getpagey pageno = fst (getpageyh pageno);;
817 let layout1 y sh =
818 let sh = sh - state.hscrollh in
819 let rec f ~pageno ~pdimno ~prev ~py ~dy ~pdims ~accu =
820 let ((w, h, ips, xoff) as curr), rest, pdimno, yinc =
821 match pdims with
822 | (pageno', w, h, xoff) :: rest when pageno' = pageno ->
823 let ips = calcips h in
824 let yinc =
825 if conf.presentation || (isbirdseye state.mode && pageno = 0)
826 then ips
827 else 0
829 (w, h, ips, xoff), rest, pdimno + 1, yinc
830 | _ ->
831 prev, pdims, pdimno, 0
833 let dy = dy + yinc in
834 let py = py + yinc in
835 if pageno = state.pagecount || dy >= sh
836 then
837 accu
838 else
839 let vy = y + dy in
840 if py + h <= vy - yinc
841 then
842 let py = py + h + ips in
843 let dy = max 0 (py - y) in
844 f ~pageno:(pageno+1)
845 ~pdimno
846 ~prev:curr
849 ~pdims:rest
850 ~accu
851 else
852 let pagey = vy - py in
853 let pagevh = h - pagey in
854 let pagevh = min (sh - dy) pagevh in
855 let off = if yinc > 0 then py - vy else 0 in
856 let py = py + h + ips in
857 let pagex, dx =
858 let xoff = xoff +
859 if state.w < conf.winw - state.scrollw
860 then (conf.winw - state.scrollw - state.w) / 2
861 else 0
863 let dispx = xoff + state.x in
864 if dispx < 0
865 then (-dispx, 0)
866 else (0, dispx)
868 let pagevw =
869 let lw = w - pagex in
870 min lw (conf.winw - state.scrollw)
872 let e =
873 { pageno = pageno
874 ; pagedimno = pdimno
875 ; pagew = w
876 ; pageh = h
877 ; pagex = pagex
878 ; pagey = pagey + off
879 ; pagevw = pagevw
880 ; pagevh = pagevh - off
881 ; pagedispx = dx
882 ; pagedispy = dy + off
885 let accu = e :: accu in
886 f ~pageno:(pageno+1)
887 ~pdimno
888 ~prev:curr
890 ~dy:(dy+pagevh+ips)
891 ~pdims:rest
892 ~accu
894 if state.invalidated = 0
895 then (
896 let accu =
898 ~pageno:0
899 ~pdimno:~-1
900 ~prev:(0,0,0,0)
901 ~py:0
902 ~dy:0
903 ~pdims:state.pdims
904 ~accu:[]
906 List.rev accu
908 else
912 let layoutN ((columns, coverA, coverB), b) y sh =
913 let sh = sh - state.hscrollh in
914 let rec fold accu n =
915 if n = Array.length b
916 then accu
917 else
918 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
919 if (vy - y) > sh &&
920 (n = coverA - 1
921 || n = state.pagecount - coverB
922 || (n - coverA) mod columns = columns - 1)
923 then accu
924 else
925 let accu =
926 if vy + h > y
927 then
928 let pagey = max 0 (y - vy) in
929 let pagedispy = if pagey > 0 then 0 else vy - y in
930 let pagedispx, pagex, pagevw =
931 let pdx =
932 if n = coverA - 1 || n = state.pagecount - coverB
933 then state.x + (conf.winw - state.scrollw - w) / 2
934 else dx + xoff + state.x
936 if pdx < 0
937 then 0, -pdx, w + pdx
938 else pdx, 0, min (conf.winw - state.scrollw) w
940 let pagevh = min (h - pagey) (sh - pagedispy) in
941 if pagedispx < conf.winw - state.scrollw && pagevw > 0 && pagevh > 0
942 then
943 let e =
944 { pageno = n
945 ; pagedimno = pdimno
946 ; pagew = w
947 ; pageh = h
948 ; pagex = pagex
949 ; pagey = pagey
950 ; pagevw = pagevw
951 ; pagevh = pagevh
952 ; pagedispx = pagedispx
953 ; pagedispy = pagedispy
956 e :: accu
957 else
958 accu
959 else
960 accu
962 fold accu (n+1)
964 if state.invalidated = 0
965 then List.rev (fold [] 0)
966 else []
969 let layout y sh =
970 match conf.columns with
971 | None -> layout1 y sh
972 | Some c -> layoutN c y sh
975 let clamp incr =
976 let y = state.y + incr in
977 let y = max 0 y in
978 let y = min y (state.maxy - (if conf.maxhfit then conf.winh else 0)) in
982 let getopaque pageno =
983 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
984 with Not_found -> None
987 let putopaque pageno opaque =
988 Hashtbl.replace state.pagemap (pageno, state.gen) opaque
991 let itertiles l f =
992 let tilex = l.pagex mod conf.tilew in
993 let tiley = l.pagey mod conf.tileh in
995 let col = l.pagex / conf.tilew in
996 let row = l.pagey / conf.tileh in
998 let vw =
999 let a = l.pagew - l.pagex in
1000 let b = conf.winw - state.scrollw in
1001 min a b
1002 and vh = l.pagevh in
1004 let rec rowloop row y0 dispy h =
1005 if h = 0
1006 then ()
1007 else (
1008 let dh = conf.tileh - y0 in
1009 let dh = min h dh in
1010 let rec colloop col x0 dispx w =
1011 if w = 0
1012 then ()
1013 else (
1014 let dw = conf.tilew - x0 in
1015 let dw = min w dw in
1017 f col row dispx dispy x0 y0 dw dh;
1018 colloop (col+1) 0 (dispx+dw) (w-dw)
1021 colloop col tilex l.pagedispx vw;
1022 rowloop (row+1) 0 (dispy+dh) (h-dh)
1025 if vw > 0 && vh > 0
1026 then rowloop row tiley l.pagedispy vh;
1029 let gettileopaque l col row =
1030 let key =
1031 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
1033 try Some (Hashtbl.find state.tilemap key)
1034 with Not_found -> None
1037 let puttileopaque l col row gen colorspace angle opaque size elapsed =
1038 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
1039 Hashtbl.add state.tilemap key (opaque, size, elapsed)
1042 let drawtiles l color =
1043 GlDraw.color color;
1044 let f col row x y tilex tiley w h =
1045 match gettileopaque l col row with
1046 | Some (opaque, _, t) ->
1047 let params = x, y, w, h, tilex, tiley in
1048 if conf.invert
1049 then (
1050 Gl.enable `blend;
1051 GlFunc.blend_func `zero `one_minus_src_color;
1053 drawtile params opaque;
1054 if conf.invert
1055 then Gl.disable `blend;
1056 if conf.debug
1057 then (
1058 let s = Printf.sprintf
1059 "%d[%d,%d] %f sec"
1060 l.pageno col row t
1062 let w = measurestr fstate.fontsize s in
1063 GlMisc.push_attrib [`current];
1064 GlDraw.color (0.0, 0.0, 0.0);
1065 GlDraw.rect
1066 (float (x-2), float (y-2))
1067 (float (x+2) +. w, float (y + fstate.fontsize + 2));
1068 GlDraw.color (1.0, 1.0, 1.0);
1069 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
1070 GlMisc.pop_attrib ();
1073 | _ ->
1074 let w =
1075 let lw = conf.winw - state.scrollw - x in
1076 min lw w
1077 and h =
1078 let lh = conf.winh - y in
1079 min lh h
1081 Gl.enable `texture_2d;
1082 begin match state.texid with
1083 | Some id ->
1084 GlTex.bind_texture `texture_2d id;
1085 let x0 = float x
1086 and y0 = float y
1087 and x1 = float (x+w)
1088 and y1 = float (y+h) in
1090 let tw = float w /. 64.0
1091 and th = float h /. 64.0 in
1092 let tx0 = float tilex /. 64.0
1093 and ty0 = float tiley /. 64.0 in
1094 let tx1 = tx0 +. tw
1095 and ty1 = ty0 +. th in
1096 GlDraw.begins `quads;
1097 GlTex.coord2 (tx0, ty0); GlDraw.vertex2 (x0, y0);
1098 GlTex.coord2 (tx0, ty1); GlDraw.vertex2 (x0, y1);
1099 GlTex.coord2 (tx1, ty1); GlDraw.vertex2 (x1, y1);
1100 GlTex.coord2 (tx1, ty0); GlDraw.vertex2 (x1, y0);
1101 GlDraw.ends ();
1103 Gl.disable `texture_2d;
1104 | None ->
1105 GlDraw.color (1.0, 1.0, 1.0);
1106 GlDraw.rect
1107 (float x, float y)
1108 (float (x+w), float (y+h));
1109 end;
1110 if w > 128 && h > fstate.fontsize + 10
1111 then (
1112 GlDraw.color (0.0, 0.0, 0.0);
1113 let c, r =
1114 if conf.verbose
1115 then (col*conf.tilew, row*conf.tileh)
1116 else col, row
1118 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
1120 GlDraw.color color;
1122 itertiles l f
1125 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
1127 let tilevisible1 l x y =
1128 let ax0 = l.pagex
1129 and ax1 = l.pagex + l.pagevw
1130 and ay0 = l.pagey
1131 and ay1 = l.pagey + l.pagevh in
1133 let bx0 = x
1134 and by0 = y in
1135 let bx1 = min (bx0 + conf.tilew) l.pagew
1136 and by1 = min (by0 + conf.tileh) l.pageh in
1138 let rx0 = max ax0 bx0
1139 and ry0 = max ay0 by0
1140 and rx1 = min ax1 bx1
1141 and ry1 = min ay1 by1 in
1143 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
1144 nonemptyintersection
1147 let tilevisible layout n x y =
1148 let rec findpageinlayout = function
1149 | l :: _ when l.pageno = n -> tilevisible1 l x y
1150 | _ :: rest -> findpageinlayout rest
1151 | [] -> false
1153 findpageinlayout layout
1156 let tileready l x y =
1157 tilevisible1 l x y &&
1158 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
1161 let tilepage n p layout =
1162 let rec loop = function
1163 | l :: rest ->
1164 if l.pageno = n
1165 then
1166 let f col row _ _ _ _ _ _ =
1167 if state.currently = Idle
1168 then
1169 match gettileopaque l col row with
1170 | Some _ -> ()
1171 | None ->
1172 let x = col*conf.tilew
1173 and y = row*conf.tileh in
1174 let w =
1175 let w = l.pagew - x in
1176 min w conf.tilew
1178 let h =
1179 let h = l.pageh - y in
1180 min h conf.tileh
1182 wcmd "tile"
1183 [`s p
1184 ;`i x
1185 ;`i y
1186 ;`i w
1187 ;`i h
1189 state.currently <-
1190 Tiling (
1191 l, p, conf.colorspace, conf.angle, state.gen, col, row,
1192 conf.tilew, conf.tileh
1195 itertiles l f;
1196 else
1197 loop rest
1199 | [] -> ()
1201 if state.invalidated = 0 then loop layout;
1204 let preloadlayout visiblepages =
1205 let presentation = conf.presentation in
1206 let interpagespace = conf.interpagespace in
1207 let maxy = state.maxy in
1208 conf.presentation <- false;
1209 conf.interpagespace <- 0;
1210 state.maxy <- calcheight ();
1211 let y =
1212 match visiblepages with
1213 | [] -> 0
1214 | l :: _ -> getpagey l.pageno + l.pagey
1216 let y = if y < conf.winh then 0 else y - conf.winh in
1217 let h = state.y - y + conf.winh*3 in
1218 let pages = layout y h in
1219 conf.presentation <- presentation;
1220 conf.interpagespace <- interpagespace;
1221 state.maxy <- maxy;
1222 pages;
1225 let load pages =
1226 let rec loop pages =
1227 if state.currently != Idle
1228 then ()
1229 else
1230 match pages with
1231 | l :: rest ->
1232 begin match getopaque l.pageno with
1233 | None ->
1234 wcmd "page" [`i l.pageno; `i l.pagedimno];
1235 state.currently <- Loading (l, state.gen);
1236 | Some opaque ->
1237 tilepage l.pageno opaque pages;
1238 loop rest
1239 end;
1240 | _ -> ()
1242 if state.invalidated = 0 then loop pages
1245 let preload pages =
1246 load pages;
1247 if conf.preload && state.currently = Idle
1248 then load (preloadlayout pages);
1251 let layoutready layout =
1252 let rec fold all ls =
1253 all && match ls with
1254 | l :: rest ->
1255 let seen = ref false in
1256 let allvisible = ref true in
1257 let foo col row _ _ _ _ _ _ =
1258 seen := true;
1259 allvisible := !allvisible &&
1260 begin match gettileopaque l col row with
1261 | Some _ -> true
1262 | None -> false
1265 itertiles l foo;
1266 fold (!seen && !allvisible) rest
1267 | [] -> true
1269 let alltilesvisible = fold true layout in
1270 alltilesvisible;
1273 let gotoy y =
1274 let y = bound y 0 state.maxy in
1275 let y, layout, proceed =
1276 match conf.maxwait with
1277 | Some time when state.ghyll == noghyll ->
1278 begin match state.throttle with
1279 | None ->
1280 let layout = layout y conf.winh in
1281 let ready = layoutready layout in
1282 if not ready
1283 then (
1284 load layout;
1285 state.throttle <- Some (layout, y, now ());
1287 else G.postRedisplay "gotoy showall (None)";
1288 y, layout, ready
1289 | Some (_, _, started) ->
1290 let dt = now () -. started in
1291 if dt > time
1292 then (
1293 state.throttle <- None;
1294 let layout = layout y conf.winh in
1295 load layout;
1296 G.postRedisplay "maxwait";
1297 y, layout, true
1299 else -1, [], false
1302 | _ ->
1303 let layout = layout y conf.winh in
1304 if true || layoutready layout
1305 then G.postRedisplay "gotoy ready";
1306 y, layout, true
1308 if proceed
1309 then (
1310 state.y <- y;
1311 state.layout <- layout;
1312 begin match state.mode with
1313 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
1314 if not (pagevisible layout pageno)
1315 then (
1316 match state.layout with
1317 | [] -> ()
1318 | l :: _ ->
1319 state.mode <- Birdseye (
1320 conf, leftx, l.pageno, hooverpageno, anchor
1323 | _ -> ()
1324 end;
1325 preload layout;
1327 state.ghyll <- noghyll;
1330 let conttiling pageno opaque =
1331 tilepage pageno opaque
1332 (if conf.preload then preloadlayout state.layout else state.layout)
1335 let gotoy_and_clear_text y =
1336 gotoy y;
1337 if not conf.verbose then state.text <- "";
1340 let getanchor () =
1341 match state.layout with
1342 | [] -> emptyanchor
1343 | l :: _ -> (l.pageno, float l.pagey /. float l.pageh)
1346 let getanchory (n, top) =
1347 let y, h = getpageyh n in
1348 y + (truncate (top *. float h));
1351 let gotoanchor anchor =
1352 gotoy (getanchory anchor);
1355 let addnav () =
1356 cbput state.hists.nav (getanchor ());
1359 let getnav dir =
1360 let anchor = cbgetc state.hists.nav dir in
1361 getanchory anchor;
1364 let gotoghyll y =
1365 let rec scroll f n a b =
1366 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
1367 let snake f a b =
1368 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
1369 if f < a
1370 then s (float f /. float a)
1371 else (
1372 if f > b
1373 then 1.0 -. s ((float (f-b) /. float (n-b)))
1374 else 1.0
1377 snake f a b
1378 and summa f n a b =
1379 (* courtesy:
1380 http://integrals.wolfram.com/index.jsp?expr=3x%5E2-2x%5E3&random=false *)
1381 let iv x = -.((-.2.0 +. x)*.x**3.0)/.2.0 in
1382 let iv1 = iv f in
1383 let ins = float a *. iv1
1384 and outs = float (n-b) *. iv1 in
1385 let ones = b - a in
1386 ins +. outs +. float ones
1388 let rec set (_N, _A, _B) y sy =
1389 let sum = summa 1.0 _N _A _B in
1390 let dy = float (y - sy) in
1391 state.ghyll <- (
1392 let rec gf n y1 o =
1393 if n >= _N
1394 then state.ghyll <- noghyll
1395 else
1396 let go n =
1397 let s = scroll n _N _A _B in
1398 let y1 = y1 +. ((s *. dy) /. sum) in
1399 gotoy_and_clear_text (truncate y1);
1400 state.ghyll <- gf (n+1) y1;
1402 match o with
1403 | None -> go n
1404 | Some y' -> set (_N/2, 0, 0) y' state.y
1406 gf 0 (float state.y)
1409 match conf.ghyllscroll with
1410 | None ->
1411 gotoy_and_clear_text y
1412 | Some nab ->
1413 if state.ghyll == noghyll
1414 then set nab y state.y
1415 else state.ghyll (Some y)
1418 let gotopage n top =
1419 let y, h = getpageyh n in
1420 let y = y + (truncate (top *. float h)) in
1421 gotoghyll y
1424 let gotopage1 n top =
1425 let y = getpagey n in
1426 let y = y + top in
1427 gotoghyll y
1430 let invalidate () =
1431 state.layout <- [];
1432 state.pdims <- [];
1433 state.rects <- [];
1434 state.rects1 <- [];
1435 state.invalidated <- state.invalidated + 1;
1438 let writeopen path password =
1439 writecmd state.csock ("open " ^ path ^ "\000" ^ password ^ "\000");
1442 let opendoc path password =
1443 invalidate ();
1444 state.path <- path;
1445 state.password <- password;
1446 state.gen <- state.gen + 1;
1447 state.docinfo <- [];
1449 setaalevel conf.aalevel;
1450 writeopen path password;
1451 Glut.setWindowTitle ("llpp " ^ Filename.basename path);
1452 wcmd "geometry" [`i state.w; `i conf.winh];
1455 let scalecolor c =
1456 let c = c *. conf.colorscale in
1457 (c, c, c);
1460 let scalecolor2 (r, g, b) =
1461 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
1464 let represent () =
1465 let docolumns = function
1466 | None -> ()
1467 | Some ((columns, coverA, coverB), _) ->
1468 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1469 let rec loop pageno pdimno pdim x y rowh pdims =
1470 if pageno = state.pagecount
1471 then ()
1472 else
1473 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1474 match pdims with
1475 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1476 pdimno+1, pdim, rest
1477 | _ ->
1478 pdimno, pdim, pdims
1480 let x, y, rowh' =
1481 if pageno = coverA - 1 || pageno = state.pagecount - coverB
1482 then (
1483 (conf.winw - state.scrollw - w) / 2,
1484 y + rowh + conf.interpagespace, h
1486 else (
1487 if (pageno - coverA) mod columns = 0
1488 then 0, y + rowh + conf.interpagespace, h
1489 else x, y, max rowh h
1492 let rec fixrow m = if m = pageno then () else
1493 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
1494 if h < rowh
1495 then (
1496 let y = y + (rowh - h) / 2 in
1497 a.(m) <- (pdimno, x, y, pdim);
1499 fixrow (m+1)
1501 if pageno > 1 && (pageno - coverA) mod columns = 0
1502 then fixrow (pageno - columns);
1503 a.(pageno) <- (pdimno, x, y, pdim);
1504 let x = x + w + xoff*2 + conf.interpagespace in
1505 loop (pageno+1) pdimno pdim x y rowh' pdims
1507 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
1508 conf.columns <- Some ((columns, coverA, coverB), a);
1510 docolumns conf.columns;
1511 state.maxy <- calcheight ();
1512 state.hscrollh <-
1513 if state.w <= conf.winw - state.scrollw
1514 then 0
1515 else state.scrollw
1517 match state.mode with
1518 | Birdseye (_, _, pageno, _, _) ->
1519 let y, h = getpageyh pageno in
1520 let top = (conf.winh - h) / 2 in
1521 gotoy (max 0 (y - top))
1522 | _ -> gotoanchor state.anchor
1525 let reshape =
1526 let firsttime = ref true in
1527 fun ~w ~h ->
1528 GlDraw.viewport 0 0 w h;
1529 if state.invalidated = 0 && not !firsttime
1530 then state.anchor <- getanchor ();
1532 firsttime := false;
1533 conf.winw <- w;
1534 let w = truncate (float w *. conf.zoom) - state.scrollw in
1535 let w = max w 2 in
1536 state.w <- w;
1537 conf.winh <- h;
1538 setfontsize fstate.fontsize;
1539 GlMat.mode `modelview;
1540 GlMat.load_identity ();
1542 GlMat.mode `projection;
1543 GlMat.load_identity ();
1544 GlMat.rotate ~x:1.0 ~angle:180.0 ();
1545 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
1546 GlMat.scale3 (2.0 /. float conf.winw, 2.0 /. float conf.winh, 1.0);
1548 let w =
1549 match conf.columns with
1550 | None -> w
1551 | Some ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
1553 invalidate ();
1554 wcmd "geometry" [`i w; `i h];
1557 let enttext () =
1558 let len = String.length state.text in
1559 let drawstring s =
1560 let hscrollh =
1561 match state.mode with
1562 | View -> state.hscrollh
1563 | _ -> 0
1565 let rect x w =
1566 GlDraw.rect
1567 (x, float (conf.winh - (fstate.fontsize + 4) - hscrollh))
1568 (x+.w, float (conf.winh - hscrollh))
1571 let w = float (conf.winw - state.scrollw - 1) in
1572 if state.progress >= 0.0 && state.progress < 1.0
1573 then (
1574 GlDraw.color (0.3, 0.3, 0.3);
1575 let w1 = w *. state.progress in
1576 rect 0.0 w1;
1577 GlDraw.color (0.0, 0.0, 0.0);
1578 rect w1 (w-.w1)
1580 else (
1581 GlDraw.color (0.0, 0.0, 0.0);
1582 rect 0.0 w;
1585 GlDraw.color (1.0, 1.0, 1.0);
1586 drawstring fstate.fontsize
1587 (if len > 0 then 8 else 2) (conf.winh - hscrollh - 5) s;
1589 let s =
1590 match state.mode with
1591 | Textentry ((prefix, text, _, _, _), _) ->
1592 let s =
1593 if len > 0
1594 then
1595 Printf.sprintf "%s%s_ [%s]" prefix text state.text
1596 else
1597 Printf.sprintf "%s%s_" prefix text
1601 | _ -> state.text
1603 let s =
1604 if state.newerrmsgs
1605 then (
1606 if not (istextentry state.mode)
1607 then
1608 let s1 = "(press 'e' to review error messasges)" in
1609 if String.length s > 0 then s ^ " " ^ s1 else s1
1610 else s
1612 else s
1614 if String.length s > 0
1615 then drawstring s
1618 let showtext c s =
1619 state.text <- Printf.sprintf "%c%s" c s;
1620 G.postRedisplay "showtext";
1623 let gctiles () =
1624 let len = Queue.length state.tilelru in
1625 let rec loop qpos =
1626 if state.memused <= conf.memlimit
1627 then ()
1628 else (
1629 if qpos < len
1630 then
1631 let (k, p, s) as lruitem = Queue.pop state.tilelru in
1632 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
1633 let (_, pw, ph, _) = getpagedim n in
1635 gen = state.gen
1636 && colorspace = conf.colorspace
1637 && angle = conf.angle
1638 && pagew = pw
1639 && pageh = ph
1640 && (
1641 let layout =
1642 match state.throttle with
1643 | None ->
1644 if conf.preload
1645 then preloadlayout state.layout
1646 else state.layout
1647 | Some (layout, _, _) ->
1648 layout
1650 let x = col*conf.tilew
1651 and y = row*conf.tileh in
1652 tilevisible layout n x y
1654 then Queue.push lruitem state.tilelru
1655 else (
1656 wcmd "freetile" [`s p];
1657 state.memused <- state.memused - s;
1658 state.uioh#infochanged Memused;
1659 Hashtbl.remove state.tilemap k;
1661 loop (qpos+1)
1664 loop 0
1667 let flushtiles () =
1668 Queue.iter (fun (k, p, s) ->
1669 wcmd "freetile" [`s p];
1670 state.memused <- state.memused - s;
1671 state.uioh#infochanged Memused;
1672 Hashtbl.remove state.tilemap k;
1673 ) state.tilelru;
1674 Queue.clear state.tilelru;
1675 load state.layout;
1678 let logcurrently = function
1679 | Idle -> dolog "Idle"
1680 | Loading (l, gen) ->
1681 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
1682 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
1683 dolog
1684 "Tiling %d[%d,%d] page=%s cs=%s angle"
1685 l.pageno col row pageopaque
1686 (colorspace_to_string colorspace)
1688 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
1689 angle gen conf.angle state.gen
1690 tilew tileh
1691 conf.tilew conf.tileh
1693 | Outlining _ ->
1694 dolog "outlining"
1697 let act cmds =
1698 (* dolog "%S" cmds; *)
1699 let op, args =
1700 let spacepos =
1701 try String.index cmds ' '
1702 with Not_found -> -1
1704 if spacepos = -1
1705 then cmds, ""
1706 else
1707 let l = String.length cmds in
1708 let op = String.sub cmds 0 spacepos in
1709 op, begin
1710 if l - spacepos < 2 then ""
1711 else String.sub cmds (spacepos+1) (l-spacepos-1)
1714 match op with
1715 | "clear" ->
1716 state.uioh#infochanged Pdim;
1717 state.pdims <- [];
1719 | "clearrects" ->
1720 state.rects <- state.rects1;
1721 G.postRedisplay "clearrects";
1723 | "continue" ->
1724 let n =
1725 try Scanf.sscanf args "%u" (fun n -> n)
1726 with exn ->
1727 dolog "error processing 'continue' %S: %s"
1728 cmds (Printexc.to_string exn);
1729 exit 1;
1731 state.pagecount <- n;
1732 state.invalidated <- state.invalidated - 1;
1733 begin match state.currently with
1734 | Outlining l ->
1735 state.currently <- Idle;
1736 state.outlines <- Array.of_list (List.rev l)
1737 | _ -> ()
1738 end;
1739 if state.invalidated = 0
1740 then represent ();
1741 if conf.maxwait = None
1742 then G.postRedisplay "continue";
1744 | "title" ->
1745 Glut.setWindowTitle args
1747 | "msg" ->
1748 showtext ' ' args
1750 | "vmsg" ->
1751 if conf.verbose
1752 then showtext ' ' args
1754 | "progress" ->
1755 let progress, text =
1757 Scanf.sscanf args "%f %n"
1758 (fun f pos ->
1759 f, String.sub args pos (String.length args - pos))
1760 with exn ->
1761 dolog "error processing 'progress' %S: %s"
1762 cmds (Printexc.to_string exn);
1763 exit 1;
1765 state.text <- text;
1766 state.progress <- progress;
1767 G.postRedisplay "progress"
1769 | "firstmatch" ->
1770 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1772 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
1773 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1774 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1775 with exn ->
1776 dolog "error processing 'firstmatch' %S: %s"
1777 cmds (Printexc.to_string exn);
1778 exit 1;
1780 let y = (getpagey pageno) + truncate y0 in
1781 addnav ();
1782 gotoy y;
1783 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
1785 | "match" ->
1786 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1788 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
1789 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1790 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1791 with exn ->
1792 dolog "error processing 'match' %S: %s"
1793 cmds (Printexc.to_string exn);
1794 exit 1;
1796 state.rects1 <-
1797 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
1799 | "page" ->
1800 let pageopaque, t =
1802 Scanf.sscanf args "%s %f" (fun p t -> p, t)
1803 with exn ->
1804 dolog "error processing 'page' %S: %s"
1805 cmds (Printexc.to_string exn);
1806 exit 1;
1808 begin match state.currently with
1809 | Loading (l, gen) ->
1810 vlog "page %d took %f sec" l.pageno t;
1811 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
1812 begin match state.throttle with
1813 | None ->
1814 let preloadedpages =
1815 if conf.preload
1816 then preloadlayout state.layout
1817 else state.layout
1819 let evict () =
1820 let module IntSet =
1821 Set.Make (struct type t = int let compare = (-) end) in
1822 let set =
1823 List.fold_left (fun s l -> IntSet.add l.pageno s)
1824 IntSet.empty preloadedpages
1826 let evictedpages =
1827 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
1828 if not (IntSet.mem pageno set)
1829 then (
1830 wcmd "freepage" [`s opaque];
1831 key :: accu
1833 else accu
1834 ) state.pagemap []
1836 List.iter (Hashtbl.remove state.pagemap) evictedpages;
1838 evict ();
1839 state.currently <- Idle;
1840 if gen = state.gen
1841 then (
1842 tilepage l.pageno pageopaque state.layout;
1843 load state.layout;
1844 load preloadedpages;
1845 if pagevisible state.layout l.pageno
1846 && layoutready state.layout
1847 then G.postRedisplay "page";
1850 | Some (layout, _, _) ->
1851 state.currently <- Idle;
1852 tilepage l.pageno pageopaque layout;
1853 load state.layout
1854 end;
1856 | _ ->
1857 dolog "Inconsistent loading state";
1858 logcurrently state.currently;
1859 raise Quit;
1862 | "tile" ->
1863 let (x, y, opaque, size, t) =
1865 Scanf.sscanf args "%u %u %s %u %f"
1866 (fun x y p size t -> (x, y, p, size, t))
1867 with exn ->
1868 dolog "error processing 'tile' %S: %s"
1869 cmds (Printexc.to_string exn);
1870 exit 1;
1872 begin match state.currently with
1873 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
1874 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
1876 if tilew != conf.tilew || tileh != conf.tileh
1877 then (
1878 wcmd "freetile" [`s opaque];
1879 state.currently <- Idle;
1880 load state.layout;
1882 else (
1883 puttileopaque l col row gen cs angle opaque size t;
1884 state.memused <- state.memused + size;
1885 state.uioh#infochanged Memused;
1886 gctiles ();
1887 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
1888 opaque, size) state.tilelru;
1890 let layout =
1891 match state.throttle with
1892 | None -> state.layout
1893 | Some (layout, _, _) -> layout
1896 state.currently <- Idle;
1897 if gen = state.gen
1898 && conf.colorspace = cs
1899 && conf.angle = angle
1900 && tilevisible layout l.pageno x y
1901 then conttiling l.pageno pageopaque;
1903 begin match state.throttle with
1904 | None ->
1905 preload state.layout;
1906 if gen = state.gen
1907 && conf.colorspace = cs
1908 && conf.angle = angle
1909 && tilevisible state.layout l.pageno x y
1910 then G.postRedisplay "tile nothrottle";
1912 | Some (layout, y, _) ->
1913 let ready = layoutready layout in
1914 if ready
1915 then (
1916 state.y <- y;
1917 state.layout <- layout;
1918 state.throttle <- None;
1919 G.postRedisplay "throttle";
1921 else load layout;
1922 end;
1925 | _ ->
1926 dolog "Inconsistent tiling state";
1927 logcurrently state.currently;
1928 raise Quit;
1931 | "pdim" ->
1932 let pdim =
1934 Scanf.sscanf args "%u %u %u %u" (fun n w h x -> n, w, h, x)
1935 with exn ->
1936 dolog "error processing 'pdim' %S: %s"
1937 cmds (Printexc.to_string exn);
1938 exit 1;
1940 state.uioh#infochanged Pdim;
1941 state.pdims <- pdim :: state.pdims
1943 | "o" ->
1944 let (l, n, t, h, pos) =
1946 Scanf.sscanf args "%u %u %d %u %n"
1947 (fun l n t h pos -> l, n, t, h, pos)
1948 with exn ->
1949 dolog "error processing 'o' %S: %s"
1950 cmds (Printexc.to_string exn);
1951 exit 1;
1953 let s = String.sub args pos (String.length args - pos) in
1954 let outline = (s, l, (n, float t /. float h)) in
1955 begin match state.currently with
1956 | Outlining outlines ->
1957 state.currently <- Outlining (outline :: outlines)
1958 | Idle ->
1959 state.currently <- Outlining [outline]
1960 | currently ->
1961 dolog "invalid outlining state";
1962 logcurrently currently
1965 | "info" ->
1966 state.docinfo <- (1, args) :: state.docinfo
1968 | "infoend" ->
1969 state.uioh#infochanged Docinfo;
1970 state.docinfo <- List.rev state.docinfo
1972 | _ ->
1973 dolog "unknown cmd `%S'" cmds
1976 let idle () =
1977 if state.deadline == nan then state.deadline <- now ();
1978 let r =
1979 match state.errfd with
1980 | None -> [state.csock]
1981 | Some fd -> [state.csock; fd]
1983 let rec loop delay =
1984 let deadline =
1985 if state.ghyll == noghyll
1986 then state.deadline
1987 else now () +. 0.02
1989 let timeout =
1990 if delay > 0.0
1991 then max 0.0 (deadline -. now ())
1992 else 0.0
1994 let r, _, _ = Unix.select r [] [] timeout in
1995 begin match r with
1996 | [] ->
1997 state.ghyll None;
1998 begin match state.autoscroll with
1999 | Some step when step != 0 ->
2000 let y = state.y + step in
2001 let y =
2002 if y < 0
2003 then state.maxy
2004 else if y >= state.maxy then 0 else y
2006 gotoy y;
2007 if state.mode = View
2008 then state.text <- "";
2009 state.deadline <- state.deadline +. 0.005;
2011 | _ ->
2012 state.deadline <- state.deadline +. delay;
2013 end;
2015 | l ->
2016 let rec checkfds c = function
2017 | [] -> c
2018 | fd :: rest when fd = state.csock ->
2019 let cmd = readcmd state.csock in
2020 act cmd;
2021 checkfds true rest
2022 | fd :: rest ->
2023 let s = String.create 80 in
2024 let n = Unix.read fd s 0 80 in
2025 if conf.redirectstderr
2026 then (
2027 Buffer.add_substring state.errmsgs s 0 n;
2028 state.newerrmsgs <- true;
2029 Glut.postRedisplay ();
2031 else (
2032 prerr_string (String.sub s 0 n);
2033 flush stderr;
2035 checkfds c rest
2037 if checkfds false l
2038 then loop 0.0
2039 end;
2040 in loop 0.007
2043 let onhist cb =
2044 let rc = cb.rc in
2045 let action = function
2046 | HCprev -> cbget cb ~-1
2047 | HCnext -> cbget cb 1
2048 | HCfirst -> cbget cb ~-(cb.rc)
2049 | HClast -> cbget cb (cb.len - 1 - cb.rc)
2050 and cancel () = cb.rc <- rc
2051 in (action, cancel)
2054 let search pattern forward =
2055 if String.length pattern > 0
2056 then
2057 let pn, py =
2058 match state.layout with
2059 | [] -> 0, 0
2060 | l :: _ ->
2061 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
2063 let cmd =
2064 let b = makecmd "search"
2065 [`b conf.icase; `i pn; `i py; `i (if forward then 1 else 0)]
2067 Buffer.add_char b ',';
2068 Buffer.add_string b pattern;
2069 Buffer.add_char b '\000';
2070 Buffer.contents b;
2072 writecmd state.csock cmd;
2075 let intentry text key =
2076 let c = Char.unsafe_chr key in
2077 match c with
2078 | '0' .. '9' ->
2079 let text = addchar text c in
2080 TEcont text
2082 | _ ->
2083 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2084 TEcont text
2087 let textentry text key =
2088 let c = Char.unsafe_chr key in
2089 match c with
2090 | _ when key >= 32 && key < 127 ->
2091 let text = addchar text c in
2092 TEcont text
2094 | _ ->
2095 dolog "unhandled key %d char `%c'" key (Char.unsafe_chr key);
2096 TEcont text
2099 let reqlayout angle proportional =
2100 match state.throttle with
2101 | None ->
2102 if state.invalidated = 0 then state.anchor <- getanchor ();
2103 conf.angle <- angle mod 360;
2104 conf.proportional <- proportional;
2105 invalidate ();
2106 wcmd "reqlayout" [`i conf.angle; `b proportional];
2107 | _ -> ()
2110 let settrim trimmargins trimfuzz =
2111 if state.invalidated = 0 then state.anchor <- getanchor ();
2112 conf.trimmargins <- trimmargins;
2113 conf.trimfuzz <- trimfuzz;
2114 let x0, y0, x1, y1 = trimfuzz in
2115 invalidate ();
2116 wcmd "settrim" [
2117 `b conf.trimmargins;
2118 `i x0;
2119 `i y0;
2120 `i x1;
2121 `i y1;
2123 Hashtbl.iter (fun _ opaque ->
2124 wcmd "freepage" [`s opaque];
2125 ) state.pagemap;
2126 Hashtbl.clear state.pagemap;
2129 let setzoom zoom =
2130 match state.throttle with
2131 | None ->
2132 let zoom = max 0.01 zoom in
2133 if zoom <> conf.zoom
2134 then (
2135 state.prevzoom <- conf.zoom;
2136 let relx =
2137 if zoom <= 1.0
2138 then (state.x <- 0; 0.0)
2139 else float state.x /. float state.w
2141 conf.zoom <- zoom;
2142 reshape conf.winw conf.winh;
2143 if zoom > 1.0
2144 then (
2145 let x = relx *. float state.w in
2146 state.x <- truncate x;
2148 state.text <- Printf.sprintf "zoom is now %-5.1f" (zoom *. 100.0);
2151 | Some (layout, y, started) ->
2152 let time =
2153 match conf.maxwait with
2154 | None -> 0.0
2155 | Some t -> t
2157 let dt = now () -. started in
2158 if dt > time
2159 then (
2160 state.y <- y;
2161 load layout;
2165 let setcolumns columns coverA coverB =
2166 if columns < 2
2167 then (
2168 conf.columns <- None;
2169 state.x <- 0;
2170 setzoom 1.0;
2172 else (
2173 conf.columns <- Some ((columns, coverA, coverB), [||]);
2174 conf.zoom <- 1.0;
2176 reshape conf.winw conf.winh;
2179 let enterbirdseye () =
2180 let zoom = float conf.thumbw /. float conf.winw in
2181 let birdseyepageno =
2182 let cy = conf.winh / 2 in
2183 let fold = function
2184 | [] -> 0
2185 | l :: rest ->
2186 let rec fold best = function
2187 | [] -> best.pageno
2188 | l :: rest ->
2189 let d = cy - (l.pagedispy + l.pagevh/2)
2190 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2191 if abs d < abs dbest
2192 then fold l rest
2193 else best.pageno
2194 in fold l rest
2196 fold state.layout
2198 state.mode <- Birdseye (
2199 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2201 conf.zoom <- zoom;
2202 conf.presentation <- false;
2203 conf.interpagespace <- 10;
2204 conf.hlinks <- false;
2205 state.x <- 0;
2206 state.mstate <- Mnone;
2207 conf.maxwait <- None;
2208 conf.columns <- (
2209 match conf.beyecolumns with
2210 | Some c ->
2211 conf.zoom <- 1.0;
2212 Some ((c, 0, 0), [||])
2213 | None -> None
2215 Glut.setCursor Glut.CURSOR_INHERIT;
2216 if conf.verbose
2217 then
2218 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2219 (100.0*.zoom)
2220 else
2221 state.text <- ""
2223 reshape conf.winw conf.winh;
2226 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2227 state.mode <- View;
2228 conf.zoom <- c.zoom;
2229 conf.presentation <- c.presentation;
2230 conf.interpagespace <- c.interpagespace;
2231 conf.maxwait <- c.maxwait;
2232 conf.hlinks <- c.hlinks;
2233 conf.beyecolumns <- (
2234 match conf.columns with
2235 | Some ((c, _, _), _) -> Some c
2236 | None -> None
2238 conf.columns <- (
2239 match c.columns with
2240 | Some (c, _) -> Some (c, [||])
2241 | None -> None
2243 state.x <- leftx;
2244 if conf.verbose
2245 then
2246 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2247 (100.0*.conf.zoom)
2249 reshape conf.winw conf.winh;
2250 state.anchor <- if goback then anchor else (pageno, 0.0);
2253 let togglebirdseye () =
2254 match state.mode with
2255 | Birdseye vals -> leavebirdseye vals true
2256 | View -> enterbirdseye ()
2257 | _ -> ()
2260 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2261 let pageno = max 0 (pageno - incr) in
2262 let rec loop = function
2263 | [] -> gotopage1 pageno 0
2264 | l :: _ when l.pageno = pageno ->
2265 if l.pagedispy >= 0 && l.pagey = 0
2266 then G.postRedisplay "upbirdseye"
2267 else gotopage1 pageno 0
2268 | _ :: rest -> loop rest
2270 loop state.layout;
2271 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2274 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2275 let pageno = min (state.pagecount - 1) (pageno + incr) in
2276 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2277 let rec loop = function
2278 | [] ->
2279 let y, h = getpageyh pageno in
2280 let dy = (y - state.y) - (conf.winh - h - conf.interpagespace) in
2281 gotoy (clamp dy)
2282 | l :: _ when l.pageno = pageno ->
2283 if l.pagevh != l.pageh
2284 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
2285 else G.postRedisplay "downbirdseye"
2286 | _ :: rest -> loop rest
2288 loop state.layout
2291 let optentry mode _ key =
2292 let btos b = if b then "on" else "off" in
2293 let c = Char.unsafe_chr key in
2294 match c with
2295 | 's' ->
2296 let ondone s =
2297 try conf.scrollstep <- int_of_string s with exc ->
2298 state.text <- Printf.sprintf "bad integer `%s': %s"
2299 s (Printexc.to_string exc)
2301 TEswitch ("scroll step: ", "", None, intentry, ondone)
2303 | 'A' ->
2304 let ondone s =
2306 conf.autoscrollstep <- int_of_string s;
2307 if state.autoscroll <> None
2308 then state.autoscroll <- Some conf.autoscrollstep
2309 with exc ->
2310 state.text <- Printf.sprintf "bad integer `%s': %s"
2311 s (Printexc.to_string exc)
2313 TEswitch ("auto scroll step: ", "", None, intentry, ondone)
2315 | 'C' ->
2316 let ondone s =
2318 let n, a, b = columns_of_string s in
2319 setcolumns n a b;
2320 with exc ->
2321 state.text <- Printf.sprintf "bad columns `%s': %s"
2322 s (Printexc.to_string exc)
2324 TEswitch ("columns: ", "", None, textentry, ondone)
2326 | 'Z' ->
2327 let ondone s =
2329 let zoom = float (int_of_string s) /. 100.0 in
2330 setzoom zoom
2331 with exc ->
2332 state.text <- Printf.sprintf "bad integer `%s': %s"
2333 s (Printexc.to_string exc)
2335 TEswitch ("zoom: ", "", None, intentry, ondone)
2337 | 't' ->
2338 let ondone s =
2340 conf.thumbw <- bound (int_of_string s) 2 4096;
2341 state.text <-
2342 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
2343 begin match mode with
2344 | Birdseye beye ->
2345 leavebirdseye beye false;
2346 enterbirdseye ();
2347 | _ -> ();
2349 with exc ->
2350 state.text <- Printf.sprintf "bad integer `%s': %s"
2351 s (Printexc.to_string exc)
2353 TEswitch ("thumbnail width: ", "", None, intentry, ondone)
2355 | 'R' ->
2356 let ondone s =
2357 match try
2358 Some (int_of_string s)
2359 with exc ->
2360 state.text <- Printf.sprintf "bad integer `%s': %s"
2361 s (Printexc.to_string exc);
2362 None
2363 with
2364 | Some angle -> reqlayout angle conf.proportional
2365 | None -> ()
2367 TEswitch ("rotation: ", "", None, intentry, ondone)
2369 | 'i' ->
2370 conf.icase <- not conf.icase;
2371 TEdone ("case insensitive search " ^ (btos conf.icase))
2373 | 'p' ->
2374 conf.preload <- not conf.preload;
2375 gotoy state.y;
2376 TEdone ("preload " ^ (btos conf.preload))
2378 | 'v' ->
2379 conf.verbose <- not conf.verbose;
2380 TEdone ("verbose " ^ (btos conf.verbose))
2382 | 'd' ->
2383 conf.debug <- not conf.debug;
2384 TEdone ("debug " ^ (btos conf.debug))
2386 | 'h' ->
2387 conf.maxhfit <- not conf.maxhfit;
2388 state.maxy <-
2389 state.maxy + (if conf.maxhfit then -conf.winh else conf.winh);
2390 TEdone ("maxhfit " ^ (btos conf.maxhfit))
2392 | 'c' ->
2393 conf.crophack <- not conf.crophack;
2394 TEdone ("crophack " ^ btos conf.crophack)
2396 | 'a' ->
2397 let s =
2398 match conf.maxwait with
2399 | None ->
2400 conf.maxwait <- Some infinity;
2401 "always wait for page to complete"
2402 | Some _ ->
2403 conf.maxwait <- None;
2404 "show placeholder if page is not ready"
2406 TEdone s
2408 | 'f' ->
2409 conf.underinfo <- not conf.underinfo;
2410 TEdone ("underinfo " ^ btos conf.underinfo)
2412 | 'P' ->
2413 conf.savebmarks <- not conf.savebmarks;
2414 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
2416 | 'S' ->
2417 let ondone s =
2419 let pageno, py =
2420 match state.layout with
2421 | [] -> 0, 0
2422 | l :: _ ->
2423 l.pageno, l.pagey
2425 conf.interpagespace <- int_of_string s;
2426 state.maxy <- calcheight ();
2427 let y = getpagey pageno in
2428 gotoy (y + py)
2429 with exc ->
2430 state.text <- Printf.sprintf "bad integer `%s': %s"
2431 s (Printexc.to_string exc)
2433 TEswitch ("vertical margin: ", "", None, intentry, ondone)
2435 | 'l' ->
2436 reqlayout conf.angle (not conf.proportional);
2437 TEdone ("proportional display " ^ btos conf.proportional)
2439 | 'T' ->
2440 settrim (not conf.trimmargins) conf.trimfuzz;
2441 TEdone ("trim margins " ^ btos conf.trimmargins)
2443 | 'I' ->
2444 conf.invert <- not conf.invert;
2445 TEdone ("invert colors " ^ btos conf.invert)
2447 | _ ->
2448 state.text <- Printf.sprintf "bad option %d `%c'" key c;
2449 TEstop
2452 class type lvsource = object
2453 method getitemcount : int
2454 method getitem : int -> (string * int)
2455 method hasaction : int -> bool
2456 method exit :
2457 uioh:uioh ->
2458 cancel:bool ->
2459 active:int ->
2460 first:int ->
2461 pan:int ->
2462 qsearch:string ->
2463 uioh option
2464 method getactive : int
2465 method getfirst : int
2466 method getqsearch : string
2467 method setqsearch : string -> unit
2468 method getpan : int
2469 end;;
2471 class virtual lvsourcebase = object
2472 val mutable m_active = 0
2473 val mutable m_first = 0
2474 val mutable m_qsearch = ""
2475 val mutable m_pan = 0
2476 method getactive = m_active
2477 method getfirst = m_first
2478 method getqsearch = m_qsearch
2479 method getpan = m_pan
2480 method setqsearch s = m_qsearch <- s
2481 end;;
2483 let textentryspecial key = function
2484 | ((c, _, (Some (action, _) as onhist), onkey, ondone), mode) ->
2485 let s =
2486 match key with
2487 | Glut.KEY_UP -> action HCprev
2488 | Glut.KEY_DOWN -> action HCnext
2489 | Glut.KEY_HOME -> action HCfirst
2490 | Glut.KEY_END -> action HClast
2491 | _ -> state.text
2493 state.mode <- Textentry ((c, s, onhist, onkey, ondone), mode);
2494 G.postRedisplay "special textentry";
2495 | _ -> ()
2498 let textentrykeyboard key ((c, text, opthist, onkey, ondone), onleave) =
2499 let enttext te =
2500 state.mode <- Textentry (te, onleave);
2501 state.text <- "";
2502 enttext ();
2503 G.postRedisplay "textentrykeyboard enttext";
2505 match Char.unsafe_chr key with
2506 | '\008' -> (* backspace *)
2507 let len = String.length text in
2508 if len = 0
2509 then (
2510 onleave Cancel;
2511 G.postRedisplay "textentrykeyboard after cancel";
2513 else (
2514 let s = String.sub text 0 (len - 1) in
2515 enttext (c, s, opthist, onkey, ondone)
2518 | '\r' | '\n' ->
2519 ondone text;
2520 onleave Confirm;
2521 G.postRedisplay "textentrykeyboard after confirm"
2523 | '\007' (* ctrl-g *)
2524 | '\027' -> (* escape *)
2525 if String.length text = 0
2526 then (
2527 begin match opthist with
2528 | None -> ()
2529 | Some (_, onhistcancel) -> onhistcancel ()
2530 end;
2531 onleave Cancel;
2532 state.text <- "";
2533 G.postRedisplay "textentrykeyboard after cancel2"
2535 else (
2536 enttext (c, "", opthist, onkey, ondone)
2539 | '\127' -> () (* delete *)
2541 | _ ->
2542 begin match onkey text key with
2543 | TEdone text ->
2544 ondone text;
2545 onleave Confirm;
2546 G.postRedisplay "textentrykeyboard after confirm2";
2548 | TEcont text ->
2549 enttext (c, text, opthist, onkey, ondone);
2551 | TEstop ->
2552 onleave Cancel;
2553 G.postRedisplay "textentrykeyboard after cancel3"
2555 | TEswitch te ->
2556 state.mode <- Textentry (te, onleave);
2557 G.postRedisplay "textentrykeyboard switch";
2558 end;
2561 let firstof first active =
2562 if first > active || abs (first - active) > fstate.maxrows - 1
2563 then max 0 (active - (fstate.maxrows/2))
2564 else first
2567 let calcfirst first active =
2568 if active > first
2569 then
2570 let rows = active - first in
2571 if rows > fstate.maxrows then active - fstate.maxrows else first
2572 else active
2575 let scrollph y maxy =
2576 let sh = (float (maxy + conf.winh) /. float conf.winh) in
2577 let sh = float conf.winh /. sh in
2578 let sh = max sh (float conf.scrollh) in
2580 let percent =
2581 if y = state.maxy
2582 then 1.0
2583 else float y /. float maxy
2585 let position = (float conf.winh -. sh) *. percent in
2587 let position =
2588 if position +. sh > float conf.winh
2589 then float conf.winh -. sh
2590 else position
2592 position, sh;
2595 let coe s = (s :> uioh);;
2597 class listview ~(source:lvsource) ~trusted =
2598 object (self)
2599 val m_pan = source#getpan
2600 val m_first = source#getfirst
2601 val m_active = source#getactive
2602 val m_qsearch = source#getqsearch
2603 val m_prev_uioh = state.uioh
2605 method private elemunder y =
2606 let n = y / (fstate.fontsize+1) in
2607 if m_first + n < source#getitemcount
2608 then (
2609 if source#hasaction (m_first + n)
2610 then Some (m_first + n)
2611 else None
2613 else None
2615 method display =
2616 Gl.enable `blend;
2617 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2618 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2619 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
2620 GlDraw.color (1., 1., 1.);
2621 Gl.enable `texture_2d;
2622 let fs = fstate.fontsize in
2623 let nfs = fs + 1 in
2624 let ww = fstate.wwidth in
2625 let tabw = 30.0*.ww in
2626 let itemcount = source#getitemcount in
2627 let rec loop row =
2628 if (row - m_first) * nfs > conf.winh
2629 then ()
2630 else (
2631 if row >= 0 && row < itemcount
2632 then (
2633 let (s, level) = source#getitem row in
2634 let y = (row - m_first) * nfs in
2635 let x = 5.0 +. float (level + m_pan) *. ww in
2636 if row = m_active
2637 then (
2638 Gl.disable `texture_2d;
2639 GlDraw.polygon_mode `both `line;
2640 GlDraw.color (1., 1., 1.) ~alpha:0.9;
2641 GlDraw.rect (1., float (y + 1))
2642 (float (conf.winw - conf.scrollbw - 1), float (y + fs + 3));
2643 GlDraw.polygon_mode `both `fill;
2644 GlDraw.color (1., 1., 1.);
2645 Gl.enable `texture_2d;
2648 let drawtabularstring s =
2649 let drawstr x s = drawstring1 fs (truncate x) (y+nfs) s in
2650 if trusted
2651 then
2652 let tabpos = try String.index s '\t' with Not_found -> -1 in
2653 if tabpos > 0
2654 then
2655 let len = String.length s - tabpos - 1 in
2656 let s1 = String.sub s 0 tabpos
2657 and s2 = String.sub s (tabpos + 1) len in
2658 let nx = drawstr x s1 in
2659 let sw = nx -. x in
2660 let x = x +. (max tabw sw) in
2661 drawstr x s2
2662 else
2663 drawstr x s
2664 else
2665 drawstr x s
2667 let _ = drawtabularstring s in
2668 loop (row+1)
2672 loop m_first;
2673 Gl.disable `blend;
2674 Gl.disable `texture_2d;
2676 method updownlevel incr =
2677 let len = source#getitemcount in
2678 let curlevel =
2679 if m_active >= 0 && m_active < len
2680 then snd (source#getitem m_active)
2681 else -1
2683 let rec flow i =
2684 if i = len then i-1 else if i = -1 then 0 else
2685 let _, l = source#getitem i in
2686 if l != curlevel then i else flow (i+incr)
2688 let active = flow m_active in
2689 let first = calcfirst m_first active in
2690 G.postRedisplay "special outline updownlevel";
2691 {< m_active = active; m_first = first >}
2693 method private key1 key =
2694 let set active first qsearch =
2695 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
2697 let search active pattern incr =
2698 let dosearch re =
2699 let rec loop n =
2700 if n >= 0 && n < source#getitemcount
2701 then (
2702 let s, _ = source#getitem n in
2704 (try ignore (Str.search_forward re s 0); true
2705 with Not_found -> false)
2706 then Some n
2707 else loop (n + incr)
2709 else None
2711 loop active
2714 let re = Str.regexp_case_fold pattern in
2715 dosearch re
2716 with Failure s ->
2717 state.text <- s;
2718 None
2720 match key with
2721 | 18 | 19 -> (* ctrl-r/ctlr-s *)
2722 let incr = if key = 18 then -1 else 1 in
2723 let active, first =
2724 match search (m_active + incr) m_qsearch incr with
2725 | None ->
2726 state.text <- m_qsearch ^ " [not found]";
2727 m_active, m_first
2728 | Some active ->
2729 state.text <- m_qsearch;
2730 active, firstof m_first active
2732 G.postRedisplay "listview ctrl-r/s";
2733 set active first m_qsearch;
2735 | 8 -> (* backspace *)
2736 let len = String.length m_qsearch in
2737 if len = 0
2738 then coe self
2739 else (
2740 if len = 1
2741 then (
2742 state.text <- "";
2743 G.postRedisplay "listview empty qsearch";
2744 set m_active m_first "";
2746 else
2747 let qsearch = String.sub m_qsearch 0 (len - 1) in
2748 let active, first =
2749 match search m_active qsearch ~-1 with
2750 | None ->
2751 state.text <- qsearch ^ " [not found]";
2752 m_active, m_first
2753 | Some active ->
2754 state.text <- qsearch;
2755 active, firstof m_first active
2757 G.postRedisplay "listview backspace qsearch";
2758 set active first qsearch
2761 | _ when key >= 32 && key < 127 ->
2762 let pattern = addchar m_qsearch (Char.chr key) in
2763 let active, first =
2764 match search m_active pattern 1 with
2765 | None ->
2766 state.text <- pattern ^ " [not found]";
2767 m_active, m_first
2768 | Some active ->
2769 state.text <- pattern;
2770 active, firstof m_first active
2772 G.postRedisplay "listview qsearch add";
2773 set active first pattern;
2775 | 27 -> (* escape *)
2776 state.text <- "";
2777 if String.length m_qsearch = 0
2778 then (
2779 G.postRedisplay "list view escape";
2780 begin
2781 match
2782 source#exit (coe self) true m_active m_first m_pan m_qsearch
2783 with
2784 | None -> m_prev_uioh
2785 | Some uioh -> uioh
2788 else (
2789 G.postRedisplay "list view kill qsearch";
2790 source#setqsearch "";
2791 coe {< m_qsearch = "" >}
2794 | 13 -> (* enter *)
2795 state.text <- "";
2796 let self = {< m_qsearch = "" >} in
2797 source#setqsearch "";
2798 let opt =
2799 G.postRedisplay "listview enter";
2800 if m_active >= 0 && m_active < source#getitemcount
2801 then (
2802 source#exit (coe self) false m_active m_first m_pan "";
2804 else (
2805 source#exit (coe self) true m_active m_first m_pan "";
2808 begin match opt with
2809 | None -> m_prev_uioh
2810 | Some uioh -> uioh
2813 | 127 -> (* delete *)
2814 coe self
2816 | _ -> dolog "unknown key %d" key; coe self
2818 method private special1 key =
2819 let itemcount = source#getitemcount in
2820 let find start incr =
2821 let rec find i =
2822 if i = -1 || i = itemcount
2823 then -1
2824 else (
2825 if source#hasaction i
2826 then i
2827 else find (i + incr)
2830 find start
2832 let set active first =
2833 let first = bound first 0 (itemcount - fstate.maxrows) in
2834 state.text <- "";
2835 coe {< m_active = active; m_first = first >}
2837 let navigate incr =
2838 let isvisible first n = n >= first && n - first <= fstate.maxrows in
2839 let active, first =
2840 let incr1 = if incr > 0 then 1 else -1 in
2841 if isvisible m_first m_active
2842 then
2843 let next =
2844 let next = m_active + incr in
2845 let next =
2846 if next < 0 || next >= itemcount
2847 then -1
2848 else find next incr1
2850 if next = -1 || abs (m_active - next) > fstate.maxrows
2851 then -1
2852 else next
2854 if next = -1
2855 then
2856 let first = m_first + incr in
2857 let first = bound first 0 (itemcount - 1) in
2858 let next =
2859 let next = m_active + incr in
2860 let next = bound next 0 (itemcount - 1) in
2861 find next ~-incr1
2863 let active = if next = -1 then m_active else next in
2864 active, first
2865 else
2866 let first = min next m_first in
2867 let first =
2868 if abs (next - first) > fstate.maxrows
2869 then first + incr
2870 else first
2872 next, first
2873 else
2874 let first = m_first + incr in
2875 let first = bound first 0 (itemcount - 1) in
2876 let active =
2877 let next = m_active + incr in
2878 let next = bound next 0 (itemcount - 1) in
2879 let next = find next incr1 in
2880 let active =
2881 if next = -1 || abs (m_active - first) > fstate.maxrows
2882 then (
2883 let active = if m_active = -1 then next else m_active in
2884 active
2886 else next
2888 if isvisible first active
2889 then active
2890 else -1
2892 active, first
2894 G.postRedisplay "listview navigate";
2895 set active first;
2897 begin match key with
2898 | Glut.KEY_UP -> navigate ~-1
2899 | Glut.KEY_DOWN -> navigate 1
2900 | Glut.KEY_PAGE_UP -> navigate ~-(fstate.maxrows)
2901 | Glut.KEY_PAGE_DOWN -> navigate fstate.maxrows
2903 | Glut.KEY_RIGHT ->
2904 state.text <- "";
2905 G.postRedisplay "listview right";
2906 coe {< m_pan = m_pan - 1 >}
2908 | Glut.KEY_LEFT ->
2909 state.text <- "";
2910 G.postRedisplay "listview left";
2911 coe {< m_pan = m_pan + 1 >}
2913 | Glut.KEY_HOME ->
2914 let active = find 0 1 in
2915 G.postRedisplay "listview home";
2916 set active 0;
2918 | Glut.KEY_END ->
2919 let first = max 0 (itemcount - fstate.maxrows) in
2920 let active = find (itemcount - 1) ~-1 in
2921 G.postRedisplay "listview end";
2922 set active first;
2924 | _ -> coe self
2925 end;
2927 method key key =
2928 match state.mode with
2929 | Textentry te -> textentrykeyboard key te; coe self
2930 | _ -> self#key1 key
2932 method special key =
2933 match state.mode with
2934 | Textentry te -> textentryspecial key te; coe self
2935 | _ -> self#special1 key
2937 method button button bstate x y =
2938 let opt =
2939 match button with
2940 | Glut.LEFT_BUTTON when x > conf.winw - conf.scrollbw ->
2941 G.postRedisplay "listview scroll";
2942 if bstate = Glut.DOWN
2943 then
2944 let _, position, sh = self#scrollph in
2945 if y > truncate position && y < truncate (position +. sh)
2946 then (
2947 state.mstate <- Mscrolly;
2948 Some (coe self)
2950 else
2951 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
2952 let first = truncate (s *. float source#getitemcount) in
2953 let first = min source#getitemcount first in
2954 Some (coe {< m_first = first; m_active = first >})
2955 else (
2956 state.mstate <- Mnone;
2957 Some (coe self);
2959 | Glut.LEFT_BUTTON when bstate = Glut.UP ->
2960 begin match self#elemunder y with
2961 | Some n ->
2962 G.postRedisplay "listview click";
2963 source#exit
2964 (coe {< m_active = n >}) false n m_first m_pan m_qsearch
2965 | _ ->
2966 Some (coe self)
2968 | Glut.OTHER_BUTTON n when (n == 3 || n == 4) && bstate = Glut.UP ->
2969 let len = source#getitemcount in
2970 let first =
2971 if n = 4 && m_first + fstate.maxrows >= len
2972 then
2973 m_first
2974 else
2975 let first = m_first + (if n == 3 then -1 else 1) in
2976 bound first 0 (len - 1)
2978 G.postRedisplay "listview wheel";
2979 Some (coe {< m_first = first >})
2980 | _ ->
2981 Some (coe self)
2983 match opt with
2984 | None -> m_prev_uioh
2985 | Some uioh -> uioh
2987 method motion _ y =
2988 match state.mstate with
2989 | Mscrolly ->
2990 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
2991 let first = truncate (s *. float source#getitemcount) in
2992 let first = min source#getitemcount first in
2993 G.postRedisplay "listview motion";
2994 coe {< m_first = first; m_active = first >}
2995 | _ -> coe self
2997 method pmotion x y =
2998 if x < conf.winw - conf.scrollbw
2999 then
3000 let n =
3001 match self#elemunder y with
3002 | None -> Glut.setCursor Glut.CURSOR_INHERIT; m_active
3003 | Some n -> Glut.setCursor Glut.CURSOR_INFO; n
3005 let o =
3006 if n != m_active
3007 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3008 else self
3010 coe o
3011 else (
3012 Glut.setCursor Glut.CURSOR_INHERIT;
3013 coe self
3016 method infochanged _ = ()
3018 method scrollpw = (0, 0.0, 0.0)
3019 method scrollph =
3020 let nfs = fstate.fontsize + 1 in
3021 let y = m_first * nfs in
3022 let itemcount = source#getitemcount in
3023 let maxi = max 0 (itemcount - fstate.maxrows) in
3024 let maxy = maxi * nfs in
3025 let p, h = scrollph y maxy in
3026 conf.scrollbw, p, h
3027 end;;
3029 class outlinelistview ~source =
3030 object (self)
3031 inherit listview ~source:(source :> lvsource) ~trusted:false as super
3033 method key key =
3034 match key with
3035 | 14 -> (* ctrl-n *)
3036 source#narrow m_qsearch;
3037 G.postRedisplay "outline ctrl-n";
3038 coe {< m_first = 0; m_active = 0 >}
3040 | 21 -> (* ctrl-u *)
3041 source#denarrow;
3042 G.postRedisplay "outline ctrl-u";
3043 state.text <- "";
3044 coe {< m_first = 0; m_active = 0 >}
3046 | 12 -> (* ctrl-l *)
3047 let first = m_active - (fstate.maxrows / 2) in
3048 G.postRedisplay "outline ctrl-l";
3049 coe {< m_first = first >}
3051 | 127 -> (* delete *)
3052 source#remove m_active;
3053 G.postRedisplay "outline delete";
3054 let active = max 0 (m_active-1) in
3055 coe {< m_first = firstof m_first active;
3056 m_active = active >}
3058 | key -> super#key key
3060 method special key =
3061 let calcfirst first active =
3062 if active > first
3063 then
3064 let rows = active - first in
3065 if rows > fstate.maxrows then active - fstate.maxrows else first
3066 else active
3068 let navigate incr =
3069 let active = m_active + incr in
3070 let active = bound active 0 (source#getitemcount - 1) in
3071 let first = calcfirst m_first active in
3072 G.postRedisplay "special outline navigate";
3073 coe {< m_active = active; m_first = first >}
3075 match key with
3076 | Glut.KEY_UP -> navigate ~-1
3077 | Glut.KEY_DOWN -> navigate 1
3078 | Glut.KEY_PAGE_UP -> navigate ~-(fstate.maxrows)
3079 | Glut.KEY_PAGE_DOWN -> navigate fstate.maxrows
3081 | Glut.KEY_RIGHT ->
3082 let o =
3083 if Glut.getModifiers () land Glut.active_ctrl != 0
3084 then (
3085 G.postRedisplay "special outline right";
3086 {< m_pan = m_pan + 1 >}
3088 else self#updownlevel 1
3090 coe o
3092 | Glut.KEY_LEFT ->
3093 let o =
3094 if Glut.getModifiers () land Glut.active_ctrl != 0
3095 then (
3096 G.postRedisplay "special outline left";
3097 {< m_pan = m_pan - 1 >}
3099 else self#updownlevel ~-1
3101 coe o
3103 | Glut.KEY_HOME ->
3104 G.postRedisplay "special outline home";
3105 coe {< m_first = 0; m_active = 0 >}
3107 | Glut.KEY_END ->
3108 let active = source#getitemcount - 1 in
3109 let first = max 0 (active - fstate.maxrows) in
3110 G.postRedisplay "special outline end";
3111 coe {< m_active = active; m_first = first >}
3113 | _ -> super#special key
3116 let outlinesource usebookmarks =
3117 let empty = [||] in
3118 (object
3119 inherit lvsourcebase
3120 val mutable m_items = empty
3121 val mutable m_orig_items = empty
3122 val mutable m_prev_items = empty
3123 val mutable m_narrow_pattern = ""
3124 val mutable m_hadremovals = false
3126 method getitemcount =
3127 Array.length m_items + (if m_hadremovals then 1 else 0)
3129 method getitem n =
3130 if n == Array.length m_items && m_hadremovals
3131 then
3132 ("[Confirm removal]", 0)
3133 else
3134 let s, n, _ = m_items.(n) in
3135 (s, n)
3137 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3138 ignore (uioh, first, pan, qsearch);
3139 let confrimremoval = m_hadremovals && active = Array.length m_items in
3140 let items =
3141 if String.length m_narrow_pattern = 0
3142 then m_orig_items
3143 else m_items
3145 if not cancel
3146 then (
3147 if not confrimremoval
3148 then(
3149 let _, _, anchor = m_items.(active) in
3150 gotoanchor anchor;
3151 m_items <- items;
3153 else (
3154 state.bookmarks <- Array.to_list m_items;
3155 m_orig_items <- m_items;
3158 else m_items <- items;
3159 None
3161 method hasaction _ = true
3163 method greetmsg =
3164 if Array.length m_items != Array.length m_orig_items
3165 then "Narrowed to " ^ m_narrow_pattern ^ " (ctrl-u to restore)"
3166 else ""
3168 method narrow pattern =
3169 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
3170 match reopt with
3171 | None -> ()
3172 | Some re ->
3173 let rec loop accu n =
3174 if n = -1
3175 then (
3176 m_narrow_pattern <- pattern;
3177 m_items <- Array.of_list accu
3179 else
3180 let (s, _, _) as o = m_items.(n) in
3181 let accu =
3182 if (try ignore (Str.search_forward re s 0); true
3183 with Not_found -> false)
3184 then o :: accu
3185 else accu
3187 loop accu (n-1)
3189 loop [] (Array.length m_items - 1)
3191 method denarrow =
3192 m_orig_items <- (
3193 if usebookmarks
3194 then Array.of_list state.bookmarks
3195 else state.outlines
3197 m_items <- m_orig_items
3199 method remove m =
3200 if usebookmarks
3201 then
3202 if m >= 0 && m < Array.length m_items
3203 then (
3204 m_hadremovals <- true;
3205 m_items <- Array.init (Array.length m_items - 1) (fun n ->
3206 let n = if n >= m then n+1 else n in
3207 m_items.(n)
3211 method reset anchor items =
3212 m_hadremovals <- false;
3213 if m_orig_items == empty || m_prev_items != items
3214 then (
3215 m_orig_items <- items;
3216 if String.length m_narrow_pattern = 0
3217 then m_items <- items;
3219 m_prev_items <- items;
3220 let rely = getanchory anchor in
3221 let active =
3222 let rec loop n best bestd =
3223 if n = Array.length m_items
3224 then best
3225 else
3226 let (_, _, anchor) = m_items.(n) in
3227 let orely = getanchory anchor in
3228 let d = abs (orely - rely) in
3229 if d < bestd
3230 then loop (n+1) n d
3231 else loop (n+1) best bestd
3233 loop 0 ~-1 max_int
3235 m_active <- active;
3236 m_first <- firstof m_first active
3237 end)
3240 let enterselector usebookmarks =
3241 let source = outlinesource usebookmarks in
3242 fun errmsg ->
3243 let outlines =
3244 if usebookmarks
3245 then Array.of_list state.bookmarks
3246 else state.outlines
3248 if Array.length outlines = 0
3249 then (
3250 showtext ' ' errmsg;
3252 else (
3253 state.text <- source#greetmsg;
3254 Glut.setCursor Glut.CURSOR_INHERIT;
3255 let anchor = getanchor () in
3256 source#reset anchor outlines;
3257 state.uioh <- coe (new outlinelistview ~source);
3258 G.postRedisplay "enter selector";
3262 let enteroutlinemode =
3263 let f = enterselector false in
3264 fun ()-> f "Document has no outline";
3267 let enterbookmarkmode =
3268 let f = enterselector true in
3269 fun () -> f "Document has no bookmarks (yet)";
3272 let color_of_string s =
3273 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
3274 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
3278 let color_to_string (r, g, b) =
3279 let r = truncate (r *. 256.0)
3280 and g = truncate (g *. 256.0)
3281 and b = truncate (b *. 256.0) in
3282 Printf.sprintf "%d/%d/%d" r g b
3285 let irect_of_string s =
3286 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
3289 let irect_to_string (x0,y0,x1,y1) =
3290 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
3293 let makecheckers () =
3294 (* Appropriated from lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
3295 following to say:
3296 converted by Issac Trotts. July 25, 2002 *)
3297 let image_height = 64
3298 and image_width = 64 in
3300 let make_image () =
3301 let image =
3302 GlPix.create `ubyte ~format:`rgb ~width:image_width ~height:image_height
3304 for i = 0 to image_width - 1 do
3305 for j = 0 to image_height - 1 do
3306 Raw.sets (GlPix.to_raw image) ~pos:(3*(i*image_height+j))
3307 (if (i land 8 ) lxor (j land 8) = 0
3308 then [|255;255;255|] else [|200;200;200|])
3309 done
3310 done;
3311 image
3313 let image = make_image () in
3314 let id = GlTex.gen_texture () in
3315 GlTex.bind_texture `texture_2d id;
3316 GlPix.store (`unpack_alignment 1);
3317 GlTex.image2d image;
3318 List.iter (GlTex.parameter ~target:`texture_2d)
3319 [ `wrap_s `repeat;
3320 `wrap_t `repeat;
3321 `mag_filter `nearest;
3322 `min_filter `nearest ];
3326 let setcheckers enabled =
3327 match state.texid with
3328 | None ->
3329 if enabled then state.texid <- Some (makecheckers ())
3331 | Some texid ->
3332 if not enabled
3333 then (
3334 GlTex.delete_texture texid;
3335 state.texid <- None;
3339 let int_of_string_with_suffix s =
3340 let l = String.length s in
3341 let s1, shift =
3342 if l > 1
3343 then
3344 let suffix = Char.lowercase s.[l-1] in
3345 match suffix with
3346 | 'k' -> String.sub s 0 (l-1), 10
3347 | 'm' -> String.sub s 0 (l-1), 20
3348 | 'g' -> String.sub s 0 (l-1), 30
3349 | _ -> s, 0
3350 else s, 0
3352 let n = int_of_string s1 in
3353 let m = n lsl shift in
3354 if m < 0 || m < n
3355 then raise (Failure "value too large")
3356 else m
3359 let string_with_suffix_of_int n =
3360 if n = 0
3361 then "0"
3362 else
3363 let n, s =
3364 if n = 0
3365 then 0, ""
3366 else (
3367 if n land ((1 lsl 20) - 1) = 0
3368 then n lsr 20, "M"
3369 else (
3370 if n land ((1 lsl 10) - 1) = 0
3371 then n lsr 10, "K"
3372 else n, ""
3376 let rec loop s n =
3377 let h = n mod 1000 in
3378 let n = n / 1000 in
3379 if n = 0
3380 then string_of_int h ^ s
3381 else (
3382 let s = Printf.sprintf "_%03d%s" h s in
3383 loop s n
3386 loop "" n ^ s;
3389 let defghyllscroll = (40, 8, 32);;
3390 let ghyllscroll_of_string s =
3391 let (n, a, b) as nab =
3392 if s = "default"
3393 then defghyllscroll
3394 else Scanf.sscanf s "%u,%u,%u" (fun n a b -> n, a, b)
3396 if n <= a || n <= b || a >= b
3397 then failwith "invalid ghyll N,A,B (N <= A, A < B, N <= B)";
3398 nab;
3401 let ghyllscroll_to_string ((n, a, b) as nab) =
3402 if nab = defghyllscroll
3403 then "default"
3404 else Printf.sprintf "%d,%d,%d" n a b;
3407 let describe_location () =
3408 let f (fn, _) l =
3409 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
3411 let fn, ln = List.fold_left f (-1, -1) state.layout in
3412 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
3413 let percent =
3414 if maxy <= 0
3415 then 100.
3416 else (100. *. (float state.y /. float maxy))
3418 if fn = ln
3419 then
3420 Printf.sprintf "page %d of %d [%.2f%%]"
3421 (fn+1) state.pagecount percent
3422 else
3423 Printf.sprintf
3424 "pages %d-%d of %d [%.2f%%]"
3425 (fn+1) (ln+1) state.pagecount percent
3428 let enterinfomode =
3429 let btos b = if b then "\xe2\x88\x9a" else "" in
3430 let showextended = ref false in
3431 let leave mode = function
3432 | Confirm -> state.mode <- mode
3433 | Cancel -> state.mode <- mode in
3434 let src =
3435 (object
3436 val mutable m_first_time = true
3437 val mutable m_l = []
3438 val mutable m_a = [||]
3439 val mutable m_prev_uioh = nouioh
3440 val mutable m_prev_mode = View
3442 inherit lvsourcebase
3444 method reset prev_mode prev_uioh =
3445 m_a <- Array.of_list (List.rev m_l);
3446 m_l <- [];
3447 m_prev_mode <- prev_mode;
3448 m_prev_uioh <- prev_uioh;
3449 if m_first_time
3450 then (
3451 let rec loop n =
3452 if n >= Array.length m_a
3453 then ()
3454 else
3455 match m_a.(n) with
3456 | _, _, _, Action _ -> m_active <- n
3457 | _ -> loop (n+1)
3459 loop 0;
3460 m_first_time <- false;
3463 method int name get set =
3464 m_l <-
3465 (name, `int get, 1, Action (
3466 fun u ->
3467 let ondone s =
3468 try set (int_of_string s)
3469 with exn ->
3470 state.text <- Printf.sprintf "bad integer `%s': %s"
3471 s (Printexc.to_string exn)
3473 state.text <- "";
3474 let te = name ^ ": ", "", None, intentry, ondone in
3475 state.mode <- Textentry (te, leave m_prev_mode);
3477 )) :: m_l
3479 method int_with_suffix name get set =
3480 m_l <-
3481 (name, `intws get, 1, Action (
3482 fun u ->
3483 let ondone s =
3484 try set (int_of_string_with_suffix s)
3485 with exn ->
3486 state.text <- Printf.sprintf "bad integer `%s': %s"
3487 s (Printexc.to_string exn)
3489 state.text <- "";
3490 let te =
3491 name ^ ": ", "", None, intentry_with_suffix, ondone
3493 state.mode <- Textentry (te, leave m_prev_mode);
3495 )) :: m_l
3497 method bool ?(offset=1) ?(btos=btos) name get set =
3498 m_l <-
3499 (name, `bool (btos, get), offset, Action (
3500 fun u ->
3501 let v = get () in
3502 set (not v);
3504 )) :: m_l
3506 method color name get set =
3507 m_l <-
3508 (name, `color get, 1, Action (
3509 fun u ->
3510 let invalid = (nan, nan, nan) in
3511 let ondone s =
3512 let c =
3513 try color_of_string s
3514 with exn ->
3515 state.text <- Printf.sprintf "bad color `%s': %s"
3516 s (Printexc.to_string exn);
3517 invalid
3519 if c <> invalid
3520 then set c;
3522 let te = name ^ ": ", "", None, textentry, ondone in
3523 state.text <- color_to_string (get ());
3524 state.mode <- Textentry (te, leave m_prev_mode);
3526 )) :: m_l
3528 method string name get set =
3529 m_l <-
3530 (name, `string get, 1, Action (
3531 fun u ->
3532 let ondone s = set s in
3533 let te = name ^ ": ", "", None, textentry, ondone in
3534 state.mode <- Textentry (te, leave m_prev_mode);
3536 )) :: m_l
3538 method colorspace name get set =
3539 m_l <-
3540 (name, `string get, 1, Action (
3541 fun _ ->
3542 let source =
3543 let vals = [| "rgb"; "bgr"; "gray" |] in
3544 (object
3545 inherit lvsourcebase
3547 initializer
3548 m_active <- int_of_colorspace conf.colorspace;
3549 m_first <- 0;
3551 method getitemcount = Array.length vals
3552 method getitem n = (vals.(n), 0)
3553 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3554 ignore (uioh, first, pan, qsearch);
3555 if not cancel then set active;
3556 None
3557 method hasaction _ = true
3558 end)
3560 state.text <- "";
3561 coe (new listview ~source ~trusted:true)
3562 )) :: m_l
3564 method caption s offset =
3565 m_l <- (s, `empty, offset, Noaction) :: m_l
3567 method caption2 s f offset =
3568 m_l <- (s, `string f, offset, Noaction) :: m_l
3570 method getitemcount = Array.length m_a
3572 method getitem n =
3573 let tostr = function
3574 | `int f -> string_of_int (f ())
3575 | `intws f -> string_with_suffix_of_int (f ())
3576 | `string f -> f ()
3577 | `color f -> color_to_string (f ())
3578 | `bool (btos, f) -> btos (f ())
3579 | `empty -> ""
3581 let name, t, offset, _ = m_a.(n) in
3582 ((let s = tostr t in
3583 if String.length s > 0
3584 then Printf.sprintf "%s\t%s" name s
3585 else name),
3586 offset)
3588 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3589 let uiohopt =
3590 if not cancel
3591 then (
3592 m_qsearch <- qsearch;
3593 let uioh =
3594 match m_a.(active) with
3595 | _, _, _, Action f -> f uioh
3596 | _ -> uioh
3598 Some uioh
3600 else None
3602 m_active <- active;
3603 m_first <- first;
3604 m_pan <- pan;
3605 uiohopt
3607 method hasaction n =
3608 match m_a.(n) with
3609 | _, _, _, Action _ -> true
3610 | _ -> false
3611 end)
3613 let rec fillsrc prevmode prevuioh =
3614 let sep () = src#caption "" 0 in
3615 let colorp name get set =
3616 src#string name
3617 (fun () -> color_to_string (get ()))
3618 (fun v ->
3620 let c = color_of_string v in
3621 set c
3622 with exn ->
3623 state.text <- Printf.sprintf "bad color `%s': %s"
3624 v (Printexc.to_string exn);
3627 let oldmode = state.mode in
3628 let birdseye = isbirdseye state.mode in
3630 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
3632 src#bool "presentation mode"
3633 (fun () -> conf.presentation)
3634 (fun v ->
3635 conf.presentation <- v;
3636 state.anchor <- getanchor ();
3637 represent ());
3639 src#bool "ignore case in searches"
3640 (fun () -> conf.icase)
3641 (fun v -> conf.icase <- v);
3643 src#bool "preload"
3644 (fun () -> conf.preload)
3645 (fun v -> conf.preload <- v);
3647 src#bool "highlight links"
3648 (fun () -> conf.hlinks)
3649 (fun v -> conf.hlinks <- v);
3651 src#bool "under info"
3652 (fun () -> conf.underinfo)
3653 (fun v -> conf.underinfo <- v);
3655 src#bool "persistent bookmarks"
3656 (fun () -> conf.savebmarks)
3657 (fun v -> conf.savebmarks <- v);
3659 src#bool "proportional display"
3660 (fun () -> conf.proportional)
3661 (fun v -> reqlayout conf.angle v);
3663 src#bool "trim margins"
3664 (fun () -> conf.trimmargins)
3665 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
3667 src#bool "persistent location"
3668 (fun () -> conf.jumpback)
3669 (fun v -> conf.jumpback <- v);
3671 sep ();
3672 src#int "inter-page space"
3673 (fun () -> conf.interpagespace)
3674 (fun n ->
3675 conf.interpagespace <- n;
3676 let pageno, py =
3677 match state.layout with
3678 | [] -> 0, 0
3679 | l :: _ ->
3680 l.pageno, l.pagey
3682 state.maxy <- calcheight ();
3683 let y = getpagey pageno in
3684 gotoy (y + py)
3687 src#int "page bias"
3688 (fun () -> conf.pagebias)
3689 (fun v -> conf.pagebias <- v);
3691 src#int "scroll step"
3692 (fun () -> conf.scrollstep)
3693 (fun n -> conf.scrollstep <- n);
3695 src#int "auto scroll step"
3696 (fun () ->
3697 match state.autoscroll with
3698 | Some step -> step
3699 | _ -> conf.autoscrollstep)
3700 (fun n ->
3701 if state.autoscroll <> None
3702 then state.autoscroll <- Some n;
3703 conf.autoscrollstep <- n);
3705 src#int "zoom"
3706 (fun () -> truncate (conf.zoom *. 100.))
3707 (fun v -> setzoom ((float v) /. 100.));
3709 src#int "rotation"
3710 (fun () -> conf.angle)
3711 (fun v -> reqlayout v conf.proportional);
3713 src#int "scroll bar width"
3714 (fun () -> state.scrollw)
3715 (fun v ->
3716 state.scrollw <- v;
3717 conf.scrollbw <- v;
3718 reshape conf.winw conf.winh;
3721 src#int "scroll handle height"
3722 (fun () -> conf.scrollh)
3723 (fun v -> conf.scrollh <- v;);
3725 src#int "thumbnail width"
3726 (fun () -> conf.thumbw)
3727 (fun v ->
3728 conf.thumbw <- min 4096 v;
3729 match oldmode with
3730 | Birdseye beye ->
3731 leavebirdseye beye false;
3732 enterbirdseye ()
3733 | _ -> ()
3736 src#string "columns"
3737 (fun () ->
3738 match conf.columns with
3739 | None -> "1"
3740 | Some (multicol, _) -> columns_to_string multicol)
3741 (fun v ->
3742 let n, a, b = columns_of_string v in
3743 setcolumns n a b);
3745 sep ();
3746 src#caption "Presentation mode" 0;
3747 src#bool "scrollbar visible"
3748 (fun () -> conf.scrollbarinpm)
3749 (fun v ->
3750 if v != conf.scrollbarinpm
3751 then (
3752 conf.scrollbarinpm <- v;
3753 if conf.presentation
3754 then (
3755 state.scrollw <- if v then conf.scrollbw else 0;
3756 reshape conf.winw conf.winh;
3761 sep ();
3762 src#caption "Pixmap cache" 0;
3763 src#int_with_suffix "size (advisory)"
3764 (fun () -> conf.memlimit)
3765 (fun v -> conf.memlimit <- v);
3767 src#caption2 "used"
3768 (fun () -> Printf.sprintf "%s bytes, %d tiles"
3769 (string_with_suffix_of_int state.memused)
3770 (Hashtbl.length state.tilemap)) 1;
3772 sep ();
3773 src#caption "Layout" 0;
3774 src#caption2 "Dimension"
3775 (fun () ->
3776 Printf.sprintf "%dx%d (virtual %dx%d)"
3777 conf.winw conf.winh
3778 state.w state.maxy)
3780 if conf.debug
3781 then
3782 src#caption2 "Position" (fun () ->
3783 Printf.sprintf "%dx%d" state.x state.y
3785 else
3786 src#caption2 "Visible" (fun () -> describe_location ()) 1
3789 sep ();
3790 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
3791 "Save these parameters as global defaults at exit"
3792 (fun () -> conf.bedefault)
3793 (fun v -> conf.bedefault <- v)
3796 sep ();
3797 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
3798 src#bool ~offset:0 ~btos "Extended parameters"
3799 (fun () -> !showextended)
3800 (fun v -> showextended := v; fillsrc prevmode prevuioh);
3801 if !showextended
3802 then (
3803 src#bool "checkers"
3804 (fun () -> conf.checkers)
3805 (fun v -> conf.checkers <- v; setcheckers v);
3806 src#bool "verbose"
3807 (fun () -> conf.verbose)
3808 (fun v -> conf.verbose <- v);
3809 src#bool "invert colors"
3810 (fun () -> conf.invert)
3811 (fun v -> conf.invert <- v);
3812 src#bool "max fit"
3813 (fun () -> conf.maxhfit)
3814 (fun v -> conf.maxhfit <- v);
3815 src#bool "redirect stderr"
3816 (fun () -> conf.redirectstderr)
3817 (fun v -> conf.redirectstderr <- v; redirectstderr ());
3818 src#string "uri launcher"
3819 (fun () -> conf.urilauncher)
3820 (fun v -> conf.urilauncher <- v);
3821 src#string "tile size"
3822 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
3823 (fun v ->
3825 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
3826 conf.tileh <- max 64 w;
3827 conf.tilew <- max 64 h;
3828 flushtiles ();
3829 with exn ->
3830 state.text <- Printf.sprintf "bad tile size `%s': %s"
3831 v (Printexc.to_string exn));
3832 src#int "texture count"
3833 (fun () -> conf.texcount)
3834 (fun v ->
3835 if realloctexts v
3836 then conf.texcount <- v
3837 else showtext '!' " Failed to set texture count please retry later"
3839 src#int "slice height"
3840 (fun () -> conf.sliceheight)
3841 (fun v ->
3842 conf.sliceheight <- v;
3843 wcmd "sliceh" [`i conf.sliceheight];
3845 src#int "anti-aliasing level"
3846 (fun () -> conf.aalevel)
3847 (fun v ->
3848 conf.aalevel <- bound v 0 8;
3849 state.anchor <- getanchor ();
3850 opendoc state.path state.password;
3852 src#int "ui font size"
3853 (fun () -> fstate.fontsize)
3854 (fun v -> setfontsize (bound v 5 100));
3855 colorp "background color"
3856 (fun () -> conf.bgcolor)
3857 (fun v -> conf.bgcolor <- v);
3858 src#bool "crop hack"
3859 (fun () -> conf.crophack)
3860 (fun v -> conf.crophack <- v);
3861 src#string "trim fuzz"
3862 (fun () -> irect_to_string conf.trimfuzz)
3863 (fun v ->
3865 conf.trimfuzz <- irect_of_string v;
3866 if conf.trimmargins
3867 then settrim true conf.trimfuzz;
3868 with exn ->
3869 state.text <- Printf.sprintf "bad irect `%s': %s"
3870 v (Printexc.to_string exn)
3872 src#string "throttle"
3873 (fun () ->
3874 match conf.maxwait with
3875 | None -> "show place holder if page is not ready"
3876 | Some time ->
3877 if time = infinity
3878 then "wait for page to fully render"
3879 else
3880 "wait " ^ string_of_float time
3881 ^ " seconds before showing placeholder"
3883 (fun v ->
3885 let f = float_of_string v in
3886 if f <= 0.0
3887 then conf.maxwait <- None
3888 else conf.maxwait <- Some f
3889 with exn ->
3890 state.text <- Printf.sprintf "bad time `%s': %s"
3891 v (Printexc.to_string exn)
3893 src#string "ghyll scroll"
3894 (fun () ->
3895 match conf.ghyllscroll with
3896 | None -> ""
3897 | Some nab -> ghyllscroll_to_string nab
3899 (fun v ->
3901 let gs =
3902 if String.length v = 0
3903 then None
3904 else Some (ghyllscroll_of_string v)
3906 conf.ghyllscroll <- gs
3907 with exn ->
3908 state.text <- Printf.sprintf "bad ghyll `%s': %s"
3909 v (Printexc.to_string exn)
3911 src#colorspace "color space"
3912 (fun () -> colorspace_to_string conf.colorspace)
3913 (fun v ->
3914 conf.colorspace <- colorspace_of_int v;
3915 wcmd "cs" [`i v];
3916 load state.layout;
3920 sep ();
3921 src#caption "Document" 0;
3922 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
3923 src#caption2 "Pages"
3924 (fun () -> string_of_int state.pagecount) 1;
3925 src#caption2 "Dimensions"
3926 (fun () -> string_of_int (List.length state.pdims)) 1;
3927 if conf.trimmargins
3928 then (
3929 sep ();
3930 src#caption "Trimmed margins" 0;
3931 src#caption2 "Dimensions"
3932 (fun () -> string_of_int (List.length state.pdims)) 1;
3935 src#reset prevmode prevuioh;
3937 fun () ->
3938 state.text <- "";
3939 let prevmode = state.mode
3940 and prevuioh = state.uioh in
3941 fillsrc prevmode prevuioh;
3942 let source = (src :> lvsource) in
3943 state.uioh <- coe (object (self)
3944 inherit listview ~source ~trusted:true as super
3945 val mutable m_prevmemused = 0
3946 method infochanged = function
3947 | Memused ->
3948 if m_prevmemused != state.memused
3949 then (
3950 m_prevmemused <- state.memused;
3951 G.postRedisplay "memusedchanged";
3953 | Pdim -> G.postRedisplay "pdimchanged"
3954 | Docinfo -> fillsrc prevmode prevuioh
3956 method special key =
3957 if Glut.getModifiers () land Glut.active_ctrl = 0
3958 then
3959 match key with
3960 | Glut.KEY_LEFT -> coe (self#updownlevel ~-1)
3961 | Glut.KEY_RIGHT -> coe (self#updownlevel 1)
3962 | _ -> super#special key
3963 else super#special key
3964 end);
3965 G.postRedisplay "info";
3968 let enterhelpmode =
3969 let source =
3970 (object
3971 inherit lvsourcebase
3972 method getitemcount = Array.length state.help
3973 method getitem n =
3974 let s, n, _ = state.help.(n) in
3975 (s, n)
3977 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3978 let optuioh =
3979 if not cancel
3980 then (
3981 m_qsearch <- qsearch;
3982 match state.help.(active) with
3983 | _, _, Action f -> Some (f uioh)
3984 | _ -> Some (uioh)
3986 else None
3988 m_active <- active;
3989 m_first <- first;
3990 m_pan <- pan;
3991 optuioh
3993 method hasaction n =
3994 match state.help.(n) with
3995 | _, _, Action _ -> true
3996 | _ -> false
3998 initializer
3999 m_active <- -1
4000 end)
4001 in fun () ->
4002 state.uioh <- coe (new listview ~source ~trusted:true);
4003 G.postRedisplay "help";
4006 let entermsgsmode =
4007 let msgsource =
4008 let re = Str.regexp "[\r\n]" in
4009 (object
4010 inherit lvsourcebase
4011 val mutable m_items = [||]
4013 method getitemcount = 1 + Array.length m_items
4015 method getitem n =
4016 if n = 0
4017 then "[Clear]", 0
4018 else m_items.(n-1), 0
4020 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4021 ignore uioh;
4022 if not cancel
4023 then (
4024 if active = 0
4025 then Buffer.clear state.errmsgs;
4026 m_qsearch <- qsearch;
4028 m_active <- active;
4029 m_first <- first;
4030 m_pan <- pan;
4031 None
4033 method hasaction n =
4034 n = 0
4036 method reset =
4037 state.newerrmsgs <- false;
4038 let l = Str.split re (Buffer.contents state.errmsgs) in
4039 m_items <- Array.of_list l
4041 initializer
4042 m_active <- 0
4043 end)
4044 in fun () ->
4045 state.text <- "";
4046 msgsource#reset;
4047 let source = (msgsource :> lvsource) in
4048 state.uioh <- coe (object
4049 inherit listview ~source ~trusted:false as super
4050 method display =
4051 if state.newerrmsgs
4052 then msgsource#reset;
4053 super#display
4054 end);
4055 G.postRedisplay "msgs";
4058 let quickbookmark ?title () =
4059 match state.layout with
4060 | [] -> ()
4061 | l :: _ ->
4062 let title =
4063 match title with
4064 | None ->
4065 let sec = Unix.gettimeofday () in
4066 let tm = Unix.localtime sec in
4067 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
4068 (l.pageno+1)
4069 tm.Unix.tm_mday
4070 tm.Unix.tm_mon
4071 (tm.Unix.tm_year + 1900)
4072 tm.Unix.tm_hour
4073 tm.Unix.tm_min
4074 | Some title -> title
4076 state.bookmarks <-
4077 (title, 0, (l.pageno, float l.pagey /. float l.pageh))
4078 :: state.bookmarks
4081 let doreshape w h =
4082 state.fullscreen <- None;
4083 Glut.reshapeWindow w h;
4086 let viewkeyboard key =
4087 let enttext te =
4088 let mode = state.mode in
4089 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
4090 state.text <- "";
4091 enttext ();
4092 G.postRedisplay "view:enttext"
4094 let c = Char.chr key in
4095 match c with
4096 | '\027' | 'q' -> (* escape *)
4097 begin match state.mstate with
4098 | Mzoomrect _ ->
4099 state.mstate <- Mnone;
4100 Glut.setCursor Glut.CURSOR_INHERIT;
4101 G.postRedisplay "kill zoom rect";
4102 | _ ->
4103 raise Quit
4104 end;
4106 | '\008' -> (* backspace *)
4107 let y = getnav ~-1 in
4108 gotoy_and_clear_text y
4110 | 'o' ->
4111 enteroutlinemode ()
4113 | 'u' ->
4114 state.rects <- [];
4115 state.text <- "";
4116 G.postRedisplay "dehighlight";
4118 | '/' | '?' ->
4119 let ondone isforw s =
4120 cbput state.hists.pat s;
4121 state.searchpattern <- s;
4122 search s isforw
4124 let s = String.create 1 in
4125 s.[0] <- c;
4126 enttext (s, "", Some (onhist state.hists.pat),
4127 textentry, ondone (c ='/'))
4129 | '+' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
4130 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
4131 setzoom (conf.zoom +. incr)
4133 | '+' ->
4134 let ondone s =
4135 let n =
4136 try int_of_string s with exc ->
4137 state.text <- Printf.sprintf "bad integer `%s': %s"
4138 s (Printexc.to_string exc);
4139 max_int
4141 if n != max_int
4142 then (
4143 conf.pagebias <- n;
4144 state.text <- "page bias is now " ^ string_of_int n;
4147 enttext ("page bias: ", "", None, intentry, ondone)
4149 | '-' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
4150 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
4151 setzoom (max 0.01 (conf.zoom -. decr))
4153 | '-' ->
4154 let ondone msg = state.text <- msg in
4155 enttext (
4156 "option [acfhilpstvACPRSZTI]: ", "", None,
4157 optentry state.mode, ondone
4160 | '0' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
4161 setzoom 1.0
4163 | '1' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
4164 let zoom = zoomforh conf.winw conf.winh state.scrollw in
4165 if zoom < 1.0
4166 then setzoom zoom
4168 | '9' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
4169 togglebirdseye ()
4171 | '0' .. '9' ->
4172 let ondone s =
4173 let n =
4174 try int_of_string s with exc ->
4175 state.text <- Printf.sprintf "bad integer `%s': %s"
4176 s (Printexc.to_string exc);
4179 if n >= 0
4180 then (
4181 addnav ();
4182 cbput state.hists.pag (string_of_int n);
4183 gotopage1 (n + conf.pagebias - 1) 0;
4186 let pageentry text key =
4187 match Char.unsafe_chr key with
4188 | 'g' -> TEdone text
4189 | _ -> intentry text key
4191 let text = "x" in text.[0] <- c;
4192 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone)
4194 | 'b' ->
4195 state.scrollw <- if state.scrollw > 0 then 0 else conf.scrollbw;
4196 reshape conf.winw conf.winh;
4198 | 'l' ->
4199 conf.hlinks <- not conf.hlinks;
4200 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
4201 G.postRedisplay "toggle highlightlinks";
4203 | 'a' ->
4204 begin match state.autoscroll with
4205 | Some step ->
4206 conf.autoscrollstep <- step;
4207 state.autoscroll <- None
4208 | None ->
4209 if conf.autoscrollstep = 0
4210 then state.autoscroll <- Some 1
4211 else state.autoscroll <- Some conf.autoscrollstep
4214 | 'P' ->
4215 conf.presentation <- not conf.presentation;
4216 if conf.presentation
4217 then (
4218 if not conf.scrollbarinpm
4219 then state.scrollw <- 0;
4221 else
4222 state.scrollw <- conf.scrollbw;
4224 showtext ' ' ("presentation mode " ^
4225 if conf.presentation then "on" else "off");
4226 state.anchor <- getanchor ();
4227 represent ()
4229 | 'f' ->
4230 begin match state.fullscreen with
4231 | None ->
4232 state.fullscreen <- Some (conf.winw, conf.winh);
4233 Glut.fullScreen ()
4234 | Some (w, h) ->
4235 state.fullscreen <- None;
4236 doreshape w h
4239 | 'g' ->
4240 gotoy_and_clear_text 0
4242 | 'G' ->
4243 gotopage1 (state.pagecount - 1) 0
4245 | 'n' ->
4246 search state.searchpattern true
4248 | 'p' | 'N' ->
4249 search state.searchpattern false
4251 | 't' ->
4252 begin match state.layout with
4253 | [] -> ()
4254 | l :: _ ->
4255 gotoy_and_clear_text (getpagey l.pageno)
4258 | ' ' ->
4259 begin match List.rev state.layout with
4260 | [] -> ()
4261 | l :: _ ->
4262 let pageno = min (l.pageno+1) (state.pagecount-1) in
4263 gotoy_and_clear_text (getpagey pageno)
4266 | '\127' -> (* del *)
4267 begin match state.layout with
4268 | [] -> ()
4269 | l :: _ ->
4270 let pageno = max 0 (l.pageno-1) in
4271 gotoy_and_clear_text (getpagey pageno)
4274 | '=' ->
4275 showtext ' ' (describe_location ());
4277 | 'w' ->
4278 begin match state.layout with
4279 | [] -> ()
4280 | l :: _ ->
4281 doreshape (l.pagew + state.scrollw) l.pageh;
4282 G.postRedisplay "w"
4285 | '\'' ->
4286 enterbookmarkmode ()
4288 | 'h' ->
4289 enterhelpmode ()
4291 | 'i' ->
4292 enterinfomode ()
4294 | 'e' when conf.redirectstderr ->
4295 entermsgsmode ()
4297 | 'm' ->
4298 let ondone s =
4299 match state.layout with
4300 | l :: _ ->
4301 state.bookmarks <-
4302 (s, 0, (l.pageno, float l.pagey /. float l.pageh))
4303 :: state.bookmarks
4304 | _ -> ()
4306 enttext ("bookmark: ", "", None, textentry, ondone)
4308 | '~' ->
4309 quickbookmark ();
4310 showtext ' ' "Quick bookmark added";
4312 | 'z' ->
4313 begin match state.layout with
4314 | l :: _ ->
4315 let rect = getpdimrect l.pagedimno in
4316 let w, h =
4317 if conf.crophack
4318 then
4319 (truncate (1.8 *. (rect.(1) -. rect.(0))),
4320 truncate (1.2 *. (rect.(3) -. rect.(0))))
4321 else
4322 (truncate (rect.(1) -. rect.(0)),
4323 truncate (rect.(3) -. rect.(0)))
4325 let w = truncate ((float w)*.conf.zoom)
4326 and h = truncate ((float h)*.conf.zoom) in
4327 if w != 0 && h != 0
4328 then (
4329 state.anchor <- getanchor ();
4330 doreshape (w + state.scrollw) (h + conf.interpagespace)
4332 G.postRedisplay "z";
4334 | [] -> ()
4337 | '\000' -> (* ctrl-2 *)
4338 let maxw = getmaxw () in
4339 if maxw > 0.0
4340 then setzoom (maxw /. float conf.winw)
4342 | '<' | '>' ->
4343 reqlayout (conf.angle + (if c = '>' then 30 else -30)) conf.proportional
4345 | '[' | ']' ->
4346 conf.colorscale <-
4347 bound (conf.colorscale +. (if c = ']' then 0.1 else -0.1)) 0.0 1.0
4349 G.postRedisplay "brightness";
4351 | 'k' ->
4352 begin match state.mode with
4353 | Birdseye beye -> upbirdseye 1 beye
4354 | _ -> gotoy (clamp (-conf.scrollstep))
4357 | 'j' ->
4358 begin match state.mode with
4359 | Birdseye beye -> downbirdseye 1 beye
4360 | _ -> gotoy (clamp conf.scrollstep)
4363 | 'r' ->
4364 state.anchor <- getanchor ();
4365 opendoc state.path state.password
4367 | 'v' when not conf.debug ->
4368 List.iter debugl state.layout;
4370 | 'v' when conf.debug ->
4371 state.rects <- [];
4372 List.iter (fun l ->
4373 match getopaque l.pageno with
4374 | None -> ()
4375 | Some opaque ->
4376 let x0, y0, x1, y1 = pagebbox opaque in
4377 let a,b = float x0, float y0 in
4378 let c,d = float x1, float y0 in
4379 let e,f = float x1, float y1 in
4380 let h,j = float x0, float y1 in
4381 let rect = (a,b,c,d,e,f,h,j) in
4382 debugrect rect;
4383 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
4384 ) state.layout;
4385 G.postRedisplay "v";
4387 | _ ->
4388 vlog "huh? %d %c" key (Char.chr key);
4391 let birdseyekeyboard key ((_, _, pageno, _, _) as beye) =
4392 match key with
4393 | 27 -> (* escape *)
4394 leavebirdseye beye true
4396 | 12 -> (* ctrl-l *)
4397 let y, h = getpageyh pageno in
4398 let top = (conf.winh - h) / 2 in
4399 gotoy (max 0 (y - top))
4401 | 13 -> (* enter *)
4402 leavebirdseye beye false
4404 | _ ->
4405 viewkeyboard key
4408 let keyboard ~key ~x ~y =
4409 ignore x;
4410 ignore y;
4411 if key = 7 && not (istextentry state.mode) (* ctrl-g *)
4412 then wcmd "interrupt" []
4413 else state.uioh <- state.uioh#key key
4416 let birdseyespecial key ((oconf, leftx, _, hooverpageno, anchor) as beye) =
4417 let incr =
4418 match conf.columns with
4419 | None -> 1
4420 | Some ((c, _, _), _) -> c
4422 match key with
4423 | Glut.KEY_UP -> upbirdseye incr beye
4424 | Glut.KEY_DOWN -> downbirdseye incr beye
4425 | Glut.KEY_LEFT -> upbirdseye 1 beye
4426 | Glut.KEY_RIGHT -> downbirdseye 1 beye
4428 | Glut.KEY_PAGE_UP ->
4429 begin match state.layout with
4430 | l :: _ ->
4431 if l.pagey != 0
4432 then (
4433 state.mode <- Birdseye (
4434 oconf, leftx, l.pageno, hooverpageno, anchor
4436 gotopage1 l.pageno 0;
4438 else (
4439 let layout = layout (state.y-conf.winh) conf.winh in
4440 match layout with
4441 | [] -> gotoy (clamp (-conf.winh))
4442 | l :: _ ->
4443 state.mode <- Birdseye (
4444 oconf, leftx, l.pageno, hooverpageno, anchor
4446 gotopage1 l.pageno 0
4449 | [] -> gotoy (clamp (-conf.winh))
4450 end;
4452 | Glut.KEY_PAGE_DOWN ->
4453 begin match List.rev state.layout with
4454 | l :: _ ->
4455 let layout = layout (state.y + conf.winh) conf.winh in
4456 begin match layout with
4457 | [] ->
4458 let incr = l.pageh - l.pagevh in
4459 if incr = 0
4460 then (
4461 state.mode <-
4462 Birdseye (
4463 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
4465 G.postRedisplay "birdseye pagedown";
4467 else gotoy (clamp (incr + conf.interpagespace*2));
4469 | l :: _ ->
4470 state.mode <-
4471 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
4472 gotopage1 l.pageno 0;
4475 | [] -> gotoy (clamp conf.winh)
4476 end;
4478 | Glut.KEY_HOME ->
4479 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
4480 gotopage1 0 0
4482 | Glut.KEY_END ->
4483 let pageno = state.pagecount - 1 in
4484 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
4485 if not (pagevisible state.layout pageno)
4486 then
4487 let h =
4488 match List.rev state.pdims with
4489 | [] -> conf.winh
4490 | (_, _, h, _) :: _ -> h
4492 gotoy (max 0 (getpagey pageno - (conf.winh - h - conf.interpagespace)))
4493 else G.postRedisplay "birdseye end";
4494 | _ -> ()
4497 let setautoscrollspeed step goingdown =
4498 let incr = max 1 ((abs step) / 2) in
4499 let incr = if goingdown then incr else -incr in
4500 let astep = step + incr in
4501 state.autoscroll <- Some astep;
4504 let special ~key ~x ~y =
4505 ignore x;
4506 ignore y;
4507 state.uioh <- state.uioh#special key
4510 let drawpage l =
4511 let color =
4512 match state.mode with
4513 | Textentry _ -> scalecolor 0.4
4514 | View -> scalecolor 1.0
4515 | Birdseye (_, _, pageno, hooverpageno, _) ->
4516 if l.pageno = hooverpageno
4517 then scalecolor 0.9
4518 else (
4519 if l.pageno = pageno
4520 then scalecolor 1.0
4521 else scalecolor 0.8
4524 drawtiles l color;
4525 begin match getopaque l.pageno with
4526 | Some opaque ->
4527 if tileready l l.pagex l.pagey
4528 then
4529 let x = l.pagedispx - l.pagex
4530 and y = l.pagedispy - l.pagey in
4531 postprocess opaque conf.hlinks x y;
4533 | _ -> ()
4534 end;
4537 let scrollindicator () =
4538 let sbw, ph, sh = state.uioh#scrollph in
4539 let sbh, pw, sw = state.uioh#scrollpw in
4541 GlDraw.color (0.64, 0.64, 0.64);
4542 GlDraw.rect
4543 (float (conf.winw - sbw), 0.)
4544 (float conf.winw, float conf.winh)
4546 GlDraw.rect
4547 (0., float (conf.winh - sbh))
4548 (float (conf.winw - state.scrollw - 1), float conf.winh)
4550 GlDraw.color (0.0, 0.0, 0.0);
4552 GlDraw.rect
4553 (float (conf.winw - sbw), ph)
4554 (float conf.winw, ph +. sh)
4556 GlDraw.rect
4557 (pw, float (conf.winh - sbh))
4558 (pw +. sw, float conf.winh)
4562 let pagetranslatepoint l x y =
4563 let dy = y - l.pagedispy in
4564 let y = dy + l.pagey in
4565 let dx = x - l.pagedispx in
4566 let x = dx + l.pagex in
4567 (x, y);
4570 let showsel () =
4571 match state.mstate with
4572 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
4575 | Msel ((x0, y0), (x1, y1)) ->
4576 let rec loop = function
4577 | l :: ls ->
4578 if ((y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
4579 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh))))
4580 && ((x0 >= l.pagedispx && x0 <= (l.pagedispx + l.pagevw))
4581 || ((x1 >= l.pagedispx && x1 <= (l.pagedispx + l.pagevw))))
4582 then
4583 match getopaque l.pageno with
4584 | Some opaque ->
4585 let dx, dy = pagetranslatepoint l 0 0 in
4586 let x0 = x0 + dx
4587 and y0 = y0 + dy
4588 and x1 = x1 + dx
4589 and y1 = y1 + dy in
4590 GlMat.mode `modelview;
4591 GlMat.push ();
4592 GlMat.translate ~x:(float ~-dx) ~y:(float ~-dy) ();
4593 seltext opaque (x0, y0, x1, y1);
4594 GlMat.pop ();
4595 | _ -> ()
4596 else loop ls
4597 | [] -> ()
4599 loop state.layout
4602 let showrects () =
4603 Gl.enable `blend;
4604 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
4605 GlDraw.polygon_mode `both `fill;
4606 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
4607 List.iter
4608 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
4609 List.iter (fun l ->
4610 if l.pageno = pageno
4611 then (
4612 let dx = float (l.pagedispx - l.pagex) in
4613 let dy = float (l.pagedispy - l.pagey) in
4614 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
4615 GlDraw.begins `quads;
4617 GlDraw.vertex2 (x0+.dx, y0+.dy);
4618 GlDraw.vertex2 (x1+.dx, y1+.dy);
4619 GlDraw.vertex2 (x2+.dx, y2+.dy);
4620 GlDraw.vertex2 (x3+.dx, y3+.dy);
4622 GlDraw.ends ();
4624 ) state.layout
4625 ) state.rects
4627 Gl.disable `blend;
4630 let display () =
4631 GlClear.color (scalecolor2 conf.bgcolor);
4632 GlClear.clear [`color];
4633 List.iter drawpage state.layout;
4634 showrects ();
4635 showsel ();
4636 state.uioh#display;
4637 scrollindicator ();
4638 begin match state.mstate with
4639 | Mzoomrect ((x0, y0), (x1, y1)) ->
4640 Gl.enable `blend;
4641 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
4642 GlDraw.polygon_mode `both `fill;
4643 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
4644 GlDraw.rect (float x0, float y0)
4645 (float x1, float y1);
4646 Gl.disable `blend;
4647 | _ -> ()
4648 end;
4649 enttext ();
4650 Glut.swapBuffers ();
4653 let getunder x y =
4654 let rec f = function
4655 | l :: rest ->
4656 begin match getopaque l.pageno with
4657 | Some opaque ->
4658 let x0 = l.pagedispx in
4659 let x1 = x0 + l.pagevw in
4660 let y0 = l.pagedispy in
4661 let y1 = y0 + l.pagevh in
4662 if y >= y0 && y <= y1 && x >= x0 && x <= x1
4663 then
4664 let px, py = pagetranslatepoint l x y in
4665 match whatsunder opaque px py with
4666 | Unone -> f rest
4667 | under -> under
4668 else f rest
4669 | _ ->
4670 f rest
4672 | [] -> Unone
4674 f state.layout
4677 let zoomrect x y x1 y1 =
4678 let x0 = min x x1
4679 and x1 = max x x1
4680 and y0 = min y y1 in
4681 gotoy (state.y + y0);
4682 state.anchor <- getanchor ();
4683 let zoom = (float conf.winw *. conf.zoom) /. float (x1 - x0) in
4684 let margin =
4685 if state.w < conf.winw - state.scrollw
4686 then (conf.winw - state.scrollw - state.w) / 2
4687 else 0
4689 state.x <- (state.x + margin) - x0;
4690 setzoom zoom;
4691 Glut.setCursor Glut.CURSOR_INHERIT;
4692 state.mstate <- Mnone;
4695 let scrollx x =
4696 let winw = conf.winw - state.scrollw - 1 in
4697 let s = float x /. float winw in
4698 let destx = truncate (float (state.w + winw) *. s) in
4699 state.x <- winw - destx;
4700 gotoy_and_clear_text state.y;
4701 state.mstate <- Mscrollx;
4704 let scrolly y =
4705 let s = float y /. float conf.winh in
4706 let desty = truncate (float (state.maxy - conf.winh) *. s) in
4707 gotoy_and_clear_text desty;
4708 state.mstate <- Mscrolly;
4711 let viewmouse button bstate x y =
4712 match button with
4713 | Glut.OTHER_BUTTON n when (n == 3 || n == 4) && bstate = Glut.UP ->
4714 if Glut.getModifiers () land Glut.active_ctrl != 0
4715 then (
4716 match state.mstate with
4717 | Mzoom (oldn, i) ->
4718 if oldn = n
4719 then (
4720 if i = 2
4721 then
4722 let incr =
4723 match n with
4724 | 4 ->
4725 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
4726 | _ ->
4727 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
4729 let zoom = conf.zoom -. incr in
4730 setzoom zoom;
4731 state.mstate <- Mzoom (n, 0);
4732 else
4733 state.mstate <- Mzoom (n, i+1);
4735 else state.mstate <- Mzoom (n, 0)
4737 | _ -> state.mstate <- Mzoom (n, 0)
4739 else (
4740 match state.autoscroll with
4741 | Some step -> setautoscrollspeed step (n=4)
4742 | None ->
4743 let incr =
4744 if n = 3
4745 then -conf.scrollstep
4746 else conf.scrollstep
4748 let incr = incr * 2 in
4749 let y = clamp incr in
4750 gotoy_and_clear_text y
4753 | Glut.LEFT_BUTTON when Glut.getModifiers () land Glut.active_ctrl != 0 ->
4754 if bstate = Glut.DOWN
4755 then (
4756 Glut.setCursor Glut.CURSOR_CROSSHAIR;
4757 state.mstate <- Mpan (x, y)
4759 else
4760 state.mstate <- Mnone
4762 | Glut.RIGHT_BUTTON ->
4763 if bstate = Glut.DOWN
4764 then (
4765 Glut.setCursor Glut.CURSOR_CYCLE;
4766 let p = (x, y) in
4767 state.mstate <- Mzoomrect (p, p)
4769 else (
4770 match state.mstate with
4771 | Mzoomrect ((x0, y0), _) ->
4772 if abs (x-x0) > 10 && abs (y - y0) > 10
4773 then zoomrect x0 y0 x y
4774 else (
4775 state.mstate <- Mnone;
4776 Glut.setCursor Glut.CURSOR_INHERIT;
4777 G.postRedisplay "kill accidental zoom rect";
4779 | _ ->
4780 Glut.setCursor Glut.CURSOR_INHERIT;
4781 state.mstate <- Mnone
4784 | Glut.LEFT_BUTTON when x > conf.winw - state.scrollw ->
4785 if bstate = Glut.DOWN
4786 then
4787 let _, position, sh = state.uioh#scrollph in
4788 if y > truncate position && y < truncate (position +. sh)
4789 then state.mstate <- Mscrolly
4790 else scrolly y
4791 else
4792 state.mstate <- Mnone
4794 | Glut.LEFT_BUTTON when y > conf.winh - state.hscrollh ->
4795 if bstate = Glut.DOWN
4796 then
4797 let _, position, sw = state.uioh#scrollpw in
4798 if x > truncate position && x < truncate (position +. sw)
4799 then state.mstate <- Mscrollx
4800 else scrollx x
4801 else
4802 state.mstate <- Mnone
4804 | Glut.LEFT_BUTTON ->
4805 let dest = if bstate = Glut.DOWN then getunder x y else Unone in
4806 begin match dest with
4807 | Ulinkgoto (pageno, top) ->
4808 if pageno >= 0
4809 then (
4810 addnav ();
4811 gotopage1 pageno top;
4814 | Ulinkuri s ->
4815 gotouri s
4817 | Unone when bstate = Glut.DOWN ->
4818 Glut.setCursor Glut.CURSOR_CROSSHAIR;
4819 state.mstate <- Mpan (x, y);
4821 | Unone | Utext _ ->
4822 if bstate = Glut.DOWN
4823 then (
4824 if conf.angle mod 360 = 0
4825 then (
4826 state.mstate <- Msel ((x, y), (x, y));
4827 G.postRedisplay "mouse select";
4830 else (
4831 match state.mstate with
4832 | Mnone -> ()
4834 | Mzoom _ | Mscrollx | Mscrolly ->
4835 state.mstate <- Mnone
4837 | Mzoomrect ((x0, y0), _) ->
4838 zoomrect x0 y0 x y
4840 | Mpan _ ->
4841 Glut.setCursor Glut.CURSOR_INHERIT;
4842 state.mstate <- Mnone
4844 | Msel ((_, y0), (_, y1)) ->
4845 let f l =
4846 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
4847 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
4848 then
4849 match getopaque l.pageno with
4850 | Some opaque ->
4851 copysel opaque
4852 | _ -> ()
4854 List.iter f state.layout;
4855 copysel ""; (* ugly *)
4856 Glut.setCursor Glut.CURSOR_INHERIT;
4857 state.mstate <- Mnone;
4861 | _ -> ()
4864 let birdseyemouse button bstate x y
4865 (conf, leftx, _, hooverpageno, anchor) =
4866 match button with
4867 | Glut.LEFT_BUTTON when bstate = Glut.UP ->
4868 let rec loop = function
4869 | [] -> ()
4870 | l :: rest ->
4871 if y > l.pagedispy && y < l.pagedispy + l.pagevh
4872 && x > l.pagedispx && x < l.pagedispx + l.pagevw
4873 then (
4874 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
4876 else loop rest
4878 loop state.layout
4879 | Glut.OTHER_BUTTON _ -> viewmouse button bstate x y
4880 | _ -> ()
4883 let mouse bstate button x y =
4884 state.uioh <- state.uioh#button button bstate x y;
4887 let mouse ~button ~state ~x ~y = mouse state button x y;;
4889 let motion ~x ~y =
4890 state.uioh <- state.uioh#motion x y
4893 let pmotion ~x ~y =
4894 state.uioh <- state.uioh#pmotion x y;
4897 let uioh = object
4898 method display = ()
4900 method key key =
4901 begin match state.mode with
4902 | Textentry textentry -> textentrykeyboard key textentry
4903 | Birdseye birdseye -> birdseyekeyboard key birdseye
4904 | View -> viewkeyboard key
4905 end;
4906 state.uioh
4908 method special key =
4909 begin match state.mode with
4910 | View | (Birdseye _) when key = Glut.KEY_F9 ->
4911 togglebirdseye ()
4913 | Birdseye vals ->
4914 birdseyespecial key vals
4916 | View when key = Glut.KEY_F1 ->
4917 enterhelpmode ()
4919 | View ->
4920 begin match state.autoscroll with
4921 | Some step when key = Glut.KEY_DOWN || key = Glut.KEY_UP ->
4922 setautoscrollspeed step (key = Glut.KEY_DOWN)
4924 | _ ->
4925 let y =
4926 match key with
4927 | Glut.KEY_F3 -> search state.searchpattern true; state.y
4928 | Glut.KEY_UP ->
4929 if Glut.getModifiers () land Glut.active_ctrl != 0
4930 then
4931 if Glut.getModifiers () land Glut.active_shift != 0
4932 then (setzoom state.prevzoom; state.y)
4933 else clamp (-conf.winh/2)
4934 else clamp (-conf.scrollstep)
4935 | Glut.KEY_DOWN ->
4936 if Glut.getModifiers () land Glut.active_ctrl != 0
4937 then
4938 if Glut.getModifiers () land Glut.active_shift != 0
4939 then (setzoom state.prevzoom; state.y)
4940 else clamp (conf.winh/2)
4941 else clamp (conf.scrollstep)
4942 | Glut.KEY_PAGE_UP ->
4943 if Glut.getModifiers () land Glut.active_ctrl != 0
4944 then
4945 match state.layout with
4946 | [] -> state.y
4947 | l :: _ -> state.y - l.pagey
4948 else
4949 clamp (-conf.winh)
4950 | Glut.KEY_PAGE_DOWN ->
4951 if Glut.getModifiers () land Glut.active_ctrl != 0
4952 then
4953 match List.rev state.layout with
4954 | [] -> state.y
4955 | l :: _ -> getpagey l.pageno
4956 else
4957 clamp conf.winh
4958 | Glut.KEY_HOME ->
4959 addnav ();
4961 | Glut.KEY_END ->
4962 addnav ();
4963 state.maxy - (if conf.maxhfit then conf.winh else 0)
4965 | (Glut.KEY_RIGHT | Glut.KEY_LEFT) when
4966 Glut.getModifiers () land Glut.active_alt != 0 ->
4967 getnav (if key = Glut.KEY_LEFT then 1 else -1)
4969 | Glut.KEY_RIGHT when conf.zoom > 1.0 ->
4970 let dx =
4971 if Glut.getModifiers () land Glut.active_ctrl != 0
4972 then (conf.winw / 2)
4973 else 10
4975 state.x <- state.x - dx;
4976 state.y
4977 | Glut.KEY_LEFT when conf.zoom > 1.0 ->
4978 let dx =
4979 if Glut.getModifiers () land Glut.active_ctrl != 0
4980 then (conf.winw / 2)
4981 else 10
4983 state.x <- state.x + dx;
4984 state.y
4986 | _ -> state.y
4988 if abs (state.y - y) > conf.scrollstep*2
4989 then gotoghyll y
4990 else gotoy_and_clear_text y
4993 | Textentry te -> textentryspecial key te
4994 end;
4995 state.uioh
4997 method button button bstate x y =
4998 begin match state.mode with
4999 | View -> viewmouse button bstate x y
5000 | Birdseye beye -> birdseyemouse button bstate x y beye
5001 | Textentry _ -> ()
5002 end;
5003 state.uioh
5005 method motion x y =
5006 begin match state.mode with
5007 | Textentry _ -> ()
5008 | View | Birdseye _ ->
5009 match state.mstate with
5010 | Mzoom _ | Mnone -> ()
5012 | Mpan (x0, y0) ->
5013 let dx = x - x0
5014 and dy = y0 - y in
5015 state.mstate <- Mpan (x, y);
5016 if conf.zoom > 1.0 then state.x <- state.x + dx;
5017 let y = clamp dy in
5018 gotoy_and_clear_text y
5020 | Msel (a, _) ->
5021 state.mstate <- Msel (a, (x, y));
5022 G.postRedisplay "motion select";
5024 | Mscrolly ->
5025 let y = min conf.winh (max 0 y) in
5026 scrolly y
5028 | Mscrollx ->
5029 let x = min conf.winw (max 0 x) in
5030 scrollx x
5032 | Mzoomrect (p0, _) ->
5033 state.mstate <- Mzoomrect (p0, (x, y));
5034 G.postRedisplay "motion zoomrect";
5035 end;
5036 state.uioh
5038 method pmotion x y =
5039 begin match state.mode with
5040 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
5041 let rec loop = function
5042 | [] ->
5043 if hooverpageno != -1
5044 then (
5045 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
5046 G.postRedisplay "pmotion birdseye no hoover";
5048 | l :: rest ->
5049 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5050 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5051 then (
5052 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
5053 G.postRedisplay "pmotion birdseye hoover";
5055 else loop rest
5057 loop state.layout
5059 | Textentry _ -> ()
5061 | View ->
5062 match state.mstate with
5063 | Mnone ->
5064 begin match getunder x y with
5065 | Unone -> Glut.setCursor Glut.CURSOR_INHERIT
5066 | Ulinkuri uri ->
5067 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
5068 Glut.setCursor Glut.CURSOR_INFO
5069 | Ulinkgoto (page, _) ->
5070 if conf.underinfo
5071 then showtext 'p' ("age: " ^ string_of_int (page+1));
5072 Glut.setCursor Glut.CURSOR_INFO
5073 | Utext s ->
5074 if conf.underinfo then showtext 'f' ("ont: " ^ s);
5075 Glut.setCursor Glut.CURSOR_TEXT
5078 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
5080 end;
5081 state.uioh
5083 method infochanged _ = ()
5085 method scrollph =
5086 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
5087 let p, h = scrollph state.y maxy in
5088 state.scrollw, p, h
5090 method scrollpw =
5091 let winw = conf.winw - state.scrollw - 1 in
5092 let fwinw = float winw in
5093 let sw =
5094 let sw = fwinw /. float state.w in
5095 let sw = fwinw *. sw in
5096 max sw (float conf.scrollh)
5098 let position, sw =
5099 let f = state.w+winw in
5100 let r = float (winw-state.x) /. float f in
5101 let p = fwinw *. r in
5102 p-.sw/.2., sw
5104 let sw =
5105 if position +. sw > fwinw
5106 then fwinw -. position
5107 else sw
5109 state.hscrollh, position, sw
5110 end;;
5112 module Config =
5113 struct
5114 open Parser
5116 let fontpath = ref "";;
5117 let wmclasshack = ref false;;
5119 let unent s =
5120 let l = String.length s in
5121 let b = Buffer.create l in
5122 unent b s 0 l;
5123 Buffer.contents b;
5126 let home =
5128 match platform with
5129 | Pwindows | Pmingw -> Sys.getenv "HOMEPATH"
5130 | _ -> Sys.getenv "HOME"
5131 with exn ->
5132 prerr_endline
5133 ("Can not determine home directory location: " ^
5134 Printexc.to_string exn);
5138 let config_of c attrs =
5139 let apply c k v =
5141 match k with
5142 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
5143 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
5144 | "case-insensitive-search" -> { c with icase = bool_of_string v }
5145 | "preload" -> { c with preload = bool_of_string v }
5146 | "page-bias" -> { c with pagebias = int_of_string v }
5147 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
5148 | "auto-scroll-step" ->
5149 { c with autoscrollstep = max 0 (int_of_string v) }
5150 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
5151 | "crop-hack" -> { c with crophack = bool_of_string v }
5152 | "throttle" ->
5153 let mw =
5154 match String.lowercase v with
5155 | "true" -> Some infinity
5156 | "false" -> None
5157 | f -> Some (float_of_string f)
5159 { c with maxwait = mw}
5160 | "highlight-links" -> { c with hlinks = bool_of_string v }
5161 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
5162 | "vertical-margin" ->
5163 { c with interpagespace = max 0 (int_of_string v) }
5164 | "zoom" ->
5165 let zoom = float_of_string v /. 100. in
5166 let zoom = max zoom 0.0 in
5167 { c with zoom = zoom }
5168 | "presentation" -> { c with presentation = bool_of_string v }
5169 | "rotation-angle" -> { c with angle = int_of_string v }
5170 | "width" -> { c with winw = max 20 (int_of_string v) }
5171 | "height" -> { c with winh = max 20 (int_of_string v) }
5172 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
5173 | "proportional-display" -> { c with proportional = bool_of_string v }
5174 | "pixmap-cache-size" ->
5175 { c with memlimit = max 2 (int_of_string_with_suffix v) }
5176 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
5177 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
5178 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
5179 | "persistent-location" -> { c with jumpback = bool_of_string v }
5180 | "background-color" -> { c with bgcolor = color_of_string v }
5181 | "scrollbar-in-presentation" ->
5182 { c with scrollbarinpm = bool_of_string v }
5183 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
5184 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
5185 | "mupdf-store-size" ->
5186 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
5187 | "checkers" -> { c with checkers = bool_of_string v }
5188 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
5189 | "trim-margins" -> { c with trimmargins = bool_of_string v }
5190 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
5191 | "wmclass-hack" -> wmclasshack := bool_of_string v; c
5192 | "uri-launcher" -> { c with urilauncher = unent v }
5193 | "color-space" -> { c with colorspace = colorspace_of_string v }
5194 | "invert-colors" -> { c with invert = bool_of_string v }
5195 | "brightness" -> { c with colorscale = float_of_string v }
5196 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
5197 | "ghyllscroll" ->
5198 { c with ghyllscroll = Some (ghyllscroll_of_string v) }
5199 | "columns" ->
5200 let nab = columns_of_string v in
5201 { c with columns = Some (nab, [||]) }
5202 | "birds-eye-columns" ->
5203 { c with beyecolumns = Some (max (int_of_string v) 2) }
5204 | _ -> c
5205 with exn ->
5206 prerr_endline ("Error processing attribute (`" ^
5207 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
5210 let rec fold c = function
5211 | [] -> c
5212 | (k, v) :: rest ->
5213 let c = apply c k v in
5214 fold c rest
5216 fold c attrs;
5219 let fromstring f pos n v d =
5220 try f v
5221 with exn ->
5222 dolog "Error processing attribute (%S=%S) at %d\n%s"
5223 n v pos (Printexc.to_string exn)
5228 let bookmark_of attrs =
5229 let rec fold title page rely = function
5230 | ("title", v) :: rest -> fold v page rely rest
5231 | ("page", v) :: rest -> fold title v rely rest
5232 | ("rely", v) :: rest -> fold title page v rest
5233 | _ :: rest -> fold title page rely rest
5234 | [] -> title, page, rely
5236 fold "invalid" "0" "0" attrs
5239 let doc_of attrs =
5240 let rec fold path page rely pan = function
5241 | ("path", v) :: rest -> fold v page rely pan rest
5242 | ("page", v) :: rest -> fold path v rely pan rest
5243 | ("rely", v) :: rest -> fold path page v pan rest
5244 | ("pan", v) :: rest -> fold path page rely v rest
5245 | _ :: rest -> fold path page rely pan rest
5246 | [] -> path, page, rely, pan
5248 fold "" "0" "0" "0" attrs
5251 let setconf dst src =
5252 dst.scrollbw <- src.scrollbw;
5253 dst.scrollh <- src.scrollh;
5254 dst.icase <- src.icase;
5255 dst.preload <- src.preload;
5256 dst.pagebias <- src.pagebias;
5257 dst.verbose <- src.verbose;
5258 dst.scrollstep <- src.scrollstep;
5259 dst.maxhfit <- src.maxhfit;
5260 dst.crophack <- src.crophack;
5261 dst.autoscrollstep <- src.autoscrollstep;
5262 dst.maxwait <- src.maxwait;
5263 dst.hlinks <- src.hlinks;
5264 dst.underinfo <- src.underinfo;
5265 dst.interpagespace <- src.interpagespace;
5266 dst.zoom <- src.zoom;
5267 dst.presentation <- src.presentation;
5268 dst.angle <- src.angle;
5269 dst.winw <- src.winw;
5270 dst.winh <- src.winh;
5271 dst.savebmarks <- src.savebmarks;
5272 dst.memlimit <- src.memlimit;
5273 dst.proportional <- src.proportional;
5274 dst.texcount <- src.texcount;
5275 dst.sliceheight <- src.sliceheight;
5276 dst.thumbw <- src.thumbw;
5277 dst.jumpback <- src.jumpback;
5278 dst.bgcolor <- src.bgcolor;
5279 dst.scrollbarinpm <- src.scrollbarinpm;
5280 dst.tilew <- src.tilew;
5281 dst.tileh <- src.tileh;
5282 dst.mustoresize <- src.mustoresize;
5283 dst.checkers <- src.checkers;
5284 dst.aalevel <- src.aalevel;
5285 dst.trimmargins <- src.trimmargins;
5286 dst.trimfuzz <- src.trimfuzz;
5287 dst.urilauncher <- src.urilauncher;
5288 dst.colorspace <- src.colorspace;
5289 dst.invert <- src.invert;
5290 dst.colorscale <- src.colorscale;
5291 dst.redirectstderr <- src.redirectstderr;
5292 dst.ghyllscroll <- src.ghyllscroll;
5293 dst.columns <- src.columns;
5294 dst.beyecolumns <- src.beyecolumns;
5297 let get s =
5298 let h = Hashtbl.create 10 in
5299 let dc = { defconf with angle = defconf.angle } in
5300 let rec toplevel v t spos _ =
5301 match t with
5302 | Vdata | Vcdata | Vend -> v
5303 | Vopen ("llppconfig", _, closed) ->
5304 if closed
5305 then v
5306 else { v with f = llppconfig }
5307 | Vopen _ ->
5308 error "unexpected subelement at top level" s spos
5309 | Vclose _ -> error "unexpected close at top level" s spos
5311 and llppconfig v t spos _ =
5312 match t with
5313 | Vdata | Vcdata -> v
5314 | Vend -> error "unexpected end of input in llppconfig" s spos
5315 | Vopen ("defaults", attrs, closed) ->
5316 let c = config_of dc attrs in
5317 setconf dc c;
5318 if closed
5319 then v
5320 else { v with f = skip "defaults" (fun () -> v) }
5322 | Vopen ("ui-font", attrs, closed) ->
5323 let rec getsize size = function
5324 | [] -> size
5325 | ("size", v) :: rest ->
5326 let size =
5327 fromstring int_of_string spos "size" v fstate.fontsize in
5328 getsize size rest
5329 | l -> getsize size l
5331 fstate.fontsize <- getsize fstate.fontsize attrs;
5332 if closed
5333 then v
5334 else { v with f = uifont (Buffer.create 10) }
5336 | Vopen ("doc", attrs, closed) ->
5337 let pathent, spage, srely, span = doc_of attrs in
5338 let path = unent pathent
5339 and pageno = fromstring int_of_string spos "page" spage 0
5340 and rely = fromstring float_of_string spos "rely" srely 0.0
5341 and pan = fromstring int_of_string spos "pan" span 0 in
5342 let c = config_of dc attrs in
5343 let anchor = (pageno, rely) in
5344 if closed
5345 then (Hashtbl.add h path (c, [], pan, anchor); v)
5346 else { v with f = doc path pan anchor c [] }
5348 | Vopen _ ->
5349 error "unexpected subelement in llppconfig" s spos
5351 | Vclose "llppconfig" -> { v with f = toplevel }
5352 | Vclose _ -> error "unexpected close in llppconfig" s spos
5354 and uifont b v t spos epos =
5355 match t with
5356 | Vdata | Vcdata ->
5357 Buffer.add_substring b s spos (epos - spos);
5359 | Vopen (_, _, _) ->
5360 error "unexpected subelement in ui-font" s spos
5361 | Vclose "ui-font" ->
5362 if String.length !fontpath = 0
5363 then fontpath := Buffer.contents b;
5364 { v with f = llppconfig }
5365 | Vclose _ -> error "unexpected close in ui-font" s spos
5366 | Vend -> error "unexpected end of input in ui-font" s spos
5368 and doc path pan anchor c bookmarks v t spos _ =
5369 match t with
5370 | Vdata | Vcdata -> v
5371 | Vend -> error "unexpected end of input in doc" s spos
5372 | Vopen ("bookmarks", _, closed) ->
5373 if closed
5374 then v
5375 else { v with f = pbookmarks path pan anchor c bookmarks }
5377 | Vopen (_, _, _) ->
5378 error "unexpected subelement in doc" s spos
5380 | Vclose "doc" ->
5381 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
5382 { v with f = llppconfig }
5384 | Vclose _ -> error "unexpected close in doc" s spos
5386 and pbookmarks path pan anchor c bookmarks v t spos _ =
5387 match t with
5388 | Vdata | Vcdata -> v
5389 | Vend -> error "unexpected end of input in bookmarks" s spos
5390 | Vopen ("item", attrs, closed) ->
5391 let titleent, spage, srely = bookmark_of attrs in
5392 let page = fromstring int_of_string spos "page" spage 0
5393 and rely = fromstring float_of_string spos "rely" srely 0.0 in
5394 let bookmarks = (unent titleent, 0, (page, rely)) :: bookmarks in
5395 if closed
5396 then { v with f = pbookmarks path pan anchor c bookmarks }
5397 else
5398 let f () = v in
5399 { v with f = skip "item" f }
5401 | Vopen _ ->
5402 error "unexpected subelement in bookmarks" s spos
5404 | Vclose "bookmarks" ->
5405 { v with f = doc path pan anchor c bookmarks }
5407 | Vclose _ -> error "unexpected close in bookmarks" s spos
5409 and skip tag f v t spos _ =
5410 match t with
5411 | Vdata | Vcdata -> v
5412 | Vend ->
5413 error ("unexpected end of input in skipped " ^ tag) s spos
5414 | Vopen (tag', _, closed) ->
5415 if closed
5416 then v
5417 else
5418 let f' () = { v with f = skip tag f } in
5419 { v with f = skip tag' f' }
5420 | Vclose ctag ->
5421 if tag = ctag
5422 then f ()
5423 else error ("unexpected close in skipped " ^ tag) s spos
5426 parse { f = toplevel; accu = () } s;
5427 h, dc;
5430 let do_load f ic =
5432 let len = in_channel_length ic in
5433 let s = String.create len in
5434 really_input ic s 0 len;
5435 f s;
5436 with
5437 | Parse_error (msg, s, pos) ->
5438 let subs = subs s pos in
5439 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
5440 failwith ("parse error: " ^ s)
5442 | exn ->
5443 failwith ("config load error: " ^ Printexc.to_string exn)
5446 let defconfpath =
5447 let dir =
5449 let dir = Filename.concat home ".config" in
5450 if Sys.is_directory dir then dir else home
5451 with _ -> home
5453 Filename.concat dir "llpp.conf"
5456 let confpath = ref defconfpath;;
5458 let load1 f =
5459 if Sys.file_exists !confpath
5460 then
5461 match
5462 (try Some (open_in_bin !confpath)
5463 with exn ->
5464 prerr_endline
5465 ("Error opening configuation file `" ^ !confpath ^ "': " ^
5466 Printexc.to_string exn);
5467 None
5469 with
5470 | Some ic ->
5471 begin try
5472 f (do_load get ic)
5473 with exn ->
5474 prerr_endline
5475 ("Error loading configuation from `" ^ !confpath ^ "': " ^
5476 Printexc.to_string exn);
5477 end;
5478 close_in ic;
5480 | None -> ()
5481 else
5482 f (Hashtbl.create 0, defconf)
5485 let load () =
5486 let f (h, dc) =
5487 let pc, pb, px, pa =
5489 Hashtbl.find h (Filename.basename state.path)
5490 with Not_found -> dc, [], 0, (0, 0.0)
5492 setconf defconf dc;
5493 setconf conf pc;
5494 state.bookmarks <- pb;
5495 state.x <- px;
5496 state.scrollw <- conf.scrollbw;
5497 if conf.jumpback
5498 then state.anchor <- pa;
5499 cbput state.hists.nav pa;
5501 load1 f
5504 let add_attrs bb always dc c =
5505 let ob s a b =
5506 if always || a != b
5507 then Printf.bprintf bb "\n %s='%b'" s a
5508 and oi s a b =
5509 if always || a != b
5510 then Printf.bprintf bb "\n %s='%d'" s a
5511 and oI s a b =
5512 if always || a != b
5513 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
5514 and oz s a b =
5515 if always || a <> b
5516 then Printf.bprintf bb "\n %s='%d'" s (truncate (a*.100.))
5517 and oF s a b =
5518 if always || a <> b
5519 then Printf.bprintf bb "\n %s='%f'" s a
5520 and oc s a b =
5521 if always || a <> b
5522 then
5523 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
5524 and oC s a b =
5525 if always || a <> b
5526 then
5527 Printf.bprintf bb "\n %s='%s'" s (colorspace_to_string a)
5528 and oR s a b =
5529 if always || a <> b
5530 then
5531 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
5532 and os s a b =
5533 if always || a <> b
5534 then
5535 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
5536 and og s a b =
5537 if always || a <> b
5538 then
5539 match a with
5540 | None -> ()
5541 | Some (_N, _A, _B) ->
5542 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
5543 and oW s a b =
5544 if always || a <> b
5545 then
5546 let v =
5547 match a with
5548 | None -> "false"
5549 | Some f ->
5550 if f = infinity
5551 then "true"
5552 else string_of_float f
5554 Printf.bprintf bb "\n %s='%s'" s v
5555 and oco s a b =
5556 if always || a <> b
5557 then
5558 match a with
5559 | Some ((n, a, b), _) when n > 1 ->
5560 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
5561 | _ -> ()
5562 and obeco s a b =
5563 if always || a <> b
5564 then
5565 match a with
5566 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
5567 | _ -> ()
5569 let w, h =
5570 if always
5571 then dc.winw, dc.winh
5572 else
5573 match state.fullscreen with
5574 | Some wh -> wh
5575 | None -> c.winw, c.winh
5577 let zoom, presentation, interpagespace, maxwait =
5578 if always
5579 then dc.zoom, dc.presentation, dc.interpagespace, dc.maxwait
5580 else
5581 match state.mode with
5582 | Birdseye (bc, _, _, _, _) ->
5583 bc.zoom, bc.presentation, bc.interpagespace, bc.maxwait
5584 | _ -> c.zoom, c.presentation, c.interpagespace, c.maxwait
5586 oi "width" w dc.winw;
5587 oi "height" h dc.winh;
5588 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
5589 oi "scroll-handle-height" c.scrollh dc.scrollh;
5590 ob "case-insensitive-search" c.icase dc.icase;
5591 ob "preload" c.preload dc.preload;
5592 oi "page-bias" c.pagebias dc.pagebias;
5593 oi "scroll-step" c.scrollstep dc.scrollstep;
5594 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
5595 ob "max-height-fit" c.maxhfit dc.maxhfit;
5596 ob "crop-hack" c.crophack dc.crophack;
5597 oW "throttle" maxwait dc.maxwait;
5598 ob "highlight-links" c.hlinks dc.hlinks;
5599 ob "under-cursor-info" c.underinfo dc.underinfo;
5600 oi "vertical-margin" interpagespace dc.interpagespace;
5601 oz "zoom" zoom dc.zoom;
5602 ob "presentation" presentation dc.presentation;
5603 oi "rotation-angle" c.angle dc.angle;
5604 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
5605 ob "proportional-display" c.proportional dc.proportional;
5606 oI "pixmap-cache-size" c.memlimit dc.memlimit;
5607 oi "tex-count" c.texcount dc.texcount;
5608 oi "slice-height" c.sliceheight dc.sliceheight;
5609 oi "thumbnail-width" c.thumbw dc.thumbw;
5610 ob "persistent-location" c.jumpback dc.jumpback;
5611 oc "background-color" c.bgcolor dc.bgcolor;
5612 ob "scrollbar-in-presentation" c.scrollbarinpm dc.scrollbarinpm;
5613 oi "tile-width" c.tilew dc.tilew;
5614 oi "tile-height" c.tileh dc.tileh;
5615 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
5616 ob "checkers" c.checkers dc.checkers;
5617 oi "aalevel" c.aalevel dc.aalevel;
5618 ob "trim-margins" c.trimmargins dc.trimmargins;
5619 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
5620 os "uri-launcher" c.urilauncher dc.urilauncher;
5621 oC "color-space" c.colorspace dc.colorspace;
5622 ob "invert-colors" c.invert dc.invert;
5623 oF "brightness" c.colorscale dc.colorscale;
5624 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
5625 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
5626 oco "columns" c.columns dc.columns;
5627 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
5628 if always
5629 then ob "wmclass-hack" !wmclasshack false;
5632 let save () =
5633 let uifontsize = fstate.fontsize in
5634 let bb = Buffer.create 32768 in
5635 let f (h, dc) =
5636 let dc = if conf.bedefault then conf else dc in
5637 Buffer.add_string bb "<llppconfig>\n";
5639 if String.length !fontpath > 0
5640 then
5641 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
5642 uifontsize
5643 !fontpath
5644 else (
5645 if uifontsize <> 14
5646 then
5647 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
5650 Buffer.add_string bb "<defaults ";
5651 add_attrs bb true dc dc;
5652 Buffer.add_string bb "/>\n";
5654 let adddoc path pan anchor c bookmarks =
5655 if bookmarks == [] && c = dc && anchor = emptyanchor
5656 then ()
5657 else (
5658 Printf.bprintf bb "<doc path='%s'"
5659 (enent path 0 (String.length path));
5661 if anchor <> emptyanchor
5662 then (
5663 let n, y = anchor in
5664 Printf.bprintf bb " page='%d'" n;
5665 if y > 1e-6
5666 then
5667 Printf.bprintf bb " rely='%f'" y
5671 if pan != 0
5672 then Printf.bprintf bb " pan='%d'" pan;
5674 add_attrs bb false dc c;
5676 begin match bookmarks with
5677 | [] -> Buffer.add_string bb "/>\n"
5678 | _ ->
5679 Buffer.add_string bb ">\n<bookmarks>\n";
5680 List.iter (fun (title, _level, (page, rely)) ->
5681 Printf.bprintf bb
5682 "<item title='%s' page='%d'"
5683 (enent title 0 (String.length title))
5684 page
5686 if rely > 1e-6
5687 then
5688 Printf.bprintf bb " rely='%f'" rely
5690 Buffer.add_string bb "/>\n";
5691 ) bookmarks;
5692 Buffer.add_string bb "</bookmarks>\n</doc>\n";
5693 end;
5697 let pan, conf =
5698 match state.mode with
5699 | Birdseye (c, pan, _, _, _) ->
5700 let beyecolumns =
5701 match conf.columns with
5702 | Some ((c, _, _), _) -> Some c
5703 | None -> None
5704 and columns =
5705 match c.columns with
5706 | Some (c, _) -> Some (c, [||])
5707 | None -> None
5709 pan, { c with beyecolumns = beyecolumns; columns = columns }
5710 | _ -> state.x, conf
5712 let basename = Filename.basename state.path in
5713 adddoc basename pan (getanchor ())
5714 { conf with
5715 autoscrollstep =
5716 match state.autoscroll with
5717 | Some step -> step
5718 | None -> conf.autoscrollstep }
5719 (if conf.savebmarks then state.bookmarks else []);
5721 Hashtbl.iter (fun path (c, bookmarks, x, y) ->
5722 if basename <> path
5723 then adddoc path x y c bookmarks
5724 ) h;
5725 Buffer.add_string bb "</llppconfig>";
5727 load1 f;
5728 if Buffer.length bb > 0
5729 then
5731 let tmp = !confpath ^ ".tmp" in
5732 let oc = open_out_bin tmp in
5733 Buffer.output_buffer oc bb;
5734 close_out oc;
5735 Unix.rename tmp !confpath;
5736 with exn ->
5737 prerr_endline
5738 ("error while saving configuration: " ^ Printexc.to_string exn)
5740 end;;
5742 let () =
5743 Arg.parse
5744 (Arg.align
5745 [("-p", Arg.String (fun s -> state.password <- s) ,
5746 "<password> Set password");
5748 ("-f", Arg.String (fun s -> Config.fontpath := s),
5749 "<path> Set path to the user interface font");
5751 ("-c", Arg.String (fun s -> Config.confpath := s),
5752 "<path> Set path to the configuration file");
5754 ("-v", Arg.Unit (fun () ->
5755 Printf.printf
5756 "%s\nconfiguration path: %s\n"
5757 (version ())
5758 Config.defconfpath
5760 exit 0), " Print version and exit");
5763 (fun s -> state.path <- s)
5764 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
5766 if String.length state.path = 0
5767 then (prerr_endline "file name missing"; exit 1);
5769 Config.load ();
5771 let _ = Glut.init Sys.argv in
5772 let () = Glut.initDisplayMode ~depth:false ~double_buffer:true () in
5773 let () = Glut.initWindowSize conf.winw conf.winh in
5774 let _ = Glut.createWindow ("llpp " ^ Filename.basename state.path) in
5776 if not (Glut.extensionSupported "GL_ARB_texture_rectangle"
5777 || Glut.extensionSupported "GL_EXT_texture_rectangle")
5778 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
5780 let csock, ssock =
5781 if not is_windows
5782 then
5783 Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0
5784 else
5785 let addr = Unix.ADDR_INET (Unix.inet_addr_loopback, 1337) in
5786 let sock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
5787 Unix.setsockopt sock Unix.SO_REUSEADDR true;
5788 Unix.bind sock addr;
5789 Unix.listen sock 1;
5790 let csock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
5791 Unix.connect csock addr;
5792 let ssock, _ = Unix.accept sock in
5793 Unix.close sock;
5794 let opts sock =
5795 Unix.setsockopt sock Unix.TCP_NODELAY true;
5796 Unix.setsockopt_optint sock Unix.SO_LINGER None;
5798 opts ssock;
5799 opts csock;
5800 ssock, csock
5803 let () = Glut.displayFunc display in
5804 let () = Glut.reshapeFunc reshape in
5805 let () = Glut.keyboardFunc keyboard in
5806 let () = Glut.specialFunc special in
5807 let () = Glut.idleFunc (Some idle) in
5808 let () = Glut.mouseFunc mouse in
5809 let () = Glut.motionFunc motion in
5810 let () = Glut.passiveMotionFunc pmotion in
5812 setcheckers conf.checkers;
5813 init ssock (
5814 conf.angle, conf.proportional, (conf.trimmargins, conf.trimfuzz),
5815 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
5816 !Config.wmclasshack, !Config.fontpath
5818 state.csock <- csock;
5819 state.ssock <- ssock;
5820 state.text <- "Opening " ^ state.path;
5821 setaalevel conf.aalevel;
5822 writeopen state.path state.password;
5823 state.uioh <- uioh;
5824 setfontsize fstate.fontsize;
5826 redirectstderr ();
5828 while true do
5830 Glut.mainLoop ();
5831 with
5832 | Glut.BadEnum "key in special_of_int" ->
5833 showtext '!' " LablGlut bug: special key not recognized";
5835 | Quit ->
5836 wcmd "quit" [];
5837 Config.save ();
5838 exit 0
5840 | exn when conf.redirectstderr ->
5841 let s =
5842 Printf.sprintf "exception %s\n%s"
5843 (Printexc.to_string exn)
5844 (Printexc.get_backtrace ())
5846 ignore (try
5847 Unix.single_write state.stderr s 0 (String.length s);
5848 with _ -> 0);
5849 exit 1
5850 done;