Fix fix
[llpp.git] / main.ml
blobe9f45aa01e4fdc38025ba3fa109437ea81110a68
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 | Pmingw | Pcygwin;;
43 external init : Unix.file_descr -> params -> unit = "ml_init";;
44 external seltext : string -> (int * int * int * int) -> unit = "ml_seltext";;
45 external copysel : string -> unit = "ml_copysel";;
46 external getpdimrect : int -> float array = "ml_getpdimrect";;
47 external whatsunder : string -> int -> int -> under = "ml_whatsunder";;
48 external zoomforh : int -> int -> int -> float = "ml_zoom_for_height";;
49 external drawstr : int -> int -> int -> string -> float = "ml_draw_string";;
50 external measurestr : int -> string -> float = "ml_measure_string";;
51 external getmaxw : unit -> float = "ml_getmaxw";;
52 external postprocess : opaque -> bool -> int -> int -> unit = "ml_postprocess";;
53 external pagebbox : opaque -> (int * int * int * int) = "ml_getpagebox";;
54 external platform : unit -> platform = "ml_platform";;
55 external setaalevel : int -> unit = "ml_setaalevel";;
56 external realloctexts : int -> bool = "ml_realloctexts";;
58 let platform_to_string = function
59 | Punknown -> "unknown"
60 | Plinux -> "Linux"
61 | Pwindows -> "Windows"
62 | Posx -> "OSX"
63 | Psun -> "Sun"
64 | Pfreebsd -> "FreeBSD"
65 | Pdragonflybsd -> "DragonflyBSD"
66 | Popenbsd -> "OpenBSD"
67 | Pcygwin -> "Cygwin"
68 | Pmingw -> "MingW"
71 let platform = platform ();;
73 let is_windows =
74 match platform with
75 | Pwindows | Pmingw -> true
76 | _ -> false
79 type x = int
80 and y = int
81 and tilex = int
82 and tiley = int
83 and tileparams = (x * y * width * height * tilex * tiley)
86 external drawtile : tileparams -> opaque -> unit = "ml_drawtile";;
88 type mpos = int * int
89 and mstate =
90 | Msel of (mpos * mpos)
91 | Mpan of mpos
92 | Mscrolly | Mscrollx
93 | Mzoom of (int * int)
94 | Mzoomrect of (mpos * mpos)
95 | Mnone
98 type textentry = string * string * onhist option * onkey * ondone
99 and onkey = string -> int -> te
100 and ondone = string -> unit
101 and histcancel = unit -> unit
102 and onhist = ((histcmd -> string) * histcancel)
103 and histcmd = HCnext | HCprev | HCfirst | HClast
104 and te =
105 | TEstop
106 | TEdone of string
107 | TEcont of string
108 | TEswitch of textentry
111 type 'a circbuf =
112 { store : 'a array
113 ; mutable rc : int
114 ; mutable wc : int
115 ; mutable len : int
119 let bound v minv maxv =
120 max minv (min maxv v);
123 let cbnew n v =
124 { store = Array.create n v
125 ; rc = 0
126 ; wc = 0
127 ; len = 0
131 let drawstring size x y s =
132 Gl.enable `blend;
133 Gl.enable `texture_2d;
134 ignore (drawstr size x y s);
135 Gl.disable `blend;
136 Gl.disable `texture_2d;
139 let drawstring1 size x y s =
140 drawstr size x y s;
143 let drawstring2 size x y fmt =
144 Printf.kprintf (drawstring size (x+1) (y+size+1)) fmt
147 let cbcap b = Array.length b.store;;
149 let cbput b v =
150 let cap = cbcap b in
151 b.store.(b.wc) <- v;
152 b.wc <- (b.wc + 1) mod cap;
153 b.rc <- b.wc;
154 b.len <- min (b.len + 1) cap;
157 let cbempty b = b.len = 0;;
159 let cbgetg b circular dir =
160 if cbempty b
161 then b.store.(0)
162 else
163 let rc = b.rc + dir in
164 let rc =
165 if circular
166 then (
167 if rc = -1
168 then b.len-1
169 else (
170 if rc = b.len
171 then 0
172 else rc
175 else max 0 (min rc (b.len-1))
177 b.rc <- rc;
178 b.store.(rc);
181 let cbget b = cbgetg b false;;
182 let cbgetc b = cbgetg b true;;
184 type page =
185 { pageno : int
186 ; pagedimno : int
187 ; pagew : int
188 ; pageh : int
189 ; pagex : int
190 ; pagey : int
191 ; pagevw : int
192 ; pagevh : int
193 ; pagedispx : int
194 ; pagedispy : int
198 let debugl l =
199 dolog "l %d dim=%d {" l.pageno l.pagedimno;
200 dolog " WxH %dx%d" l.pagew l.pageh;
201 dolog " vWxH %dx%d" l.pagevw l.pagevh;
202 dolog " pagex,y %d,%d" l.pagex l.pagey;
203 dolog " dispx,y %d,%d" l.pagedispx l.pagedispy;
204 dolog "}";
207 let debugrect (x0, y0, x1, y1, x2, y2, x3, y3) =
208 dolog "rect {";
209 dolog " x0,y0=(% f, % f)" x0 y0;
210 dolog " x1,y1=(% f, % f)" x1 y1;
211 dolog " x2,y2=(% f, % f)" x2 y2;
212 dolog " x3,y3=(% f, % f)" x3 y3;
213 dolog "}";
216 type columns =
217 multicol * ((pdimno * x * y * (pageno * width * height * leftx)) array)
218 and multicol = columncount * covercount * covercount
219 and pdimno = int
220 and columncount = int
221 and covercount = int;;
223 type conf =
224 { mutable scrollbw : int
225 ; mutable scrollh : int
226 ; mutable icase : bool
227 ; mutable preload : bool
228 ; mutable pagebias : int
229 ; mutable verbose : bool
230 ; mutable debug : bool
231 ; mutable scrollstep : int
232 ; mutable maxhfit : bool
233 ; mutable crophack : bool
234 ; mutable autoscrollstep : int
235 ; mutable maxwait : float option
236 ; mutable hlinks : bool
237 ; mutable underinfo : bool
238 ; mutable interpagespace : interpagespace
239 ; mutable zoom : float
240 ; mutable presentation : bool
241 ; mutable angle : angle
242 ; mutable winw : int
243 ; mutable winh : int
244 ; mutable savebmarks : bool
245 ; mutable proportional : proportional
246 ; mutable trimmargins : trimmargins
247 ; mutable trimfuzz : irect
248 ; mutable memlimit : memsize
249 ; mutable texcount : texcount
250 ; mutable sliceheight : sliceheight
251 ; mutable thumbw : width
252 ; mutable jumpback : bool
253 ; mutable bgcolor : float * float * float
254 ; mutable bedefault : bool
255 ; mutable scrollbarinpm : bool
256 ; mutable tilew : int
257 ; mutable tileh : int
258 ; mutable mustoresize : memsize
259 ; mutable checkers : bool
260 ; mutable aalevel : int
261 ; mutable urilauncher : string
262 ; mutable colorspace : colorspace
263 ; mutable invert : bool
264 ; mutable colorscale : float
265 ; mutable redirectstderr : bool
266 ; mutable ghyllscroll : (int * int * int) option
267 ; mutable columns : columns option
268 ; mutable beyecolumns : columncount option
272 type anchor = pageno * top;;
274 type outline = string * int * anchor;;
276 type rect = float * float * float * float * float * float * float * float;;
278 type tile = opaque * pixmapsize * elapsed
279 and elapsed = float;;
280 type pagemapkey = pageno * gen;;
281 type tilemapkey = pageno * gen * colorspace * angle * width * height * col * row
282 and row = int
283 and col = int;;
285 let emptyanchor = (0, 0.0);;
287 type infochange = | Memused | Docinfo | Pdim;;
289 class type uioh = object
290 method display : unit
291 method key : int -> uioh
292 method special : Glut.special_key_t -> uioh
293 method button :
294 Glut.button_t -> Glut.mouse_button_state_t -> int -> int -> uioh
295 method motion : int -> int -> uioh
296 method pmotion : int -> int -> uioh
297 method infochanged : infochange -> unit
298 method scrollpw : (int * float * float)
299 method scrollph : (int * float * float)
300 end;;
302 type mode =
303 | Birdseye of (conf * leftx * pageno * pageno * anchor)
304 | Textentry of (textentry * onleave)
305 | View
306 and onleave = leavetextentrystatus -> unit
307 and leavetextentrystatus = | Cancel | Confirm
308 and helpitem = string * int * action
309 and action =
310 | Noaction
311 | Action of (uioh -> uioh)
314 let isbirdseye = function Birdseye _ -> true | _ -> false;;
315 let istextentry = function Textentry _ -> true | _ -> false;;
317 type currently =
318 | Idle
319 | Loading of (page * gen)
320 | Tiling of (
321 page * opaque * colorspace * angle * gen * col * row * width * height
323 | Outlining of outline list
326 let nouioh : uioh = object (self)
327 method display = ()
328 method key _ = self
329 method special _ = self
330 method button _ _ _ _ = self
331 method motion _ _ = self
332 method pmotion _ _ = self
333 method infochanged _ = ()
334 method scrollpw = (0, nan, nan)
335 method scrollph = (0, nan, nan)
336 end;;
338 type state =
339 { mutable csock : Unix.file_descr
340 ; mutable ssock : Unix.file_descr
341 ; mutable errfd : Unix.file_descr option
342 ; mutable stderr : Unix.file_descr
343 ; mutable errmsgs : Buffer.t
344 ; mutable newerrmsgs : bool
345 ; mutable w : int
346 ; mutable x : int
347 ; mutable y : int
348 ; mutable scrollw : int
349 ; mutable hscrollh : int
350 ; mutable anchor : anchor
351 ; mutable maxy : int
352 ; mutable layout : page list
353 ; pagemap : (pagemapkey, opaque) Hashtbl.t
354 ; tilemap : (tilemapkey, tile) Hashtbl.t
355 ; tilelru : (tilemapkey * opaque * pixmapsize) Queue.t
356 ; mutable pdims : (pageno * width * height * leftx) list
357 ; mutable pagecount : int
358 ; mutable currently : currently
359 ; mutable mstate : mstate
360 ; mutable searchpattern : string
361 ; mutable rects : (pageno * recttype * rect) list
362 ; mutable rects1 : (pageno * recttype * rect) list
363 ; mutable text : string
364 ; mutable fullscreen : (width * height) option
365 ; mutable mode : mode
366 ; mutable uioh : uioh
367 ; mutable outlines : outline array
368 ; mutable bookmarks : outline list
369 ; mutable path : string
370 ; mutable password : string
371 ; mutable invalidated : int
372 ; mutable memused : memsize
373 ; mutable gen : gen
374 ; mutable throttle : (page list * int * float) option
375 ; mutable autoscroll : int option
376 ; mutable ghyll : int option -> unit
377 ; mutable help : helpitem array
378 ; mutable docinfo : (int * string) list
379 ; mutable deadline : float
380 ; mutable texid : GlTex.texture_id option
381 ; hists : hists
382 ; mutable prevzoom : float
383 ; mutable progress : float
385 and hists =
386 { pat : string circbuf
387 ; pag : string circbuf
388 ; nav : anchor circbuf
392 let defconf =
393 { scrollbw = 7
394 ; scrollh = 12
395 ; icase = true
396 ; preload = true
397 ; pagebias = 0
398 ; verbose = false
399 ; debug = false
400 ; scrollstep = 24
401 ; maxhfit = true
402 ; crophack = false
403 ; autoscrollstep = 2
404 ; maxwait = None
405 ; hlinks = false
406 ; underinfo = false
407 ; interpagespace = 2
408 ; zoom = 1.0
409 ; presentation = false
410 ; angle = 0
411 ; winw = 900
412 ; winh = 900
413 ; savebmarks = true
414 ; proportional = true
415 ; trimmargins = false
416 ; trimfuzz = (0,0,0,0)
417 ; memlimit = 32 lsl 20
418 ; texcount = 256
419 ; sliceheight = 24
420 ; thumbw = 76
421 ; jumpback = true
422 ; bgcolor = (0.5, 0.5, 0.5)
423 ; bedefault = false
424 ; scrollbarinpm = true
425 ; tilew = 2048
426 ; tileh = 2048
427 ; mustoresize = 128 lsl 20
428 ; checkers = true
429 ; aalevel = 8
430 ; urilauncher =
431 (match platform with
432 | Plinux | Pfreebsd | Pdragonflybsd | Popenbsd | Psun -> "xdg-open \"%s\""
433 | Posx -> "open \"%s\""
434 | Pwindows | Pcygwin | Pmingw -> "iexplore \"%s\""
435 | _ -> "")
436 ; colorspace = Rgb
437 ; invert = false
438 ; colorscale = 1.0
439 ; redirectstderr = false
440 ; ghyllscroll = None
441 ; columns = None
442 ; beyecolumns = None
446 let conf = { defconf with angle = defconf.angle };;
448 type fontstate =
449 { mutable fontsize : int
450 ; mutable wwidth : float
451 ; mutable maxrows : int
455 let fstate =
456 { fontsize = 14
457 ; wwidth = nan
458 ; maxrows = -1
462 let setfontsize n =
463 fstate.fontsize <- n;
464 fstate.wwidth <- measurestr fstate.fontsize "w";
465 fstate.maxrows <- (conf.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
468 let gotouri uri =
469 if String.length conf.urilauncher = 0
470 then print_endline uri
471 else
472 let re = Str.regexp "%s" in
473 let command = Str.global_replace re uri conf.urilauncher in
474 let optic =
475 try Some (Unix.open_process_in command)
476 with exn ->
477 Printf.eprintf
478 "failed to execute `%s': %s\n" command (Printexc.to_string exn);
479 flush stderr;
480 None
482 match optic with
483 | Some ic -> close_in ic
484 | None -> ()
487 let version () =
488 Printf.sprintf "llpp version %s (%s/%dbit, ocaml %s)" Help.version
489 (platform_to_string platform) Sys.word_size Sys.ocaml_version
492 let makehelp () =
493 let strings = version () :: "" :: Help.keys in
494 Array.of_list (
495 let r = Str.regexp "\\(http://[^ ]+\\)" in
496 List.map (fun s ->
497 if (try Str.search_forward r s 0 with Not_found -> -1) >= 0
498 then
499 let uri = Str.matched_string s in
500 (s, 0, Action (fun u -> gotouri uri; u))
501 else s, 0, Noaction) strings
505 let noghyll _ = ();;
507 let state =
508 { csock = Unix.stdin
509 ; ssock = Unix.stdin
510 ; errfd = None
511 ; stderr = Unix.stderr
512 ; errmsgs = Buffer.create 0
513 ; newerrmsgs = false
514 ; x = 0
515 ; y = 0
516 ; w = 0
517 ; scrollw = 0
518 ; hscrollh = 0
519 ; anchor = emptyanchor
520 ; layout = []
521 ; maxy = max_int
522 ; tilelru = Queue.create ()
523 ; pagemap = Hashtbl.create 10
524 ; tilemap = Hashtbl.create 10
525 ; pdims = []
526 ; pagecount = 0
527 ; currently = Idle
528 ; mstate = Mnone
529 ; rects = []
530 ; rects1 = []
531 ; text = ""
532 ; mode = View
533 ; fullscreen = None
534 ; searchpattern = ""
535 ; outlines = [||]
536 ; bookmarks = []
537 ; path = ""
538 ; password = ""
539 ; invalidated = 0
540 ; hists =
541 { nav = cbnew 10 (0, 0.0)
542 ; pat = cbnew 1 ""
543 ; pag = cbnew 1 ""
545 ; memused = 0
546 ; gen = 0
547 ; throttle = None
548 ; autoscroll = None
549 ; ghyll = noghyll
550 ; help = makehelp ()
551 ; docinfo = []
552 ; deadline = nan
553 ; texid = None
554 ; prevzoom = 1.0
555 ; progress = -1.0
556 ; uioh = nouioh
560 let vlog fmt =
561 if conf.verbose
562 then
563 Printf.kprintf prerr_endline fmt
564 else
565 Printf.kprintf ignore fmt
568 let redirectstderr () =
569 if conf.redirectstderr
570 then
571 let rfd, wfd = Unix.pipe () in
572 state.stderr <- Unix.dup Unix.stderr;
573 state.errfd <- Some rfd;
574 Unix.dup2 wfd Unix.stderr;
575 else (
576 state.newerrmsgs <- false;
577 begin match state.errfd with
578 | Some fd ->
579 Unix.close fd;
580 Unix.dup2 state.stderr Unix.stderr;
581 state.errfd <- None;
582 | None -> ()
583 end;
584 prerr_string (Buffer.contents state.errmsgs);
585 flush stderr;
586 Buffer.clear state.errmsgs;
590 module G =
591 struct
592 let postRedisplay who =
593 if conf.verbose
594 then prerr_endline ("redisplay for " ^ who);
595 Glut.postRedisplay ();
597 end;;
599 let addchar s c =
600 let b = Buffer.create (String.length s + 1) in
601 Buffer.add_string b s;
602 Buffer.add_char b c;
603 Buffer.contents b;
606 let colorspace_of_string s =
607 match String.lowercase s with
608 | "rgb" -> Rgb
609 | "bgr" -> Bgr
610 | "gray" -> Gray
611 | _ -> failwith "invalid colorspace"
614 let int_of_colorspace = function
615 | Rgb -> 0
616 | Bgr -> 1
617 | Gray -> 2
620 let colorspace_of_int = function
621 | 0 -> Rgb
622 | 1 -> Bgr
623 | 2 -> Gray
624 | n -> failwith ("invalid colorspace index " ^ string_of_int n)
627 let colorspace_to_string = function
628 | Rgb -> "rgb"
629 | Bgr -> "bgr"
630 | Gray -> "gray"
633 let intentry_with_suffix text key =
634 let c = Char.unsafe_chr key in
635 match Char.lowercase c with
636 | '0' .. '9' ->
637 let text = addchar text c in
638 TEcont text
640 | 'k' | 'm' | 'g' ->
641 let text = addchar text c in
642 TEcont text
644 | _ ->
645 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
646 TEcont text
649 let columns_to_string (n, a, b) =
650 if a = 0 && b = 0
651 then Printf.sprintf "%d" n
652 else Printf.sprintf "%d,%d,%d" n a b;
655 let columns_of_string s =
657 (int_of_string s, 0, 0)
658 with _ ->
659 Scanf.sscanf s "%u,%u,%u" (fun n a b -> (n, a, b));
662 let writecmd fd s =
663 let len = String.length s in
664 let n = 4 + len in
665 let b = Buffer.create n in
666 Buffer.add_char b (Char.chr ((len lsr 24) land 0xff));
667 Buffer.add_char b (Char.chr ((len lsr 16) land 0xff));
668 Buffer.add_char b (Char.chr ((len lsr 8) land 0xff));
669 Buffer.add_char b (Char.chr ((len lsr 0) land 0xff));
670 Buffer.add_string b s;
671 let s' = Buffer.contents b in
672 let n' = Unix.write fd s' 0 n in
673 if n' != n then failwith "write failed";
676 let readcmd fd =
677 let s = "xxxx" in
678 let n = Unix.read fd s 0 4 in
679 if n != 4 then failwith "incomplete read(len)";
680 let len = 0
681 lor (Char.code s.[0] lsl 24)
682 lor (Char.code s.[1] lsl 16)
683 lor (Char.code s.[2] lsl 8)
684 lor (Char.code s.[3] lsl 0)
686 let s = String.create len in
687 let n = Unix.read fd s 0 len in
688 if n != len then failwith "incomplete read(data)";
692 let makecmd s l =
693 let b = Buffer.create 10 in
694 Buffer.add_string b s;
695 let rec combine = function
696 | [] -> b
697 | x :: xs ->
698 Buffer.add_char b ' ';
699 let s =
700 match x with
701 | `b b -> if b then "1" else "0"
702 | `s s -> s
703 | `i i -> string_of_int i
704 | `f f -> string_of_float f
705 | `I f -> string_of_int (truncate f)
707 Buffer.add_string b s;
708 combine xs;
710 combine l;
713 let wcmd s l =
714 let cmd = Buffer.contents (makecmd s l) in
715 writecmd state.csock cmd;
718 let calcips h =
719 if conf.presentation
720 then
721 let d = conf.winh - h in
722 max 0 ((d + 1) / 2)
723 else
724 conf.interpagespace
727 let calcheight () =
728 let rec f pn ph pi fh l =
729 match l with
730 | (n, _, h, _) :: rest ->
731 let ips = calcips h in
732 let fh =
733 if conf.presentation
734 then fh+ips
735 else (
736 if isbirdseye state.mode && pn = 0
737 then fh + ips
738 else fh
741 let fh = fh + ((n - pn) * (ph + pi)) in
742 f n h ips fh rest;
744 | [] ->
745 let inc =
746 if conf.presentation || (isbirdseye state.mode && pn = 0)
747 then 0
748 else -pi
750 let fh = fh + ((state.pagecount - pn) * (ph + pi)) + inc in
751 max 0 fh
753 let fh = f 0 0 0 0 state.pdims in
757 let calcheight () =
758 match conf.columns with
759 | None -> calcheight ()
760 | Some (_, b) ->
761 if Array.length b > 0
762 then
763 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
764 y + h
765 else 0
768 let getpageyh pageno =
769 let rec f pn ph pi y l =
770 match l with
771 | (n, _, h, _) :: rest ->
772 let ips = calcips h in
773 if n >= pageno
774 then
775 let h = if n = pageno then h else ph in
776 if conf.presentation && n = pageno
777 then
778 y + (pageno - pn) * (ph + pi) + pi, h
779 else
780 y + (pageno - pn) * (ph + pi), h
781 else
782 let y = y + (if conf.presentation then pi else 0) in
783 let y = y + (n - pn) * (ph + pi) in
784 f n h ips y rest
786 | [] ->
787 y + (pageno - pn) * (ph + pi), ph
789 f 0 0 0 0 state.pdims
792 let getpageyh pageno =
793 match conf.columns with
794 | None -> getpageyh pageno
795 | Some (_, b) ->
796 let (_, _, y, (_, _, h, _)) = b.(pageno) in
797 y, h
800 let getpagedim pageno =
801 let rec f ppdim l =
802 match l with
803 | (n, _, _, _) as pdim :: rest ->
804 if n >= pageno
805 then (if n = pageno then pdim else ppdim)
806 else f pdim rest
808 | [] -> ppdim
810 f (-1, -1, -1, -1) state.pdims
813 let getpagey pageno = fst (getpageyh pageno);;
815 let layout1 y sh =
816 let sh = sh - state.hscrollh in
817 let rec f ~pageno ~pdimno ~prev ~py ~dy ~pdims ~accu =
818 let ((w, h, ips, xoff) as curr), rest, pdimno, yinc =
819 match pdims with
820 | (pageno', w, h, xoff) :: rest when pageno' = pageno ->
821 let ips = calcips h in
822 let yinc =
823 if conf.presentation || (isbirdseye state.mode && pageno = 0)
824 then ips
825 else 0
827 (w, h, ips, xoff), rest, pdimno + 1, yinc
828 | _ ->
829 prev, pdims, pdimno, 0
831 let dy = dy + yinc in
832 let py = py + yinc in
833 if pageno = state.pagecount || dy >= sh
834 then
835 accu
836 else
837 let vy = y + dy in
838 if py + h <= vy - yinc
839 then
840 let py = py + h + ips in
841 let dy = max 0 (py - y) in
842 f ~pageno:(pageno+1)
843 ~pdimno
844 ~prev:curr
847 ~pdims:rest
848 ~accu
849 else
850 let pagey = vy - py in
851 let pagevh = h - pagey in
852 let pagevh = min (sh - dy) pagevh in
853 let off = if yinc > 0 then py - vy else 0 in
854 let py = py + h + ips in
855 let pagex, dx =
856 let xoff = xoff +
857 if state.w < conf.winw - state.scrollw
858 then (conf.winw - state.scrollw - state.w) / 2
859 else 0
861 let dispx = xoff + state.x in
862 if dispx < 0
863 then (-dispx, 0)
864 else (0, dispx)
866 let pagevw =
867 let lw = w - pagex in
868 min lw (conf.winw - state.scrollw)
870 let e =
871 { pageno = pageno
872 ; pagedimno = pdimno
873 ; pagew = w
874 ; pageh = h
875 ; pagex = pagex
876 ; pagey = pagey + off
877 ; pagevw = pagevw
878 ; pagevh = pagevh - off
879 ; pagedispx = dx
880 ; pagedispy = dy + off
883 let accu = e :: accu in
884 f ~pageno:(pageno+1)
885 ~pdimno
886 ~prev:curr
888 ~dy:(dy+pagevh+ips)
889 ~pdims:rest
890 ~accu
892 if state.invalidated = 0
893 then (
894 let accu =
896 ~pageno:0
897 ~pdimno:~-1
898 ~prev:(0,0,0,0)
899 ~py:0
900 ~dy:0
901 ~pdims:state.pdims
902 ~accu:[]
904 List.rev accu
906 else
910 let layoutN (_, b) y sh =
911 let sh = sh - state.hscrollh in
912 let rec fold accu n =
913 if n = Array.length b
914 then accu
915 else
916 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
917 if (vy - y) > sh
918 then accu
919 else
920 let accu =
921 if vy + h > y
922 then
923 let pagey = max 0 (y - vy) in
924 let pagedispy = if pagey > 0 then 0 else vy - y in
925 let pagedispx, pagex, pagevw =
926 let pdx = dx + xoff + state.x in
927 if pdx < 0
928 then 0, -pdx, w + pdx
929 else pdx, 0, min (conf.winw - state.scrollw) w
931 let pagevh = h - pagey in
932 if pagedispx < conf.winw - state.scrollw && pagevw > 0 && pagevh > 0
933 then
934 let e =
935 { pageno = n
936 ; pagedimno = pdimno
937 ; pagew = w
938 ; pageh = h
939 ; pagex = pagex
940 ; pagey = pagey
941 ; pagevw = pagevw
942 ; pagevh = pagevh
943 ; pagedispx = pagedispx
944 ; pagedispy = pagedispy
947 e :: accu
948 else
949 accu
950 else
951 accu
953 fold accu (n+1)
955 if state.invalidated = 0
956 then List.rev (fold [] 0)
957 else []
960 let layout y sh =
961 match conf.columns with
962 | None -> layout1 y sh
963 | Some c -> layoutN c y sh
966 let clamp incr =
967 let y = state.y + incr in
968 let y = max 0 y in
969 let y = min y (state.maxy - (if conf.maxhfit then conf.winh else 0)) in
973 let getopaque pageno =
974 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
975 with Not_found -> None
978 let putopaque pageno opaque =
979 Hashtbl.replace state.pagemap (pageno, state.gen) opaque
982 let itertiles l f =
983 let tilex = l.pagex mod conf.tilew in
984 let tiley = l.pagey mod conf.tileh in
986 let col = l.pagex / conf.tilew in
987 let row = l.pagey / conf.tileh in
989 let vw =
990 let a = l.pagew - l.pagex in
991 let b = conf.winw - state.scrollw in
992 min a b
993 and vh = l.pagevh in
995 let rec rowloop row y0 dispy h =
996 if h = 0
997 then ()
998 else (
999 let dh = conf.tileh - y0 in
1000 let dh = min h dh in
1001 let rec colloop col x0 dispx w =
1002 if w = 0
1003 then ()
1004 else (
1005 let dw = conf.tilew - x0 in
1006 let dw = min w dw in
1008 f col row dispx dispy x0 y0 dw dh;
1009 colloop (col+1) 0 (dispx+dw) (w-dw)
1012 colloop col tilex l.pagedispx vw;
1013 rowloop (row+1) 0 (dispy+dh) (h-dh)
1016 if vw > 0 && vh > 0
1017 then rowloop row tiley l.pagedispy vh;
1020 let gettileopaque l col row =
1021 let key =
1022 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
1024 try Some (Hashtbl.find state.tilemap key)
1025 with Not_found -> None
1028 let puttileopaque l col row gen colorspace angle opaque size elapsed =
1029 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
1030 Hashtbl.add state.tilemap key (opaque, size, elapsed)
1033 let drawtiles l color =
1034 GlDraw.color color;
1035 let f col row x y tilex tiley w h =
1036 match gettileopaque l col row with
1037 | Some (opaque, _, t) ->
1038 let params = x, y, w, h, tilex, tiley in
1039 if conf.invert
1040 then (
1041 Gl.enable `blend;
1042 GlFunc.blend_func `zero `one_minus_src_color;
1044 drawtile params opaque;
1045 if conf.invert
1046 then Gl.disable `blend;
1047 if conf.debug
1048 then (
1049 let s = Printf.sprintf
1050 "%d[%d,%d] %f sec"
1051 l.pageno col row t
1053 let w = measurestr fstate.fontsize s in
1054 GlMisc.push_attrib [`current];
1055 GlDraw.color (0.0, 0.0, 0.0);
1056 GlDraw.rect
1057 (float (x-2), float (y-2))
1058 (float (x+2) +. w, float (y + fstate.fontsize + 2));
1059 GlDraw.color (1.0, 1.0, 1.0);
1060 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
1061 GlMisc.pop_attrib ();
1064 | _ ->
1065 let w =
1066 let lw = conf.winw - state.scrollw - x in
1067 min lw w
1068 and h =
1069 let lh = conf.winh - y in
1070 min lh h
1072 Gl.enable `texture_2d;
1073 begin match state.texid with
1074 | Some id ->
1075 GlTex.bind_texture `texture_2d id;
1076 let x0 = float x
1077 and y0 = float y
1078 and x1 = float (x+w)
1079 and y1 = float (y+h) in
1081 let tw = float w /. 64.0
1082 and th = float h /. 64.0 in
1083 let tx0 = float tilex /. 64.0
1084 and ty0 = float tiley /. 64.0 in
1085 let tx1 = tx0 +. tw
1086 and ty1 = ty0 +. th in
1087 GlDraw.begins `quads;
1088 GlTex.coord2 (tx0, ty0); GlDraw.vertex2 (x0, y0);
1089 GlTex.coord2 (tx0, ty1); GlDraw.vertex2 (x0, y1);
1090 GlTex.coord2 (tx1, ty1); GlDraw.vertex2 (x1, y1);
1091 GlTex.coord2 (tx1, ty0); GlDraw.vertex2 (x1, y0);
1092 GlDraw.ends ();
1094 Gl.disable `texture_2d;
1095 | None ->
1096 GlDraw.color (1.0, 1.0, 1.0);
1097 GlDraw.rect
1098 (float x, float y)
1099 (float (x+w), float (y+h));
1100 end;
1101 if w > 128 && h > fstate.fontsize + 10
1102 then (
1103 GlDraw.color (0.0, 0.0, 0.0);
1104 let c, r =
1105 if conf.verbose
1106 then (col*conf.tilew, row*conf.tileh)
1107 else col, row
1109 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
1111 GlDraw.color color;
1113 itertiles l f
1116 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
1118 let tilevisible1 l x y =
1119 let ax0 = l.pagex
1120 and ax1 = l.pagex + l.pagevw
1121 and ay0 = l.pagey
1122 and ay1 = l.pagey + l.pagevh in
1124 let bx0 = x
1125 and by0 = y in
1126 let bx1 = min (bx0 + conf.tilew) l.pagew
1127 and by1 = min (by0 + conf.tileh) l.pageh in
1129 let rx0 = max ax0 bx0
1130 and ry0 = max ay0 by0
1131 and rx1 = min ax1 bx1
1132 and ry1 = min ay1 by1 in
1134 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
1135 nonemptyintersection
1138 let tilevisible layout n x y =
1139 let rec findpageinlayout = function
1140 | l :: _ when l.pageno = n -> tilevisible1 l x y
1141 | _ :: rest -> findpageinlayout rest
1142 | [] -> false
1144 findpageinlayout layout
1147 let tileready l x y =
1148 tilevisible1 l x y &&
1149 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
1152 let tilepage n p layout =
1153 let rec loop = function
1154 | l :: rest ->
1155 if l.pageno = n
1156 then
1157 let f col row _ _ _ _ _ _ =
1158 if state.currently = Idle
1159 then
1160 match gettileopaque l col row with
1161 | Some _ -> ()
1162 | None ->
1163 let x = col*conf.tilew
1164 and y = row*conf.tileh in
1165 let w =
1166 let w = l.pagew - x in
1167 min w conf.tilew
1169 let h =
1170 let h = l.pageh - y in
1171 min h conf.tileh
1173 wcmd "tile"
1174 [`s p
1175 ;`i x
1176 ;`i y
1177 ;`i w
1178 ;`i h
1180 state.currently <-
1181 Tiling (
1182 l, p, conf.colorspace, conf.angle, state.gen, col, row,
1183 conf.tilew, conf.tileh
1186 itertiles l f;
1187 else
1188 loop rest
1190 | [] -> ()
1192 if state.invalidated = 0 then loop layout;
1195 let preloadlayout visiblepages =
1196 let presentation = conf.presentation in
1197 let interpagespace = conf.interpagespace in
1198 let maxy = state.maxy in
1199 conf.presentation <- false;
1200 conf.interpagespace <- 0;
1201 state.maxy <- calcheight ();
1202 let y =
1203 match visiblepages with
1204 | [] -> 0
1205 | l :: _ -> getpagey l.pageno + l.pagey
1207 let y = if y < conf.winh then 0 else y - conf.winh in
1208 let h = state.y - y + conf.winh*3 in
1209 let pages = layout y h in
1210 conf.presentation <- presentation;
1211 conf.interpagespace <- interpagespace;
1212 state.maxy <- maxy;
1213 pages;
1216 let load pages =
1217 let rec loop pages =
1218 if state.currently != Idle
1219 then ()
1220 else
1221 match pages with
1222 | l :: rest ->
1223 begin match getopaque l.pageno with
1224 | None ->
1225 wcmd "page" [`i l.pageno; `i l.pagedimno];
1226 state.currently <- Loading (l, state.gen);
1227 | Some opaque ->
1228 tilepage l.pageno opaque pages;
1229 loop rest
1230 end;
1231 | _ -> ()
1233 if state.invalidated = 0 then loop pages
1236 let preload pages =
1237 load pages;
1238 if conf.preload && state.currently = Idle
1239 then load (preloadlayout pages);
1242 let layoutready layout =
1243 let rec fold all ls =
1244 all && match ls with
1245 | l :: rest ->
1246 let seen = ref false in
1247 let allvisible = ref true in
1248 let foo col row _ _ _ _ _ _ =
1249 seen := true;
1250 allvisible := !allvisible &&
1251 begin match gettileopaque l col row with
1252 | Some _ -> true
1253 | None -> false
1256 itertiles l foo;
1257 fold (!seen && !allvisible) rest
1258 | [] -> true
1260 let alltilesvisible = fold true layout in
1261 alltilesvisible;
1264 let gotoy y =
1265 let y = bound y 0 state.maxy in
1266 let y, layout, proceed =
1267 match conf.maxwait with
1268 | Some time when state.ghyll == noghyll ->
1269 begin match state.throttle with
1270 | None ->
1271 let layout = layout y conf.winh in
1272 let ready = layoutready layout in
1273 if not ready
1274 then (
1275 load layout;
1276 state.throttle <- Some (layout, y, now ());
1278 else G.postRedisplay "gotoy showall (None)";
1279 y, layout, ready
1280 | Some (_, _, started) ->
1281 let dt = now () -. started in
1282 if dt > time
1283 then (
1284 state.throttle <- None;
1285 let layout = layout y conf.winh in
1286 load layout;
1287 G.postRedisplay "maxwait";
1288 y, layout, true
1290 else -1, [], false
1293 | _ ->
1294 let layout = layout y conf.winh in
1295 if true || layoutready layout
1296 then G.postRedisplay "gotoy ready";
1297 y, layout, true
1299 if proceed
1300 then (
1301 state.y <- y;
1302 state.layout <- layout;
1303 begin match state.mode with
1304 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
1305 if not (pagevisible layout pageno)
1306 then (
1307 match state.layout with
1308 | [] -> ()
1309 | l :: _ ->
1310 state.mode <- Birdseye (
1311 conf, leftx, l.pageno, hooverpageno, anchor
1314 | _ -> ()
1315 end;
1316 preload layout;
1318 state.ghyll <- noghyll;
1321 let conttiling pageno opaque =
1322 tilepage pageno opaque
1323 (if conf.preload then preloadlayout state.layout else state.layout)
1326 let gotoy_and_clear_text y =
1327 gotoy y;
1328 if not conf.verbose then state.text <- "";
1331 let getanchor () =
1332 match state.layout with
1333 | [] -> emptyanchor
1334 | l :: _ -> (l.pageno, float l.pagey /. float l.pageh)
1337 let getanchory (n, top) =
1338 let y, h = getpageyh n in
1339 y + (truncate (top *. float h));
1342 let gotoanchor anchor =
1343 gotoy (getanchory anchor);
1346 let addnav () =
1347 cbput state.hists.nav (getanchor ());
1350 let getnav dir =
1351 let anchor = cbgetc state.hists.nav dir in
1352 getanchory anchor;
1355 let gotoghyll y =
1356 let rec scroll f n a b =
1357 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
1358 let snake f a b =
1359 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
1360 if f < a
1361 then s (float f /. float a)
1362 else (
1363 if f > b
1364 then 1.0 -. s ((float (f-b) /. float (n-b)))
1365 else 1.0
1368 snake f a b
1369 and summa f n a b =
1370 (* courtesy:
1371 http://integrals.wolfram.com/index.jsp?expr=3x%5E2-2x%5E3&random=false *)
1372 let iv x = -.((-.2.0 +. x)*.x**3.0)/.2.0 in
1373 let iv1 = iv f in
1374 let ins = float a *. iv1
1375 and outs = float (n-b) *. iv1 in
1376 let ones = b - a in
1377 ins +. outs +. float ones
1379 let rec set (_N, _A, _B) y sy =
1380 let sum = summa 1.0 _N _A _B in
1381 let dy = float (y - sy) in
1382 state.ghyll <- (
1383 let rec gf n y1 o =
1384 if n >= _N
1385 then state.ghyll <- noghyll
1386 else
1387 let go n =
1388 let s = scroll n _N _A _B in
1389 let y1 = y1 +. ((s *. dy) /. sum) in
1390 gotoy_and_clear_text (truncate y1);
1391 state.ghyll <- gf (n+1) y1;
1393 match o with
1394 | None -> go n
1395 | Some y' -> set (_N/2, 0, 0) y' state.y
1397 gf 0 (float state.y)
1400 match conf.ghyllscroll with
1401 | None ->
1402 gotoy_and_clear_text y
1403 | Some nab ->
1404 if state.ghyll == noghyll
1405 then set nab y state.y
1406 else state.ghyll (Some y)
1409 let gotopage n top =
1410 let y, h = getpageyh n in
1411 let y = y + (truncate (top *. float h)) in
1412 gotoghyll y
1415 let gotopage1 n top =
1416 let y = getpagey n in
1417 let y = y + top in
1418 gotoghyll y
1421 let invalidate () =
1422 state.layout <- [];
1423 state.pdims <- [];
1424 state.rects <- [];
1425 state.rects1 <- [];
1426 state.invalidated <- state.invalidated + 1;
1429 let writeopen path password =
1430 writecmd state.csock ("open " ^ path ^ "\000" ^ password ^ "\000");
1433 let opendoc path password =
1434 invalidate ();
1435 state.path <- path;
1436 state.password <- password;
1437 state.gen <- state.gen + 1;
1438 state.docinfo <- [];
1440 setaalevel conf.aalevel;
1441 writeopen path password;
1442 Glut.setWindowTitle ("llpp " ^ Filename.basename path);
1443 wcmd "geometry" [`i state.w; `i conf.winh];
1446 let scalecolor c =
1447 let c = c *. conf.colorscale in
1448 (c, c, c);
1451 let scalecolor2 (r, g, b) =
1452 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
1455 let represent () =
1456 let docolumns = function
1457 | None -> ()
1458 | Some ((columns, coverA, coverB), _) ->
1459 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1460 let rec loop pageno pdimno pdim x y rowh pdims =
1461 if pageno = state.pagecount
1462 then ()
1463 else
1464 let pdimno, ((_, w, h, _) as pdim), pdims =
1465 match pdims with
1466 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1467 pdimno+1, pdim, rest
1468 | _ ->
1469 pdimno, pdim, pdims
1471 let x, y, rowh' =
1472 if pageno = coverA - 1 || pageno = state.pagecount - coverB
1473 then (
1474 (conf.winw - state.scrollw - w) / 2,
1475 y + rowh + conf.interpagespace, h
1477 else (
1478 if (pageno - coverA) mod columns = 0
1479 then 0, y + rowh + conf.interpagespace, h
1480 else x + w + conf.interpagespace, y, max rowh h
1483 let rec fixrow m = if m = pageno then () else
1484 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
1485 if h < rowh
1486 then (
1487 let y = y + (rowh - h) / 2 in
1488 a.(m) <- (pdimno, x, y, pdim);
1490 fixrow (m+1)
1492 if pageno > 1 && pageno mod columns = 0
1493 then fixrow (pageno - columns);
1494 a.(pageno) <- (pdimno, x, y, pdim);
1495 loop (pageno+1) pdimno pdim x y rowh' pdims
1497 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
1498 conf.columns <- Some ((columns, coverA, coverB), a);
1500 docolumns conf.columns;
1501 state.maxy <- calcheight ();
1502 state.hscrollh <-
1503 if state.w <= conf.winw - state.scrollw
1504 then 0
1505 else state.scrollw
1507 match state.mode with
1508 | Birdseye (_, _, pageno, _, _) ->
1509 let y, h = getpageyh pageno in
1510 let top = (conf.winh - h) / 2 in
1511 gotoy (max 0 (y - top))
1512 | _ -> gotoanchor state.anchor
1515 let reshape =
1516 let firsttime = ref true in
1517 fun ~w ~h ->
1518 GlDraw.viewport 0 0 w h;
1519 if state.invalidated = 0 && not !firsttime
1520 then state.anchor <- getanchor ();
1522 firsttime := false;
1523 conf.winw <- w;
1524 let w = truncate (float w *. conf.zoom) - state.scrollw in
1525 let w = max w 2 in
1526 state.w <- w;
1527 conf.winh <- h;
1528 setfontsize fstate.fontsize;
1529 GlMat.mode `modelview;
1530 GlMat.load_identity ();
1532 GlMat.mode `projection;
1533 GlMat.load_identity ();
1534 GlMat.rotate ~x:1.0 ~angle:180.0 ();
1535 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
1536 GlMat.scale3 (2.0 /. float conf.winw, 2.0 /. float conf.winh, 1.0);
1538 let w =
1539 match conf.columns with
1540 | None -> w
1541 | Some ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
1543 invalidate ();
1544 wcmd "geometry" [`i w; `i h];
1547 let enttext () =
1548 let len = String.length state.text in
1549 let drawstring s =
1550 let hscrollh =
1551 match state.mode with
1552 | View -> state.hscrollh
1553 | _ -> 0
1555 let rect x w =
1556 GlDraw.rect
1557 (x, float (conf.winh - (fstate.fontsize + 4) - hscrollh))
1558 (x+.w, float (conf.winh - hscrollh))
1561 let w = float (conf.winw - state.scrollw - 1) in
1562 if state.progress >= 0.0 && state.progress < 1.0
1563 then (
1564 GlDraw.color (0.3, 0.3, 0.3);
1565 let w1 = w *. state.progress in
1566 rect 0.0 w1;
1567 GlDraw.color (0.0, 0.0, 0.0);
1568 rect w1 (w-.w1)
1570 else (
1571 GlDraw.color (0.0, 0.0, 0.0);
1572 rect 0.0 w;
1575 GlDraw.color (1.0, 1.0, 1.0);
1576 drawstring fstate.fontsize
1577 (if len > 0 then 8 else 2) (conf.winh - hscrollh - 5) s;
1579 let s =
1580 match state.mode with
1581 | Textentry ((prefix, text, _, _, _), _) ->
1582 let s =
1583 if len > 0
1584 then
1585 Printf.sprintf "%s%s_ [%s]" prefix text state.text
1586 else
1587 Printf.sprintf "%s%s_" prefix text
1591 | _ -> state.text
1593 let s =
1594 if state.newerrmsgs
1595 then (
1596 if not (istextentry state.mode)
1597 then
1598 let s1 = "(press 'e' to review error messasges)" in
1599 if String.length s > 0 then s ^ " " ^ s1 else s1
1600 else s
1602 else s
1604 if String.length s > 0
1605 then drawstring s
1608 let showtext c s =
1609 state.text <- Printf.sprintf "%c%s" c s;
1610 G.postRedisplay "showtext";
1613 let gctiles () =
1614 let len = Queue.length state.tilelru in
1615 let rec loop qpos =
1616 if state.memused <= conf.memlimit
1617 then ()
1618 else (
1619 if qpos < len
1620 then
1621 let (k, p, s) as lruitem = Queue.pop state.tilelru in
1622 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
1623 let (_, pw, ph, _) = getpagedim n in
1625 gen = state.gen
1626 && colorspace = conf.colorspace
1627 && angle = conf.angle
1628 && pagew = pw
1629 && pageh = ph
1630 && (
1631 let layout =
1632 match state.throttle with
1633 | None ->
1634 if conf.preload
1635 then preloadlayout state.layout
1636 else state.layout
1637 | Some (layout, _, _) ->
1638 layout
1640 let x = col*conf.tilew
1641 and y = row*conf.tileh in
1642 tilevisible layout n x y
1644 then Queue.push lruitem state.tilelru
1645 else (
1646 wcmd "freetile" [`s p];
1647 state.memused <- state.memused - s;
1648 state.uioh#infochanged Memused;
1649 Hashtbl.remove state.tilemap k;
1651 loop (qpos+1)
1654 loop 0
1657 let flushtiles () =
1658 Queue.iter (fun (k, p, s) ->
1659 wcmd "freetile" [`s p];
1660 state.memused <- state.memused - s;
1661 state.uioh#infochanged Memused;
1662 Hashtbl.remove state.tilemap k;
1663 ) state.tilelru;
1664 Queue.clear state.tilelru;
1665 load state.layout;
1668 let logcurrently = function
1669 | Idle -> dolog "Idle"
1670 | Loading (l, gen) ->
1671 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
1672 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
1673 dolog
1674 "Tiling %d[%d,%d] page=%s cs=%s angle"
1675 l.pageno col row pageopaque
1676 (colorspace_to_string colorspace)
1678 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
1679 angle gen conf.angle state.gen
1680 tilew tileh
1681 conf.tilew conf.tileh
1683 | Outlining _ ->
1684 dolog "outlining"
1687 let act cmds =
1688 (* dolog "%S" cmds; *)
1689 let op, args =
1690 let spacepos =
1691 try String.index cmds ' '
1692 with Not_found -> -1
1694 if spacepos = -1
1695 then cmds, ""
1696 else
1697 let l = String.length cmds in
1698 let op = String.sub cmds 0 spacepos in
1699 op, begin
1700 if l - spacepos < 2 then ""
1701 else String.sub cmds (spacepos+1) (l-spacepos-1)
1704 match op with
1705 | "clear" ->
1706 state.uioh#infochanged Pdim;
1707 state.pdims <- [];
1709 | "clearrects" ->
1710 state.rects <- state.rects1;
1711 G.postRedisplay "clearrects";
1713 | "continue" ->
1714 let n =
1715 try Scanf.sscanf args "%u" (fun n -> n)
1716 with exn ->
1717 dolog "error processing 'continue' %S: %s"
1718 cmds (Printexc.to_string exn);
1719 exit 1;
1721 state.pagecount <- n;
1722 state.invalidated <- state.invalidated - 1;
1723 begin match state.currently with
1724 | Outlining l ->
1725 state.currently <- Idle;
1726 state.outlines <- Array.of_list (List.rev l)
1727 | _ -> ()
1728 end;
1729 if state.invalidated = 0
1730 then represent ();
1731 if conf.maxwait = None
1732 then G.postRedisplay "continue";
1734 | "title" ->
1735 Glut.setWindowTitle args
1737 | "msg" ->
1738 showtext ' ' args
1740 | "vmsg" ->
1741 if conf.verbose
1742 then showtext ' ' args
1744 | "progress" ->
1745 let progress, text =
1747 Scanf.sscanf args "%f %n"
1748 (fun f pos ->
1749 f, String.sub args pos (String.length args - pos))
1750 with exn ->
1751 dolog "error processing 'progress' %S: %s"
1752 cmds (Printexc.to_string exn);
1753 exit 1;
1755 state.text <- text;
1756 state.progress <- progress;
1757 G.postRedisplay "progress"
1759 | "firstmatch" ->
1760 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1762 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
1763 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1764 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1765 with exn ->
1766 dolog "error processing 'firstmatch' %S: %s"
1767 cmds (Printexc.to_string exn);
1768 exit 1;
1770 let y = (getpagey pageno) + truncate y0 in
1771 addnav ();
1772 gotoy y;
1773 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
1775 | "match" ->
1776 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1778 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
1779 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1780 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1781 with exn ->
1782 dolog "error processing 'match' %S: %s"
1783 cmds (Printexc.to_string exn);
1784 exit 1;
1786 state.rects1 <-
1787 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
1789 | "page" ->
1790 let pageopaque, t =
1792 Scanf.sscanf args "%s %f" (fun p t -> p, t)
1793 with exn ->
1794 dolog "error processing 'page' %S: %s"
1795 cmds (Printexc.to_string exn);
1796 exit 1;
1798 begin match state.currently with
1799 | Loading (l, gen) ->
1800 vlog "page %d took %f sec" l.pageno t;
1801 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
1802 begin match state.throttle with
1803 | None ->
1804 let preloadedpages =
1805 if conf.preload
1806 then preloadlayout state.layout
1807 else state.layout
1809 let evict () =
1810 let module IntSet =
1811 Set.Make (struct type t = int let compare = (-) end) in
1812 let set =
1813 List.fold_left (fun s l -> IntSet.add l.pageno s)
1814 IntSet.empty preloadedpages
1816 let evictedpages =
1817 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
1818 if not (IntSet.mem pageno set)
1819 then (
1820 wcmd "freepage" [`s opaque];
1821 key :: accu
1823 else accu
1824 ) state.pagemap []
1826 List.iter (Hashtbl.remove state.pagemap) evictedpages;
1828 evict ();
1829 state.currently <- Idle;
1830 if gen = state.gen
1831 then (
1832 tilepage l.pageno pageopaque state.layout;
1833 load state.layout;
1834 load preloadedpages;
1835 if pagevisible state.layout l.pageno
1836 && layoutready state.layout
1837 then G.postRedisplay "page";
1840 | Some (layout, _, _) ->
1841 state.currently <- Idle;
1842 tilepage l.pageno pageopaque layout;
1843 load state.layout
1844 end;
1846 | _ ->
1847 dolog "Inconsistent loading state";
1848 logcurrently state.currently;
1849 raise Quit;
1852 | "tile" ->
1853 let (x, y, opaque, size, t) =
1855 Scanf.sscanf args "%u %u %s %u %f"
1856 (fun x y p size t -> (x, y, p, size, t))
1857 with exn ->
1858 dolog "error processing 'tile' %S: %s"
1859 cmds (Printexc.to_string exn);
1860 exit 1;
1862 begin match state.currently with
1863 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
1864 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
1866 if tilew != conf.tilew || tileh != conf.tileh
1867 then (
1868 wcmd "freetile" [`s opaque];
1869 state.currently <- Idle;
1870 load state.layout;
1872 else (
1873 puttileopaque l col row gen cs angle opaque size t;
1874 state.memused <- state.memused + size;
1875 state.uioh#infochanged Memused;
1876 gctiles ();
1877 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
1878 opaque, size) state.tilelru;
1880 let layout =
1881 match state.throttle with
1882 | None -> state.layout
1883 | Some (layout, _, _) -> layout
1886 state.currently <- Idle;
1887 if gen = state.gen
1888 && conf.colorspace = cs
1889 && conf.angle = angle
1890 && tilevisible layout l.pageno x y
1891 then conttiling l.pageno pageopaque;
1893 begin match state.throttle with
1894 | None ->
1895 preload state.layout;
1896 if gen = state.gen
1897 && conf.colorspace = cs
1898 && conf.angle = angle
1899 && tilevisible state.layout l.pageno x y
1900 then G.postRedisplay "tile nothrottle";
1902 | Some (layout, y, _) ->
1903 let ready = layoutready layout in
1904 if ready
1905 then (
1906 state.y <- y;
1907 state.layout <- layout;
1908 state.throttle <- None;
1909 G.postRedisplay "throttle";
1911 else load layout;
1912 end;
1915 | _ ->
1916 dolog "Inconsistent tiling state";
1917 logcurrently state.currently;
1918 raise Quit;
1921 | "pdim" ->
1922 let pdim =
1924 Scanf.sscanf args "%u %u %u %u" (fun n w h x -> n, w, h, x)
1925 with exn ->
1926 dolog "error processing 'pdim' %S: %s"
1927 cmds (Printexc.to_string exn);
1928 exit 1;
1930 state.uioh#infochanged Pdim;
1931 state.pdims <- pdim :: state.pdims
1933 | "o" ->
1934 let (l, n, t, h, pos) =
1936 Scanf.sscanf args "%u %u %d %u %n"
1937 (fun l n t h pos -> l, n, t, h, pos)
1938 with exn ->
1939 dolog "error processing 'o' %S: %s"
1940 cmds (Printexc.to_string exn);
1941 exit 1;
1943 let s = String.sub args pos (String.length args - pos) in
1944 let outline = (s, l, (n, float t /. float h)) in
1945 begin match state.currently with
1946 | Outlining outlines ->
1947 state.currently <- Outlining (outline :: outlines)
1948 | Idle ->
1949 state.currently <- Outlining [outline]
1950 | currently ->
1951 dolog "invalid outlining state";
1952 logcurrently currently
1955 | "info" ->
1956 state.docinfo <- (1, args) :: state.docinfo
1958 | "infoend" ->
1959 state.uioh#infochanged Docinfo;
1960 state.docinfo <- List.rev state.docinfo
1962 | _ ->
1963 dolog "unknown cmd `%S'" cmds
1966 let idle () =
1967 if state.deadline == nan then state.deadline <- now ();
1968 let r =
1969 match state.errfd with
1970 | None -> [state.csock]
1971 | Some fd -> [state.csock; fd]
1973 let rec loop delay =
1974 let deadline =
1975 if state.ghyll == noghyll
1976 then state.deadline
1977 else now () +. 0.02
1979 let timeout =
1980 if delay > 0.0
1981 then max 0.0 (deadline -. now ())
1982 else 0.0
1984 let r, _, _ = Unix.select r [] [] timeout in
1985 begin match r with
1986 | [] ->
1987 state.ghyll None;
1988 begin match state.autoscroll with
1989 | Some step when step != 0 ->
1990 let y = state.y + step in
1991 let y =
1992 if y < 0
1993 then state.maxy
1994 else if y >= state.maxy then 0 else y
1996 gotoy y;
1997 if state.mode = View
1998 then state.text <- "";
1999 state.deadline <- state.deadline +. 0.005;
2001 | _ ->
2002 state.deadline <- state.deadline +. delay;
2003 end;
2005 | l ->
2006 let rec checkfds c = function
2007 | [] -> c
2008 | fd :: rest when fd = state.csock ->
2009 let cmd = readcmd state.csock in
2010 act cmd;
2011 checkfds true rest
2012 | fd :: rest ->
2013 let s = String.create 80 in
2014 let n = Unix.read fd s 0 80 in
2015 if conf.redirectstderr
2016 then (
2017 Buffer.add_substring state.errmsgs s 0 n;
2018 state.newerrmsgs <- true;
2019 Glut.postRedisplay ();
2021 else (
2022 prerr_string (String.sub s 0 n);
2023 flush stderr;
2025 checkfds c rest
2027 if checkfds false l
2028 then loop 0.0
2029 end;
2030 in loop 0.007
2033 let onhist cb =
2034 let rc = cb.rc in
2035 let action = function
2036 | HCprev -> cbget cb ~-1
2037 | HCnext -> cbget cb 1
2038 | HCfirst -> cbget cb ~-(cb.rc)
2039 | HClast -> cbget cb (cb.len - 1 - cb.rc)
2040 and cancel () = cb.rc <- rc
2041 in (action, cancel)
2044 let search pattern forward =
2045 if String.length pattern > 0
2046 then
2047 let pn, py =
2048 match state.layout with
2049 | [] -> 0, 0
2050 | l :: _ ->
2051 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
2053 let cmd =
2054 let b = makecmd "search"
2055 [`b conf.icase; `i pn; `i py; `i (if forward then 1 else 0)]
2057 Buffer.add_char b ',';
2058 Buffer.add_string b pattern;
2059 Buffer.add_char b '\000';
2060 Buffer.contents b;
2062 writecmd state.csock cmd;
2065 let intentry text key =
2066 let c = Char.unsafe_chr key in
2067 match c with
2068 | '0' .. '9' ->
2069 let text = addchar text c in
2070 TEcont text
2072 | _ ->
2073 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2074 TEcont text
2077 let textentry text key =
2078 let c = Char.unsafe_chr key in
2079 match c with
2080 | _ when key >= 32 && key < 127 ->
2081 let text = addchar text c in
2082 TEcont text
2084 | _ ->
2085 dolog "unhandled key %d char `%c'" key (Char.unsafe_chr key);
2086 TEcont text
2089 let reqlayout angle proportional =
2090 match state.throttle with
2091 | None ->
2092 if state.invalidated = 0 then state.anchor <- getanchor ();
2093 conf.angle <- angle mod 360;
2094 conf.proportional <- proportional;
2095 invalidate ();
2096 wcmd "reqlayout" [`i conf.angle; `b proportional];
2097 | _ -> ()
2100 let settrim trimmargins trimfuzz =
2101 if state.invalidated = 0 then state.anchor <- getanchor ();
2102 conf.trimmargins <- trimmargins;
2103 conf.trimfuzz <- trimfuzz;
2104 let x0, y0, x1, y1 = trimfuzz in
2105 invalidate ();
2106 wcmd "settrim" [
2107 `b conf.trimmargins;
2108 `i x0;
2109 `i y0;
2110 `i x1;
2111 `i y1;
2113 Hashtbl.iter (fun _ opaque ->
2114 wcmd "freepage" [`s opaque];
2115 ) state.pagemap;
2116 Hashtbl.clear state.pagemap;
2119 let setzoom zoom =
2120 match state.throttle with
2121 | None ->
2122 let zoom = max 0.01 zoom in
2123 if zoom <> conf.zoom
2124 then (
2125 state.prevzoom <- conf.zoom;
2126 let relx =
2127 if zoom <= 1.0
2128 then (state.x <- 0; 0.0)
2129 else float state.x /. float state.w
2131 conf.zoom <- zoom;
2132 reshape conf.winw conf.winh;
2133 if zoom > 1.0
2134 then (
2135 let x = relx *. float state.w in
2136 state.x <- truncate x;
2138 state.text <- Printf.sprintf "zoom is now %-5.1f" (zoom *. 100.0);
2141 | Some (layout, y, started) ->
2142 let time =
2143 match conf.maxwait with
2144 | None -> 0.0
2145 | Some t -> t
2147 let dt = now () -. started in
2148 if dt > time
2149 then (
2150 state.y <- y;
2151 load layout;
2155 let setcolumns columns coverA coverB =
2156 if columns < 2
2157 then (
2158 conf.columns <- None;
2159 state.x <- 0;
2160 setzoom 1.0;
2162 else (
2163 conf.columns <- Some ((columns, coverA, coverB), [||]);
2164 conf.zoom <- 1.0;
2166 reshape conf.winw conf.winh;
2169 let enterbirdseye () =
2170 let zoom = float conf.thumbw /. float conf.winw in
2171 let birdseyepageno =
2172 let cy = conf.winh / 2 in
2173 let fold = function
2174 | [] -> 0
2175 | l :: rest ->
2176 let rec fold best = function
2177 | [] -> best.pageno
2178 | l :: rest ->
2179 let d = cy - (l.pagedispy + l.pagevh/2)
2180 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2181 if abs d < abs dbest
2182 then fold l rest
2183 else best.pageno
2184 in fold l rest
2186 fold state.layout
2188 state.mode <- Birdseye (
2189 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2191 conf.zoom <- zoom;
2192 conf.presentation <- false;
2193 conf.interpagespace <- 10;
2194 conf.hlinks <- false;
2195 state.x <- 0;
2196 state.mstate <- Mnone;
2197 conf.maxwait <- None;
2198 conf.columns <- (
2199 match conf.beyecolumns with
2200 | Some c ->
2201 conf.zoom <- 1.0;
2202 Some ((c, 0, 0), [||])
2203 | None -> None
2205 Glut.setCursor Glut.CURSOR_INHERIT;
2206 if conf.verbose
2207 then
2208 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2209 (100.0*.zoom)
2210 else
2211 state.text <- ""
2213 reshape conf.winw conf.winh;
2216 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2217 state.mode <- View;
2218 conf.zoom <- c.zoom;
2219 conf.presentation <- c.presentation;
2220 conf.interpagespace <- c.interpagespace;
2221 conf.maxwait <- c.maxwait;
2222 conf.hlinks <- c.hlinks;
2223 conf.beyecolumns <- (
2224 match conf.columns with
2225 | Some ((c, _, _), _) -> Some c
2226 | None -> None
2228 conf.columns <- (
2229 match c.columns with
2230 | Some (c, _) -> Some (c, [||])
2231 | None -> None
2233 state.x <- leftx;
2234 if conf.verbose
2235 then
2236 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2237 (100.0*.conf.zoom)
2239 reshape conf.winw conf.winh;
2240 state.anchor <- if goback then anchor else (pageno, 0.0);
2243 let togglebirdseye () =
2244 match state.mode with
2245 | Birdseye vals -> leavebirdseye vals true
2246 | View -> enterbirdseye ()
2247 | _ -> ()
2250 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2251 let pageno = max 0 (pageno - incr) in
2252 let rec loop = function
2253 | [] -> gotopage1 pageno 0
2254 | l :: _ when l.pageno = pageno ->
2255 if l.pagedispy >= 0 && l.pagey = 0
2256 then G.postRedisplay "upbirdseye"
2257 else gotopage1 pageno 0
2258 | _ :: rest -> loop rest
2260 loop state.layout;
2261 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2264 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2265 let pageno = min (state.pagecount - 1) (pageno + incr) in
2266 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2267 let rec loop = function
2268 | [] ->
2269 let y, h = getpageyh pageno in
2270 let dy = (y - state.y) - (conf.winh - h - conf.interpagespace) in
2271 gotoy (clamp dy)
2272 | l :: _ when l.pageno = pageno ->
2273 if l.pagevh != l.pageh
2274 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
2275 else G.postRedisplay "downbirdseye"
2276 | _ :: rest -> loop rest
2278 loop state.layout
2281 let optentry mode _ key =
2282 let btos b = if b then "on" else "off" in
2283 let c = Char.unsafe_chr key in
2284 match c with
2285 | 's' ->
2286 let ondone s =
2287 try conf.scrollstep <- int_of_string s with exc ->
2288 state.text <- Printf.sprintf "bad integer `%s': %s"
2289 s (Printexc.to_string exc)
2291 TEswitch ("scroll step: ", "", None, intentry, ondone)
2293 | 'A' ->
2294 let ondone s =
2296 conf.autoscrollstep <- int_of_string s;
2297 if state.autoscroll <> None
2298 then state.autoscroll <- Some conf.autoscrollstep
2299 with exc ->
2300 state.text <- Printf.sprintf "bad integer `%s': %s"
2301 s (Printexc.to_string exc)
2303 TEswitch ("auto scroll step: ", "", None, intentry, ondone)
2305 | 'C' ->
2306 let ondone s =
2308 let n, a, b = columns_of_string s in
2309 setcolumns n a b;
2310 with exc ->
2311 state.text <- Printf.sprintf "bad columns `%s': %s"
2312 s (Printexc.to_string exc)
2314 TEswitch ("columns: ", "", None, textentry, ondone)
2316 | 'Z' ->
2317 let ondone s =
2319 let zoom = float (int_of_string s) /. 100.0 in
2320 setzoom zoom
2321 with exc ->
2322 state.text <- Printf.sprintf "bad integer `%s': %s"
2323 s (Printexc.to_string exc)
2325 TEswitch ("zoom: ", "", None, intentry, ondone)
2327 | 't' ->
2328 let ondone s =
2330 conf.thumbw <- bound (int_of_string s) 2 4096;
2331 state.text <-
2332 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
2333 begin match mode with
2334 | Birdseye beye ->
2335 leavebirdseye beye false;
2336 enterbirdseye ();
2337 | _ -> ();
2339 with exc ->
2340 state.text <- Printf.sprintf "bad integer `%s': %s"
2341 s (Printexc.to_string exc)
2343 TEswitch ("thumbnail width: ", "", None, intentry, ondone)
2345 | 'R' ->
2346 let ondone s =
2347 match try
2348 Some (int_of_string s)
2349 with exc ->
2350 state.text <- Printf.sprintf "bad integer `%s': %s"
2351 s (Printexc.to_string exc);
2352 None
2353 with
2354 | Some angle -> reqlayout angle conf.proportional
2355 | None -> ()
2357 TEswitch ("rotation: ", "", None, intentry, ondone)
2359 | 'i' ->
2360 conf.icase <- not conf.icase;
2361 TEdone ("case insensitive search " ^ (btos conf.icase))
2363 | 'p' ->
2364 conf.preload <- not conf.preload;
2365 gotoy state.y;
2366 TEdone ("preload " ^ (btos conf.preload))
2368 | 'v' ->
2369 conf.verbose <- not conf.verbose;
2370 TEdone ("verbose " ^ (btos conf.verbose))
2372 | 'd' ->
2373 conf.debug <- not conf.debug;
2374 TEdone ("debug " ^ (btos conf.debug))
2376 | 'h' ->
2377 conf.maxhfit <- not conf.maxhfit;
2378 state.maxy <-
2379 state.maxy + (if conf.maxhfit then -conf.winh else conf.winh);
2380 TEdone ("maxhfit " ^ (btos conf.maxhfit))
2382 | 'c' ->
2383 conf.crophack <- not conf.crophack;
2384 TEdone ("crophack " ^ btos conf.crophack)
2386 | 'a' ->
2387 let s =
2388 match conf.maxwait with
2389 | None ->
2390 conf.maxwait <- Some infinity;
2391 "always wait for page to complete"
2392 | Some _ ->
2393 conf.maxwait <- None;
2394 "show placeholder if page is not ready"
2396 TEdone s
2398 | 'f' ->
2399 conf.underinfo <- not conf.underinfo;
2400 TEdone ("underinfo " ^ btos conf.underinfo)
2402 | 'P' ->
2403 conf.savebmarks <- not conf.savebmarks;
2404 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
2406 | 'S' ->
2407 let ondone s =
2409 let pageno, py =
2410 match state.layout with
2411 | [] -> 0, 0
2412 | l :: _ ->
2413 l.pageno, l.pagey
2415 conf.interpagespace <- int_of_string s;
2416 state.maxy <- calcheight ();
2417 let y = getpagey pageno in
2418 gotoy (y + py)
2419 with exc ->
2420 state.text <- Printf.sprintf "bad integer `%s': %s"
2421 s (Printexc.to_string exc)
2423 TEswitch ("vertical margin: ", "", None, intentry, ondone)
2425 | 'l' ->
2426 reqlayout conf.angle (not conf.proportional);
2427 TEdone ("proportional display " ^ btos conf.proportional)
2429 | 'T' ->
2430 settrim (not conf.trimmargins) conf.trimfuzz;
2431 TEdone ("trim margins " ^ btos conf.trimmargins)
2433 | 'I' ->
2434 conf.invert <- not conf.invert;
2435 TEdone ("invert colors " ^ btos conf.invert)
2437 | _ ->
2438 state.text <- Printf.sprintf "bad option %d `%c'" key c;
2439 TEstop
2442 class type lvsource = object
2443 method getitemcount : int
2444 method getitem : int -> (string * int)
2445 method hasaction : int -> bool
2446 method exit :
2447 uioh:uioh ->
2448 cancel:bool ->
2449 active:int ->
2450 first:int ->
2451 pan:int ->
2452 qsearch:string ->
2453 uioh option
2454 method getactive : int
2455 method getfirst : int
2456 method getqsearch : string
2457 method setqsearch : string -> unit
2458 method getpan : int
2459 end;;
2461 class virtual lvsourcebase = object
2462 val mutable m_active = 0
2463 val mutable m_first = 0
2464 val mutable m_qsearch = ""
2465 val mutable m_pan = 0
2466 method getactive = m_active
2467 method getfirst = m_first
2468 method getqsearch = m_qsearch
2469 method getpan = m_pan
2470 method setqsearch s = m_qsearch <- s
2471 end;;
2473 let textentryspecial key = function
2474 | ((c, _, (Some (action, _) as onhist), onkey, ondone), mode) ->
2475 let s =
2476 match key with
2477 | Glut.KEY_UP -> action HCprev
2478 | Glut.KEY_DOWN -> action HCnext
2479 | Glut.KEY_HOME -> action HCfirst
2480 | Glut.KEY_END -> action HClast
2481 | _ -> state.text
2483 state.mode <- Textentry ((c, s, onhist, onkey, ondone), mode);
2484 G.postRedisplay "special textentry";
2485 | _ -> ()
2488 let textentrykeyboard key ((c, text, opthist, onkey, ondone), onleave) =
2489 let enttext te =
2490 state.mode <- Textentry (te, onleave);
2491 state.text <- "";
2492 enttext ();
2493 G.postRedisplay "textentrykeyboard enttext";
2495 match Char.unsafe_chr key with
2496 | '\008' -> (* backspace *)
2497 let len = String.length text in
2498 if len = 0
2499 then (
2500 onleave Cancel;
2501 G.postRedisplay "textentrykeyboard after cancel";
2503 else (
2504 let s = String.sub text 0 (len - 1) in
2505 enttext (c, s, opthist, onkey, ondone)
2508 | '\r' | '\n' ->
2509 ondone text;
2510 onleave Confirm;
2511 G.postRedisplay "textentrykeyboard after confirm"
2513 | '\007' (* ctrl-g *)
2514 | '\027' -> (* escape *)
2515 if String.length text = 0
2516 then (
2517 begin match opthist with
2518 | None -> ()
2519 | Some (_, onhistcancel) -> onhistcancel ()
2520 end;
2521 onleave Cancel;
2522 state.text <- "";
2523 G.postRedisplay "textentrykeyboard after cancel2"
2525 else (
2526 enttext (c, "", opthist, onkey, ondone)
2529 | '\127' -> () (* delete *)
2531 | _ ->
2532 begin match onkey text key with
2533 | TEdone text ->
2534 ondone text;
2535 onleave Confirm;
2536 G.postRedisplay "textentrykeyboard after confirm2";
2538 | TEcont text ->
2539 enttext (c, text, opthist, onkey, ondone);
2541 | TEstop ->
2542 onleave Cancel;
2543 G.postRedisplay "textentrykeyboard after cancel3"
2545 | TEswitch te ->
2546 state.mode <- Textentry (te, onleave);
2547 G.postRedisplay "textentrykeyboard switch";
2548 end;
2551 let firstof first active =
2552 if first > active || abs (first - active) > fstate.maxrows - 1
2553 then max 0 (active - (fstate.maxrows/2))
2554 else first
2557 let calcfirst first active =
2558 if active > first
2559 then
2560 let rows = active - first in
2561 if rows > fstate.maxrows then active - fstate.maxrows else first
2562 else active
2565 let scrollph y maxy =
2566 let sh = (float (maxy + conf.winh) /. float conf.winh) in
2567 let sh = float conf.winh /. sh in
2568 let sh = max sh (float conf.scrollh) in
2570 let percent =
2571 if y = state.maxy
2572 then 1.0
2573 else float y /. float maxy
2575 let position = (float conf.winh -. sh) *. percent in
2577 let position =
2578 if position +. sh > float conf.winh
2579 then float conf.winh -. sh
2580 else position
2582 position, sh;
2585 let coe s = (s :> uioh);;
2587 class listview ~(source:lvsource) ~trusted =
2588 object (self)
2589 val m_pan = source#getpan
2590 val m_first = source#getfirst
2591 val m_active = source#getactive
2592 val m_qsearch = source#getqsearch
2593 val m_prev_uioh = state.uioh
2595 method private elemunder y =
2596 let n = y / (fstate.fontsize+1) in
2597 if m_first + n < source#getitemcount
2598 then (
2599 if source#hasaction (m_first + n)
2600 then Some (m_first + n)
2601 else None
2603 else None
2605 method display =
2606 Gl.enable `blend;
2607 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2608 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2609 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
2610 GlDraw.color (1., 1., 1.);
2611 Gl.enable `texture_2d;
2612 let fs = fstate.fontsize in
2613 let nfs = fs + 1 in
2614 let ww = fstate.wwidth in
2615 let tabw = 30.0*.ww in
2616 let itemcount = source#getitemcount in
2617 let rec loop row =
2618 if (row - m_first) * nfs > conf.winh
2619 then ()
2620 else (
2621 if row >= 0 && row < itemcount
2622 then (
2623 let (s, level) = source#getitem row in
2624 let y = (row - m_first) * nfs in
2625 let x = 5.0 +. float (level + m_pan) *. ww in
2626 if row = m_active
2627 then (
2628 Gl.disable `texture_2d;
2629 GlDraw.polygon_mode `both `line;
2630 GlDraw.color (1., 1., 1.) ~alpha:0.9;
2631 GlDraw.rect (1., float (y + 1))
2632 (float (conf.winw - conf.scrollbw - 1), float (y + fs + 3));
2633 GlDraw.polygon_mode `both `fill;
2634 GlDraw.color (1., 1., 1.);
2635 Gl.enable `texture_2d;
2638 let drawtabularstring s =
2639 let drawstr x s = drawstring1 fs (truncate x) (y+nfs) s in
2640 if trusted
2641 then
2642 let tabpos = try String.index s '\t' with Not_found -> -1 in
2643 if tabpos > 0
2644 then
2645 let len = String.length s - tabpos - 1 in
2646 let s1 = String.sub s 0 tabpos
2647 and s2 = String.sub s (tabpos + 1) len in
2648 let nx = drawstr x s1 in
2649 let sw = nx -. x in
2650 let x = x +. (max tabw sw) in
2651 drawstr x s2
2652 else
2653 drawstr x s
2654 else
2655 drawstr x s
2657 let _ = drawtabularstring s in
2658 loop (row+1)
2662 loop m_first;
2663 Gl.disable `blend;
2664 Gl.disable `texture_2d;
2666 method updownlevel incr =
2667 let len = source#getitemcount in
2668 let curlevel =
2669 if m_active >= 0 && m_active < len
2670 then snd (source#getitem m_active)
2671 else -1
2673 let rec flow i =
2674 if i = len then i-1 else if i = -1 then 0 else
2675 let _, l = source#getitem i in
2676 if l != curlevel then i else flow (i+incr)
2678 let active = flow m_active in
2679 let first = calcfirst m_first active in
2680 G.postRedisplay "special outline updownlevel";
2681 {< m_active = active; m_first = first >}
2683 method private key1 key =
2684 let set active first qsearch =
2685 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
2687 let search active pattern incr =
2688 let dosearch re =
2689 let rec loop n =
2690 if n >= 0 && n < source#getitemcount
2691 then (
2692 let s, _ = source#getitem n in
2694 (try ignore (Str.search_forward re s 0); true
2695 with Not_found -> false)
2696 then Some n
2697 else loop (n + incr)
2699 else None
2701 loop active
2704 let re = Str.regexp_case_fold pattern in
2705 dosearch re
2706 with Failure s ->
2707 state.text <- s;
2708 None
2710 match key with
2711 | 18 | 19 -> (* ctrl-r/ctlr-s *)
2712 let incr = if key = 18 then -1 else 1 in
2713 let active, first =
2714 match search (m_active + incr) m_qsearch incr with
2715 | None ->
2716 state.text <- m_qsearch ^ " [not found]";
2717 m_active, m_first
2718 | Some active ->
2719 state.text <- m_qsearch;
2720 active, firstof m_first active
2722 G.postRedisplay "listview ctrl-r/s";
2723 set active first m_qsearch;
2725 | 8 -> (* backspace *)
2726 let len = String.length m_qsearch in
2727 if len = 0
2728 then coe self
2729 else (
2730 if len = 1
2731 then (
2732 state.text <- "";
2733 G.postRedisplay "listview empty qsearch";
2734 set m_active m_first "";
2736 else
2737 let qsearch = String.sub m_qsearch 0 (len - 1) in
2738 let active, first =
2739 match search m_active qsearch ~-1 with
2740 | None ->
2741 state.text <- qsearch ^ " [not found]";
2742 m_active, m_first
2743 | Some active ->
2744 state.text <- qsearch;
2745 active, firstof m_first active
2747 G.postRedisplay "listview backspace qsearch";
2748 set active first qsearch
2751 | _ when key >= 32 && key < 127 ->
2752 let pattern = addchar m_qsearch (Char.chr key) in
2753 let active, first =
2754 match search m_active pattern 1 with
2755 | None ->
2756 state.text <- pattern ^ " [not found]";
2757 m_active, m_first
2758 | Some active ->
2759 state.text <- pattern;
2760 active, firstof m_first active
2762 G.postRedisplay "listview qsearch add";
2763 set active first pattern;
2765 | 27 -> (* escape *)
2766 state.text <- "";
2767 if String.length m_qsearch = 0
2768 then (
2769 G.postRedisplay "list view escape";
2770 begin
2771 match
2772 source#exit (coe self) true m_active m_first m_pan m_qsearch
2773 with
2774 | None -> m_prev_uioh
2775 | Some uioh -> uioh
2778 else (
2779 G.postRedisplay "list view kill qsearch";
2780 source#setqsearch "";
2781 coe {< m_qsearch = "" >}
2784 | 13 -> (* enter *)
2785 state.text <- "";
2786 let self = {< m_qsearch = "" >} in
2787 source#setqsearch "";
2788 let opt =
2789 G.postRedisplay "listview enter";
2790 if m_active >= 0 && m_active < source#getitemcount
2791 then (
2792 source#exit (coe self) false m_active m_first m_pan "";
2794 else (
2795 source#exit (coe self) true m_active m_first m_pan "";
2798 begin match opt with
2799 | None -> m_prev_uioh
2800 | Some uioh -> uioh
2803 | 127 -> (* delete *)
2804 coe self
2806 | _ -> dolog "unknown key %d" key; coe self
2808 method private special1 key =
2809 let itemcount = source#getitemcount in
2810 let find start incr =
2811 let rec find i =
2812 if i = -1 || i = itemcount
2813 then -1
2814 else (
2815 if source#hasaction i
2816 then i
2817 else find (i + incr)
2820 find start
2822 let set active first =
2823 let first = bound first 0 (itemcount - fstate.maxrows) in
2824 state.text <- "";
2825 coe {< m_active = active; m_first = first >}
2827 let navigate incr =
2828 let isvisible first n = n >= first && n - first <= fstate.maxrows in
2829 let active, first =
2830 let incr1 = if incr > 0 then 1 else -1 in
2831 if isvisible m_first m_active
2832 then
2833 let next =
2834 let next = m_active + incr in
2835 let next =
2836 if next < 0 || next >= itemcount
2837 then -1
2838 else find next incr1
2840 if next = -1 || abs (m_active - next) > fstate.maxrows
2841 then -1
2842 else next
2844 if next = -1
2845 then
2846 let first = m_first + incr in
2847 let first = bound first 0 (itemcount - 1) in
2848 let next =
2849 let next = m_active + incr in
2850 let next = bound next 0 (itemcount - 1) in
2851 find next ~-incr1
2853 let active = if next = -1 then m_active else next in
2854 active, first
2855 else
2856 let first = min next m_first in
2857 let first =
2858 if abs (next - first) > fstate.maxrows
2859 then first + incr
2860 else first
2862 next, first
2863 else
2864 let first = m_first + incr in
2865 let first = bound first 0 (itemcount - 1) in
2866 let active =
2867 let next = m_active + incr in
2868 let next = bound next 0 (itemcount - 1) in
2869 let next = find next incr1 in
2870 let active =
2871 if next = -1 || abs (m_active - first) > fstate.maxrows
2872 then (
2873 let active = if m_active = -1 then next else m_active in
2874 active
2876 else next
2878 if isvisible first active
2879 then active
2880 else -1
2882 active, first
2884 G.postRedisplay "listview navigate";
2885 set active first;
2887 begin match key with
2888 | Glut.KEY_UP -> navigate ~-1
2889 | Glut.KEY_DOWN -> navigate 1
2890 | Glut.KEY_PAGE_UP -> navigate ~-(fstate.maxrows)
2891 | Glut.KEY_PAGE_DOWN -> navigate fstate.maxrows
2893 | Glut.KEY_RIGHT ->
2894 state.text <- "";
2895 G.postRedisplay "listview right";
2896 coe {< m_pan = m_pan - 1 >}
2898 | Glut.KEY_LEFT ->
2899 state.text <- "";
2900 G.postRedisplay "listview left";
2901 coe {< m_pan = m_pan + 1 >}
2903 | Glut.KEY_HOME ->
2904 let active = find 0 1 in
2905 G.postRedisplay "listview home";
2906 set active 0;
2908 | Glut.KEY_END ->
2909 let first = max 0 (itemcount - fstate.maxrows) in
2910 let active = find (itemcount - 1) ~-1 in
2911 G.postRedisplay "listview end";
2912 set active first;
2914 | _ -> coe self
2915 end;
2917 method key key =
2918 match state.mode with
2919 | Textentry te -> textentrykeyboard key te; coe self
2920 | _ -> self#key1 key
2922 method special key =
2923 match state.mode with
2924 | Textentry te -> textentryspecial key te; coe self
2925 | _ -> self#special1 key
2927 method button button bstate x y =
2928 let opt =
2929 match button with
2930 | Glut.LEFT_BUTTON when x > conf.winw - conf.scrollbw ->
2931 G.postRedisplay "listview scroll";
2932 if bstate = Glut.DOWN
2933 then
2934 let _, position, sh = self#scrollph in
2935 if y > truncate position && y < truncate (position +. sh)
2936 then (
2937 state.mstate <- Mscrolly;
2938 Some (coe self)
2940 else
2941 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
2942 let first = truncate (s *. float source#getitemcount) in
2943 let first = min source#getitemcount first in
2944 Some (coe {< m_first = first; m_active = first >})
2945 else (
2946 state.mstate <- Mnone;
2947 Some (coe self);
2949 | Glut.LEFT_BUTTON when bstate = Glut.UP ->
2950 begin match self#elemunder y with
2951 | Some n ->
2952 G.postRedisplay "listview click";
2953 source#exit
2954 (coe {< m_active = n >}) false n m_first m_pan m_qsearch
2955 | _ ->
2956 Some (coe self)
2958 | Glut.OTHER_BUTTON n when (n == 3 || n == 4) && bstate = Glut.UP ->
2959 let len = source#getitemcount in
2960 let first =
2961 if n = 4 && m_first + fstate.maxrows >= len
2962 then
2963 m_first
2964 else
2965 let first = m_first + (if n == 3 then -1 else 1) in
2966 bound first 0 (len - 1)
2968 G.postRedisplay "listview wheel";
2969 Some (coe {< m_first = first >})
2970 | _ ->
2971 Some (coe self)
2973 match opt with
2974 | None -> m_prev_uioh
2975 | Some uioh -> uioh
2977 method motion _ y =
2978 match state.mstate with
2979 | Mscrolly ->
2980 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
2981 let first = truncate (s *. float source#getitemcount) in
2982 let first = min source#getitemcount first in
2983 G.postRedisplay "listview motion";
2984 coe {< m_first = first; m_active = first >}
2985 | _ -> coe self
2987 method pmotion x y =
2988 if x < conf.winw - conf.scrollbw
2989 then
2990 let n =
2991 match self#elemunder y with
2992 | None -> Glut.setCursor Glut.CURSOR_INHERIT; m_active
2993 | Some n -> Glut.setCursor Glut.CURSOR_INFO; n
2995 let o =
2996 if n != m_active
2997 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
2998 else self
3000 coe o
3001 else (
3002 Glut.setCursor Glut.CURSOR_INHERIT;
3003 coe self
3006 method infochanged _ = ()
3008 method scrollpw = (0, 0.0, 0.0)
3009 method scrollph =
3010 let nfs = fstate.fontsize + 1 in
3011 let y = m_first * nfs in
3012 let itemcount = source#getitemcount in
3013 let maxi = max 0 (itemcount - fstate.maxrows) in
3014 let maxy = maxi * nfs in
3015 let p, h = scrollph y maxy in
3016 conf.scrollbw, p, h
3017 end;;
3019 class outlinelistview ~source =
3020 object (self)
3021 inherit listview ~source:(source :> lvsource) ~trusted:false as super
3023 method key key =
3024 match key with
3025 | 14 -> (* ctrl-n *)
3026 source#narrow m_qsearch;
3027 G.postRedisplay "outline ctrl-n";
3028 coe {< m_first = 0; m_active = 0 >}
3030 | 21 -> (* ctrl-u *)
3031 source#denarrow;
3032 G.postRedisplay "outline ctrl-u";
3033 state.text <- "";
3034 coe {< m_first = 0; m_active = 0 >}
3036 | 12 -> (* ctrl-l *)
3037 let first = m_active - (fstate.maxrows / 2) in
3038 G.postRedisplay "outline ctrl-l";
3039 coe {< m_first = first >}
3041 | 127 -> (* delete *)
3042 source#remove m_active;
3043 G.postRedisplay "outline delete";
3044 let active = max 0 (m_active-1) in
3045 coe {< m_first = firstof m_first active;
3046 m_active = active >}
3048 | key -> super#key key
3050 method special key =
3051 let calcfirst first active =
3052 if active > first
3053 then
3054 let rows = active - first in
3055 if rows > fstate.maxrows then active - fstate.maxrows else first
3056 else active
3058 let navigate incr =
3059 let active = m_active + incr in
3060 let active = bound active 0 (source#getitemcount - 1) in
3061 let first = calcfirst m_first active in
3062 G.postRedisplay "special outline navigate";
3063 coe {< m_active = active; m_first = first >}
3065 match key with
3066 | Glut.KEY_UP -> navigate ~-1
3067 | Glut.KEY_DOWN -> navigate 1
3068 | Glut.KEY_PAGE_UP -> navigate ~-(fstate.maxrows)
3069 | Glut.KEY_PAGE_DOWN -> navigate fstate.maxrows
3071 | Glut.KEY_RIGHT ->
3072 let o =
3073 if Glut.getModifiers () land Glut.active_ctrl != 0
3074 then (
3075 G.postRedisplay "special outline right";
3076 {< m_pan = m_pan + 1 >}
3078 else self#updownlevel 1
3080 coe o
3082 | Glut.KEY_LEFT ->
3083 let o =
3084 if Glut.getModifiers () land Glut.active_ctrl != 0
3085 then (
3086 G.postRedisplay "special outline left";
3087 {< m_pan = m_pan - 1 >}
3089 else self#updownlevel ~-1
3091 coe o
3093 | Glut.KEY_HOME ->
3094 G.postRedisplay "special outline home";
3095 coe {< m_first = 0; m_active = 0 >}
3097 | Glut.KEY_END ->
3098 let active = source#getitemcount - 1 in
3099 let first = max 0 (active - fstate.maxrows) in
3100 G.postRedisplay "special outline end";
3101 coe {< m_active = active; m_first = first >}
3103 | _ -> super#special key
3106 let outlinesource usebookmarks =
3107 let empty = [||] in
3108 (object
3109 inherit lvsourcebase
3110 val mutable m_items = empty
3111 val mutable m_orig_items = empty
3112 val mutable m_prev_items = empty
3113 val mutable m_narrow_pattern = ""
3114 val mutable m_hadremovals = false
3116 method getitemcount =
3117 Array.length m_items + (if m_hadremovals then 1 else 0)
3119 method getitem n =
3120 if n == Array.length m_items && m_hadremovals
3121 then
3122 ("[Confirm removal]", 0)
3123 else
3124 let s, n, _ = m_items.(n) in
3125 (s, n)
3127 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3128 ignore (uioh, first, pan, qsearch);
3129 let confrimremoval = m_hadremovals && active = Array.length m_items in
3130 let items =
3131 if String.length m_narrow_pattern = 0
3132 then m_orig_items
3133 else m_items
3135 if not cancel
3136 then (
3137 if not confrimremoval
3138 then(
3139 let _, _, anchor = m_items.(active) in
3140 gotoanchor anchor;
3141 m_items <- items;
3143 else (
3144 state.bookmarks <- Array.to_list m_items;
3145 m_orig_items <- m_items;
3148 else m_items <- items;
3149 None
3151 method hasaction _ = true
3153 method greetmsg =
3154 if Array.length m_items != Array.length m_orig_items
3155 then "Narrowed to " ^ m_narrow_pattern ^ " (ctrl-u to restore)"
3156 else ""
3158 method narrow pattern =
3159 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
3160 match reopt with
3161 | None -> ()
3162 | Some re ->
3163 let rec loop accu n =
3164 if n = -1
3165 then (
3166 m_narrow_pattern <- pattern;
3167 m_items <- Array.of_list accu
3169 else
3170 let (s, _, _) as o = m_items.(n) in
3171 let accu =
3172 if (try ignore (Str.search_forward re s 0); true
3173 with Not_found -> false)
3174 then o :: accu
3175 else accu
3177 loop accu (n-1)
3179 loop [] (Array.length m_items - 1)
3181 method denarrow =
3182 m_orig_items <- (
3183 if usebookmarks
3184 then Array.of_list state.bookmarks
3185 else state.outlines
3187 m_items <- m_orig_items
3189 method remove m =
3190 if usebookmarks
3191 then
3192 if m >= 0 && m < Array.length m_items
3193 then (
3194 m_hadremovals <- true;
3195 m_items <- Array.init (Array.length m_items - 1) (fun n ->
3196 let n = if n >= m then n+1 else n in
3197 m_items.(n)
3201 method reset anchor items =
3202 m_hadremovals <- false;
3203 if m_orig_items == empty || m_prev_items != items
3204 then (
3205 m_orig_items <- items;
3206 if String.length m_narrow_pattern = 0
3207 then m_items <- items;
3209 m_prev_items <- items;
3210 let rely = getanchory anchor in
3211 let active =
3212 let rec loop n best bestd =
3213 if n = Array.length m_items
3214 then best
3215 else
3216 let (_, _, anchor) = m_items.(n) in
3217 let orely = getanchory anchor in
3218 let d = abs (orely - rely) in
3219 if d < bestd
3220 then loop (n+1) n d
3221 else loop (n+1) best bestd
3223 loop 0 ~-1 max_int
3225 m_active <- active;
3226 m_first <- firstof m_first active
3227 end)
3230 let enterselector usebookmarks =
3231 let source = outlinesource usebookmarks in
3232 fun errmsg ->
3233 let outlines =
3234 if usebookmarks
3235 then Array.of_list state.bookmarks
3236 else state.outlines
3238 if Array.length outlines = 0
3239 then (
3240 showtext ' ' errmsg;
3242 else (
3243 state.text <- source#greetmsg;
3244 Glut.setCursor Glut.CURSOR_INHERIT;
3245 let anchor = getanchor () in
3246 source#reset anchor outlines;
3247 state.uioh <- coe (new outlinelistview ~source);
3248 G.postRedisplay "enter selector";
3252 let enteroutlinemode =
3253 let f = enterselector false in
3254 fun ()-> f "Document has no outline";
3257 let enterbookmarkmode =
3258 let f = enterselector true in
3259 fun () -> f "Document has no bookmarks (yet)";
3262 let color_of_string s =
3263 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
3264 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
3268 let color_to_string (r, g, b) =
3269 let r = truncate (r *. 256.0)
3270 and g = truncate (g *. 256.0)
3271 and b = truncate (b *. 256.0) in
3272 Printf.sprintf "%d/%d/%d" r g b
3275 let irect_of_string s =
3276 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
3279 let irect_to_string (x0,y0,x1,y1) =
3280 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
3283 let makecheckers () =
3284 (* Appropriated from lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
3285 following to say:
3286 converted by Issac Trotts. July 25, 2002 *)
3287 let image_height = 64
3288 and image_width = 64 in
3290 let make_image () =
3291 let image =
3292 GlPix.create `ubyte ~format:`rgb ~width:image_width ~height:image_height
3294 for i = 0 to image_width - 1 do
3295 for j = 0 to image_height - 1 do
3296 Raw.sets (GlPix.to_raw image) ~pos:(3*(i*image_height+j))
3297 (if (i land 8 ) lxor (j land 8) = 0
3298 then [|255;255;255|] else [|200;200;200|])
3299 done
3300 done;
3301 image
3303 let image = make_image () in
3304 let id = GlTex.gen_texture () in
3305 GlTex.bind_texture `texture_2d id;
3306 GlPix.store (`unpack_alignment 1);
3307 GlTex.image2d image;
3308 List.iter (GlTex.parameter ~target:`texture_2d)
3309 [ `wrap_s `repeat;
3310 `wrap_t `repeat;
3311 `mag_filter `nearest;
3312 `min_filter `nearest ];
3316 let setcheckers enabled =
3317 match state.texid with
3318 | None ->
3319 if enabled then state.texid <- Some (makecheckers ())
3321 | Some texid ->
3322 if not enabled
3323 then (
3324 GlTex.delete_texture texid;
3325 state.texid <- None;
3329 let int_of_string_with_suffix s =
3330 let l = String.length s in
3331 let s1, shift =
3332 if l > 1
3333 then
3334 let suffix = Char.lowercase s.[l-1] in
3335 match suffix with
3336 | 'k' -> String.sub s 0 (l-1), 10
3337 | 'm' -> String.sub s 0 (l-1), 20
3338 | 'g' -> String.sub s 0 (l-1), 30
3339 | _ -> s, 0
3340 else s, 0
3342 let n = int_of_string s1 in
3343 let m = n lsl shift in
3344 if m < 0 || m < n
3345 then raise (Failure "value too large")
3346 else m
3349 let string_with_suffix_of_int n =
3350 if n = 0
3351 then "0"
3352 else
3353 let n, s =
3354 if n = 0
3355 then 0, ""
3356 else (
3357 if n land ((1 lsl 20) - 1) = 0
3358 then n lsr 20, "M"
3359 else (
3360 if n land ((1 lsl 10) - 1) = 0
3361 then n lsr 10, "K"
3362 else n, ""
3366 let rec loop s n =
3367 let h = n mod 1000 in
3368 let n = n / 1000 in
3369 if n = 0
3370 then string_of_int h ^ s
3371 else (
3372 let s = Printf.sprintf "_%03d%s" h s in
3373 loop s n
3376 loop "" n ^ s;
3379 let defghyllscroll = (40, 8, 32);;
3380 let ghyllscroll_of_string s =
3381 let (n, a, b) as nab =
3382 if s = "default"
3383 then defghyllscroll
3384 else Scanf.sscanf s "%u,%u,%u" (fun n a b -> n, a, b)
3386 if n <= a || n <= b || a >= b
3387 then failwith "invalid ghyll N,A,B (N <= A, A < B, N <= B)";
3388 nab;
3391 let ghyllscroll_to_string ((n, a, b) as nab) =
3392 if nab = defghyllscroll
3393 then "default"
3394 else Printf.sprintf "%d,%d,%d" n a b;
3397 let describe_location () =
3398 let f (fn, _) l =
3399 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
3401 let fn, ln = List.fold_left f (-1, -1) state.layout in
3402 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
3403 let percent =
3404 if maxy <= 0
3405 then 100.
3406 else (100. *. (float state.y /. float maxy))
3408 if fn = ln
3409 then
3410 Printf.sprintf "page %d of %d [%.2f%%]"
3411 (fn+1) state.pagecount percent
3412 else
3413 Printf.sprintf
3414 "pages %d-%d of %d [%.2f%%]"
3415 (fn+1) (ln+1) state.pagecount percent
3418 let enterinfomode =
3419 let btos b = if b then "\xe2\x88\x9a" else "" in
3420 let showextended = ref false in
3421 let leave mode = function
3422 | Confirm -> state.mode <- mode
3423 | Cancel -> state.mode <- mode in
3424 let src =
3425 (object
3426 val mutable m_first_time = true
3427 val mutable m_l = []
3428 val mutable m_a = [||]
3429 val mutable m_prev_uioh = nouioh
3430 val mutable m_prev_mode = View
3432 inherit lvsourcebase
3434 method reset prev_mode prev_uioh =
3435 m_a <- Array.of_list (List.rev m_l);
3436 m_l <- [];
3437 m_prev_mode <- prev_mode;
3438 m_prev_uioh <- prev_uioh;
3439 if m_first_time
3440 then (
3441 let rec loop n =
3442 if n >= Array.length m_a
3443 then ()
3444 else
3445 match m_a.(n) with
3446 | _, _, _, Action _ -> m_active <- n
3447 | _ -> loop (n+1)
3449 loop 0;
3450 m_first_time <- false;
3453 method int name get set =
3454 m_l <-
3455 (name, `int get, 1, Action (
3456 fun u ->
3457 let ondone s =
3458 try set (int_of_string s)
3459 with exn ->
3460 state.text <- Printf.sprintf "bad integer `%s': %s"
3461 s (Printexc.to_string exn)
3463 state.text <- "";
3464 let te = name ^ ": ", "", None, intentry, ondone in
3465 state.mode <- Textentry (te, leave m_prev_mode);
3467 )) :: m_l
3469 method int_with_suffix name get set =
3470 m_l <-
3471 (name, `intws get, 1, Action (
3472 fun u ->
3473 let ondone s =
3474 try set (int_of_string_with_suffix s)
3475 with exn ->
3476 state.text <- Printf.sprintf "bad integer `%s': %s"
3477 s (Printexc.to_string exn)
3479 state.text <- "";
3480 let te =
3481 name ^ ": ", "", None, intentry_with_suffix, ondone
3483 state.mode <- Textentry (te, leave m_prev_mode);
3485 )) :: m_l
3487 method bool ?(offset=1) ?(btos=btos) name get set =
3488 m_l <-
3489 (name, `bool (btos, get), offset, Action (
3490 fun u ->
3491 let v = get () in
3492 set (not v);
3494 )) :: m_l
3496 method color name get set =
3497 m_l <-
3498 (name, `color get, 1, Action (
3499 fun u ->
3500 let invalid = (nan, nan, nan) in
3501 let ondone s =
3502 let c =
3503 try color_of_string s
3504 with exn ->
3505 state.text <- Printf.sprintf "bad color `%s': %s"
3506 s (Printexc.to_string exn);
3507 invalid
3509 if c <> invalid
3510 then set c;
3512 let te = name ^ ": ", "", None, textentry, ondone in
3513 state.text <- color_to_string (get ());
3514 state.mode <- Textentry (te, leave m_prev_mode);
3516 )) :: m_l
3518 method string name get set =
3519 m_l <-
3520 (name, `string get, 1, Action (
3521 fun u ->
3522 let ondone s = set s in
3523 let te = name ^ ": ", "", None, textentry, ondone in
3524 state.mode <- Textentry (te, leave m_prev_mode);
3526 )) :: m_l
3528 method colorspace name get set =
3529 m_l <-
3530 (name, `string get, 1, Action (
3531 fun _ ->
3532 let source =
3533 let vals = [| "rgb"; "bgr"; "gray" |] in
3534 (object
3535 inherit lvsourcebase
3537 initializer
3538 m_active <- int_of_colorspace conf.colorspace;
3539 m_first <- 0;
3541 method getitemcount = Array.length vals
3542 method getitem n = (vals.(n), 0)
3543 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3544 ignore (uioh, first, pan, qsearch);
3545 if not cancel then set active;
3546 None
3547 method hasaction _ = true
3548 end)
3550 state.text <- "";
3551 coe (new listview ~source ~trusted:true)
3552 )) :: m_l
3554 method caption s offset =
3555 m_l <- (s, `empty, offset, Noaction) :: m_l
3557 method caption2 s f offset =
3558 m_l <- (s, `string f, offset, Noaction) :: m_l
3560 method getitemcount = Array.length m_a
3562 method getitem n =
3563 let tostr = function
3564 | `int f -> string_of_int (f ())
3565 | `intws f -> string_with_suffix_of_int (f ())
3566 | `string f -> f ()
3567 | `color f -> color_to_string (f ())
3568 | `bool (btos, f) -> btos (f ())
3569 | `empty -> ""
3571 let name, t, offset, _ = m_a.(n) in
3572 ((let s = tostr t in
3573 if String.length s > 0
3574 then Printf.sprintf "%s\t%s" name s
3575 else name),
3576 offset)
3578 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3579 let uiohopt =
3580 if not cancel
3581 then (
3582 m_qsearch <- qsearch;
3583 let uioh =
3584 match m_a.(active) with
3585 | _, _, _, Action f -> f uioh
3586 | _ -> uioh
3588 Some uioh
3590 else None
3592 m_active <- active;
3593 m_first <- first;
3594 m_pan <- pan;
3595 uiohopt
3597 method hasaction n =
3598 match m_a.(n) with
3599 | _, _, _, Action _ -> true
3600 | _ -> false
3601 end)
3603 let rec fillsrc prevmode prevuioh =
3604 let sep () = src#caption "" 0 in
3605 let colorp name get set =
3606 src#string name
3607 (fun () -> color_to_string (get ()))
3608 (fun v ->
3610 let c = color_of_string v in
3611 set c
3612 with exn ->
3613 state.text <- Printf.sprintf "bad color `%s': %s"
3614 v (Printexc.to_string exn);
3617 let oldmode = state.mode in
3618 let birdseye = isbirdseye state.mode in
3620 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
3622 src#bool "presentation mode"
3623 (fun () -> conf.presentation)
3624 (fun v ->
3625 conf.presentation <- v;
3626 state.anchor <- getanchor ();
3627 represent ());
3629 src#bool "ignore case in searches"
3630 (fun () -> conf.icase)
3631 (fun v -> conf.icase <- v);
3633 src#bool "preload"
3634 (fun () -> conf.preload)
3635 (fun v -> conf.preload <- v);
3637 src#bool "highlight links"
3638 (fun () -> conf.hlinks)
3639 (fun v -> conf.hlinks <- v);
3641 src#bool "under info"
3642 (fun () -> conf.underinfo)
3643 (fun v -> conf.underinfo <- v);
3645 src#bool "persistent bookmarks"
3646 (fun () -> conf.savebmarks)
3647 (fun v -> conf.savebmarks <- v);
3649 src#bool "proportional display"
3650 (fun () -> conf.proportional)
3651 (fun v -> reqlayout conf.angle v);
3653 src#bool "trim margins"
3654 (fun () -> conf.trimmargins)
3655 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
3657 src#bool "persistent location"
3658 (fun () -> conf.jumpback)
3659 (fun v -> conf.jumpback <- v);
3661 sep ();
3662 src#int "inter-page space"
3663 (fun () -> conf.interpagespace)
3664 (fun n ->
3665 conf.interpagespace <- n;
3666 let pageno, py =
3667 match state.layout with
3668 | [] -> 0, 0
3669 | l :: _ ->
3670 l.pageno, l.pagey
3672 state.maxy <- calcheight ();
3673 let y = getpagey pageno in
3674 gotoy (y + py)
3677 src#int "page bias"
3678 (fun () -> conf.pagebias)
3679 (fun v -> conf.pagebias <- v);
3681 src#int "scroll step"
3682 (fun () -> conf.scrollstep)
3683 (fun n -> conf.scrollstep <- n);
3685 src#int "auto scroll step"
3686 (fun () ->
3687 match state.autoscroll with
3688 | Some step -> step
3689 | _ -> conf.autoscrollstep)
3690 (fun n ->
3691 if state.autoscroll <> None
3692 then state.autoscroll <- Some n;
3693 conf.autoscrollstep <- n);
3695 src#int "zoom"
3696 (fun () -> truncate (conf.zoom *. 100.))
3697 (fun v -> setzoom ((float v) /. 100.));
3699 src#int "rotation"
3700 (fun () -> conf.angle)
3701 (fun v -> reqlayout v conf.proportional);
3703 src#int "scroll bar width"
3704 (fun () -> state.scrollw)
3705 (fun v ->
3706 state.scrollw <- v;
3707 conf.scrollbw <- v;
3708 reshape conf.winw conf.winh;
3711 src#int "scroll handle height"
3712 (fun () -> conf.scrollh)
3713 (fun v -> conf.scrollh <- v;);
3715 src#int "thumbnail width"
3716 (fun () -> conf.thumbw)
3717 (fun v ->
3718 conf.thumbw <- min 4096 v;
3719 match oldmode with
3720 | Birdseye beye ->
3721 leavebirdseye beye false;
3722 enterbirdseye ()
3723 | _ -> ()
3726 src#string "columns"
3727 (fun () ->
3728 match conf.columns with
3729 | None -> "1"
3730 | Some (multicol, _) -> columns_to_string multicol)
3731 (fun v ->
3732 let n, a, b = columns_of_string v in
3733 setcolumns n a b);
3735 sep ();
3736 src#caption "Presentation mode" 0;
3737 src#bool "scrollbar visible"
3738 (fun () -> conf.scrollbarinpm)
3739 (fun v ->
3740 if v != conf.scrollbarinpm
3741 then (
3742 conf.scrollbarinpm <- v;
3743 if conf.presentation
3744 then (
3745 state.scrollw <- if v then conf.scrollbw else 0;
3746 reshape conf.winw conf.winh;
3751 sep ();
3752 src#caption "Pixmap cache" 0;
3753 src#int_with_suffix "size (advisory)"
3754 (fun () -> conf.memlimit)
3755 (fun v -> conf.memlimit <- v);
3757 src#caption2 "used"
3758 (fun () -> Printf.sprintf "%s bytes, %d tiles"
3759 (string_with_suffix_of_int state.memused)
3760 (Hashtbl.length state.tilemap)) 1;
3762 sep ();
3763 src#caption "Layout" 0;
3764 src#caption2 "Dimension"
3765 (fun () ->
3766 Printf.sprintf "%dx%d (virtual %dx%d)"
3767 conf.winw conf.winh
3768 state.w state.maxy)
3770 if conf.debug
3771 then
3772 src#caption2 "Position" (fun () ->
3773 Printf.sprintf "%dx%d" state.x state.y
3775 else
3776 src#caption2 "Visible" (fun () -> describe_location ()) 1
3779 sep ();
3780 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
3781 "Save these parameters as global defaults at exit"
3782 (fun () -> conf.bedefault)
3783 (fun v -> conf.bedefault <- v)
3786 sep ();
3787 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
3788 src#bool ~offset:0 ~btos "Extended parameters"
3789 (fun () -> !showextended)
3790 (fun v -> showextended := v; fillsrc prevmode prevuioh);
3791 if !showextended
3792 then (
3793 src#bool "checkers"
3794 (fun () -> conf.checkers)
3795 (fun v -> conf.checkers <- v; setcheckers v);
3796 src#bool "verbose"
3797 (fun () -> conf.verbose)
3798 (fun v -> conf.verbose <- v);
3799 src#bool "invert colors"
3800 (fun () -> conf.invert)
3801 (fun v -> conf.invert <- v);
3802 src#bool "max fit"
3803 (fun () -> conf.maxhfit)
3804 (fun v -> conf.maxhfit <- v);
3805 src#bool "redirect stderr"
3806 (fun () -> conf.redirectstderr)
3807 (fun v -> conf.redirectstderr <- v; redirectstderr ());
3808 src#string "uri launcher"
3809 (fun () -> conf.urilauncher)
3810 (fun v -> conf.urilauncher <- v);
3811 src#string "tile size"
3812 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
3813 (fun v ->
3815 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
3816 conf.tileh <- max 64 w;
3817 conf.tilew <- max 64 h;
3818 flushtiles ();
3819 with exn ->
3820 state.text <- Printf.sprintf "bad tile size `%s': %s"
3821 v (Printexc.to_string exn));
3822 src#int "texture count"
3823 (fun () -> conf.texcount)
3824 (fun v ->
3825 if realloctexts v
3826 then conf.texcount <- v
3827 else showtext '!' " Failed to set texture count please retry later"
3829 src#int "slice height"
3830 (fun () -> conf.sliceheight)
3831 (fun v ->
3832 conf.sliceheight <- v;
3833 wcmd "sliceh" [`i conf.sliceheight];
3835 src#int "anti-aliasing level"
3836 (fun () -> conf.aalevel)
3837 (fun v ->
3838 conf.aalevel <- bound v 0 8;
3839 state.anchor <- getanchor ();
3840 opendoc state.path state.password;
3842 src#int "ui font size"
3843 (fun () -> fstate.fontsize)
3844 (fun v -> setfontsize (bound v 5 100));
3845 colorp "background color"
3846 (fun () -> conf.bgcolor)
3847 (fun v -> conf.bgcolor <- v);
3848 src#bool "crop hack"
3849 (fun () -> conf.crophack)
3850 (fun v -> conf.crophack <- v);
3851 src#string "trim fuzz"
3852 (fun () -> irect_to_string conf.trimfuzz)
3853 (fun v ->
3855 conf.trimfuzz <- irect_of_string v;
3856 if conf.trimmargins
3857 then settrim true conf.trimfuzz;
3858 with exn ->
3859 state.text <- Printf.sprintf "bad irect `%s': %s"
3860 v (Printexc.to_string exn)
3862 src#string "throttle"
3863 (fun () ->
3864 match conf.maxwait with
3865 | None -> "show place holder if page is not ready"
3866 | Some time ->
3867 if time = infinity
3868 then "wait for page to fully render"
3869 else
3870 "wait " ^ string_of_float time
3871 ^ " seconds before showing placeholder"
3873 (fun v ->
3875 let f = float_of_string v in
3876 if f <= 0.0
3877 then conf.maxwait <- None
3878 else conf.maxwait <- Some f
3879 with exn ->
3880 state.text <- Printf.sprintf "bad time `%s': %s"
3881 v (Printexc.to_string exn)
3883 src#string "ghyll scroll"
3884 (fun () ->
3885 match conf.ghyllscroll with
3886 | None -> ""
3887 | Some nab -> ghyllscroll_to_string nab
3889 (fun v ->
3891 let gs =
3892 if String.length v = 0
3893 then None
3894 else Some (ghyllscroll_of_string v)
3896 conf.ghyllscroll <- gs
3897 with exn ->
3898 state.text <- Printf.sprintf "bad ghyll `%s': %s"
3899 v (Printexc.to_string exn)
3901 src#colorspace "color space"
3902 (fun () -> colorspace_to_string conf.colorspace)
3903 (fun v ->
3904 conf.colorspace <- colorspace_of_int v;
3905 wcmd "cs" [`i v];
3906 load state.layout;
3910 sep ();
3911 src#caption "Document" 0;
3912 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
3913 src#caption2 "Pages"
3914 (fun () -> string_of_int state.pagecount) 1;
3915 src#caption2 "Dimensions"
3916 (fun () -> string_of_int (List.length state.pdims)) 1;
3917 if conf.trimmargins
3918 then (
3919 sep ();
3920 src#caption "Trimmed margins" 0;
3921 src#caption2 "Dimensions"
3922 (fun () -> string_of_int (List.length state.pdims)) 1;
3925 src#reset prevmode prevuioh;
3927 fun () ->
3928 state.text <- "";
3929 let prevmode = state.mode
3930 and prevuioh = state.uioh in
3931 fillsrc prevmode prevuioh;
3932 let source = (src :> lvsource) in
3933 state.uioh <- coe (object (self)
3934 inherit listview ~source ~trusted:true as super
3935 val mutable m_prevmemused = 0
3936 method infochanged = function
3937 | Memused ->
3938 if m_prevmemused != state.memused
3939 then (
3940 m_prevmemused <- state.memused;
3941 G.postRedisplay "memusedchanged";
3943 | Pdim -> G.postRedisplay "pdimchanged"
3944 | Docinfo -> fillsrc prevmode prevuioh
3946 method special key =
3947 if Glut.getModifiers () land Glut.active_ctrl = 0
3948 then
3949 match key with
3950 | Glut.KEY_LEFT -> coe (self#updownlevel ~-1)
3951 | Glut.KEY_RIGHT -> coe (self#updownlevel 1)
3952 | _ -> super#special key
3953 else super#special key
3954 end);
3955 G.postRedisplay "info";
3958 let enterhelpmode =
3959 let source =
3960 (object
3961 inherit lvsourcebase
3962 method getitemcount = Array.length state.help
3963 method getitem n =
3964 let s, n, _ = state.help.(n) in
3965 (s, n)
3967 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3968 let optuioh =
3969 if not cancel
3970 then (
3971 m_qsearch <- qsearch;
3972 match state.help.(active) with
3973 | _, _, Action f -> Some (f uioh)
3974 | _ -> Some (uioh)
3976 else None
3978 m_active <- active;
3979 m_first <- first;
3980 m_pan <- pan;
3981 optuioh
3983 method hasaction n =
3984 match state.help.(n) with
3985 | _, _, Action _ -> true
3986 | _ -> false
3988 initializer
3989 m_active <- -1
3990 end)
3991 in fun () ->
3992 state.uioh <- coe (new listview ~source ~trusted:true);
3993 G.postRedisplay "help";
3996 let entermsgsmode =
3997 let msgsource =
3998 let re = Str.regexp "[\r\n]" in
3999 (object
4000 inherit lvsourcebase
4001 val mutable m_items = [||]
4003 method getitemcount = 1 + Array.length m_items
4005 method getitem n =
4006 if n = 0
4007 then "[Clear]", 0
4008 else m_items.(n-1), 0
4010 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4011 ignore uioh;
4012 if not cancel
4013 then (
4014 if active = 0
4015 then Buffer.clear state.errmsgs;
4016 m_qsearch <- qsearch;
4018 m_active <- active;
4019 m_first <- first;
4020 m_pan <- pan;
4021 None
4023 method hasaction n =
4024 n = 0
4026 method reset =
4027 state.newerrmsgs <- false;
4028 let l = Str.split re (Buffer.contents state.errmsgs) in
4029 m_items <- Array.of_list l
4031 initializer
4032 m_active <- 0
4033 end)
4034 in fun () ->
4035 state.text <- "";
4036 msgsource#reset;
4037 let source = (msgsource :> lvsource) in
4038 state.uioh <- coe (object
4039 inherit listview ~source ~trusted:false as super
4040 method display =
4041 if state.newerrmsgs
4042 then msgsource#reset;
4043 super#display
4044 end);
4045 G.postRedisplay "msgs";
4048 let quickbookmark ?title () =
4049 match state.layout with
4050 | [] -> ()
4051 | l :: _ ->
4052 let title =
4053 match title with
4054 | None ->
4055 let sec = Unix.gettimeofday () in
4056 let tm = Unix.localtime sec in
4057 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
4058 (l.pageno+1)
4059 tm.Unix.tm_mday
4060 tm.Unix.tm_mon
4061 (tm.Unix.tm_year + 1900)
4062 tm.Unix.tm_hour
4063 tm.Unix.tm_min
4064 | Some title -> title
4066 state.bookmarks <-
4067 (title, 0, (l.pageno, float l.pagey /. float l.pageh))
4068 :: state.bookmarks
4071 let doreshape w h =
4072 state.fullscreen <- None;
4073 Glut.reshapeWindow w h;
4076 let viewkeyboard key =
4077 let enttext te =
4078 let mode = state.mode in
4079 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
4080 state.text <- "";
4081 enttext ();
4082 G.postRedisplay "view:enttext"
4084 let c = Char.chr key in
4085 match c with
4086 | '\027' | 'q' -> (* escape *)
4087 begin match state.mstate with
4088 | Mzoomrect _ ->
4089 state.mstate <- Mnone;
4090 Glut.setCursor Glut.CURSOR_INHERIT;
4091 G.postRedisplay "kill zoom rect";
4092 | _ ->
4093 raise Quit
4094 end;
4096 | '\008' -> (* backspace *)
4097 let y = getnav ~-1 in
4098 gotoy_and_clear_text y
4100 | 'o' ->
4101 enteroutlinemode ()
4103 | 'u' ->
4104 state.rects <- [];
4105 state.text <- "";
4106 G.postRedisplay "dehighlight";
4108 | '/' | '?' ->
4109 let ondone isforw s =
4110 cbput state.hists.pat s;
4111 state.searchpattern <- s;
4112 search s isforw
4114 let s = String.create 1 in
4115 s.[0] <- c;
4116 enttext (s, "", Some (onhist state.hists.pat),
4117 textentry, ondone (c ='/'))
4119 | '+' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
4120 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
4121 setzoom (conf.zoom +. incr)
4123 | '+' ->
4124 let ondone s =
4125 let n =
4126 try int_of_string s with exc ->
4127 state.text <- Printf.sprintf "bad integer `%s': %s"
4128 s (Printexc.to_string exc);
4129 max_int
4131 if n != max_int
4132 then (
4133 conf.pagebias <- n;
4134 state.text <- "page bias is now " ^ string_of_int n;
4137 enttext ("page bias: ", "", None, intentry, ondone)
4139 | '-' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
4140 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
4141 setzoom (max 0.01 (conf.zoom -. decr))
4143 | '-' ->
4144 let ondone msg = state.text <- msg in
4145 enttext (
4146 "option [acfhilpstvACPRSZTI]: ", "", None,
4147 optentry state.mode, ondone
4150 | '0' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
4151 setzoom 1.0
4153 | '1' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
4154 let zoom = zoomforh conf.winw conf.winh state.scrollw in
4155 if zoom < 1.0
4156 then setzoom zoom
4158 | '9' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
4159 togglebirdseye ()
4161 | '0' .. '9' ->
4162 let ondone s =
4163 let n =
4164 try int_of_string s with exc ->
4165 state.text <- Printf.sprintf "bad integer `%s': %s"
4166 s (Printexc.to_string exc);
4169 if n >= 0
4170 then (
4171 addnav ();
4172 cbput state.hists.pag (string_of_int n);
4173 gotopage1 (n + conf.pagebias - 1) 0;
4176 let pageentry text key =
4177 match Char.unsafe_chr key with
4178 | 'g' -> TEdone text
4179 | _ -> intentry text key
4181 let text = "x" in text.[0] <- c;
4182 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone)
4184 | 'b' ->
4185 state.scrollw <- if state.scrollw > 0 then 0 else conf.scrollbw;
4186 reshape conf.winw conf.winh;
4188 | 'l' ->
4189 conf.hlinks <- not conf.hlinks;
4190 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
4191 G.postRedisplay "toggle highlightlinks";
4193 | 'a' ->
4194 begin match state.autoscroll with
4195 | Some step ->
4196 conf.autoscrollstep <- step;
4197 state.autoscroll <- None
4198 | None ->
4199 if conf.autoscrollstep = 0
4200 then state.autoscroll <- Some 1
4201 else state.autoscroll <- Some conf.autoscrollstep
4204 | 'P' ->
4205 conf.presentation <- not conf.presentation;
4206 if conf.presentation
4207 then (
4208 if not conf.scrollbarinpm
4209 then state.scrollw <- 0;
4211 else
4212 state.scrollw <- conf.scrollbw;
4214 showtext ' ' ("presentation mode " ^
4215 if conf.presentation then "on" else "off");
4216 state.anchor <- getanchor ();
4217 represent ()
4219 | 'f' ->
4220 begin match state.fullscreen with
4221 | None ->
4222 state.fullscreen <- Some (conf.winw, conf.winh);
4223 Glut.fullScreen ()
4224 | Some (w, h) ->
4225 state.fullscreen <- None;
4226 doreshape w h
4229 | 'g' ->
4230 gotoy_and_clear_text 0
4232 | 'G' ->
4233 gotopage1 (state.pagecount - 1) 0
4235 | 'n' ->
4236 search state.searchpattern true
4238 | 'p' | 'N' ->
4239 search state.searchpattern false
4241 | 't' ->
4242 begin match state.layout with
4243 | [] -> ()
4244 | l :: _ ->
4245 gotoy_and_clear_text (getpagey l.pageno)
4248 | ' ' ->
4249 begin match List.rev state.layout with
4250 | [] -> ()
4251 | l :: _ ->
4252 let pageno = min (l.pageno+1) (state.pagecount-1) in
4253 gotoy_and_clear_text (getpagey pageno)
4256 | '\127' -> (* del *)
4257 begin match state.layout with
4258 | [] -> ()
4259 | l :: _ ->
4260 let pageno = max 0 (l.pageno-1) in
4261 gotoy_and_clear_text (getpagey pageno)
4264 | '=' ->
4265 showtext ' ' (describe_location ());
4267 | 'w' ->
4268 begin match state.layout with
4269 | [] -> ()
4270 | l :: _ ->
4271 doreshape (l.pagew + state.scrollw) l.pageh;
4272 G.postRedisplay "w"
4275 | '\'' ->
4276 enterbookmarkmode ()
4278 | 'h' ->
4279 enterhelpmode ()
4281 | 'i' ->
4282 enterinfomode ()
4284 | 'e' when conf.redirectstderr ->
4285 entermsgsmode ()
4287 | 'm' ->
4288 let ondone s =
4289 match state.layout with
4290 | l :: _ ->
4291 state.bookmarks <-
4292 (s, 0, (l.pageno, float l.pagey /. float l.pageh))
4293 :: state.bookmarks
4294 | _ -> ()
4296 enttext ("bookmark: ", "", None, textentry, ondone)
4298 | '~' ->
4299 quickbookmark ();
4300 showtext ' ' "Quick bookmark added";
4302 | 'z' ->
4303 begin match state.layout with
4304 | l :: _ ->
4305 let rect = getpdimrect l.pagedimno in
4306 let w, h =
4307 if conf.crophack
4308 then
4309 (truncate (1.8 *. (rect.(1) -. rect.(0))),
4310 truncate (1.2 *. (rect.(3) -. rect.(0))))
4311 else
4312 (truncate (rect.(1) -. rect.(0)),
4313 truncate (rect.(3) -. rect.(0)))
4315 let w = truncate ((float w)*.conf.zoom)
4316 and h = truncate ((float h)*.conf.zoom) in
4317 if w != 0 && h != 0
4318 then (
4319 state.anchor <- getanchor ();
4320 doreshape (w + state.scrollw) (h + conf.interpagespace)
4322 G.postRedisplay "z";
4324 | [] -> ()
4327 | '\000' -> (* ctrl-2 *)
4328 let maxw = getmaxw () in
4329 if maxw > 0.0
4330 then setzoom (maxw /. float conf.winw)
4332 | '<' | '>' ->
4333 reqlayout (conf.angle + (if c = '>' then 30 else -30)) conf.proportional
4335 | '[' | ']' ->
4336 conf.colorscale <-
4337 bound (conf.colorscale +. (if c = ']' then 0.1 else -0.1)) 0.0 1.0
4339 G.postRedisplay "brightness";
4341 | 'k' ->
4342 begin match state.mode with
4343 | Birdseye beye -> upbirdseye 1 beye
4344 | _ -> gotoy (clamp (-conf.scrollstep))
4347 | 'j' ->
4348 begin match state.mode with
4349 | Birdseye beye -> downbirdseye 1 beye
4350 | _ -> gotoy (clamp conf.scrollstep)
4353 | 'r' ->
4354 state.anchor <- getanchor ();
4355 opendoc state.path state.password
4357 | 'v' when not conf.debug ->
4358 List.iter debugl state.layout;
4360 | 'v' when conf.debug ->
4361 state.rects <- [];
4362 List.iter (fun l ->
4363 match getopaque l.pageno with
4364 | None -> ()
4365 | Some opaque ->
4366 let x0, y0, x1, y1 = pagebbox opaque in
4367 let a,b = float x0, float y0 in
4368 let c,d = float x1, float y0 in
4369 let e,f = float x1, float y1 in
4370 let h,j = float x0, float y1 in
4371 let rect = (a,b,c,d,e,f,h,j) in
4372 debugrect rect;
4373 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
4374 ) state.layout;
4375 G.postRedisplay "v";
4377 | _ ->
4378 vlog "huh? %d %c" key (Char.chr key);
4381 let birdseyekeyboard key ((_, _, pageno, _, _) as beye) =
4382 match key with
4383 | 27 -> (* escape *)
4384 leavebirdseye beye true
4386 | 12 -> (* ctrl-l *)
4387 let y, h = getpageyh pageno in
4388 let top = (conf.winh - h) / 2 in
4389 gotoy (max 0 (y - top))
4391 | 13 -> (* enter *)
4392 leavebirdseye beye false
4394 | _ ->
4395 viewkeyboard key
4398 let keyboard ~key ~x ~y =
4399 ignore x;
4400 ignore y;
4401 if key = 7 && not (istextentry state.mode) (* ctrl-g *)
4402 then wcmd "interrupt" []
4403 else state.uioh <- state.uioh#key key
4406 let birdseyespecial key ((oconf, leftx, _, hooverpageno, anchor) as beye) =
4407 let incr =
4408 match conf.columns with
4409 | None -> 1
4410 | Some ((c, _, _), _) -> c
4412 match key with
4413 | Glut.KEY_UP -> upbirdseye incr beye
4414 | Glut.KEY_DOWN -> downbirdseye incr beye
4415 | Glut.KEY_LEFT -> upbirdseye 1 beye
4416 | Glut.KEY_RIGHT -> downbirdseye 1 beye
4418 | Glut.KEY_PAGE_UP ->
4419 begin match state.layout with
4420 | l :: _ ->
4421 if l.pagey != 0
4422 then (
4423 state.mode <- Birdseye (
4424 oconf, leftx, l.pageno, hooverpageno, anchor
4426 gotopage1 l.pageno 0;
4428 else (
4429 let layout = layout (state.y-conf.winh) conf.winh in
4430 match layout with
4431 | [] -> gotoy (clamp (-conf.winh))
4432 | l :: _ ->
4433 state.mode <- Birdseye (
4434 oconf, leftx, l.pageno, hooverpageno, anchor
4436 gotopage1 l.pageno 0
4439 | [] -> gotoy (clamp (-conf.winh))
4440 end;
4442 | Glut.KEY_PAGE_DOWN ->
4443 begin match List.rev state.layout with
4444 | l :: _ ->
4445 let layout = layout (state.y + conf.winh) conf.winh in
4446 begin match layout with
4447 | [] ->
4448 let incr = l.pageh - l.pagevh in
4449 if incr = 0
4450 then (
4451 state.mode <-
4452 Birdseye (
4453 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
4455 G.postRedisplay "birdseye pagedown";
4457 else gotoy (clamp (incr + conf.interpagespace*2));
4459 | l :: _ ->
4460 state.mode <-
4461 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
4462 gotopage1 l.pageno 0;
4465 | [] -> gotoy (clamp conf.winh)
4466 end;
4468 | Glut.KEY_HOME ->
4469 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
4470 gotopage1 0 0
4472 | Glut.KEY_END ->
4473 let pageno = state.pagecount - 1 in
4474 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
4475 if not (pagevisible state.layout pageno)
4476 then
4477 let h =
4478 match List.rev state.pdims with
4479 | [] -> conf.winh
4480 | (_, _, h, _) :: _ -> h
4482 gotoy (max 0 (getpagey pageno - (conf.winh - h - conf.interpagespace)))
4483 else G.postRedisplay "birdseye end";
4484 | _ -> ()
4487 let setautoscrollspeed step goingdown =
4488 let incr = max 1 ((abs step) / 2) in
4489 let incr = if goingdown then incr else -incr in
4490 let astep = step + incr in
4491 state.autoscroll <- Some astep;
4494 let special ~key ~x ~y =
4495 ignore x;
4496 ignore y;
4497 state.uioh <- state.uioh#special key
4500 let drawpage l =
4501 let color =
4502 match state.mode with
4503 | Textentry _ -> scalecolor 0.4
4504 | View -> scalecolor 1.0
4505 | Birdseye (_, _, pageno, hooverpageno, _) ->
4506 if l.pageno = hooverpageno
4507 then scalecolor 0.9
4508 else (
4509 if l.pageno = pageno
4510 then scalecolor 1.0
4511 else scalecolor 0.8
4514 drawtiles l color;
4515 begin match getopaque l.pageno with
4516 | Some opaque ->
4517 if tileready l l.pagex l.pagey
4518 then
4519 let x = l.pagedispx - l.pagex
4520 and y = l.pagedispy - l.pagey in
4521 postprocess opaque conf.hlinks x y;
4523 | _ -> ()
4524 end;
4527 let scrollindicator () =
4528 let sbw, ph, sh = state.uioh#scrollph in
4529 let sbh, pw, sw = state.uioh#scrollpw in
4531 GlDraw.color (0.64, 0.64, 0.64);
4532 GlDraw.rect
4533 (float (conf.winw - sbw), 0.)
4534 (float conf.winw, float conf.winh)
4536 GlDraw.rect
4537 (0., float (conf.winh - sbh))
4538 (float (conf.winw - state.scrollw - 1), float conf.winh)
4540 GlDraw.color (0.0, 0.0, 0.0);
4542 GlDraw.rect
4543 (float (conf.winw - sbw), ph)
4544 (float conf.winw, ph +. sh)
4546 GlDraw.rect
4547 (pw, float (conf.winh - sbh))
4548 (pw +. sw, float conf.winh)
4552 let pagetranslatepoint l x y =
4553 let dy = y - l.pagedispy in
4554 let y = dy + l.pagey in
4555 let dx = x - l.pagedispx in
4556 let x = dx + l.pagex in
4557 (x, y);
4560 let showsel () =
4561 match state.mstate with
4562 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
4565 | Msel ((x0, y0), (x1, y1)) ->
4566 let rec loop = function
4567 | l :: ls ->
4568 if ((y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
4569 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh))))
4570 && ((x0 >= l.pagedispx && x0 <= (l.pagedispx + l.pagevw))
4571 || ((x1 >= l.pagedispx && x1 <= (l.pagedispx + l.pagevw))))
4572 then
4573 match getopaque l.pageno with
4574 | Some opaque ->
4575 let dx, dy = pagetranslatepoint l 0 0 in
4576 let x0 = x0 + dx
4577 and y0 = y0 + dy
4578 and x1 = x1 + dx
4579 and y1 = y1 + dy in
4580 GlMat.mode `modelview;
4581 GlMat.push ();
4582 GlMat.translate ~x:(float ~-dx) ~y:(float ~-dy) ();
4583 seltext opaque (x0, y0, x1, y1);
4584 GlMat.pop ();
4585 | _ -> ()
4586 else loop ls
4587 | [] -> ()
4589 loop state.layout
4592 let showrects () =
4593 Gl.enable `blend;
4594 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
4595 GlDraw.polygon_mode `both `fill;
4596 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
4597 List.iter
4598 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
4599 List.iter (fun l ->
4600 if l.pageno = pageno
4601 then (
4602 let dx = float (l.pagedispx - l.pagex) in
4603 let dy = float (l.pagedispy - l.pagey) in
4604 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
4605 GlDraw.begins `quads;
4607 GlDraw.vertex2 (x0+.dx, y0+.dy);
4608 GlDraw.vertex2 (x1+.dx, y1+.dy);
4609 GlDraw.vertex2 (x2+.dx, y2+.dy);
4610 GlDraw.vertex2 (x3+.dx, y3+.dy);
4612 GlDraw.ends ();
4614 ) state.layout
4615 ) state.rects
4617 Gl.disable `blend;
4620 let display () =
4621 GlClear.color (scalecolor2 conf.bgcolor);
4622 GlClear.clear [`color];
4623 List.iter drawpage state.layout;
4624 showrects ();
4625 showsel ();
4626 state.uioh#display;
4627 scrollindicator ();
4628 begin match state.mstate with
4629 | Mzoomrect ((x0, y0), (x1, y1)) ->
4630 Gl.enable `blend;
4631 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
4632 GlDraw.polygon_mode `both `fill;
4633 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
4634 GlDraw.rect (float x0, float y0)
4635 (float x1, float y1);
4636 Gl.disable `blend;
4637 | _ -> ()
4638 end;
4639 enttext ();
4640 Glut.swapBuffers ();
4643 let getunder x y =
4644 let rec f = function
4645 | l :: rest ->
4646 begin match getopaque l.pageno with
4647 | Some opaque ->
4648 let x0 = l.pagedispx in
4649 let x1 = x0 + l.pagevw in
4650 let y0 = l.pagedispy in
4651 let y1 = y0 + l.pagevh in
4652 if y >= y0 && y <= y1 && x >= x0 && x <= x1
4653 then
4654 let px, py = pagetranslatepoint l x y in
4655 match whatsunder opaque px py with
4656 | Unone -> f rest
4657 | under -> under
4658 else f rest
4659 | _ ->
4660 f rest
4662 | [] -> Unone
4664 f state.layout
4667 let zoomrect x y x1 y1 =
4668 let x0 = min x x1
4669 and x1 = max x x1
4670 and y0 = min y y1 in
4671 gotoy (state.y + y0);
4672 state.anchor <- getanchor ();
4673 let zoom = (float conf.winw *. conf.zoom) /. float (x1 - x0) in
4674 let margin =
4675 if state.w < conf.winw - state.scrollw
4676 then (conf.winw - state.scrollw - state.w) / 2
4677 else 0
4679 state.x <- (state.x + margin) - x0;
4680 setzoom zoom;
4681 Glut.setCursor Glut.CURSOR_INHERIT;
4682 state.mstate <- Mnone;
4685 let scrollx x =
4686 let winw = conf.winw - state.scrollw - 1 in
4687 let s = float x /. float winw in
4688 let destx = truncate (float (state.w + winw) *. s) in
4689 state.x <- winw - destx;
4690 gotoy_and_clear_text state.y;
4691 state.mstate <- Mscrollx;
4694 let scrolly y =
4695 let s = float y /. float conf.winh in
4696 let desty = truncate (float (state.maxy - conf.winh) *. s) in
4697 gotoy_and_clear_text desty;
4698 state.mstate <- Mscrolly;
4701 let viewmouse button bstate x y =
4702 match button with
4703 | Glut.OTHER_BUTTON n when (n == 3 || n == 4) && bstate = Glut.UP ->
4704 if Glut.getModifiers () land Glut.active_ctrl != 0
4705 then (
4706 match state.mstate with
4707 | Mzoom (oldn, i) ->
4708 if oldn = n
4709 then (
4710 if i = 2
4711 then
4712 let incr =
4713 match n with
4714 | 4 ->
4715 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
4716 | _ ->
4717 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
4719 let zoom = conf.zoom -. incr in
4720 setzoom zoom;
4721 state.mstate <- Mzoom (n, 0);
4722 else
4723 state.mstate <- Mzoom (n, i+1);
4725 else state.mstate <- Mzoom (n, 0)
4727 | _ -> state.mstate <- Mzoom (n, 0)
4729 else (
4730 match state.autoscroll with
4731 | Some step -> setautoscrollspeed step (n=4)
4732 | None ->
4733 let incr =
4734 if n = 3
4735 then -conf.scrollstep
4736 else conf.scrollstep
4738 let incr = incr * 2 in
4739 let y = clamp incr in
4740 gotoy_and_clear_text y
4743 | Glut.LEFT_BUTTON when Glut.getModifiers () land Glut.active_ctrl != 0 ->
4744 if bstate = Glut.DOWN
4745 then (
4746 Glut.setCursor Glut.CURSOR_CROSSHAIR;
4747 state.mstate <- Mpan (x, y)
4749 else
4750 state.mstate <- Mnone
4752 | Glut.RIGHT_BUTTON ->
4753 if bstate = Glut.DOWN
4754 then (
4755 Glut.setCursor Glut.CURSOR_CYCLE;
4756 let p = (x, y) in
4757 state.mstate <- Mzoomrect (p, p)
4759 else (
4760 match state.mstate with
4761 | Mzoomrect ((x0, y0), _) ->
4762 if abs (x-x0) > 10 && abs (y - y0) > 10
4763 then zoomrect x0 y0 x y
4764 else (
4765 state.mstate <- Mnone;
4766 Glut.setCursor Glut.CURSOR_INHERIT;
4767 G.postRedisplay "kill accidental zoom rect";
4769 | _ ->
4770 Glut.setCursor Glut.CURSOR_INHERIT;
4771 state.mstate <- Mnone
4774 | Glut.LEFT_BUTTON when x > conf.winw - state.scrollw ->
4775 if bstate = Glut.DOWN
4776 then
4777 let _, position, sh = state.uioh#scrollph in
4778 if y > truncate position && y < truncate (position +. sh)
4779 then state.mstate <- Mscrolly
4780 else scrolly y
4781 else
4782 state.mstate <- Mnone
4784 | Glut.LEFT_BUTTON when y > conf.winh - state.hscrollh ->
4785 if bstate = Glut.DOWN
4786 then
4787 let _, position, sw = state.uioh#scrollpw in
4788 if x > truncate position && x < truncate (position +. sw)
4789 then state.mstate <- Mscrollx
4790 else scrollx x
4791 else
4792 state.mstate <- Mnone
4794 | Glut.LEFT_BUTTON ->
4795 let dest = if bstate = Glut.DOWN then getunder x y else Unone in
4796 begin match dest with
4797 | Ulinkgoto (pageno, top) ->
4798 if pageno >= 0
4799 then (
4800 addnav ();
4801 gotopage1 pageno top;
4804 | Ulinkuri s ->
4805 gotouri s
4807 | Unone when bstate = Glut.DOWN ->
4808 Glut.setCursor Glut.CURSOR_CROSSHAIR;
4809 state.mstate <- Mpan (x, y);
4811 | Unone | Utext _ ->
4812 if bstate = Glut.DOWN
4813 then (
4814 if conf.angle mod 360 = 0
4815 then (
4816 state.mstate <- Msel ((x, y), (x, y));
4817 G.postRedisplay "mouse select";
4820 else (
4821 match state.mstate with
4822 | Mnone -> ()
4824 | Mzoom _ | Mscrollx | Mscrolly ->
4825 state.mstate <- Mnone
4827 | Mzoomrect ((x0, y0), _) ->
4828 zoomrect x0 y0 x y
4830 | Mpan _ ->
4831 Glut.setCursor Glut.CURSOR_INHERIT;
4832 state.mstate <- Mnone
4834 | Msel ((_, y0), (_, y1)) ->
4835 let f l =
4836 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
4837 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
4838 then
4839 match getopaque l.pageno with
4840 | Some opaque ->
4841 copysel opaque
4842 | _ -> ()
4844 List.iter f state.layout;
4845 copysel ""; (* ugly *)
4846 Glut.setCursor Glut.CURSOR_INHERIT;
4847 state.mstate <- Mnone;
4851 | _ -> ()
4854 let birdseyemouse button bstate x y
4855 (conf, leftx, _, hooverpageno, anchor) =
4856 match button with
4857 | Glut.LEFT_BUTTON when bstate = Glut.UP ->
4858 let rec loop = function
4859 | [] -> ()
4860 | l :: rest ->
4861 if y > l.pagedispy && y < l.pagedispy + l.pagevh
4862 && x > l.pagedispx && x < l.pagedispx + l.pagevw
4863 then (
4864 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
4866 else loop rest
4868 loop state.layout
4869 | Glut.OTHER_BUTTON _ -> viewmouse button bstate x y
4870 | _ -> ()
4873 let mouse bstate button x y =
4874 state.uioh <- state.uioh#button button bstate x y;
4877 let mouse ~button ~state ~x ~y = mouse state button x y;;
4879 let motion ~x ~y =
4880 state.uioh <- state.uioh#motion x y
4883 let pmotion ~x ~y =
4884 state.uioh <- state.uioh#pmotion x y;
4887 let uioh = object
4888 method display = ()
4890 method key key =
4891 begin match state.mode with
4892 | Textentry textentry -> textentrykeyboard key textentry
4893 | Birdseye birdseye -> birdseyekeyboard key birdseye
4894 | View -> viewkeyboard key
4895 end;
4896 state.uioh
4898 method special key =
4899 begin match state.mode with
4900 | View | (Birdseye _) when key = Glut.KEY_F9 ->
4901 togglebirdseye ()
4903 | Birdseye vals ->
4904 birdseyespecial key vals
4906 | View when key = Glut.KEY_F1 ->
4907 enterhelpmode ()
4909 | View ->
4910 begin match state.autoscroll with
4911 | Some step when key = Glut.KEY_DOWN || key = Glut.KEY_UP ->
4912 setautoscrollspeed step (key = Glut.KEY_DOWN)
4914 | _ ->
4915 let y =
4916 match key with
4917 | Glut.KEY_F3 -> search state.searchpattern true; state.y
4918 | Glut.KEY_UP ->
4919 if Glut.getModifiers () land Glut.active_ctrl != 0
4920 then
4921 if Glut.getModifiers () land Glut.active_shift != 0
4922 then (setzoom state.prevzoom; state.y)
4923 else clamp (-conf.winh/2)
4924 else clamp (-conf.scrollstep)
4925 | Glut.KEY_DOWN ->
4926 if Glut.getModifiers () land Glut.active_ctrl != 0
4927 then
4928 if Glut.getModifiers () land Glut.active_shift != 0
4929 then (setzoom state.prevzoom; state.y)
4930 else clamp (conf.winh/2)
4931 else clamp (conf.scrollstep)
4932 | Glut.KEY_PAGE_UP ->
4933 if Glut.getModifiers () land Glut.active_ctrl != 0
4934 then
4935 match state.layout with
4936 | [] -> state.y
4937 | l :: _ -> state.y - l.pagey
4938 else
4939 clamp (-conf.winh)
4940 | Glut.KEY_PAGE_DOWN ->
4941 if Glut.getModifiers () land Glut.active_ctrl != 0
4942 then
4943 match List.rev state.layout with
4944 | [] -> state.y
4945 | l :: _ -> getpagey l.pageno
4946 else
4947 clamp conf.winh
4948 | Glut.KEY_HOME ->
4949 addnav ();
4951 | Glut.KEY_END ->
4952 addnav ();
4953 state.maxy - (if conf.maxhfit then conf.winh else 0)
4955 | (Glut.KEY_RIGHT | Glut.KEY_LEFT) when
4956 Glut.getModifiers () land Glut.active_alt != 0 ->
4957 getnav (if key = Glut.KEY_LEFT then 1 else -1)
4959 | Glut.KEY_RIGHT when conf.zoom > 1.0 ->
4960 let dx =
4961 if Glut.getModifiers () land Glut.active_ctrl != 0
4962 then (conf.winw / 2)
4963 else 10
4965 state.x <- state.x - dx;
4966 state.y
4967 | Glut.KEY_LEFT when conf.zoom > 1.0 ->
4968 let dx =
4969 if Glut.getModifiers () land Glut.active_ctrl != 0
4970 then (conf.winw / 2)
4971 else 10
4973 state.x <- state.x + dx;
4974 state.y
4976 | _ -> state.y
4978 if abs (state.y - y) > conf.scrollstep*2
4979 then gotoghyll y
4980 else gotoy_and_clear_text y
4983 | Textentry te -> textentryspecial key te
4984 end;
4985 state.uioh
4987 method button button bstate x y =
4988 begin match state.mode with
4989 | View -> viewmouse button bstate x y
4990 | Birdseye beye -> birdseyemouse button bstate x y beye
4991 | Textentry _ -> ()
4992 end;
4993 state.uioh
4995 method motion x y =
4996 begin match state.mode with
4997 | Textentry _ -> ()
4998 | View | Birdseye _ ->
4999 match state.mstate with
5000 | Mzoom _ | Mnone -> ()
5002 | Mpan (x0, y0) ->
5003 let dx = x - x0
5004 and dy = y0 - y in
5005 state.mstate <- Mpan (x, y);
5006 if conf.zoom > 1.0 then state.x <- state.x + dx;
5007 let y = clamp dy in
5008 gotoy_and_clear_text y
5010 | Msel (a, _) ->
5011 state.mstate <- Msel (a, (x, y));
5012 G.postRedisplay "motion select";
5014 | Mscrolly ->
5015 let y = min conf.winh (max 0 y) in
5016 scrolly y
5018 | Mscrollx ->
5019 let x = min conf.winw (max 0 x) in
5020 scrollx x
5022 | Mzoomrect (p0, _) ->
5023 state.mstate <- Mzoomrect (p0, (x, y));
5024 G.postRedisplay "motion zoomrect";
5025 end;
5026 state.uioh
5028 method pmotion x y =
5029 begin match state.mode with
5030 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
5031 let rec loop = function
5032 | [] ->
5033 if hooverpageno != -1
5034 then (
5035 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
5036 G.postRedisplay "pmotion birdseye no hoover";
5038 | l :: rest ->
5039 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5040 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5041 then (
5042 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
5043 G.postRedisplay "pmotion birdseye hoover";
5045 else loop rest
5047 loop state.layout
5049 | Textentry _ -> ()
5051 | View ->
5052 match state.mstate with
5053 | Mnone ->
5054 begin match getunder x y with
5055 | Unone -> Glut.setCursor Glut.CURSOR_INHERIT
5056 | Ulinkuri uri ->
5057 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
5058 Glut.setCursor Glut.CURSOR_INFO
5059 | Ulinkgoto (page, _) ->
5060 if conf.underinfo
5061 then showtext 'p' ("age: " ^ string_of_int (page+1));
5062 Glut.setCursor Glut.CURSOR_INFO
5063 | Utext s ->
5064 if conf.underinfo then showtext 'f' ("ont: " ^ s);
5065 Glut.setCursor Glut.CURSOR_TEXT
5068 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
5070 end;
5071 state.uioh
5073 method infochanged _ = ()
5075 method scrollph =
5076 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
5077 let p, h = scrollph state.y maxy in
5078 state.scrollw, p, h
5080 method scrollpw =
5081 let winw = conf.winw - state.scrollw - 1 in
5082 let fwinw = float winw in
5083 let sw =
5084 let sw = fwinw /. float state.w in
5085 let sw = fwinw *. sw in
5086 max sw (float conf.scrollh)
5088 let position, sw =
5089 let f = state.w+winw in
5090 let r = float (winw-state.x) /. float f in
5091 let p = fwinw *. r in
5092 p-.sw/.2., sw
5094 let sw =
5095 if position +. sw > fwinw
5096 then fwinw -. position
5097 else sw
5099 state.hscrollh, position, sw
5100 end;;
5102 module Config =
5103 struct
5104 open Parser
5106 let fontpath = ref "";;
5107 let wmclasshack = ref false;;
5109 let unent s =
5110 let l = String.length s in
5111 let b = Buffer.create l in
5112 unent b s 0 l;
5113 Buffer.contents b;
5116 let home =
5118 match platform with
5119 | Pwindows | Pmingw -> Sys.getenv "HOMEPATH"
5120 | _ -> Sys.getenv "HOME"
5121 with exn ->
5122 prerr_endline
5123 ("Can not determine home directory location: " ^
5124 Printexc.to_string exn);
5128 let config_of c attrs =
5129 let apply c k v =
5131 match k with
5132 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
5133 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
5134 | "case-insensitive-search" -> { c with icase = bool_of_string v }
5135 | "preload" -> { c with preload = bool_of_string v }
5136 | "page-bias" -> { c with pagebias = int_of_string v }
5137 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
5138 | "auto-scroll-step" ->
5139 { c with autoscrollstep = max 0 (int_of_string v) }
5140 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
5141 | "crop-hack" -> { c with crophack = bool_of_string v }
5142 | "throttle" ->
5143 let mw =
5144 match String.lowercase v with
5145 | "true" -> Some infinity
5146 | "false" -> None
5147 | f -> Some (float_of_string f)
5149 { c with maxwait = mw}
5150 | "highlight-links" -> { c with hlinks = bool_of_string v }
5151 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
5152 | "vertical-margin" ->
5153 { c with interpagespace = max 0 (int_of_string v) }
5154 | "zoom" ->
5155 let zoom = float_of_string v /. 100. in
5156 let zoom = max zoom 0.0 in
5157 { c with zoom = zoom }
5158 | "presentation" -> { c with presentation = bool_of_string v }
5159 | "rotation-angle" -> { c with angle = int_of_string v }
5160 | "width" -> { c with winw = max 20 (int_of_string v) }
5161 | "height" -> { c with winh = max 20 (int_of_string v) }
5162 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
5163 | "proportional-display" -> { c with proportional = bool_of_string v }
5164 | "pixmap-cache-size" ->
5165 { c with memlimit = max 2 (int_of_string_with_suffix v) }
5166 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
5167 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
5168 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
5169 | "persistent-location" -> { c with jumpback = bool_of_string v }
5170 | "background-color" -> { c with bgcolor = color_of_string v }
5171 | "scrollbar-in-presentation" ->
5172 { c with scrollbarinpm = bool_of_string v }
5173 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
5174 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
5175 | "mupdf-store-size" ->
5176 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
5177 | "checkers" -> { c with checkers = bool_of_string v }
5178 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
5179 | "trim-margins" -> { c with trimmargins = bool_of_string v }
5180 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
5181 | "wmclass-hack" -> wmclasshack := bool_of_string v; c
5182 | "uri-launcher" -> { c with urilauncher = unent v }
5183 | "color-space" -> { c with colorspace = colorspace_of_string v }
5184 | "invert-colors" -> { c with invert = bool_of_string v }
5185 | "brightness" -> { c with colorscale = float_of_string v }
5186 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
5187 | "ghyllscroll" ->
5188 { c with ghyllscroll = Some (ghyllscroll_of_string v) }
5189 | "columns" ->
5190 let nab = columns_of_string v in
5191 { c with columns = Some (nab, [||]) }
5192 | "birds-eye-columns" ->
5193 { c with beyecolumns = Some (max (int_of_string v) 2) }
5194 | _ -> c
5195 with exn ->
5196 prerr_endline ("Error processing attribute (`" ^
5197 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
5200 let rec fold c = function
5201 | [] -> c
5202 | (k, v) :: rest ->
5203 let c = apply c k v in
5204 fold c rest
5206 fold c attrs;
5209 let fromstring f pos n v d =
5210 try f v
5211 with exn ->
5212 dolog "Error processing attribute (%S=%S) at %d\n%s"
5213 n v pos (Printexc.to_string exn)
5218 let bookmark_of attrs =
5219 let rec fold title page rely = function
5220 | ("title", v) :: rest -> fold v page rely rest
5221 | ("page", v) :: rest -> fold title v rely rest
5222 | ("rely", v) :: rest -> fold title page v rest
5223 | _ :: rest -> fold title page rely rest
5224 | [] -> title, page, rely
5226 fold "invalid" "0" "0" attrs
5229 let doc_of attrs =
5230 let rec fold path page rely pan = function
5231 | ("path", v) :: rest -> fold v page rely pan rest
5232 | ("page", v) :: rest -> fold path v rely pan rest
5233 | ("rely", v) :: rest -> fold path page v pan rest
5234 | ("pan", v) :: rest -> fold path page rely v rest
5235 | _ :: rest -> fold path page rely pan rest
5236 | [] -> path, page, rely, pan
5238 fold "" "0" "0" "0" attrs
5241 let setconf dst src =
5242 dst.scrollbw <- src.scrollbw;
5243 dst.scrollh <- src.scrollh;
5244 dst.icase <- src.icase;
5245 dst.preload <- src.preload;
5246 dst.pagebias <- src.pagebias;
5247 dst.verbose <- src.verbose;
5248 dst.scrollstep <- src.scrollstep;
5249 dst.maxhfit <- src.maxhfit;
5250 dst.crophack <- src.crophack;
5251 dst.autoscrollstep <- src.autoscrollstep;
5252 dst.maxwait <- src.maxwait;
5253 dst.hlinks <- src.hlinks;
5254 dst.underinfo <- src.underinfo;
5255 dst.interpagespace <- src.interpagespace;
5256 dst.zoom <- src.zoom;
5257 dst.presentation <- src.presentation;
5258 dst.angle <- src.angle;
5259 dst.winw <- src.winw;
5260 dst.winh <- src.winh;
5261 dst.savebmarks <- src.savebmarks;
5262 dst.memlimit <- src.memlimit;
5263 dst.proportional <- src.proportional;
5264 dst.texcount <- src.texcount;
5265 dst.sliceheight <- src.sliceheight;
5266 dst.thumbw <- src.thumbw;
5267 dst.jumpback <- src.jumpback;
5268 dst.bgcolor <- src.bgcolor;
5269 dst.scrollbarinpm <- src.scrollbarinpm;
5270 dst.tilew <- src.tilew;
5271 dst.tileh <- src.tileh;
5272 dst.mustoresize <- src.mustoresize;
5273 dst.checkers <- src.checkers;
5274 dst.aalevel <- src.aalevel;
5275 dst.trimmargins <- src.trimmargins;
5276 dst.trimfuzz <- src.trimfuzz;
5277 dst.urilauncher <- src.urilauncher;
5278 dst.colorspace <- src.colorspace;
5279 dst.invert <- src.invert;
5280 dst.colorscale <- src.colorscale;
5281 dst.redirectstderr <- src.redirectstderr;
5282 dst.ghyllscroll <- src.ghyllscroll;
5283 dst.columns <- src.columns;
5284 dst.beyecolumns <- src.beyecolumns;
5287 let get s =
5288 let h = Hashtbl.create 10 in
5289 let dc = { defconf with angle = defconf.angle } in
5290 let rec toplevel v t spos _ =
5291 match t with
5292 | Vdata | Vcdata | Vend -> v
5293 | Vopen ("llppconfig", _, closed) ->
5294 if closed
5295 then v
5296 else { v with f = llppconfig }
5297 | Vopen _ ->
5298 error "unexpected subelement at top level" s spos
5299 | Vclose _ -> error "unexpected close at top level" s spos
5301 and llppconfig v t spos _ =
5302 match t with
5303 | Vdata | Vcdata -> v
5304 | Vend -> error "unexpected end of input in llppconfig" s spos
5305 | Vopen ("defaults", attrs, closed) ->
5306 let c = config_of dc attrs in
5307 setconf dc c;
5308 if closed
5309 then v
5310 else { v with f = skip "defaults" (fun () -> v) }
5312 | Vopen ("ui-font", attrs, closed) ->
5313 let rec getsize size = function
5314 | [] -> size
5315 | ("size", v) :: rest ->
5316 let size =
5317 fromstring int_of_string spos "size" v fstate.fontsize in
5318 getsize size rest
5319 | l -> getsize size l
5321 fstate.fontsize <- getsize fstate.fontsize attrs;
5322 if closed
5323 then v
5324 else { v with f = uifont (Buffer.create 10) }
5326 | Vopen ("doc", attrs, closed) ->
5327 let pathent, spage, srely, span = doc_of attrs in
5328 let path = unent pathent
5329 and pageno = fromstring int_of_string spos "page" spage 0
5330 and rely = fromstring float_of_string spos "rely" srely 0.0
5331 and pan = fromstring int_of_string spos "pan" span 0 in
5332 let c = config_of dc attrs in
5333 let anchor = (pageno, rely) in
5334 if closed
5335 then (Hashtbl.add h path (c, [], pan, anchor); v)
5336 else { v with f = doc path pan anchor c [] }
5338 | Vopen _ ->
5339 error "unexpected subelement in llppconfig" s spos
5341 | Vclose "llppconfig" -> { v with f = toplevel }
5342 | Vclose _ -> error "unexpected close in llppconfig" s spos
5344 and uifont b v t spos epos =
5345 match t with
5346 | Vdata | Vcdata ->
5347 Buffer.add_substring b s spos (epos - spos);
5349 | Vopen (_, _, _) ->
5350 error "unexpected subelement in ui-font" s spos
5351 | Vclose "ui-font" ->
5352 if String.length !fontpath = 0
5353 then fontpath := Buffer.contents b;
5354 { v with f = llppconfig }
5355 | Vclose _ -> error "unexpected close in ui-font" s spos
5356 | Vend -> error "unexpected end of input in ui-font" s spos
5358 and doc path pan anchor c bookmarks v t spos _ =
5359 match t with
5360 | Vdata | Vcdata -> v
5361 | Vend -> error "unexpected end of input in doc" s spos
5362 | Vopen ("bookmarks", _, closed) ->
5363 if closed
5364 then v
5365 else { v with f = pbookmarks path pan anchor c bookmarks }
5367 | Vopen (_, _, _) ->
5368 error "unexpected subelement in doc" s spos
5370 | Vclose "doc" ->
5371 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
5372 { v with f = llppconfig }
5374 | Vclose _ -> error "unexpected close in doc" s spos
5376 and pbookmarks path pan anchor c bookmarks v t spos _ =
5377 match t with
5378 | Vdata | Vcdata -> v
5379 | Vend -> error "unexpected end of input in bookmarks" s spos
5380 | Vopen ("item", attrs, closed) ->
5381 let titleent, spage, srely = bookmark_of attrs in
5382 let page = fromstring int_of_string spos "page" spage 0
5383 and rely = fromstring float_of_string spos "rely" srely 0.0 in
5384 let bookmarks = (unent titleent, 0, (page, rely)) :: bookmarks in
5385 if closed
5386 then { v with f = pbookmarks path pan anchor c bookmarks }
5387 else
5388 let f () = v in
5389 { v with f = skip "item" f }
5391 | Vopen _ ->
5392 error "unexpected subelement in bookmarks" s spos
5394 | Vclose "bookmarks" ->
5395 { v with f = doc path pan anchor c bookmarks }
5397 | Vclose _ -> error "unexpected close in bookmarks" s spos
5399 and skip tag f v t spos _ =
5400 match t with
5401 | Vdata | Vcdata -> v
5402 | Vend ->
5403 error ("unexpected end of input in skipped " ^ tag) s spos
5404 | Vopen (tag', _, closed) ->
5405 if closed
5406 then v
5407 else
5408 let f' () = { v with f = skip tag f } in
5409 { v with f = skip tag' f' }
5410 | Vclose ctag ->
5411 if tag = ctag
5412 then f ()
5413 else error ("unexpected close in skipped " ^ tag) s spos
5416 parse { f = toplevel; accu = () } s;
5417 h, dc;
5420 let do_load f ic =
5422 let len = in_channel_length ic in
5423 let s = String.create len in
5424 really_input ic s 0 len;
5425 f s;
5426 with
5427 | Parse_error (msg, s, pos) ->
5428 let subs = subs s pos in
5429 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
5430 failwith ("parse error: " ^ s)
5432 | exn ->
5433 failwith ("config load error: " ^ Printexc.to_string exn)
5436 let defconfpath =
5437 let dir =
5439 let dir = Filename.concat home ".config" in
5440 if Sys.is_directory dir then dir else home
5441 with _ -> home
5443 Filename.concat dir "llpp.conf"
5446 let confpath = ref defconfpath;;
5448 let load1 f =
5449 if Sys.file_exists !confpath
5450 then
5451 match
5452 (try Some (open_in_bin !confpath)
5453 with exn ->
5454 prerr_endline
5455 ("Error opening configuation file `" ^ !confpath ^ "': " ^
5456 Printexc.to_string exn);
5457 None
5459 with
5460 | Some ic ->
5461 begin try
5462 f (do_load get ic)
5463 with exn ->
5464 prerr_endline
5465 ("Error loading configuation from `" ^ !confpath ^ "': " ^
5466 Printexc.to_string exn);
5467 end;
5468 close_in ic;
5470 | None -> ()
5471 else
5472 f (Hashtbl.create 0, defconf)
5475 let load () =
5476 let f (h, dc) =
5477 let pc, pb, px, pa =
5479 Hashtbl.find h (Filename.basename state.path)
5480 with Not_found -> dc, [], 0, (0, 0.0)
5482 setconf defconf dc;
5483 setconf conf pc;
5484 state.bookmarks <- pb;
5485 state.x <- px;
5486 state.scrollw <- conf.scrollbw;
5487 if conf.jumpback
5488 then state.anchor <- pa;
5489 cbput state.hists.nav pa;
5491 load1 f
5494 let add_attrs bb always dc c =
5495 let ob s a b =
5496 if always || a != b
5497 then Printf.bprintf bb "\n %s='%b'" s a
5498 and oi s a b =
5499 if always || a != b
5500 then Printf.bprintf bb "\n %s='%d'" s a
5501 and oI s a b =
5502 if always || a != b
5503 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
5504 and oz s a b =
5505 if always || a <> b
5506 then Printf.bprintf bb "\n %s='%d'" s (truncate (a*.100.))
5507 and oF s a b =
5508 if always || a <> b
5509 then Printf.bprintf bb "\n %s='%f'" s a
5510 and oc s a b =
5511 if always || a <> b
5512 then
5513 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
5514 and oC s a b =
5515 if always || a <> b
5516 then
5517 Printf.bprintf bb "\n %s='%s'" s (colorspace_to_string a)
5518 and oR s a b =
5519 if always || a <> b
5520 then
5521 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
5522 and os s a b =
5523 if always || a <> b
5524 then
5525 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
5526 and og s a b =
5527 if always || a <> b
5528 then
5529 match a with
5530 | None -> ()
5531 | Some (_N, _A, _B) ->
5532 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
5533 and oW s a b =
5534 if always || a <> b
5535 then
5536 let v =
5537 match a with
5538 | None -> "false"
5539 | Some f ->
5540 if f = infinity
5541 then "true"
5542 else string_of_float f
5544 Printf.bprintf bb "\n %s='%s'" s v
5545 and oco s a b =
5546 if always || a <> b
5547 then
5548 match a with
5549 | Some ((n, a, b), _) when n > 1 ->
5550 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
5551 | _ -> ()
5552 and obeco s a b =
5553 if always || a <> b
5554 then
5555 match a with
5556 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
5557 | _ -> ()
5559 let w, h =
5560 if always
5561 then dc.winw, dc.winh
5562 else
5563 match state.fullscreen with
5564 | Some wh -> wh
5565 | None -> c.winw, c.winh
5567 let zoom, presentation, interpagespace, maxwait =
5568 if always
5569 then dc.zoom, dc.presentation, dc.interpagespace, dc.maxwait
5570 else
5571 match state.mode with
5572 | Birdseye (bc, _, _, _, _) ->
5573 bc.zoom, bc.presentation, bc.interpagespace, bc.maxwait
5574 | _ -> c.zoom, c.presentation, c.interpagespace, c.maxwait
5576 oi "width" w dc.winw;
5577 oi "height" h dc.winh;
5578 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
5579 oi "scroll-handle-height" c.scrollh dc.scrollh;
5580 ob "case-insensitive-search" c.icase dc.icase;
5581 ob "preload" c.preload dc.preload;
5582 oi "page-bias" c.pagebias dc.pagebias;
5583 oi "scroll-step" c.scrollstep dc.scrollstep;
5584 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
5585 ob "max-height-fit" c.maxhfit dc.maxhfit;
5586 ob "crop-hack" c.crophack dc.crophack;
5587 oW "throttle" maxwait dc.maxwait;
5588 ob "highlight-links" c.hlinks dc.hlinks;
5589 ob "under-cursor-info" c.underinfo dc.underinfo;
5590 oi "vertical-margin" interpagespace dc.interpagespace;
5591 oz "zoom" zoom dc.zoom;
5592 ob "presentation" presentation dc.presentation;
5593 oi "rotation-angle" c.angle dc.angle;
5594 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
5595 ob "proportional-display" c.proportional dc.proportional;
5596 oI "pixmap-cache-size" c.memlimit dc.memlimit;
5597 oi "tex-count" c.texcount dc.texcount;
5598 oi "slice-height" c.sliceheight dc.sliceheight;
5599 oi "thumbnail-width" c.thumbw dc.thumbw;
5600 ob "persistent-location" c.jumpback dc.jumpback;
5601 oc "background-color" c.bgcolor dc.bgcolor;
5602 ob "scrollbar-in-presentation" c.scrollbarinpm dc.scrollbarinpm;
5603 oi "tile-width" c.tilew dc.tilew;
5604 oi "tile-height" c.tileh dc.tileh;
5605 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
5606 ob "checkers" c.checkers dc.checkers;
5607 oi "aalevel" c.aalevel dc.aalevel;
5608 ob "trim-margins" c.trimmargins dc.trimmargins;
5609 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
5610 os "uri-launcher" c.urilauncher dc.urilauncher;
5611 oC "color-space" c.colorspace dc.colorspace;
5612 ob "invert-colors" c.invert dc.invert;
5613 oF "brightness" c.colorscale dc.colorscale;
5614 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
5615 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
5616 oco "columns" c.columns dc.columns;
5617 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
5618 if always
5619 then ob "wmclass-hack" !wmclasshack false;
5622 let save () =
5623 let uifontsize = fstate.fontsize in
5624 let bb = Buffer.create 32768 in
5625 let f (h, dc) =
5626 let dc = if conf.bedefault then conf else dc in
5627 Buffer.add_string bb "<llppconfig>\n";
5629 if String.length !fontpath > 0
5630 then
5631 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
5632 uifontsize
5633 !fontpath
5634 else (
5635 if uifontsize <> 14
5636 then
5637 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
5640 Buffer.add_string bb "<defaults ";
5641 add_attrs bb true dc dc;
5642 Buffer.add_string bb "/>\n";
5644 let adddoc path pan anchor c bookmarks =
5645 if bookmarks == [] && c = dc && anchor = emptyanchor
5646 then ()
5647 else (
5648 Printf.bprintf bb "<doc path='%s'"
5649 (enent path 0 (String.length path));
5651 if anchor <> emptyanchor
5652 then (
5653 let n, y = anchor in
5654 Printf.bprintf bb " page='%d'" n;
5655 if y > 1e-6
5656 then
5657 Printf.bprintf bb " rely='%f'" y
5661 if pan != 0
5662 then Printf.bprintf bb " pan='%d'" pan;
5664 add_attrs bb false dc c;
5666 begin match bookmarks with
5667 | [] -> Buffer.add_string bb "/>\n"
5668 | _ ->
5669 Buffer.add_string bb ">\n<bookmarks>\n";
5670 List.iter (fun (title, _level, (page, rely)) ->
5671 Printf.bprintf bb
5672 "<item title='%s' page='%d'"
5673 (enent title 0 (String.length title))
5674 page
5676 if rely > 1e-6
5677 then
5678 Printf.bprintf bb " rely='%f'" rely
5680 Buffer.add_string bb "/>\n";
5681 ) bookmarks;
5682 Buffer.add_string bb "</bookmarks>\n</doc>\n";
5683 end;
5687 let pan =
5688 match state.mode with
5689 | Birdseye (_, pan, _, _, _) -> pan
5690 | _ -> state.x
5692 let basename = Filename.basename state.path in
5693 adddoc basename pan (getanchor ())
5694 { conf with
5695 autoscrollstep =
5696 match state.autoscroll with
5697 | Some step -> step
5698 | None -> conf.autoscrollstep }
5699 (if conf.savebmarks then state.bookmarks else []);
5701 Hashtbl.iter (fun path (c, bookmarks, x, y) ->
5702 if basename <> path
5703 then adddoc path x y c bookmarks
5704 ) h;
5705 Buffer.add_string bb "</llppconfig>";
5707 load1 f;
5708 if Buffer.length bb > 0
5709 then
5711 let tmp = !confpath ^ ".tmp" in
5712 let oc = open_out_bin tmp in
5713 Buffer.output_buffer oc bb;
5714 close_out oc;
5715 Unix.rename tmp !confpath;
5716 with exn ->
5717 prerr_endline
5718 ("error while saving configuration: " ^ Printexc.to_string exn)
5720 end;;
5722 let () =
5723 Arg.parse
5724 (Arg.align
5725 [("-p", Arg.String (fun s -> state.password <- s) ,
5726 "<password> Set password");
5728 ("-f", Arg.String (fun s -> Config.fontpath := s),
5729 "<path> Set path to the user interface font");
5731 ("-c", Arg.String (fun s -> Config.confpath := s),
5732 "<path> Set path to the configuration file");
5734 ("-v", Arg.Unit (fun () ->
5735 Printf.printf
5736 "%s\nconfiguration path: %s\n"
5737 (version ())
5738 Config.defconfpath
5740 exit 0), " Print version and exit");
5743 (fun s -> state.path <- s)
5744 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
5746 if String.length state.path = 0
5747 then (prerr_endline "file name missing"; exit 1);
5749 Config.load ();
5751 let _ = Glut.init Sys.argv in
5752 let () = Glut.initDisplayMode ~depth:false ~double_buffer:true () in
5753 let () = Glut.initWindowSize conf.winw conf.winh in
5754 let _ = Glut.createWindow ("llpp " ^ Filename.basename state.path) in
5756 if not (Glut.extensionSupported "GL_ARB_texture_rectangle"
5757 || Glut.extensionSupported "GL_EXT_texture_rectangle")
5758 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
5760 let csock, ssock =
5761 if not is_windows
5762 then
5763 Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0
5764 else
5765 let addr = Unix.ADDR_INET (Unix.inet_addr_loopback, 1337) in
5766 let sock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
5767 Unix.setsockopt sock Unix.SO_REUSEADDR true;
5768 Unix.bind sock addr;
5769 Unix.listen sock 1;
5770 let csock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
5771 Unix.connect csock addr;
5772 let ssock, _ = Unix.accept sock in
5773 Unix.close sock;
5774 let opts sock =
5775 Unix.setsockopt sock Unix.TCP_NODELAY true;
5776 Unix.setsockopt_optint sock Unix.SO_LINGER None;
5778 opts ssock;
5779 opts csock;
5780 ssock, csock
5783 let () = Glut.displayFunc display in
5784 let () = Glut.reshapeFunc reshape in
5785 let () = Glut.keyboardFunc keyboard in
5786 let () = Glut.specialFunc special in
5787 let () = Glut.idleFunc (Some idle) in
5788 let () = Glut.mouseFunc mouse in
5789 let () = Glut.motionFunc motion in
5790 let () = Glut.passiveMotionFunc pmotion in
5792 setcheckers conf.checkers;
5793 init ssock (
5794 conf.angle, conf.proportional, (conf.trimmargins, conf.trimfuzz),
5795 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
5796 !Config.wmclasshack, !Config.fontpath
5798 state.csock <- csock;
5799 state.ssock <- ssock;
5800 state.text <- "Opening " ^ state.path;
5801 setaalevel conf.aalevel;
5802 writeopen state.path state.password;
5803 state.uioh <- uioh;
5804 setfontsize fstate.fontsize;
5806 redirectstderr ();
5808 while true do
5810 Glut.mainLoop ();
5811 with
5812 | Glut.BadEnum "key in special_of_int" ->
5813 showtext '!' " LablGlut bug: special key not recognized";
5815 | Quit ->
5816 wcmd "quit" [];
5817 Config.save ();
5818 exit 0
5820 | exn when conf.redirectstderr ->
5821 let s =
5822 Printf.sprintf "exception %s\n%s"
5823 (Printexc.to_string exn)
5824 (Printexc.get_backtrace ())
5826 ignore (try
5827 Unix.single_write state.stderr s 0 (String.length s);
5828 with _ -> 0);
5829 exit 1
5830 done;