Expand
[llpp.git] / main.ml
blobc6737a7327439a2026f81a8f574749a02618820a
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 a.(pageno) <- (pdimno, x, y, pdim);
1484 loop (pageno+1) pdimno pdim x y rowh pdims
1486 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
1487 let rec fix n rowh = if n = Array.length a then () else
1488 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(n) in
1489 let rowh =
1490 if n mod columns = 0
1491 then h
1492 else max h rowh
1494 if h < rowh
1495 then (
1496 let y = y + (rowh - h) / 2 in
1497 a.(n) <- (pdimno, x, y, pdim);
1499 fix (n+1) rowh
1501 fix 0 0;
1502 conf.columns <- Some ((columns, coverA, coverB), a);
1504 docolumns conf.columns;
1505 state.maxy <- calcheight ();
1506 state.hscrollh <-
1507 if state.w <= conf.winw - state.scrollw
1508 then 0
1509 else state.scrollw
1511 match state.mode with
1512 | Birdseye (_, _, pageno, _, _) ->
1513 let y, h = getpageyh pageno in
1514 let top = (conf.winh - h) / 2 in
1515 gotoy (max 0 (y - top))
1516 | _ -> gotoanchor state.anchor
1519 let reshape =
1520 let firsttime = ref true in
1521 fun ~w ~h ->
1522 GlDraw.viewport 0 0 w h;
1523 if state.invalidated = 0 && not !firsttime
1524 then state.anchor <- getanchor ();
1526 firsttime := false;
1527 conf.winw <- w;
1528 let w = truncate (float w *. conf.zoom) - state.scrollw in
1529 let w = max w 2 in
1530 state.w <- w;
1531 conf.winh <- h;
1532 setfontsize fstate.fontsize;
1533 GlMat.mode `modelview;
1534 GlMat.load_identity ();
1536 GlMat.mode `projection;
1537 GlMat.load_identity ();
1538 GlMat.rotate ~x:1.0 ~angle:180.0 ();
1539 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
1540 GlMat.scale3 (2.0 /. float conf.winw, 2.0 /. float conf.winh, 1.0);
1542 let w =
1543 match conf.columns with
1544 | None -> w
1545 | Some ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
1547 invalidate ();
1548 wcmd "geometry" [`i w; `i h];
1551 let enttext () =
1552 let len = String.length state.text in
1553 let drawstring s =
1554 let hscrollh =
1555 match state.mode with
1556 | View -> state.hscrollh
1557 | _ -> 0
1559 let rect x w =
1560 GlDraw.rect
1561 (x, float (conf.winh - (fstate.fontsize + 4) - hscrollh))
1562 (x+.w, float (conf.winh - hscrollh))
1565 let w = float (conf.winw - state.scrollw - 1) in
1566 if state.progress >= 0.0 && state.progress < 1.0
1567 then (
1568 GlDraw.color (0.3, 0.3, 0.3);
1569 let w1 = w *. state.progress in
1570 rect 0.0 w1;
1571 GlDraw.color (0.0, 0.0, 0.0);
1572 rect w1 (w-.w1)
1574 else (
1575 GlDraw.color (0.0, 0.0, 0.0);
1576 rect 0.0 w;
1579 GlDraw.color (1.0, 1.0, 1.0);
1580 drawstring fstate.fontsize
1581 (if len > 0 then 8 else 2) (conf.winh - hscrollh - 5) s;
1583 let s =
1584 match state.mode with
1585 | Textentry ((prefix, text, _, _, _), _) ->
1586 let s =
1587 if len > 0
1588 then
1589 Printf.sprintf "%s%s_ [%s]" prefix text state.text
1590 else
1591 Printf.sprintf "%s%s_" prefix text
1595 | _ -> state.text
1597 let s =
1598 if state.newerrmsgs
1599 then (
1600 if not (istextentry state.mode)
1601 then
1602 let s1 = "(press 'e' to review error messasges)" in
1603 if String.length s > 0 then s ^ " " ^ s1 else s1
1604 else s
1606 else s
1608 if String.length s > 0
1609 then drawstring s
1612 let showtext c s =
1613 state.text <- Printf.sprintf "%c%s" c s;
1614 G.postRedisplay "showtext";
1617 let gctiles () =
1618 let len = Queue.length state.tilelru in
1619 let rec loop qpos =
1620 if state.memused <= conf.memlimit
1621 then ()
1622 else (
1623 if qpos < len
1624 then
1625 let (k, p, s) as lruitem = Queue.pop state.tilelru in
1626 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
1627 let (_, pw, ph, _) = getpagedim n in
1629 gen = state.gen
1630 && colorspace = conf.colorspace
1631 && angle = conf.angle
1632 && pagew = pw
1633 && pageh = ph
1634 && (
1635 let layout =
1636 match state.throttle with
1637 | None ->
1638 if conf.preload
1639 then preloadlayout state.layout
1640 else state.layout
1641 | Some (layout, _, _) ->
1642 layout
1644 let x = col*conf.tilew
1645 and y = row*conf.tileh in
1646 tilevisible layout n x y
1648 then Queue.push lruitem state.tilelru
1649 else (
1650 wcmd "freetile" [`s p];
1651 state.memused <- state.memused - s;
1652 state.uioh#infochanged Memused;
1653 Hashtbl.remove state.tilemap k;
1655 loop (qpos+1)
1658 loop 0
1661 let flushtiles () =
1662 Queue.iter (fun (k, p, s) ->
1663 wcmd "freetile" [`s p];
1664 state.memused <- state.memused - s;
1665 state.uioh#infochanged Memused;
1666 Hashtbl.remove state.tilemap k;
1667 ) state.tilelru;
1668 Queue.clear state.tilelru;
1669 load state.layout;
1672 let logcurrently = function
1673 | Idle -> dolog "Idle"
1674 | Loading (l, gen) ->
1675 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
1676 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
1677 dolog
1678 "Tiling %d[%d,%d] page=%s cs=%s angle"
1679 l.pageno col row pageopaque
1680 (colorspace_to_string colorspace)
1682 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
1683 angle gen conf.angle state.gen
1684 tilew tileh
1685 conf.tilew conf.tileh
1687 | Outlining _ ->
1688 dolog "outlining"
1691 let act cmds =
1692 (* dolog "%S" cmds; *)
1693 let op, args =
1694 let spacepos =
1695 try String.index cmds ' '
1696 with Not_found -> -1
1698 if spacepos = -1
1699 then cmds, ""
1700 else
1701 let l = String.length cmds in
1702 let op = String.sub cmds 0 spacepos in
1703 op, begin
1704 if l - spacepos < 2 then ""
1705 else String.sub cmds (spacepos+1) (l-spacepos-1)
1708 match op with
1709 | "clear" ->
1710 state.uioh#infochanged Pdim;
1711 state.pdims <- [];
1713 | "clearrects" ->
1714 state.rects <- state.rects1;
1715 G.postRedisplay "clearrects";
1717 | "continue" ->
1718 let n =
1719 try Scanf.sscanf args "%u" (fun n -> n)
1720 with exn ->
1721 dolog "error processing 'continue' %S: %s"
1722 cmds (Printexc.to_string exn);
1723 exit 1;
1725 state.pagecount <- n;
1726 state.invalidated <- state.invalidated - 1;
1727 begin match state.currently with
1728 | Outlining l ->
1729 state.currently <- Idle;
1730 state.outlines <- Array.of_list (List.rev l)
1731 | _ -> ()
1732 end;
1733 if state.invalidated = 0
1734 then represent ();
1735 if conf.maxwait = None
1736 then G.postRedisplay "continue";
1738 | "title" ->
1739 Glut.setWindowTitle args
1741 | "msg" ->
1742 showtext ' ' args
1744 | "vmsg" ->
1745 if conf.verbose
1746 then showtext ' ' args
1748 | "progress" ->
1749 let progress, text =
1751 Scanf.sscanf args "%f %n"
1752 (fun f pos ->
1753 f, String.sub args pos (String.length args - pos))
1754 with exn ->
1755 dolog "error processing 'progress' %S: %s"
1756 cmds (Printexc.to_string exn);
1757 exit 1;
1759 state.text <- text;
1760 state.progress <- progress;
1761 G.postRedisplay "progress"
1763 | "firstmatch" ->
1764 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1766 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
1767 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1768 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1769 with exn ->
1770 dolog "error processing 'firstmatch' %S: %s"
1771 cmds (Printexc.to_string exn);
1772 exit 1;
1774 let y = (getpagey pageno) + truncate y0 in
1775 addnav ();
1776 gotoy y;
1777 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
1779 | "match" ->
1780 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1782 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
1783 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1784 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1785 with exn ->
1786 dolog "error processing 'match' %S: %s"
1787 cmds (Printexc.to_string exn);
1788 exit 1;
1790 state.rects1 <-
1791 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
1793 | "page" ->
1794 let pageopaque, t =
1796 Scanf.sscanf args "%s %f" (fun p t -> p, t)
1797 with exn ->
1798 dolog "error processing 'page' %S: %s"
1799 cmds (Printexc.to_string exn);
1800 exit 1;
1802 begin match state.currently with
1803 | Loading (l, gen) ->
1804 vlog "page %d took %f sec" l.pageno t;
1805 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
1806 begin match state.throttle with
1807 | None ->
1808 let preloadedpages =
1809 if conf.preload
1810 then preloadlayout state.layout
1811 else state.layout
1813 let evict () =
1814 let module IntSet =
1815 Set.Make (struct type t = int let compare = (-) end) in
1816 let set =
1817 List.fold_left (fun s l -> IntSet.add l.pageno s)
1818 IntSet.empty preloadedpages
1820 let evictedpages =
1821 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
1822 if not (IntSet.mem pageno set)
1823 then (
1824 wcmd "freepage" [`s opaque];
1825 key :: accu
1827 else accu
1828 ) state.pagemap []
1830 List.iter (Hashtbl.remove state.pagemap) evictedpages;
1832 evict ();
1833 state.currently <- Idle;
1834 if gen = state.gen
1835 then (
1836 tilepage l.pageno pageopaque state.layout;
1837 load state.layout;
1838 load preloadedpages;
1839 if pagevisible state.layout l.pageno
1840 && layoutready state.layout
1841 then G.postRedisplay "page";
1844 | Some (layout, _, _) ->
1845 state.currently <- Idle;
1846 tilepage l.pageno pageopaque layout;
1847 load state.layout
1848 end;
1850 | _ ->
1851 dolog "Inconsistent loading state";
1852 logcurrently state.currently;
1853 raise Quit;
1856 | "tile" ->
1857 let (x, y, opaque, size, t) =
1859 Scanf.sscanf args "%u %u %s %u %f"
1860 (fun x y p size t -> (x, y, p, size, t))
1861 with exn ->
1862 dolog "error processing 'tile' %S: %s"
1863 cmds (Printexc.to_string exn);
1864 exit 1;
1866 begin match state.currently with
1867 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
1868 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
1870 if tilew != conf.tilew || tileh != conf.tileh
1871 then (
1872 wcmd "freetile" [`s opaque];
1873 state.currently <- Idle;
1874 load state.layout;
1876 else (
1877 puttileopaque l col row gen cs angle opaque size t;
1878 state.memused <- state.memused + size;
1879 state.uioh#infochanged Memused;
1880 gctiles ();
1881 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
1882 opaque, size) state.tilelru;
1884 let layout =
1885 match state.throttle with
1886 | None -> state.layout
1887 | Some (layout, _, _) -> layout
1890 state.currently <- Idle;
1891 if gen = state.gen
1892 && conf.colorspace = cs
1893 && conf.angle = angle
1894 && tilevisible layout l.pageno x y
1895 then conttiling l.pageno pageopaque;
1897 begin match state.throttle with
1898 | None ->
1899 preload state.layout;
1900 if gen = state.gen
1901 && conf.colorspace = cs
1902 && conf.angle = angle
1903 && tilevisible state.layout l.pageno x y
1904 then G.postRedisplay "tile nothrottle";
1906 | Some (layout, y, _) ->
1907 let ready = layoutready layout in
1908 if ready
1909 then (
1910 state.y <- y;
1911 state.layout <- layout;
1912 state.throttle <- None;
1913 G.postRedisplay "throttle";
1915 else load layout;
1916 end;
1919 | _ ->
1920 dolog "Inconsistent tiling state";
1921 logcurrently state.currently;
1922 raise Quit;
1925 | "pdim" ->
1926 let pdim =
1928 Scanf.sscanf args "%u %u %u %u" (fun n w h x -> n, w, h, x)
1929 with exn ->
1930 dolog "error processing 'pdim' %S: %s"
1931 cmds (Printexc.to_string exn);
1932 exit 1;
1934 state.uioh#infochanged Pdim;
1935 state.pdims <- pdim :: state.pdims
1937 | "o" ->
1938 let (l, n, t, h, pos) =
1940 Scanf.sscanf args "%u %u %d %u %n"
1941 (fun l n t h pos -> l, n, t, h, pos)
1942 with exn ->
1943 dolog "error processing 'o' %S: %s"
1944 cmds (Printexc.to_string exn);
1945 exit 1;
1947 let s = String.sub args pos (String.length args - pos) in
1948 let outline = (s, l, (n, float t /. float h)) in
1949 begin match state.currently with
1950 | Outlining outlines ->
1951 state.currently <- Outlining (outline :: outlines)
1952 | Idle ->
1953 state.currently <- Outlining [outline]
1954 | currently ->
1955 dolog "invalid outlining state";
1956 logcurrently currently
1959 | "info" ->
1960 state.docinfo <- (1, args) :: state.docinfo
1962 | "infoend" ->
1963 state.uioh#infochanged Docinfo;
1964 state.docinfo <- List.rev state.docinfo
1966 | _ ->
1967 dolog "unknown cmd `%S'" cmds
1970 let idle () =
1971 if state.deadline == nan then state.deadline <- now ();
1972 let r =
1973 match state.errfd with
1974 | None -> [state.csock]
1975 | Some fd -> [state.csock; fd]
1977 let rec loop delay =
1978 let deadline =
1979 if state.ghyll == noghyll
1980 then state.deadline
1981 else now () +. 0.02
1983 let timeout =
1984 if delay > 0.0
1985 then max 0.0 (deadline -. now ())
1986 else 0.0
1988 let r, _, _ = Unix.select r [] [] timeout in
1989 begin match r with
1990 | [] ->
1991 state.ghyll None;
1992 begin match state.autoscroll with
1993 | Some step when step != 0 ->
1994 let y = state.y + step in
1995 let y =
1996 if y < 0
1997 then state.maxy
1998 else if y >= state.maxy then 0 else y
2000 gotoy y;
2001 if state.mode = View
2002 then state.text <- "";
2003 state.deadline <- state.deadline +. 0.005;
2005 | _ ->
2006 state.deadline <- state.deadline +. delay;
2007 end;
2009 | l ->
2010 let rec checkfds c = function
2011 | [] -> c
2012 | fd :: rest when fd = state.csock ->
2013 let cmd = readcmd state.csock in
2014 act cmd;
2015 checkfds true rest
2016 | fd :: rest ->
2017 let s = String.create 80 in
2018 let n = Unix.read fd s 0 80 in
2019 if conf.redirectstderr
2020 then (
2021 Buffer.add_substring state.errmsgs s 0 n;
2022 state.newerrmsgs <- true;
2023 Glut.postRedisplay ();
2025 else (
2026 prerr_string (String.sub s 0 n);
2027 flush stderr;
2029 checkfds c rest
2031 if checkfds false l
2032 then loop 0.0
2033 end;
2034 in loop 0.007
2037 let onhist cb =
2038 let rc = cb.rc in
2039 let action = function
2040 | HCprev -> cbget cb ~-1
2041 | HCnext -> cbget cb 1
2042 | HCfirst -> cbget cb ~-(cb.rc)
2043 | HClast -> cbget cb (cb.len - 1 - cb.rc)
2044 and cancel () = cb.rc <- rc
2045 in (action, cancel)
2048 let search pattern forward =
2049 if String.length pattern > 0
2050 then
2051 let pn, py =
2052 match state.layout with
2053 | [] -> 0, 0
2054 | l :: _ ->
2055 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
2057 let cmd =
2058 let b = makecmd "search"
2059 [`b conf.icase; `i pn; `i py; `i (if forward then 1 else 0)]
2061 Buffer.add_char b ',';
2062 Buffer.add_string b pattern;
2063 Buffer.add_char b '\000';
2064 Buffer.contents b;
2066 writecmd state.csock cmd;
2069 let intentry text key =
2070 let c = Char.unsafe_chr key in
2071 match c with
2072 | '0' .. '9' ->
2073 let text = addchar text c in
2074 TEcont text
2076 | _ ->
2077 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2078 TEcont text
2081 let textentry text key =
2082 let c = Char.unsafe_chr key in
2083 match c with
2084 | _ when key >= 32 && key < 127 ->
2085 let text = addchar text c in
2086 TEcont text
2088 | _ ->
2089 dolog "unhandled key %d char `%c'" key (Char.unsafe_chr key);
2090 TEcont text
2093 let reqlayout angle proportional =
2094 match state.throttle with
2095 | None ->
2096 if state.invalidated = 0 then state.anchor <- getanchor ();
2097 conf.angle <- angle mod 360;
2098 conf.proportional <- proportional;
2099 invalidate ();
2100 wcmd "reqlayout" [`i conf.angle; `b proportional];
2101 | _ -> ()
2104 let settrim trimmargins trimfuzz =
2105 if state.invalidated = 0 then state.anchor <- getanchor ();
2106 conf.trimmargins <- trimmargins;
2107 conf.trimfuzz <- trimfuzz;
2108 let x0, y0, x1, y1 = trimfuzz in
2109 invalidate ();
2110 wcmd "settrim" [
2111 `b conf.trimmargins;
2112 `i x0;
2113 `i y0;
2114 `i x1;
2115 `i y1;
2117 Hashtbl.iter (fun _ opaque ->
2118 wcmd "freepage" [`s opaque];
2119 ) state.pagemap;
2120 Hashtbl.clear state.pagemap;
2123 let setzoom zoom =
2124 match state.throttle with
2125 | None ->
2126 let zoom = max 0.01 zoom in
2127 if zoom <> conf.zoom
2128 then (
2129 state.prevzoom <- conf.zoom;
2130 let relx =
2131 if zoom <= 1.0
2132 then (state.x <- 0; 0.0)
2133 else float state.x /. float state.w
2135 conf.zoom <- zoom;
2136 reshape conf.winw conf.winh;
2137 if zoom > 1.0
2138 then (
2139 let x = relx *. float state.w in
2140 state.x <- truncate x;
2142 state.text <- Printf.sprintf "zoom is now %-5.1f" (zoom *. 100.0);
2145 | Some (layout, y, started) ->
2146 let time =
2147 match conf.maxwait with
2148 | None -> 0.0
2149 | Some t -> t
2151 let dt = now () -. started in
2152 if dt > time
2153 then (
2154 state.y <- y;
2155 load layout;
2159 let setcolumns columns coverA coverB =
2160 if columns < 2
2161 then (
2162 conf.columns <- None;
2163 state.x <- 0;
2164 setzoom 1.0;
2166 else (
2167 conf.columns <- Some ((columns, coverA, coverB), [||]);
2168 conf.zoom <- 1.0;
2170 reshape conf.winw conf.winh;
2173 let enterbirdseye () =
2174 let zoom = float conf.thumbw /. float conf.winw in
2175 let birdseyepageno =
2176 let cy = conf.winh / 2 in
2177 let fold = function
2178 | [] -> 0
2179 | l :: rest ->
2180 let rec fold best = function
2181 | [] -> best.pageno
2182 | l :: rest ->
2183 let d = cy - (l.pagedispy + l.pagevh/2)
2184 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2185 if abs d < abs dbest
2186 then fold l rest
2187 else best.pageno
2188 in fold l rest
2190 fold state.layout
2192 state.mode <- Birdseye (
2193 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2195 conf.zoom <- zoom;
2196 conf.presentation <- false;
2197 conf.interpagespace <- 10;
2198 conf.hlinks <- false;
2199 state.x <- 0;
2200 state.mstate <- Mnone;
2201 conf.maxwait <- None;
2202 conf.columns <- (
2203 match conf.beyecolumns with
2204 | Some c ->
2205 conf.zoom <- 1.0;
2206 Some ((c, 0, 0), [||])
2207 | None -> None
2209 Glut.setCursor Glut.CURSOR_INHERIT;
2210 if conf.verbose
2211 then
2212 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2213 (100.0*.zoom)
2214 else
2215 state.text <- ""
2217 reshape conf.winw conf.winh;
2220 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2221 state.mode <- View;
2222 conf.zoom <- c.zoom;
2223 conf.presentation <- c.presentation;
2224 conf.interpagespace <- c.interpagespace;
2225 conf.maxwait <- c.maxwait;
2226 conf.hlinks <- c.hlinks;
2227 conf.beyecolumns <- (
2228 match conf.columns with
2229 | Some ((c, _, _), _) -> Some c
2230 | None -> None
2232 conf.columns <- (
2233 match c.columns with
2234 | Some (c, _) -> Some (c, [||])
2235 | None -> None
2237 state.x <- leftx;
2238 if conf.verbose
2239 then
2240 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2241 (100.0*.conf.zoom)
2243 reshape conf.winw conf.winh;
2244 state.anchor <- if goback then anchor else (pageno, 0.0);
2247 let togglebirdseye () =
2248 match state.mode with
2249 | Birdseye vals -> leavebirdseye vals true
2250 | View -> enterbirdseye ()
2251 | _ -> ()
2254 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2255 let pageno = max 0 (pageno - incr) in
2256 let rec loop = function
2257 | [] -> gotopage1 pageno 0
2258 | l :: _ when l.pageno = pageno ->
2259 if l.pagedispy >= 0 && l.pagey = 0
2260 then G.postRedisplay "upbirdseye"
2261 else gotopage1 pageno 0
2262 | _ :: rest -> loop rest
2264 loop state.layout;
2265 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2268 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2269 let pageno = min (state.pagecount - 1) (pageno + incr) in
2270 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2271 let rec loop = function
2272 | [] ->
2273 let y, h = getpageyh pageno in
2274 let dy = (y - state.y) - (conf.winh - h - conf.interpagespace) in
2275 gotoy (clamp dy)
2276 | l :: _ when l.pageno = pageno ->
2277 if l.pagevh != l.pageh
2278 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
2279 else G.postRedisplay "downbirdseye"
2280 | _ :: rest -> loop rest
2282 loop state.layout
2285 let optentry mode _ key =
2286 let btos b = if b then "on" else "off" in
2287 let c = Char.unsafe_chr key in
2288 match c with
2289 | 's' ->
2290 let ondone s =
2291 try conf.scrollstep <- int_of_string s with exc ->
2292 state.text <- Printf.sprintf "bad integer `%s': %s"
2293 s (Printexc.to_string exc)
2295 TEswitch ("scroll step: ", "", None, intentry, ondone)
2297 | 'A' ->
2298 let ondone s =
2300 conf.autoscrollstep <- int_of_string s;
2301 if state.autoscroll <> None
2302 then state.autoscroll <- Some conf.autoscrollstep
2303 with exc ->
2304 state.text <- Printf.sprintf "bad integer `%s': %s"
2305 s (Printexc.to_string exc)
2307 TEswitch ("auto scroll step: ", "", None, intentry, ondone)
2309 | 'C' ->
2310 let ondone s =
2312 let n, a, b = columns_of_string s in
2313 setcolumns n a b;
2314 with exc ->
2315 state.text <- Printf.sprintf "bad columns `%s': %s"
2316 s (Printexc.to_string exc)
2318 TEswitch ("columns: ", "", None, textentry, ondone)
2320 | 'Z' ->
2321 let ondone s =
2323 let zoom = float (int_of_string s) /. 100.0 in
2324 setzoom zoom
2325 with exc ->
2326 state.text <- Printf.sprintf "bad integer `%s': %s"
2327 s (Printexc.to_string exc)
2329 TEswitch ("zoom: ", "", None, intentry, ondone)
2331 | 't' ->
2332 let ondone s =
2334 conf.thumbw <- bound (int_of_string s) 2 4096;
2335 state.text <-
2336 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
2337 begin match mode with
2338 | Birdseye beye ->
2339 leavebirdseye beye false;
2340 enterbirdseye ();
2341 | _ -> ();
2343 with exc ->
2344 state.text <- Printf.sprintf "bad integer `%s': %s"
2345 s (Printexc.to_string exc)
2347 TEswitch ("thumbnail width: ", "", None, intentry, ondone)
2349 | 'R' ->
2350 let ondone s =
2351 match try
2352 Some (int_of_string s)
2353 with exc ->
2354 state.text <- Printf.sprintf "bad integer `%s': %s"
2355 s (Printexc.to_string exc);
2356 None
2357 with
2358 | Some angle -> reqlayout angle conf.proportional
2359 | None -> ()
2361 TEswitch ("rotation: ", "", None, intentry, ondone)
2363 | 'i' ->
2364 conf.icase <- not conf.icase;
2365 TEdone ("case insensitive search " ^ (btos conf.icase))
2367 | 'p' ->
2368 conf.preload <- not conf.preload;
2369 gotoy state.y;
2370 TEdone ("preload " ^ (btos conf.preload))
2372 | 'v' ->
2373 conf.verbose <- not conf.verbose;
2374 TEdone ("verbose " ^ (btos conf.verbose))
2376 | 'd' ->
2377 conf.debug <- not conf.debug;
2378 TEdone ("debug " ^ (btos conf.debug))
2380 | 'h' ->
2381 conf.maxhfit <- not conf.maxhfit;
2382 state.maxy <-
2383 state.maxy + (if conf.maxhfit then -conf.winh else conf.winh);
2384 TEdone ("maxhfit " ^ (btos conf.maxhfit))
2386 | 'c' ->
2387 conf.crophack <- not conf.crophack;
2388 TEdone ("crophack " ^ btos conf.crophack)
2390 | 'a' ->
2391 let s =
2392 match conf.maxwait with
2393 | None ->
2394 conf.maxwait <- Some infinity;
2395 "always wait for page to complete"
2396 | Some _ ->
2397 conf.maxwait <- None;
2398 "show placeholder if page is not ready"
2400 TEdone s
2402 | 'f' ->
2403 conf.underinfo <- not conf.underinfo;
2404 TEdone ("underinfo " ^ btos conf.underinfo)
2406 | 'P' ->
2407 conf.savebmarks <- not conf.savebmarks;
2408 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
2410 | 'S' ->
2411 let ondone s =
2413 let pageno, py =
2414 match state.layout with
2415 | [] -> 0, 0
2416 | l :: _ ->
2417 l.pageno, l.pagey
2419 conf.interpagespace <- int_of_string s;
2420 state.maxy <- calcheight ();
2421 let y = getpagey pageno in
2422 gotoy (y + py)
2423 with exc ->
2424 state.text <- Printf.sprintf "bad integer `%s': %s"
2425 s (Printexc.to_string exc)
2427 TEswitch ("vertical margin: ", "", None, intentry, ondone)
2429 | 'l' ->
2430 reqlayout conf.angle (not conf.proportional);
2431 TEdone ("proportional display " ^ btos conf.proportional)
2433 | 'T' ->
2434 settrim (not conf.trimmargins) conf.trimfuzz;
2435 TEdone ("trim margins " ^ btos conf.trimmargins)
2437 | 'I' ->
2438 conf.invert <- not conf.invert;
2439 TEdone ("invert colors " ^ btos conf.invert)
2441 | _ ->
2442 state.text <- Printf.sprintf "bad option %d `%c'" key c;
2443 TEstop
2446 class type lvsource = object
2447 method getitemcount : int
2448 method getitem : int -> (string * int)
2449 method hasaction : int -> bool
2450 method exit :
2451 uioh:uioh ->
2452 cancel:bool ->
2453 active:int ->
2454 first:int ->
2455 pan:int ->
2456 qsearch:string ->
2457 uioh option
2458 method getactive : int
2459 method getfirst : int
2460 method getqsearch : string
2461 method setqsearch : string -> unit
2462 method getpan : int
2463 end;;
2465 class virtual lvsourcebase = object
2466 val mutable m_active = 0
2467 val mutable m_first = 0
2468 val mutable m_qsearch = ""
2469 val mutable m_pan = 0
2470 method getactive = m_active
2471 method getfirst = m_first
2472 method getqsearch = m_qsearch
2473 method getpan = m_pan
2474 method setqsearch s = m_qsearch <- s
2475 end;;
2477 let textentryspecial key = function
2478 | ((c, _, (Some (action, _) as onhist), onkey, ondone), mode) ->
2479 let s =
2480 match key with
2481 | Glut.KEY_UP -> action HCprev
2482 | Glut.KEY_DOWN -> action HCnext
2483 | Glut.KEY_HOME -> action HCfirst
2484 | Glut.KEY_END -> action HClast
2485 | _ -> state.text
2487 state.mode <- Textentry ((c, s, onhist, onkey, ondone), mode);
2488 G.postRedisplay "special textentry";
2489 | _ -> ()
2492 let textentrykeyboard key ((c, text, opthist, onkey, ondone), onleave) =
2493 let enttext te =
2494 state.mode <- Textentry (te, onleave);
2495 state.text <- "";
2496 enttext ();
2497 G.postRedisplay "textentrykeyboard enttext";
2499 match Char.unsafe_chr key with
2500 | '\008' -> (* backspace *)
2501 let len = String.length text in
2502 if len = 0
2503 then (
2504 onleave Cancel;
2505 G.postRedisplay "textentrykeyboard after cancel";
2507 else (
2508 let s = String.sub text 0 (len - 1) in
2509 enttext (c, s, opthist, onkey, ondone)
2512 | '\r' | '\n' ->
2513 ondone text;
2514 onleave Confirm;
2515 G.postRedisplay "textentrykeyboard after confirm"
2517 | '\007' (* ctrl-g *)
2518 | '\027' -> (* escape *)
2519 if String.length text = 0
2520 then (
2521 begin match opthist with
2522 | None -> ()
2523 | Some (_, onhistcancel) -> onhistcancel ()
2524 end;
2525 onleave Cancel;
2526 state.text <- "";
2527 G.postRedisplay "textentrykeyboard after cancel2"
2529 else (
2530 enttext (c, "", opthist, onkey, ondone)
2533 | '\127' -> () (* delete *)
2535 | _ ->
2536 begin match onkey text key with
2537 | TEdone text ->
2538 ondone text;
2539 onleave Confirm;
2540 G.postRedisplay "textentrykeyboard after confirm2";
2542 | TEcont text ->
2543 enttext (c, text, opthist, onkey, ondone);
2545 | TEstop ->
2546 onleave Cancel;
2547 G.postRedisplay "textentrykeyboard after cancel3"
2549 | TEswitch te ->
2550 state.mode <- Textentry (te, onleave);
2551 G.postRedisplay "textentrykeyboard switch";
2552 end;
2555 let firstof first active =
2556 if first > active || abs (first - active) > fstate.maxrows - 1
2557 then max 0 (active - (fstate.maxrows/2))
2558 else first
2561 let calcfirst first active =
2562 if active > first
2563 then
2564 let rows = active - first in
2565 if rows > fstate.maxrows then active - fstate.maxrows else first
2566 else active
2569 let scrollph y maxy =
2570 let sh = (float (maxy + conf.winh) /. float conf.winh) in
2571 let sh = float conf.winh /. sh in
2572 let sh = max sh (float conf.scrollh) in
2574 let percent =
2575 if y = state.maxy
2576 then 1.0
2577 else float y /. float maxy
2579 let position = (float conf.winh -. sh) *. percent in
2581 let position =
2582 if position +. sh > float conf.winh
2583 then float conf.winh -. sh
2584 else position
2586 position, sh;
2589 let coe s = (s :> uioh);;
2591 class listview ~(source:lvsource) ~trusted =
2592 object (self)
2593 val m_pan = source#getpan
2594 val m_first = source#getfirst
2595 val m_active = source#getactive
2596 val m_qsearch = source#getqsearch
2597 val m_prev_uioh = state.uioh
2599 method private elemunder y =
2600 let n = y / (fstate.fontsize+1) in
2601 if m_first + n < source#getitemcount
2602 then (
2603 if source#hasaction (m_first + n)
2604 then Some (m_first + n)
2605 else None
2607 else None
2609 method display =
2610 Gl.enable `blend;
2611 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2612 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2613 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
2614 GlDraw.color (1., 1., 1.);
2615 Gl.enable `texture_2d;
2616 let fs = fstate.fontsize in
2617 let nfs = fs + 1 in
2618 let ww = fstate.wwidth in
2619 let tabw = 30.0*.ww in
2620 let itemcount = source#getitemcount in
2621 let rec loop row =
2622 if (row - m_first) * nfs > conf.winh
2623 then ()
2624 else (
2625 if row >= 0 && row < itemcount
2626 then (
2627 let (s, level) = source#getitem row in
2628 let y = (row - m_first) * nfs in
2629 let x = 5.0 +. float (level + m_pan) *. ww in
2630 if row = m_active
2631 then (
2632 Gl.disable `texture_2d;
2633 GlDraw.polygon_mode `both `line;
2634 GlDraw.color (1., 1., 1.) ~alpha:0.9;
2635 GlDraw.rect (1., float (y + 1))
2636 (float (conf.winw - conf.scrollbw - 1), float (y + fs + 3));
2637 GlDraw.polygon_mode `both `fill;
2638 GlDraw.color (1., 1., 1.);
2639 Gl.enable `texture_2d;
2642 let drawtabularstring s =
2643 let drawstr x s = drawstring1 fs (truncate x) (y+nfs) s in
2644 if trusted
2645 then
2646 let tabpos = try String.index s '\t' with Not_found -> -1 in
2647 if tabpos > 0
2648 then
2649 let len = String.length s - tabpos - 1 in
2650 let s1 = String.sub s 0 tabpos
2651 and s2 = String.sub s (tabpos + 1) len in
2652 let nx = drawstr x s1 in
2653 let sw = nx -. x in
2654 let x = x +. (max tabw sw) in
2655 drawstr x s2
2656 else
2657 drawstr x s
2658 else
2659 drawstr x s
2661 let _ = drawtabularstring s in
2662 loop (row+1)
2666 loop m_first;
2667 Gl.disable `blend;
2668 Gl.disable `texture_2d;
2670 method updownlevel incr =
2671 let len = source#getitemcount in
2672 let curlevel =
2673 if m_active >= 0 && m_active < len
2674 then snd (source#getitem m_active)
2675 else -1
2677 let rec flow i =
2678 if i = len then i-1 else if i = -1 then 0 else
2679 let _, l = source#getitem i in
2680 if l != curlevel then i else flow (i+incr)
2682 let active = flow m_active in
2683 let first = calcfirst m_first active in
2684 G.postRedisplay "special outline updownlevel";
2685 {< m_active = active; m_first = first >}
2687 method private key1 key =
2688 let set active first qsearch =
2689 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
2691 let search active pattern incr =
2692 let dosearch re =
2693 let rec loop n =
2694 if n >= 0 && n < source#getitemcount
2695 then (
2696 let s, _ = source#getitem n in
2698 (try ignore (Str.search_forward re s 0); true
2699 with Not_found -> false)
2700 then Some n
2701 else loop (n + incr)
2703 else None
2705 loop active
2708 let re = Str.regexp_case_fold pattern in
2709 dosearch re
2710 with Failure s ->
2711 state.text <- s;
2712 None
2714 match key with
2715 | 18 | 19 -> (* ctrl-r/ctlr-s *)
2716 let incr = if key = 18 then -1 else 1 in
2717 let active, first =
2718 match search (m_active + incr) m_qsearch incr with
2719 | None ->
2720 state.text <- m_qsearch ^ " [not found]";
2721 m_active, m_first
2722 | Some active ->
2723 state.text <- m_qsearch;
2724 active, firstof m_first active
2726 G.postRedisplay "listview ctrl-r/s";
2727 set active first m_qsearch;
2729 | 8 -> (* backspace *)
2730 let len = String.length m_qsearch in
2731 if len = 0
2732 then coe self
2733 else (
2734 if len = 1
2735 then (
2736 state.text <- "";
2737 G.postRedisplay "listview empty qsearch";
2738 set m_active m_first "";
2740 else
2741 let qsearch = String.sub m_qsearch 0 (len - 1) in
2742 let active, first =
2743 match search m_active qsearch ~-1 with
2744 | None ->
2745 state.text <- qsearch ^ " [not found]";
2746 m_active, m_first
2747 | Some active ->
2748 state.text <- qsearch;
2749 active, firstof m_first active
2751 G.postRedisplay "listview backspace qsearch";
2752 set active first qsearch
2755 | _ when key >= 32 && key < 127 ->
2756 let pattern = addchar m_qsearch (Char.chr key) in
2757 let active, first =
2758 match search m_active pattern 1 with
2759 | None ->
2760 state.text <- pattern ^ " [not found]";
2761 m_active, m_first
2762 | Some active ->
2763 state.text <- pattern;
2764 active, firstof m_first active
2766 G.postRedisplay "listview qsearch add";
2767 set active first pattern;
2769 | 27 -> (* escape *)
2770 state.text <- "";
2771 if String.length m_qsearch = 0
2772 then (
2773 G.postRedisplay "list view escape";
2774 begin
2775 match
2776 source#exit (coe self) true m_active m_first m_pan m_qsearch
2777 with
2778 | None -> m_prev_uioh
2779 | Some uioh -> uioh
2782 else (
2783 G.postRedisplay "list view kill qsearch";
2784 source#setqsearch "";
2785 coe {< m_qsearch = "" >}
2788 | 13 -> (* enter *)
2789 state.text <- "";
2790 let self = {< m_qsearch = "" >} in
2791 source#setqsearch "";
2792 let opt =
2793 G.postRedisplay "listview enter";
2794 if m_active >= 0 && m_active < source#getitemcount
2795 then (
2796 source#exit (coe self) false m_active m_first m_pan "";
2798 else (
2799 source#exit (coe self) true m_active m_first m_pan "";
2802 begin match opt with
2803 | None -> m_prev_uioh
2804 | Some uioh -> uioh
2807 | 127 -> (* delete *)
2808 coe self
2810 | _ -> dolog "unknown key %d" key; coe self
2812 method private special1 key =
2813 let itemcount = source#getitemcount in
2814 let find start incr =
2815 let rec find i =
2816 if i = -1 || i = itemcount
2817 then -1
2818 else (
2819 if source#hasaction i
2820 then i
2821 else find (i + incr)
2824 find start
2826 let set active first =
2827 let first = bound first 0 (itemcount - fstate.maxrows) in
2828 state.text <- "";
2829 coe {< m_active = active; m_first = first >}
2831 let navigate incr =
2832 let isvisible first n = n >= first && n - first <= fstate.maxrows in
2833 let active, first =
2834 let incr1 = if incr > 0 then 1 else -1 in
2835 if isvisible m_first m_active
2836 then
2837 let next =
2838 let next = m_active + incr in
2839 let next =
2840 if next < 0 || next >= itemcount
2841 then -1
2842 else find next incr1
2844 if next = -1 || abs (m_active - next) > fstate.maxrows
2845 then -1
2846 else next
2848 if next = -1
2849 then
2850 let first = m_first + incr in
2851 let first = bound first 0 (itemcount - 1) in
2852 let next =
2853 let next = m_active + incr in
2854 let next = bound next 0 (itemcount - 1) in
2855 find next ~-incr1
2857 let active = if next = -1 then m_active else next in
2858 active, first
2859 else
2860 let first = min next m_first in
2861 let first =
2862 if abs (next - first) > fstate.maxrows
2863 then first + incr
2864 else first
2866 next, first
2867 else
2868 let first = m_first + incr in
2869 let first = bound first 0 (itemcount - 1) in
2870 let active =
2871 let next = m_active + incr in
2872 let next = bound next 0 (itemcount - 1) in
2873 let next = find next incr1 in
2874 let active =
2875 if next = -1 || abs (m_active - first) > fstate.maxrows
2876 then (
2877 let active = if m_active = -1 then next else m_active in
2878 active
2880 else next
2882 if isvisible first active
2883 then active
2884 else -1
2886 active, first
2888 G.postRedisplay "listview navigate";
2889 set active first;
2891 begin match key with
2892 | Glut.KEY_UP -> navigate ~-1
2893 | Glut.KEY_DOWN -> navigate 1
2894 | Glut.KEY_PAGE_UP -> navigate ~-(fstate.maxrows)
2895 | Glut.KEY_PAGE_DOWN -> navigate fstate.maxrows
2897 | Glut.KEY_RIGHT ->
2898 state.text <- "";
2899 G.postRedisplay "listview right";
2900 coe {< m_pan = m_pan - 1 >}
2902 | Glut.KEY_LEFT ->
2903 state.text <- "";
2904 G.postRedisplay "listview left";
2905 coe {< m_pan = m_pan + 1 >}
2907 | Glut.KEY_HOME ->
2908 let active = find 0 1 in
2909 G.postRedisplay "listview home";
2910 set active 0;
2912 | Glut.KEY_END ->
2913 let first = max 0 (itemcount - fstate.maxrows) in
2914 let active = find (itemcount - 1) ~-1 in
2915 G.postRedisplay "listview end";
2916 set active first;
2918 | _ -> coe self
2919 end;
2921 method key key =
2922 match state.mode with
2923 | Textentry te -> textentrykeyboard key te; coe self
2924 | _ -> self#key1 key
2926 method special key =
2927 match state.mode with
2928 | Textentry te -> textentryspecial key te; coe self
2929 | _ -> self#special1 key
2931 method button button bstate x y =
2932 let opt =
2933 match button with
2934 | Glut.LEFT_BUTTON when x > conf.winw - conf.scrollbw ->
2935 G.postRedisplay "listview scroll";
2936 if bstate = Glut.DOWN
2937 then
2938 let _, position, sh = self#scrollph in
2939 if y > truncate position && y < truncate (position +. sh)
2940 then (
2941 state.mstate <- Mscrolly;
2942 Some (coe self)
2944 else
2945 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
2946 let first = truncate (s *. float source#getitemcount) in
2947 let first = min source#getitemcount first in
2948 Some (coe {< m_first = first; m_active = first >})
2949 else (
2950 state.mstate <- Mnone;
2951 Some (coe self);
2953 | Glut.LEFT_BUTTON when bstate = Glut.UP ->
2954 begin match self#elemunder y with
2955 | Some n ->
2956 G.postRedisplay "listview click";
2957 source#exit
2958 (coe {< m_active = n >}) false n m_first m_pan m_qsearch
2959 | _ ->
2960 Some (coe self)
2962 | Glut.OTHER_BUTTON n when (n == 3 || n == 4) && bstate = Glut.UP ->
2963 let len = source#getitemcount in
2964 let first =
2965 if n = 4 && m_first + fstate.maxrows >= len
2966 then
2967 m_first
2968 else
2969 let first = m_first + (if n == 3 then -1 else 1) in
2970 bound first 0 (len - 1)
2972 G.postRedisplay "listview wheel";
2973 Some (coe {< m_first = first >})
2974 | _ ->
2975 Some (coe self)
2977 match opt with
2978 | None -> m_prev_uioh
2979 | Some uioh -> uioh
2981 method motion _ y =
2982 match state.mstate with
2983 | Mscrolly ->
2984 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
2985 let first = truncate (s *. float source#getitemcount) in
2986 let first = min source#getitemcount first in
2987 G.postRedisplay "listview motion";
2988 coe {< m_first = first; m_active = first >}
2989 | _ -> coe self
2991 method pmotion x y =
2992 if x < conf.winw - conf.scrollbw
2993 then
2994 let n =
2995 match self#elemunder y with
2996 | None -> Glut.setCursor Glut.CURSOR_INHERIT; m_active
2997 | Some n -> Glut.setCursor Glut.CURSOR_INFO; n
2999 let o =
3000 if n != m_active
3001 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3002 else self
3004 coe o
3005 else (
3006 Glut.setCursor Glut.CURSOR_INHERIT;
3007 coe self
3010 method infochanged _ = ()
3012 method scrollpw = (0, 0.0, 0.0)
3013 method scrollph =
3014 let nfs = fstate.fontsize + 1 in
3015 let y = m_first * nfs in
3016 let itemcount = source#getitemcount in
3017 let maxi = max 0 (itemcount - fstate.maxrows) in
3018 let maxy = maxi * nfs in
3019 let p, h = scrollph y maxy in
3020 conf.scrollbw, p, h
3021 end;;
3023 class outlinelistview ~source =
3024 object (self)
3025 inherit listview ~source:(source :> lvsource) ~trusted:false as super
3027 method key key =
3028 match key with
3029 | 14 -> (* ctrl-n *)
3030 source#narrow m_qsearch;
3031 G.postRedisplay "outline ctrl-n";
3032 coe {< m_first = 0; m_active = 0 >}
3034 | 21 -> (* ctrl-u *)
3035 source#denarrow;
3036 G.postRedisplay "outline ctrl-u";
3037 state.text <- "";
3038 coe {< m_first = 0; m_active = 0 >}
3040 | 12 -> (* ctrl-l *)
3041 let first = m_active - (fstate.maxrows / 2) in
3042 G.postRedisplay "outline ctrl-l";
3043 coe {< m_first = first >}
3045 | 127 -> (* delete *)
3046 source#remove m_active;
3047 G.postRedisplay "outline delete";
3048 let active = max 0 (m_active-1) in
3049 coe {< m_first = firstof m_first active;
3050 m_active = active >}
3052 | key -> super#key key
3054 method special key =
3055 let calcfirst first active =
3056 if active > first
3057 then
3058 let rows = active - first in
3059 if rows > fstate.maxrows then active - fstate.maxrows else first
3060 else active
3062 let navigate incr =
3063 let active = m_active + incr in
3064 let active = bound active 0 (source#getitemcount - 1) in
3065 let first = calcfirst m_first active in
3066 G.postRedisplay "special outline navigate";
3067 coe {< m_active = active; m_first = first >}
3069 match key with
3070 | Glut.KEY_UP -> navigate ~-1
3071 | Glut.KEY_DOWN -> navigate 1
3072 | Glut.KEY_PAGE_UP -> navigate ~-(fstate.maxrows)
3073 | Glut.KEY_PAGE_DOWN -> navigate fstate.maxrows
3075 | Glut.KEY_RIGHT ->
3076 let o =
3077 if Glut.getModifiers () land Glut.active_ctrl != 0
3078 then (
3079 G.postRedisplay "special outline right";
3080 {< m_pan = m_pan + 1 >}
3082 else self#updownlevel 1
3084 coe o
3086 | Glut.KEY_LEFT ->
3087 let o =
3088 if Glut.getModifiers () land Glut.active_ctrl != 0
3089 then (
3090 G.postRedisplay "special outline left";
3091 {< m_pan = m_pan - 1 >}
3093 else self#updownlevel ~-1
3095 coe o
3097 | Glut.KEY_HOME ->
3098 G.postRedisplay "special outline home";
3099 coe {< m_first = 0; m_active = 0 >}
3101 | Glut.KEY_END ->
3102 let active = source#getitemcount - 1 in
3103 let first = max 0 (active - fstate.maxrows) in
3104 G.postRedisplay "special outline end";
3105 coe {< m_active = active; m_first = first >}
3107 | _ -> super#special key
3110 let outlinesource usebookmarks =
3111 let empty = [||] in
3112 (object
3113 inherit lvsourcebase
3114 val mutable m_items = empty
3115 val mutable m_orig_items = empty
3116 val mutable m_prev_items = empty
3117 val mutable m_narrow_pattern = ""
3118 val mutable m_hadremovals = false
3120 method getitemcount =
3121 Array.length m_items + (if m_hadremovals then 1 else 0)
3123 method getitem n =
3124 if n == Array.length m_items && m_hadremovals
3125 then
3126 ("[Confirm removal]", 0)
3127 else
3128 let s, n, _ = m_items.(n) in
3129 (s, n)
3131 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3132 ignore (uioh, first, pan, qsearch);
3133 let confrimremoval = m_hadremovals && active = Array.length m_items in
3134 let items =
3135 if String.length m_narrow_pattern = 0
3136 then m_orig_items
3137 else m_items
3139 if not cancel
3140 then (
3141 if not confrimremoval
3142 then(
3143 let _, _, anchor = m_items.(active) in
3144 gotoanchor anchor;
3145 m_items <- items;
3147 else (
3148 state.bookmarks <- Array.to_list m_items;
3149 m_orig_items <- m_items;
3152 else m_items <- items;
3153 None
3155 method hasaction _ = true
3157 method greetmsg =
3158 if Array.length m_items != Array.length m_orig_items
3159 then "Narrowed to " ^ m_narrow_pattern ^ " (ctrl-u to restore)"
3160 else ""
3162 method narrow pattern =
3163 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
3164 match reopt with
3165 | None -> ()
3166 | Some re ->
3167 let rec loop accu n =
3168 if n = -1
3169 then (
3170 m_narrow_pattern <- pattern;
3171 m_items <- Array.of_list accu
3173 else
3174 let (s, _, _) as o = m_items.(n) in
3175 let accu =
3176 if (try ignore (Str.search_forward re s 0); true
3177 with Not_found -> false)
3178 then o :: accu
3179 else accu
3181 loop accu (n-1)
3183 loop [] (Array.length m_items - 1)
3185 method denarrow =
3186 m_orig_items <- (
3187 if usebookmarks
3188 then Array.of_list state.bookmarks
3189 else state.outlines
3191 m_items <- m_orig_items
3193 method remove m =
3194 if usebookmarks
3195 then
3196 if m >= 0 && m < Array.length m_items
3197 then (
3198 m_hadremovals <- true;
3199 m_items <- Array.init (Array.length m_items - 1) (fun n ->
3200 let n = if n >= m then n+1 else n in
3201 m_items.(n)
3205 method reset anchor items =
3206 m_hadremovals <- false;
3207 if m_orig_items == empty || m_prev_items != items
3208 then (
3209 m_orig_items <- items;
3210 if String.length m_narrow_pattern = 0
3211 then m_items <- items;
3213 m_prev_items <- items;
3214 let rely = getanchory anchor in
3215 let active =
3216 let rec loop n best bestd =
3217 if n = Array.length m_items
3218 then best
3219 else
3220 let (_, _, anchor) = m_items.(n) in
3221 let orely = getanchory anchor in
3222 let d = abs (orely - rely) in
3223 if d < bestd
3224 then loop (n+1) n d
3225 else loop (n+1) best bestd
3227 loop 0 ~-1 max_int
3229 m_active <- active;
3230 m_first <- firstof m_first active
3231 end)
3234 let enterselector usebookmarks =
3235 let source = outlinesource usebookmarks in
3236 fun errmsg ->
3237 let outlines =
3238 if usebookmarks
3239 then Array.of_list state.bookmarks
3240 else state.outlines
3242 if Array.length outlines = 0
3243 then (
3244 showtext ' ' errmsg;
3246 else (
3247 state.text <- source#greetmsg;
3248 Glut.setCursor Glut.CURSOR_INHERIT;
3249 let anchor = getanchor () in
3250 source#reset anchor outlines;
3251 state.uioh <- coe (new outlinelistview ~source);
3252 G.postRedisplay "enter selector";
3256 let enteroutlinemode =
3257 let f = enterselector false in
3258 fun ()-> f "Document has no outline";
3261 let enterbookmarkmode =
3262 let f = enterselector true in
3263 fun () -> f "Document has no bookmarks (yet)";
3266 let color_of_string s =
3267 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
3268 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
3272 let color_to_string (r, g, b) =
3273 let r = truncate (r *. 256.0)
3274 and g = truncate (g *. 256.0)
3275 and b = truncate (b *. 256.0) in
3276 Printf.sprintf "%d/%d/%d" r g b
3279 let irect_of_string s =
3280 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
3283 let irect_to_string (x0,y0,x1,y1) =
3284 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
3287 let makecheckers () =
3288 (* Appropriated from lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
3289 following to say:
3290 converted by Issac Trotts. July 25, 2002 *)
3291 let image_height = 64
3292 and image_width = 64 in
3294 let make_image () =
3295 let image =
3296 GlPix.create `ubyte ~format:`rgb ~width:image_width ~height:image_height
3298 for i = 0 to image_width - 1 do
3299 for j = 0 to image_height - 1 do
3300 Raw.sets (GlPix.to_raw image) ~pos:(3*(i*image_height+j))
3301 (if (i land 8 ) lxor (j land 8) = 0
3302 then [|255;255;255|] else [|200;200;200|])
3303 done
3304 done;
3305 image
3307 let image = make_image () in
3308 let id = GlTex.gen_texture () in
3309 GlTex.bind_texture `texture_2d id;
3310 GlPix.store (`unpack_alignment 1);
3311 GlTex.image2d image;
3312 List.iter (GlTex.parameter ~target:`texture_2d)
3313 [ `wrap_s `repeat;
3314 `wrap_t `repeat;
3315 `mag_filter `nearest;
3316 `min_filter `nearest ];
3320 let setcheckers enabled =
3321 match state.texid with
3322 | None ->
3323 if enabled then state.texid <- Some (makecheckers ())
3325 | Some texid ->
3326 if not enabled
3327 then (
3328 GlTex.delete_texture texid;
3329 state.texid <- None;
3333 let int_of_string_with_suffix s =
3334 let l = String.length s in
3335 let s1, shift =
3336 if l > 1
3337 then
3338 let suffix = Char.lowercase s.[l-1] in
3339 match suffix with
3340 | 'k' -> String.sub s 0 (l-1), 10
3341 | 'm' -> String.sub s 0 (l-1), 20
3342 | 'g' -> String.sub s 0 (l-1), 30
3343 | _ -> s, 0
3344 else s, 0
3346 let n = int_of_string s1 in
3347 let m = n lsl shift in
3348 if m < 0 || m < n
3349 then raise (Failure "value too large")
3350 else m
3353 let string_with_suffix_of_int n =
3354 if n = 0
3355 then "0"
3356 else
3357 let n, s =
3358 if n = 0
3359 then 0, ""
3360 else (
3361 if n land ((1 lsl 20) - 1) = 0
3362 then n lsr 20, "M"
3363 else (
3364 if n land ((1 lsl 10) - 1) = 0
3365 then n lsr 10, "K"
3366 else n, ""
3370 let rec loop s n =
3371 let h = n mod 1000 in
3372 let n = n / 1000 in
3373 if n = 0
3374 then string_of_int h ^ s
3375 else (
3376 let s = Printf.sprintf "_%03d%s" h s in
3377 loop s n
3380 loop "" n ^ s;
3383 let defghyllscroll = (40, 8, 32);;
3384 let ghyllscroll_of_string s =
3385 let (n, a, b) as nab =
3386 if s = "default"
3387 then defghyllscroll
3388 else Scanf.sscanf s "%u,%u,%u" (fun n a b -> n, a, b)
3390 if n <= a || n <= b || a >= b
3391 then failwith "invalid ghyll N,A,B (N <= A, A < B, N <= B)";
3392 nab;
3395 let ghyllscroll_to_string ((n, a, b) as nab) =
3396 if nab = defghyllscroll
3397 then "default"
3398 else Printf.sprintf "%d,%d,%d" n a b;
3401 let describe_location () =
3402 let f (fn, _) l =
3403 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
3405 let fn, ln = List.fold_left f (-1, -1) state.layout in
3406 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
3407 let percent =
3408 if maxy <= 0
3409 then 100.
3410 else (100. *. (float state.y /. float maxy))
3412 if fn = ln
3413 then
3414 Printf.sprintf "page %d of %d [%.2f%%]"
3415 (fn+1) state.pagecount percent
3416 else
3417 Printf.sprintf
3418 "pages %d-%d of %d [%.2f%%]"
3419 (fn+1) (ln+1) state.pagecount percent
3422 let enterinfomode =
3423 let btos b = if b then "\xe2\x88\x9a" else "" in
3424 let showextended = ref false in
3425 let leave mode = function
3426 | Confirm -> state.mode <- mode
3427 | Cancel -> state.mode <- mode in
3428 let src =
3429 (object
3430 val mutable m_first_time = true
3431 val mutable m_l = []
3432 val mutable m_a = [||]
3433 val mutable m_prev_uioh = nouioh
3434 val mutable m_prev_mode = View
3436 inherit lvsourcebase
3438 method reset prev_mode prev_uioh =
3439 m_a <- Array.of_list (List.rev m_l);
3440 m_l <- [];
3441 m_prev_mode <- prev_mode;
3442 m_prev_uioh <- prev_uioh;
3443 if m_first_time
3444 then (
3445 let rec loop n =
3446 if n >= Array.length m_a
3447 then ()
3448 else
3449 match m_a.(n) with
3450 | _, _, _, Action _ -> m_active <- n
3451 | _ -> loop (n+1)
3453 loop 0;
3454 m_first_time <- false;
3457 method int name get set =
3458 m_l <-
3459 (name, `int get, 1, Action (
3460 fun u ->
3461 let ondone s =
3462 try set (int_of_string s)
3463 with exn ->
3464 state.text <- Printf.sprintf "bad integer `%s': %s"
3465 s (Printexc.to_string exn)
3467 state.text <- "";
3468 let te = name ^ ": ", "", None, intentry, ondone in
3469 state.mode <- Textentry (te, leave m_prev_mode);
3471 )) :: m_l
3473 method int_with_suffix name get set =
3474 m_l <-
3475 (name, `intws get, 1, Action (
3476 fun u ->
3477 let ondone s =
3478 try set (int_of_string_with_suffix s)
3479 with exn ->
3480 state.text <- Printf.sprintf "bad integer `%s': %s"
3481 s (Printexc.to_string exn)
3483 state.text <- "";
3484 let te =
3485 name ^ ": ", "", None, intentry_with_suffix, ondone
3487 state.mode <- Textentry (te, leave m_prev_mode);
3489 )) :: m_l
3491 method bool ?(offset=1) ?(btos=btos) name get set =
3492 m_l <-
3493 (name, `bool (btos, get), offset, Action (
3494 fun u ->
3495 let v = get () in
3496 set (not v);
3498 )) :: m_l
3500 method color name get set =
3501 m_l <-
3502 (name, `color get, 1, Action (
3503 fun u ->
3504 let invalid = (nan, nan, nan) in
3505 let ondone s =
3506 let c =
3507 try color_of_string s
3508 with exn ->
3509 state.text <- Printf.sprintf "bad color `%s': %s"
3510 s (Printexc.to_string exn);
3511 invalid
3513 if c <> invalid
3514 then set c;
3516 let te = name ^ ": ", "", None, textentry, ondone in
3517 state.text <- color_to_string (get ());
3518 state.mode <- Textentry (te, leave m_prev_mode);
3520 )) :: m_l
3522 method string name get set =
3523 m_l <-
3524 (name, `string get, 1, Action (
3525 fun u ->
3526 let ondone s = set s in
3527 let te = name ^ ": ", "", None, textentry, ondone in
3528 state.mode <- Textentry (te, leave m_prev_mode);
3530 )) :: m_l
3532 method colorspace name get set =
3533 m_l <-
3534 (name, `string get, 1, Action (
3535 fun _ ->
3536 let source =
3537 let vals = [| "rgb"; "bgr"; "gray" |] in
3538 (object
3539 inherit lvsourcebase
3541 initializer
3542 m_active <- int_of_colorspace conf.colorspace;
3543 m_first <- 0;
3545 method getitemcount = Array.length vals
3546 method getitem n = (vals.(n), 0)
3547 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3548 ignore (uioh, first, pan, qsearch);
3549 if not cancel then set active;
3550 None
3551 method hasaction _ = true
3552 end)
3554 state.text <- "";
3555 coe (new listview ~source ~trusted:true)
3556 )) :: m_l
3558 method caption s offset =
3559 m_l <- (s, `empty, offset, Noaction) :: m_l
3561 method caption2 s f offset =
3562 m_l <- (s, `string f, offset, Noaction) :: m_l
3564 method getitemcount = Array.length m_a
3566 method getitem n =
3567 let tostr = function
3568 | `int f -> string_of_int (f ())
3569 | `intws f -> string_with_suffix_of_int (f ())
3570 | `string f -> f ()
3571 | `color f -> color_to_string (f ())
3572 | `bool (btos, f) -> btos (f ())
3573 | `empty -> ""
3575 let name, t, offset, _ = m_a.(n) in
3576 ((let s = tostr t in
3577 if String.length s > 0
3578 then Printf.sprintf "%s\t%s" name s
3579 else name),
3580 offset)
3582 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3583 let uiohopt =
3584 if not cancel
3585 then (
3586 m_qsearch <- qsearch;
3587 let uioh =
3588 match m_a.(active) with
3589 | _, _, _, Action f -> f uioh
3590 | _ -> uioh
3592 Some uioh
3594 else None
3596 m_active <- active;
3597 m_first <- first;
3598 m_pan <- pan;
3599 uiohopt
3601 method hasaction n =
3602 match m_a.(n) with
3603 | _, _, _, Action _ -> true
3604 | _ -> false
3605 end)
3607 let rec fillsrc prevmode prevuioh =
3608 let sep () = src#caption "" 0 in
3609 let colorp name get set =
3610 src#string name
3611 (fun () -> color_to_string (get ()))
3612 (fun v ->
3614 let c = color_of_string v in
3615 set c
3616 with exn ->
3617 state.text <- Printf.sprintf "bad color `%s': %s"
3618 v (Printexc.to_string exn);
3621 let oldmode = state.mode in
3622 let birdseye = isbirdseye state.mode in
3624 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
3626 src#bool "presentation mode"
3627 (fun () -> conf.presentation)
3628 (fun v ->
3629 conf.presentation <- v;
3630 state.anchor <- getanchor ();
3631 represent ());
3633 src#bool "ignore case in searches"
3634 (fun () -> conf.icase)
3635 (fun v -> conf.icase <- v);
3637 src#bool "preload"
3638 (fun () -> conf.preload)
3639 (fun v -> conf.preload <- v);
3641 src#bool "highlight links"
3642 (fun () -> conf.hlinks)
3643 (fun v -> conf.hlinks <- v);
3645 src#bool "under info"
3646 (fun () -> conf.underinfo)
3647 (fun v -> conf.underinfo <- v);
3649 src#bool "persistent bookmarks"
3650 (fun () -> conf.savebmarks)
3651 (fun v -> conf.savebmarks <- v);
3653 src#bool "proportional display"
3654 (fun () -> conf.proportional)
3655 (fun v -> reqlayout conf.angle v);
3657 src#bool "trim margins"
3658 (fun () -> conf.trimmargins)
3659 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
3661 src#bool "persistent location"
3662 (fun () -> conf.jumpback)
3663 (fun v -> conf.jumpback <- v);
3665 sep ();
3666 src#int "inter-page space"
3667 (fun () -> conf.interpagespace)
3668 (fun n ->
3669 conf.interpagespace <- n;
3670 let pageno, py =
3671 match state.layout with
3672 | [] -> 0, 0
3673 | l :: _ ->
3674 l.pageno, l.pagey
3676 state.maxy <- calcheight ();
3677 let y = getpagey pageno in
3678 gotoy (y + py)
3681 src#int "page bias"
3682 (fun () -> conf.pagebias)
3683 (fun v -> conf.pagebias <- v);
3685 src#int "scroll step"
3686 (fun () -> conf.scrollstep)
3687 (fun n -> conf.scrollstep <- n);
3689 src#int "auto scroll step"
3690 (fun () ->
3691 match state.autoscroll with
3692 | Some step -> step
3693 | _ -> conf.autoscrollstep)
3694 (fun n ->
3695 if state.autoscroll <> None
3696 then state.autoscroll <- Some n;
3697 conf.autoscrollstep <- n);
3699 src#int "zoom"
3700 (fun () -> truncate (conf.zoom *. 100.))
3701 (fun v -> setzoom ((float v) /. 100.));
3703 src#int "rotation"
3704 (fun () -> conf.angle)
3705 (fun v -> reqlayout v conf.proportional);
3707 src#int "scroll bar width"
3708 (fun () -> state.scrollw)
3709 (fun v ->
3710 state.scrollw <- v;
3711 conf.scrollbw <- v;
3712 reshape conf.winw conf.winh;
3715 src#int "scroll handle height"
3716 (fun () -> conf.scrollh)
3717 (fun v -> conf.scrollh <- v;);
3719 src#int "thumbnail width"
3720 (fun () -> conf.thumbw)
3721 (fun v ->
3722 conf.thumbw <- min 4096 v;
3723 match oldmode with
3724 | Birdseye beye ->
3725 leavebirdseye beye false;
3726 enterbirdseye ()
3727 | _ -> ()
3730 src#string "columns"
3731 (fun () ->
3732 match conf.columns with
3733 | None -> "1"
3734 | Some (multicol, _) -> columns_to_string multicol)
3735 (fun v ->
3736 let n, a, b = columns_of_string v in
3737 setcolumns n a b);
3739 sep ();
3740 src#caption "Presentation mode" 0;
3741 src#bool "scrollbar visible"
3742 (fun () -> conf.scrollbarinpm)
3743 (fun v ->
3744 if v != conf.scrollbarinpm
3745 then (
3746 conf.scrollbarinpm <- v;
3747 if conf.presentation
3748 then (
3749 state.scrollw <- if v then conf.scrollbw else 0;
3750 reshape conf.winw conf.winh;
3755 sep ();
3756 src#caption "Pixmap cache" 0;
3757 src#int_with_suffix "size (advisory)"
3758 (fun () -> conf.memlimit)
3759 (fun v -> conf.memlimit <- v);
3761 src#caption2 "used"
3762 (fun () -> Printf.sprintf "%s bytes, %d tiles"
3763 (string_with_suffix_of_int state.memused)
3764 (Hashtbl.length state.tilemap)) 1;
3766 sep ();
3767 src#caption "Layout" 0;
3768 src#caption2 "Dimension"
3769 (fun () ->
3770 Printf.sprintf "%dx%d (virtual %dx%d)"
3771 conf.winw conf.winh
3772 state.w state.maxy)
3774 if conf.debug
3775 then
3776 src#caption2 "Position" (fun () ->
3777 Printf.sprintf "%dx%d" state.x state.y
3779 else
3780 src#caption2 "Visible" (fun () -> describe_location ()) 1
3783 sep ();
3784 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
3785 "Save these parameters as global defaults at exit"
3786 (fun () -> conf.bedefault)
3787 (fun v -> conf.bedefault <- v)
3790 sep ();
3791 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
3792 src#bool ~offset:0 ~btos "Extended parameters"
3793 (fun () -> !showextended)
3794 (fun v -> showextended := v; fillsrc prevmode prevuioh);
3795 if !showextended
3796 then (
3797 src#bool "checkers"
3798 (fun () -> conf.checkers)
3799 (fun v -> conf.checkers <- v; setcheckers v);
3800 src#bool "verbose"
3801 (fun () -> conf.verbose)
3802 (fun v -> conf.verbose <- v);
3803 src#bool "invert colors"
3804 (fun () -> conf.invert)
3805 (fun v -> conf.invert <- v);
3806 src#bool "max fit"
3807 (fun () -> conf.maxhfit)
3808 (fun v -> conf.maxhfit <- v);
3809 src#bool "redirect stderr"
3810 (fun () -> conf.redirectstderr)
3811 (fun v -> conf.redirectstderr <- v; redirectstderr ());
3812 src#string "uri launcher"
3813 (fun () -> conf.urilauncher)
3814 (fun v -> conf.urilauncher <- v);
3815 src#string "tile size"
3816 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
3817 (fun v ->
3819 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
3820 conf.tileh <- max 64 w;
3821 conf.tilew <- max 64 h;
3822 flushtiles ();
3823 with exn ->
3824 state.text <- Printf.sprintf "bad tile size `%s': %s"
3825 v (Printexc.to_string exn));
3826 src#int "texture count"
3827 (fun () -> conf.texcount)
3828 (fun v ->
3829 if realloctexts v
3830 then conf.texcount <- v
3831 else showtext '!' " Failed to set texture count please retry later"
3833 src#int "slice height"
3834 (fun () -> conf.sliceheight)
3835 (fun v ->
3836 conf.sliceheight <- v;
3837 wcmd "sliceh" [`i conf.sliceheight];
3839 src#int "anti-aliasing level"
3840 (fun () -> conf.aalevel)
3841 (fun v ->
3842 conf.aalevel <- bound v 0 8;
3843 state.anchor <- getanchor ();
3844 opendoc state.path state.password;
3846 src#int "ui font size"
3847 (fun () -> fstate.fontsize)
3848 (fun v -> setfontsize (bound v 5 100));
3849 colorp "background color"
3850 (fun () -> conf.bgcolor)
3851 (fun v -> conf.bgcolor <- v);
3852 src#bool "crop hack"
3853 (fun () -> conf.crophack)
3854 (fun v -> conf.crophack <- v);
3855 src#string "trim fuzz"
3856 (fun () -> irect_to_string conf.trimfuzz)
3857 (fun v ->
3859 conf.trimfuzz <- irect_of_string v;
3860 if conf.trimmargins
3861 then settrim true conf.trimfuzz;
3862 with exn ->
3863 state.text <- Printf.sprintf "bad irect `%s': %s"
3864 v (Printexc.to_string exn)
3866 src#string "throttle"
3867 (fun () ->
3868 match conf.maxwait with
3869 | None -> "show place holder if page is not ready"
3870 | Some time ->
3871 if time = infinity
3872 then "wait for page to fully render"
3873 else
3874 "wait " ^ string_of_float time
3875 ^ " seconds before showing placeholder"
3877 (fun v ->
3879 let f = float_of_string v in
3880 if f <= 0.0
3881 then conf.maxwait <- None
3882 else conf.maxwait <- Some f
3883 with exn ->
3884 state.text <- Printf.sprintf "bad time `%s': %s"
3885 v (Printexc.to_string exn)
3887 src#string "ghyll scroll"
3888 (fun () ->
3889 match conf.ghyllscroll with
3890 | None -> ""
3891 | Some nab -> ghyllscroll_to_string nab
3893 (fun v ->
3895 let gs =
3896 if String.length v = 0
3897 then None
3898 else Some (ghyllscroll_of_string v)
3900 conf.ghyllscroll <- gs
3901 with exn ->
3902 state.text <- Printf.sprintf "bad ghyll `%s': %s"
3903 v (Printexc.to_string exn)
3905 src#colorspace "color space"
3906 (fun () -> colorspace_to_string conf.colorspace)
3907 (fun v ->
3908 conf.colorspace <- colorspace_of_int v;
3909 wcmd "cs" [`i v];
3910 load state.layout;
3914 sep ();
3915 src#caption "Document" 0;
3916 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
3917 src#caption2 "Pages"
3918 (fun () -> string_of_int state.pagecount) 1;
3919 src#caption2 "Dimensions"
3920 (fun () -> string_of_int (List.length state.pdims)) 1;
3921 if conf.trimmargins
3922 then (
3923 sep ();
3924 src#caption "Trimmed margins" 0;
3925 src#caption2 "Dimensions"
3926 (fun () -> string_of_int (List.length state.pdims)) 1;
3929 src#reset prevmode prevuioh;
3931 fun () ->
3932 state.text <- "";
3933 let prevmode = state.mode
3934 and prevuioh = state.uioh in
3935 fillsrc prevmode prevuioh;
3936 let source = (src :> lvsource) in
3937 state.uioh <- coe (object (self)
3938 inherit listview ~source ~trusted:true as super
3939 val mutable m_prevmemused = 0
3940 method infochanged = function
3941 | Memused ->
3942 if m_prevmemused != state.memused
3943 then (
3944 m_prevmemused <- state.memused;
3945 G.postRedisplay "memusedchanged";
3947 | Pdim -> G.postRedisplay "pdimchanged"
3948 | Docinfo -> fillsrc prevmode prevuioh
3950 method special key =
3951 if Glut.getModifiers () land Glut.active_ctrl = 0
3952 then
3953 match key with
3954 | Glut.KEY_LEFT -> coe (self#updownlevel ~-1)
3955 | Glut.KEY_RIGHT -> coe (self#updownlevel 1)
3956 | _ -> super#special key
3957 else super#special key
3958 end);
3959 G.postRedisplay "info";
3962 let enterhelpmode =
3963 let source =
3964 (object
3965 inherit lvsourcebase
3966 method getitemcount = Array.length state.help
3967 method getitem n =
3968 let s, n, _ = state.help.(n) in
3969 (s, n)
3971 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3972 let optuioh =
3973 if not cancel
3974 then (
3975 m_qsearch <- qsearch;
3976 match state.help.(active) with
3977 | _, _, Action f -> Some (f uioh)
3978 | _ -> Some (uioh)
3980 else None
3982 m_active <- active;
3983 m_first <- first;
3984 m_pan <- pan;
3985 optuioh
3987 method hasaction n =
3988 match state.help.(n) with
3989 | _, _, Action _ -> true
3990 | _ -> false
3992 initializer
3993 m_active <- -1
3994 end)
3995 in fun () ->
3996 state.uioh <- coe (new listview ~source ~trusted:true);
3997 G.postRedisplay "help";
4000 let entermsgsmode =
4001 let msgsource =
4002 let re = Str.regexp "[\r\n]" in
4003 (object
4004 inherit lvsourcebase
4005 val mutable m_items = [||]
4007 method getitemcount = 1 + Array.length m_items
4009 method getitem n =
4010 if n = 0
4011 then "[Clear]", 0
4012 else m_items.(n-1), 0
4014 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4015 ignore uioh;
4016 if not cancel
4017 then (
4018 if active = 0
4019 then Buffer.clear state.errmsgs;
4020 m_qsearch <- qsearch;
4022 m_active <- active;
4023 m_first <- first;
4024 m_pan <- pan;
4025 None
4027 method hasaction n =
4028 n = 0
4030 method reset =
4031 state.newerrmsgs <- false;
4032 let l = Str.split re (Buffer.contents state.errmsgs) in
4033 m_items <- Array.of_list l
4035 initializer
4036 m_active <- 0
4037 end)
4038 in fun () ->
4039 state.text <- "";
4040 msgsource#reset;
4041 let source = (msgsource :> lvsource) in
4042 state.uioh <- coe (object
4043 inherit listview ~source ~trusted:false as super
4044 method display =
4045 if state.newerrmsgs
4046 then msgsource#reset;
4047 super#display
4048 end);
4049 G.postRedisplay "msgs";
4052 let quickbookmark ?title () =
4053 match state.layout with
4054 | [] -> ()
4055 | l :: _ ->
4056 let title =
4057 match title with
4058 | None ->
4059 let sec = Unix.gettimeofday () in
4060 let tm = Unix.localtime sec in
4061 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
4062 (l.pageno+1)
4063 tm.Unix.tm_mday
4064 tm.Unix.tm_mon
4065 (tm.Unix.tm_year + 1900)
4066 tm.Unix.tm_hour
4067 tm.Unix.tm_min
4068 | Some title -> title
4070 state.bookmarks <-
4071 (title, 0, (l.pageno, float l.pagey /. float l.pageh))
4072 :: state.bookmarks
4075 let doreshape w h =
4076 state.fullscreen <- None;
4077 Glut.reshapeWindow w h;
4080 let viewkeyboard key =
4081 let enttext te =
4082 let mode = state.mode in
4083 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
4084 state.text <- "";
4085 enttext ();
4086 G.postRedisplay "view:enttext"
4088 let c = Char.chr key in
4089 match c with
4090 | '\027' | 'q' -> (* escape *)
4091 begin match state.mstate with
4092 | Mzoomrect _ ->
4093 state.mstate <- Mnone;
4094 Glut.setCursor Glut.CURSOR_INHERIT;
4095 G.postRedisplay "kill zoom rect";
4096 | _ ->
4097 raise Quit
4098 end;
4100 | '\008' -> (* backspace *)
4101 let y = getnav ~-1 in
4102 gotoy_and_clear_text y
4104 | 'o' ->
4105 enteroutlinemode ()
4107 | 'u' ->
4108 state.rects <- [];
4109 state.text <- "";
4110 G.postRedisplay "dehighlight";
4112 | '/' | '?' ->
4113 let ondone isforw s =
4114 cbput state.hists.pat s;
4115 state.searchpattern <- s;
4116 search s isforw
4118 let s = String.create 1 in
4119 s.[0] <- c;
4120 enttext (s, "", Some (onhist state.hists.pat),
4121 textentry, ondone (c ='/'))
4123 | '+' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
4124 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
4125 setzoom (conf.zoom +. incr)
4127 | '+' ->
4128 let ondone s =
4129 let n =
4130 try int_of_string s with exc ->
4131 state.text <- Printf.sprintf "bad integer `%s': %s"
4132 s (Printexc.to_string exc);
4133 max_int
4135 if n != max_int
4136 then (
4137 conf.pagebias <- n;
4138 state.text <- "page bias is now " ^ string_of_int n;
4141 enttext ("page bias: ", "", None, intentry, ondone)
4143 | '-' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
4144 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
4145 setzoom (max 0.01 (conf.zoom -. decr))
4147 | '-' ->
4148 let ondone msg = state.text <- msg in
4149 enttext (
4150 "option [acfhilpstvACPRSZTI]: ", "", None,
4151 optentry state.mode, ondone
4154 | '0' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
4155 setzoom 1.0
4157 | '1' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
4158 let zoom = zoomforh conf.winw conf.winh state.scrollw in
4159 if zoom < 1.0
4160 then setzoom zoom
4162 | '9' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
4163 togglebirdseye ()
4165 | '0' .. '9' ->
4166 let ondone s =
4167 let n =
4168 try int_of_string s with exc ->
4169 state.text <- Printf.sprintf "bad integer `%s': %s"
4170 s (Printexc.to_string exc);
4173 if n >= 0
4174 then (
4175 addnav ();
4176 cbput state.hists.pag (string_of_int n);
4177 gotopage1 (n + conf.pagebias - 1) 0;
4180 let pageentry text key =
4181 match Char.unsafe_chr key with
4182 | 'g' -> TEdone text
4183 | _ -> intentry text key
4185 let text = "x" in text.[0] <- c;
4186 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone)
4188 | 'b' ->
4189 state.scrollw <- if state.scrollw > 0 then 0 else conf.scrollbw;
4190 reshape conf.winw conf.winh;
4192 | 'l' ->
4193 conf.hlinks <- not conf.hlinks;
4194 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
4195 G.postRedisplay "toggle highlightlinks";
4197 | 'a' ->
4198 begin match state.autoscroll with
4199 | Some step ->
4200 conf.autoscrollstep <- step;
4201 state.autoscroll <- None
4202 | None ->
4203 if conf.autoscrollstep = 0
4204 then state.autoscroll <- Some 1
4205 else state.autoscroll <- Some conf.autoscrollstep
4208 | 'P' ->
4209 conf.presentation <- not conf.presentation;
4210 if conf.presentation
4211 then (
4212 if not conf.scrollbarinpm
4213 then state.scrollw <- 0;
4215 else
4216 state.scrollw <- conf.scrollbw;
4218 showtext ' ' ("presentation mode " ^
4219 if conf.presentation then "on" else "off");
4220 state.anchor <- getanchor ();
4221 represent ()
4223 | 'f' ->
4224 begin match state.fullscreen with
4225 | None ->
4226 state.fullscreen <- Some (conf.winw, conf.winh);
4227 Glut.fullScreen ()
4228 | Some (w, h) ->
4229 state.fullscreen <- None;
4230 doreshape w h
4233 | 'g' ->
4234 gotoy_and_clear_text 0
4236 | 'G' ->
4237 gotopage1 (state.pagecount - 1) 0
4239 | 'n' ->
4240 search state.searchpattern true
4242 | 'p' | 'N' ->
4243 search state.searchpattern false
4245 | 't' ->
4246 begin match state.layout with
4247 | [] -> ()
4248 | l :: _ ->
4249 gotoy_and_clear_text (getpagey l.pageno)
4252 | ' ' ->
4253 begin match List.rev state.layout with
4254 | [] -> ()
4255 | l :: _ ->
4256 let pageno = min (l.pageno+1) (state.pagecount-1) in
4257 gotoy_and_clear_text (getpagey pageno)
4260 | '\127' -> (* del *)
4261 begin match state.layout with
4262 | [] -> ()
4263 | l :: _ ->
4264 let pageno = max 0 (l.pageno-1) in
4265 gotoy_and_clear_text (getpagey pageno)
4268 | '=' ->
4269 showtext ' ' (describe_location ());
4271 | 'w' ->
4272 begin match state.layout with
4273 | [] -> ()
4274 | l :: _ ->
4275 doreshape (l.pagew + state.scrollw) l.pageh;
4276 G.postRedisplay "w"
4279 | '\'' ->
4280 enterbookmarkmode ()
4282 | 'h' ->
4283 enterhelpmode ()
4285 | 'i' ->
4286 enterinfomode ()
4288 | 'e' when conf.redirectstderr ->
4289 entermsgsmode ()
4291 | 'm' ->
4292 let ondone s =
4293 match state.layout with
4294 | l :: _ ->
4295 state.bookmarks <-
4296 (s, 0, (l.pageno, float l.pagey /. float l.pageh))
4297 :: state.bookmarks
4298 | _ -> ()
4300 enttext ("bookmark: ", "", None, textentry, ondone)
4302 | '~' ->
4303 quickbookmark ();
4304 showtext ' ' "Quick bookmark added";
4306 | 'z' ->
4307 begin match state.layout with
4308 | l :: _ ->
4309 let rect = getpdimrect l.pagedimno in
4310 let w, h =
4311 if conf.crophack
4312 then
4313 (truncate (1.8 *. (rect.(1) -. rect.(0))),
4314 truncate (1.2 *. (rect.(3) -. rect.(0))))
4315 else
4316 (truncate (rect.(1) -. rect.(0)),
4317 truncate (rect.(3) -. rect.(0)))
4319 let w = truncate ((float w)*.conf.zoom)
4320 and h = truncate ((float h)*.conf.zoom) in
4321 if w != 0 && h != 0
4322 then (
4323 state.anchor <- getanchor ();
4324 doreshape (w + state.scrollw) (h + conf.interpagespace)
4326 G.postRedisplay "z";
4328 | [] -> ()
4331 | '\000' -> (* ctrl-2 *)
4332 let maxw = getmaxw () in
4333 if maxw > 0.0
4334 then setzoom (maxw /. float conf.winw)
4336 | '<' | '>' ->
4337 reqlayout (conf.angle + (if c = '>' then 30 else -30)) conf.proportional
4339 | '[' | ']' ->
4340 conf.colorscale <-
4341 bound (conf.colorscale +. (if c = ']' then 0.1 else -0.1)) 0.0 1.0
4343 G.postRedisplay "brightness";
4345 | 'k' ->
4346 begin match state.mode with
4347 | Birdseye beye -> upbirdseye 1 beye
4348 | _ -> gotoy (clamp (-conf.scrollstep))
4351 | 'j' ->
4352 begin match state.mode with
4353 | Birdseye beye -> downbirdseye 1 beye
4354 | _ -> gotoy (clamp conf.scrollstep)
4357 | 'r' ->
4358 state.anchor <- getanchor ();
4359 opendoc state.path state.password
4361 | 'v' when not conf.debug ->
4362 List.iter debugl state.layout;
4364 | 'v' when conf.debug ->
4365 state.rects <- [];
4366 List.iter (fun l ->
4367 match getopaque l.pageno with
4368 | None -> ()
4369 | Some opaque ->
4370 let x0, y0, x1, y1 = pagebbox opaque in
4371 let a,b = float x0, float y0 in
4372 let c,d = float x1, float y0 in
4373 let e,f = float x1, float y1 in
4374 let h,j = float x0, float y1 in
4375 let rect = (a,b,c,d,e,f,h,j) in
4376 debugrect rect;
4377 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
4378 ) state.layout;
4379 G.postRedisplay "v";
4381 | _ ->
4382 vlog "huh? %d %c" key (Char.chr key);
4385 let birdseyekeyboard key ((_, _, pageno, _, _) as beye) =
4386 match key with
4387 | 27 -> (* escape *)
4388 leavebirdseye beye true
4390 | 12 -> (* ctrl-l *)
4391 let y, h = getpageyh pageno in
4392 let top = (conf.winh - h) / 2 in
4393 gotoy (max 0 (y - top))
4395 | 13 -> (* enter *)
4396 leavebirdseye beye false
4398 | _ ->
4399 viewkeyboard key
4402 let keyboard ~key ~x ~y =
4403 ignore x;
4404 ignore y;
4405 if key = 7 && not (istextentry state.mode) (* ctrl-g *)
4406 then wcmd "interrupt" []
4407 else state.uioh <- state.uioh#key key
4410 let birdseyespecial key ((oconf, leftx, _, hooverpageno, anchor) as beye) =
4411 let incr =
4412 match conf.columns with
4413 | None -> 1
4414 | Some ((c, _, _), _) -> c
4416 match key with
4417 | Glut.KEY_UP -> upbirdseye incr beye
4418 | Glut.KEY_DOWN -> downbirdseye incr beye
4419 | Glut.KEY_LEFT -> upbirdseye 1 beye
4420 | Glut.KEY_RIGHT -> downbirdseye 1 beye
4422 | Glut.KEY_PAGE_UP ->
4423 begin match state.layout with
4424 | l :: _ ->
4425 if l.pagey != 0
4426 then (
4427 state.mode <- Birdseye (
4428 oconf, leftx, l.pageno, hooverpageno, anchor
4430 gotopage1 l.pageno 0;
4432 else (
4433 let layout = layout (state.y-conf.winh) conf.winh in
4434 match layout with
4435 | [] -> gotoy (clamp (-conf.winh))
4436 | l :: _ ->
4437 state.mode <- Birdseye (
4438 oconf, leftx, l.pageno, hooverpageno, anchor
4440 gotopage1 l.pageno 0
4443 | [] -> gotoy (clamp (-conf.winh))
4444 end;
4446 | Glut.KEY_PAGE_DOWN ->
4447 begin match List.rev state.layout with
4448 | l :: _ ->
4449 let layout = layout (state.y + conf.winh) conf.winh in
4450 begin match layout with
4451 | [] ->
4452 let incr = l.pageh - l.pagevh in
4453 if incr = 0
4454 then (
4455 state.mode <-
4456 Birdseye (
4457 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
4459 G.postRedisplay "birdseye pagedown";
4461 else gotoy (clamp (incr + conf.interpagespace*2));
4463 | l :: _ ->
4464 state.mode <-
4465 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
4466 gotopage1 l.pageno 0;
4469 | [] -> gotoy (clamp conf.winh)
4470 end;
4472 | Glut.KEY_HOME ->
4473 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
4474 gotopage1 0 0
4476 | Glut.KEY_END ->
4477 let pageno = state.pagecount - 1 in
4478 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
4479 if not (pagevisible state.layout pageno)
4480 then
4481 let h =
4482 match List.rev state.pdims with
4483 | [] -> conf.winh
4484 | (_, _, h, _) :: _ -> h
4486 gotoy (max 0 (getpagey pageno - (conf.winh - h - conf.interpagespace)))
4487 else G.postRedisplay "birdseye end";
4488 | _ -> ()
4491 let setautoscrollspeed step goingdown =
4492 let incr = max 1 ((abs step) / 2) in
4493 let incr = if goingdown then incr else -incr in
4494 let astep = step + incr in
4495 state.autoscroll <- Some astep;
4498 let special ~key ~x ~y =
4499 ignore x;
4500 ignore y;
4501 state.uioh <- state.uioh#special key
4504 let drawpage l =
4505 let color =
4506 match state.mode with
4507 | Textentry _ -> scalecolor 0.4
4508 | View -> scalecolor 1.0
4509 | Birdseye (_, _, pageno, hooverpageno, _) ->
4510 if l.pageno = hooverpageno
4511 then scalecolor 0.9
4512 else (
4513 if l.pageno = pageno
4514 then scalecolor 1.0
4515 else scalecolor 0.8
4518 drawtiles l color;
4519 begin match getopaque l.pageno with
4520 | Some opaque ->
4521 if tileready l l.pagex l.pagey
4522 then
4523 let x = l.pagedispx - l.pagex
4524 and y = l.pagedispy - l.pagey in
4525 postprocess opaque conf.hlinks x y;
4527 | _ -> ()
4528 end;
4531 let scrollindicator () =
4532 let sbw, ph, sh = state.uioh#scrollph in
4533 let sbh, pw, sw = state.uioh#scrollpw in
4535 GlDraw.color (0.64, 0.64, 0.64);
4536 GlDraw.rect
4537 (float (conf.winw - sbw), 0.)
4538 (float conf.winw, float conf.winh)
4540 GlDraw.rect
4541 (0., float (conf.winh - sbh))
4542 (float (conf.winw - state.scrollw - 1), float conf.winh)
4544 GlDraw.color (0.0, 0.0, 0.0);
4546 GlDraw.rect
4547 (float (conf.winw - sbw), ph)
4548 (float conf.winw, ph +. sh)
4550 GlDraw.rect
4551 (pw, float (conf.winh - sbh))
4552 (pw +. sw, float conf.winh)
4556 let pagetranslatepoint l x y =
4557 let dy = y - l.pagedispy in
4558 let y = dy + l.pagey in
4559 let dx = x - l.pagedispx in
4560 let x = dx + l.pagex in
4561 (x, y);
4564 let showsel () =
4565 match state.mstate with
4566 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
4569 | Msel ((x0, y0), (x1, y1)) ->
4570 let rec loop = function
4571 | l :: ls ->
4572 if ((y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
4573 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh))))
4574 && ((x0 >= l.pagedispx && x0 <= (l.pagedispx + l.pagevw))
4575 || ((x1 >= l.pagedispx && x1 <= (l.pagedispx + l.pagevw))))
4576 then
4577 match getopaque l.pageno with
4578 | Some opaque ->
4579 let dx, dy = pagetranslatepoint l 0 0 in
4580 let x0 = x0 + dx
4581 and y0 = y0 + dy
4582 and x1 = x1 + dx
4583 and y1 = y1 + dy in
4584 GlMat.mode `modelview;
4585 GlMat.push ();
4586 GlMat.translate ~x:(float ~-dx) ~y:(float ~-dy) ();
4587 seltext opaque (x0, y0, x1, y1);
4588 GlMat.pop ();
4589 | _ -> ()
4590 else loop ls
4591 | [] -> ()
4593 loop state.layout
4596 let showrects () =
4597 Gl.enable `blend;
4598 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
4599 GlDraw.polygon_mode `both `fill;
4600 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
4601 List.iter
4602 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
4603 List.iter (fun l ->
4604 if l.pageno = pageno
4605 then (
4606 let dx = float (l.pagedispx - l.pagex) in
4607 let dy = float (l.pagedispy - l.pagey) in
4608 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
4609 GlDraw.begins `quads;
4611 GlDraw.vertex2 (x0+.dx, y0+.dy);
4612 GlDraw.vertex2 (x1+.dx, y1+.dy);
4613 GlDraw.vertex2 (x2+.dx, y2+.dy);
4614 GlDraw.vertex2 (x3+.dx, y3+.dy);
4616 GlDraw.ends ();
4618 ) state.layout
4619 ) state.rects
4621 Gl.disable `blend;
4624 let display () =
4625 GlClear.color (scalecolor2 conf.bgcolor);
4626 GlClear.clear [`color];
4627 List.iter drawpage state.layout;
4628 showrects ();
4629 showsel ();
4630 state.uioh#display;
4631 scrollindicator ();
4632 begin match state.mstate with
4633 | Mzoomrect ((x0, y0), (x1, y1)) ->
4634 Gl.enable `blend;
4635 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
4636 GlDraw.polygon_mode `both `fill;
4637 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
4638 GlDraw.rect (float x0, float y0)
4639 (float x1, float y1);
4640 Gl.disable `blend;
4641 | _ -> ()
4642 end;
4643 enttext ();
4644 Glut.swapBuffers ();
4647 let getunder x y =
4648 let rec f = function
4649 | l :: rest ->
4650 begin match getopaque l.pageno with
4651 | Some opaque ->
4652 let x0 = l.pagedispx in
4653 let x1 = x0 + l.pagevw in
4654 let y0 = l.pagedispy in
4655 let y1 = y0 + l.pagevh in
4656 if y >= y0 && y <= y1 && x >= x0 && x <= x1
4657 then
4658 let px, py = pagetranslatepoint l x y in
4659 match whatsunder opaque px py with
4660 | Unone -> f rest
4661 | under -> under
4662 else f rest
4663 | _ ->
4664 f rest
4666 | [] -> Unone
4668 f state.layout
4671 let zoomrect x y x1 y1 =
4672 let x0 = min x x1
4673 and x1 = max x x1
4674 and y0 = min y y1 in
4675 gotoy (state.y + y0);
4676 state.anchor <- getanchor ();
4677 let zoom = (float conf.winw *. conf.zoom) /. float (x1 - x0) in
4678 let margin =
4679 if state.w < conf.winw - state.scrollw
4680 then (conf.winw - state.scrollw - state.w) / 2
4681 else 0
4683 state.x <- (state.x + margin) - x0;
4684 setzoom zoom;
4685 Glut.setCursor Glut.CURSOR_INHERIT;
4686 state.mstate <- Mnone;
4689 let scrollx x =
4690 let winw = conf.winw - state.scrollw - 1 in
4691 let s = float x /. float winw in
4692 let destx = truncate (float (state.w + winw) *. s) in
4693 state.x <- winw - destx;
4694 gotoy_and_clear_text state.y;
4695 state.mstate <- Mscrollx;
4698 let scrolly y =
4699 let s = float y /. float conf.winh in
4700 let desty = truncate (float (state.maxy - conf.winh) *. s) in
4701 gotoy_and_clear_text desty;
4702 state.mstate <- Mscrolly;
4705 let viewmouse button bstate x y =
4706 match button with
4707 | Glut.OTHER_BUTTON n when (n == 3 || n == 4) && bstate = Glut.UP ->
4708 if Glut.getModifiers () land Glut.active_ctrl != 0
4709 then (
4710 match state.mstate with
4711 | Mzoom (oldn, i) ->
4712 if oldn = n
4713 then (
4714 if i = 2
4715 then
4716 let incr =
4717 match n with
4718 | 4 ->
4719 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
4720 | _ ->
4721 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
4723 let zoom = conf.zoom -. incr in
4724 setzoom zoom;
4725 state.mstate <- Mzoom (n, 0);
4726 else
4727 state.mstate <- Mzoom (n, i+1);
4729 else state.mstate <- Mzoom (n, 0)
4731 | _ -> state.mstate <- Mzoom (n, 0)
4733 else (
4734 match state.autoscroll with
4735 | Some step -> setautoscrollspeed step (n=4)
4736 | None ->
4737 let incr =
4738 if n = 3
4739 then -conf.scrollstep
4740 else conf.scrollstep
4742 let incr = incr * 2 in
4743 let y = clamp incr in
4744 gotoy_and_clear_text y
4747 | Glut.LEFT_BUTTON when Glut.getModifiers () land Glut.active_ctrl != 0 ->
4748 if bstate = Glut.DOWN
4749 then (
4750 Glut.setCursor Glut.CURSOR_CROSSHAIR;
4751 state.mstate <- Mpan (x, y)
4753 else
4754 state.mstate <- Mnone
4756 | Glut.RIGHT_BUTTON ->
4757 if bstate = Glut.DOWN
4758 then (
4759 Glut.setCursor Glut.CURSOR_CYCLE;
4760 let p = (x, y) in
4761 state.mstate <- Mzoomrect (p, p)
4763 else (
4764 match state.mstate with
4765 | Mzoomrect ((x0, y0), _) ->
4766 if abs (x-x0) > 10 && abs (y - y0) > 10
4767 then zoomrect x0 y0 x y
4768 else (
4769 state.mstate <- Mnone;
4770 Glut.setCursor Glut.CURSOR_INHERIT;
4771 G.postRedisplay "kill accidental zoom rect";
4773 | _ ->
4774 Glut.setCursor Glut.CURSOR_INHERIT;
4775 state.mstate <- Mnone
4778 | Glut.LEFT_BUTTON when x > conf.winw - state.scrollw ->
4779 if bstate = Glut.DOWN
4780 then
4781 let _, position, sh = state.uioh#scrollph in
4782 if y > truncate position && y < truncate (position +. sh)
4783 then state.mstate <- Mscrolly
4784 else scrolly y
4785 else
4786 state.mstate <- Mnone
4788 | Glut.LEFT_BUTTON when y > conf.winh - state.hscrollh ->
4789 if bstate = Glut.DOWN
4790 then
4791 let _, position, sw = state.uioh#scrollpw in
4792 if x > truncate position && x < truncate (position +. sw)
4793 then state.mstate <- Mscrollx
4794 else scrollx x
4795 else
4796 state.mstate <- Mnone
4798 | Glut.LEFT_BUTTON ->
4799 let dest = if bstate = Glut.DOWN then getunder x y else Unone in
4800 begin match dest with
4801 | Ulinkgoto (pageno, top) ->
4802 if pageno >= 0
4803 then (
4804 addnav ();
4805 gotopage1 pageno top;
4808 | Ulinkuri s ->
4809 gotouri s
4811 | Unone when bstate = Glut.DOWN ->
4812 Glut.setCursor Glut.CURSOR_CROSSHAIR;
4813 state.mstate <- Mpan (x, y);
4815 | Unone | Utext _ ->
4816 if bstate = Glut.DOWN
4817 then (
4818 if conf.angle mod 360 = 0
4819 then (
4820 state.mstate <- Msel ((x, y), (x, y));
4821 G.postRedisplay "mouse select";
4824 else (
4825 match state.mstate with
4826 | Mnone -> ()
4828 | Mzoom _ | Mscrollx | Mscrolly ->
4829 state.mstate <- Mnone
4831 | Mzoomrect ((x0, y0), _) ->
4832 zoomrect x0 y0 x y
4834 | Mpan _ ->
4835 Glut.setCursor Glut.CURSOR_INHERIT;
4836 state.mstate <- Mnone
4838 | Msel ((_, y0), (_, y1)) ->
4839 let f l =
4840 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
4841 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
4842 then
4843 match getopaque l.pageno with
4844 | Some opaque ->
4845 copysel opaque
4846 | _ -> ()
4848 List.iter f state.layout;
4849 copysel ""; (* ugly *)
4850 Glut.setCursor Glut.CURSOR_INHERIT;
4851 state.mstate <- Mnone;
4855 | _ -> ()
4858 let birdseyemouse button bstate x y
4859 (conf, leftx, _, hooverpageno, anchor) =
4860 match button with
4861 | Glut.LEFT_BUTTON when bstate = Glut.UP ->
4862 let rec loop = function
4863 | [] -> ()
4864 | l :: rest ->
4865 if y > l.pagedispy && y < l.pagedispy + l.pagevh
4866 && x > l.pagedispx && x < l.pagedispx + l.pagevw
4867 then (
4868 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
4870 else loop rest
4872 loop state.layout
4873 | Glut.OTHER_BUTTON _ -> viewmouse button bstate x y
4874 | _ -> ()
4877 let mouse bstate button x y =
4878 state.uioh <- state.uioh#button button bstate x y;
4881 let mouse ~button ~state ~x ~y = mouse state button x y;;
4883 let motion ~x ~y =
4884 state.uioh <- state.uioh#motion x y
4887 let pmotion ~x ~y =
4888 state.uioh <- state.uioh#pmotion x y;
4891 let uioh = object
4892 method display = ()
4894 method key key =
4895 begin match state.mode with
4896 | Textentry textentry -> textentrykeyboard key textentry
4897 | Birdseye birdseye -> birdseyekeyboard key birdseye
4898 | View -> viewkeyboard key
4899 end;
4900 state.uioh
4902 method special key =
4903 begin match state.mode with
4904 | View | (Birdseye _) when key = Glut.KEY_F9 ->
4905 togglebirdseye ()
4907 | Birdseye vals ->
4908 birdseyespecial key vals
4910 | View when key = Glut.KEY_F1 ->
4911 enterhelpmode ()
4913 | View ->
4914 begin match state.autoscroll with
4915 | Some step when key = Glut.KEY_DOWN || key = Glut.KEY_UP ->
4916 setautoscrollspeed step (key = Glut.KEY_DOWN)
4918 | _ ->
4919 let y =
4920 match key with
4921 | Glut.KEY_F3 -> search state.searchpattern true; state.y
4922 | Glut.KEY_UP ->
4923 if Glut.getModifiers () land Glut.active_ctrl != 0
4924 then
4925 if Glut.getModifiers () land Glut.active_shift != 0
4926 then (setzoom state.prevzoom; state.y)
4927 else clamp (-conf.winh/2)
4928 else clamp (-conf.scrollstep)
4929 | Glut.KEY_DOWN ->
4930 if Glut.getModifiers () land Glut.active_ctrl != 0
4931 then
4932 if Glut.getModifiers () land Glut.active_shift != 0
4933 then (setzoom state.prevzoom; state.y)
4934 else clamp (conf.winh/2)
4935 else clamp (conf.scrollstep)
4936 | Glut.KEY_PAGE_UP ->
4937 if Glut.getModifiers () land Glut.active_ctrl != 0
4938 then
4939 match state.layout with
4940 | [] -> state.y
4941 | l :: _ -> state.y - l.pagey
4942 else
4943 clamp (-conf.winh)
4944 | Glut.KEY_PAGE_DOWN ->
4945 if Glut.getModifiers () land Glut.active_ctrl != 0
4946 then
4947 match List.rev state.layout with
4948 | [] -> state.y
4949 | l :: _ -> getpagey l.pageno
4950 else
4951 clamp conf.winh
4952 | Glut.KEY_HOME ->
4953 addnav ();
4955 | Glut.KEY_END ->
4956 addnav ();
4957 state.maxy - (if conf.maxhfit then conf.winh else 0)
4959 | (Glut.KEY_RIGHT | Glut.KEY_LEFT) when
4960 Glut.getModifiers () land Glut.active_alt != 0 ->
4961 getnav (if key = Glut.KEY_LEFT then 1 else -1)
4963 | Glut.KEY_RIGHT when conf.zoom > 1.0 ->
4964 let dx =
4965 if Glut.getModifiers () land Glut.active_ctrl != 0
4966 then (conf.winw / 2)
4967 else 10
4969 state.x <- state.x - dx;
4970 state.y
4971 | Glut.KEY_LEFT when conf.zoom > 1.0 ->
4972 let dx =
4973 if Glut.getModifiers () land Glut.active_ctrl != 0
4974 then (conf.winw / 2)
4975 else 10
4977 state.x <- state.x + dx;
4978 state.y
4980 | _ -> state.y
4982 if abs (state.y - y) > conf.scrollstep*2
4983 then gotoghyll y
4984 else gotoy_and_clear_text y
4987 | Textentry te -> textentryspecial key te
4988 end;
4989 state.uioh
4991 method button button bstate x y =
4992 begin match state.mode with
4993 | View -> viewmouse button bstate x y
4994 | Birdseye beye -> birdseyemouse button bstate x y beye
4995 | Textentry _ -> ()
4996 end;
4997 state.uioh
4999 method motion x y =
5000 begin match state.mode with
5001 | Textentry _ -> ()
5002 | View | Birdseye _ ->
5003 match state.mstate with
5004 | Mzoom _ | Mnone -> ()
5006 | Mpan (x0, y0) ->
5007 let dx = x - x0
5008 and dy = y0 - y in
5009 state.mstate <- Mpan (x, y);
5010 if conf.zoom > 1.0 then state.x <- state.x + dx;
5011 let y = clamp dy in
5012 gotoy_and_clear_text y
5014 | Msel (a, _) ->
5015 state.mstate <- Msel (a, (x, y));
5016 G.postRedisplay "motion select";
5018 | Mscrolly ->
5019 let y = min conf.winh (max 0 y) in
5020 scrolly y
5022 | Mscrollx ->
5023 let x = min conf.winw (max 0 x) in
5024 scrollx x
5026 | Mzoomrect (p0, _) ->
5027 state.mstate <- Mzoomrect (p0, (x, y));
5028 G.postRedisplay "motion zoomrect";
5029 end;
5030 state.uioh
5032 method pmotion x y =
5033 begin match state.mode with
5034 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
5035 let rec loop = function
5036 | [] ->
5037 if hooverpageno != -1
5038 then (
5039 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
5040 G.postRedisplay "pmotion birdseye no hoover";
5042 | l :: rest ->
5043 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5044 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5045 then (
5046 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
5047 G.postRedisplay "pmotion birdseye hoover";
5049 else loop rest
5051 loop state.layout
5053 | Textentry _ -> ()
5055 | View ->
5056 match state.mstate with
5057 | Mnone ->
5058 begin match getunder x y with
5059 | Unone -> Glut.setCursor Glut.CURSOR_INHERIT
5060 | Ulinkuri uri ->
5061 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
5062 Glut.setCursor Glut.CURSOR_INFO
5063 | Ulinkgoto (page, _) ->
5064 if conf.underinfo
5065 then showtext 'p' ("age: " ^ string_of_int (page+1));
5066 Glut.setCursor Glut.CURSOR_INFO
5067 | Utext s ->
5068 if conf.underinfo then showtext 'f' ("ont: " ^ s);
5069 Glut.setCursor Glut.CURSOR_TEXT
5072 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
5074 end;
5075 state.uioh
5077 method infochanged _ = ()
5079 method scrollph =
5080 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
5081 let p, h = scrollph state.y maxy in
5082 state.scrollw, p, h
5084 method scrollpw =
5085 let winw = conf.winw - state.scrollw - 1 in
5086 let fwinw = float winw in
5087 let sw =
5088 let sw = fwinw /. float state.w in
5089 let sw = fwinw *. sw in
5090 max sw (float conf.scrollh)
5092 let position, sw =
5093 let f = state.w+winw in
5094 let r = float (winw-state.x) /. float f in
5095 let p = fwinw *. r in
5096 p-.sw/.2., sw
5098 let sw =
5099 if position +. sw > fwinw
5100 then fwinw -. position
5101 else sw
5103 state.hscrollh, position, sw
5104 end;;
5106 module Config =
5107 struct
5108 open Parser
5110 let fontpath = ref "";;
5111 let wmclasshack = ref false;;
5113 let unent s =
5114 let l = String.length s in
5115 let b = Buffer.create l in
5116 unent b s 0 l;
5117 Buffer.contents b;
5120 let home =
5122 match platform with
5123 | Pwindows | Pmingw -> Sys.getenv "HOMEPATH"
5124 | _ -> Sys.getenv "HOME"
5125 with exn ->
5126 prerr_endline
5127 ("Can not determine home directory location: " ^
5128 Printexc.to_string exn);
5132 let config_of c attrs =
5133 let apply c k v =
5135 match k with
5136 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
5137 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
5138 | "case-insensitive-search" -> { c with icase = bool_of_string v }
5139 | "preload" -> { c with preload = bool_of_string v }
5140 | "page-bias" -> { c with pagebias = int_of_string v }
5141 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
5142 | "auto-scroll-step" ->
5143 { c with autoscrollstep = max 0 (int_of_string v) }
5144 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
5145 | "crop-hack" -> { c with crophack = bool_of_string v }
5146 | "throttle" ->
5147 let mw =
5148 match String.lowercase v with
5149 | "true" -> Some infinity
5150 | "false" -> None
5151 | f -> Some (float_of_string f)
5153 { c with maxwait = mw}
5154 | "highlight-links" -> { c with hlinks = bool_of_string v }
5155 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
5156 | "vertical-margin" ->
5157 { c with interpagespace = max 0 (int_of_string v) }
5158 | "zoom" ->
5159 let zoom = float_of_string v /. 100. in
5160 let zoom = max zoom 0.0 in
5161 { c with zoom = zoom }
5162 | "presentation" -> { c with presentation = bool_of_string v }
5163 | "rotation-angle" -> { c with angle = int_of_string v }
5164 | "width" -> { c with winw = max 20 (int_of_string v) }
5165 | "height" -> { c with winh = max 20 (int_of_string v) }
5166 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
5167 | "proportional-display" -> { c with proportional = bool_of_string v }
5168 | "pixmap-cache-size" ->
5169 { c with memlimit = max 2 (int_of_string_with_suffix v) }
5170 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
5171 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
5172 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
5173 | "persistent-location" -> { c with jumpback = bool_of_string v }
5174 | "background-color" -> { c with bgcolor = color_of_string v }
5175 | "scrollbar-in-presentation" ->
5176 { c with scrollbarinpm = bool_of_string v }
5177 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
5178 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
5179 | "mupdf-store-size" ->
5180 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
5181 | "checkers" -> { c with checkers = bool_of_string v }
5182 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
5183 | "trim-margins" -> { c with trimmargins = bool_of_string v }
5184 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
5185 | "wmclass-hack" -> wmclasshack := bool_of_string v; c
5186 | "uri-launcher" -> { c with urilauncher = unent v }
5187 | "color-space" -> { c with colorspace = colorspace_of_string v }
5188 | "invert-colors" -> { c with invert = bool_of_string v }
5189 | "brightness" -> { c with colorscale = float_of_string v }
5190 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
5191 | "ghyllscroll" ->
5192 { c with ghyllscroll = Some (ghyllscroll_of_string v) }
5193 | "columns" ->
5194 let nab = columns_of_string v in
5195 { c with columns = Some (nab, [||]) }
5196 | "birds-eye-columns" ->
5197 { c with beyecolumns = Some (max (int_of_string v) 2) }
5198 | _ -> c
5199 with exn ->
5200 prerr_endline ("Error processing attribute (`" ^
5201 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
5204 let rec fold c = function
5205 | [] -> c
5206 | (k, v) :: rest ->
5207 let c = apply c k v in
5208 fold c rest
5210 fold c attrs;
5213 let fromstring f pos n v d =
5214 try f v
5215 with exn ->
5216 dolog "Error processing attribute (%S=%S) at %d\n%s"
5217 n v pos (Printexc.to_string exn)
5222 let bookmark_of attrs =
5223 let rec fold title page rely = function
5224 | ("title", v) :: rest -> fold v page rely rest
5225 | ("page", v) :: rest -> fold title v rely rest
5226 | ("rely", v) :: rest -> fold title page v rest
5227 | _ :: rest -> fold title page rely rest
5228 | [] -> title, page, rely
5230 fold "invalid" "0" "0" attrs
5233 let doc_of attrs =
5234 let rec fold path page rely pan = function
5235 | ("path", v) :: rest -> fold v page rely pan rest
5236 | ("page", v) :: rest -> fold path v rely pan rest
5237 | ("rely", v) :: rest -> fold path page v pan rest
5238 | ("pan", v) :: rest -> fold path page rely v rest
5239 | _ :: rest -> fold path page rely pan rest
5240 | [] -> path, page, rely, pan
5242 fold "" "0" "0" "0" attrs
5245 let setconf dst src =
5246 dst.scrollbw <- src.scrollbw;
5247 dst.scrollh <- src.scrollh;
5248 dst.icase <- src.icase;
5249 dst.preload <- src.preload;
5250 dst.pagebias <- src.pagebias;
5251 dst.verbose <- src.verbose;
5252 dst.scrollstep <- src.scrollstep;
5253 dst.maxhfit <- src.maxhfit;
5254 dst.crophack <- src.crophack;
5255 dst.autoscrollstep <- src.autoscrollstep;
5256 dst.maxwait <- src.maxwait;
5257 dst.hlinks <- src.hlinks;
5258 dst.underinfo <- src.underinfo;
5259 dst.interpagespace <- src.interpagespace;
5260 dst.zoom <- src.zoom;
5261 dst.presentation <- src.presentation;
5262 dst.angle <- src.angle;
5263 dst.winw <- src.winw;
5264 dst.winh <- src.winh;
5265 dst.savebmarks <- src.savebmarks;
5266 dst.memlimit <- src.memlimit;
5267 dst.proportional <- src.proportional;
5268 dst.texcount <- src.texcount;
5269 dst.sliceheight <- src.sliceheight;
5270 dst.thumbw <- src.thumbw;
5271 dst.jumpback <- src.jumpback;
5272 dst.bgcolor <- src.bgcolor;
5273 dst.scrollbarinpm <- src.scrollbarinpm;
5274 dst.tilew <- src.tilew;
5275 dst.tileh <- src.tileh;
5276 dst.mustoresize <- src.mustoresize;
5277 dst.checkers <- src.checkers;
5278 dst.aalevel <- src.aalevel;
5279 dst.trimmargins <- src.trimmargins;
5280 dst.trimfuzz <- src.trimfuzz;
5281 dst.urilauncher <- src.urilauncher;
5282 dst.colorspace <- src.colorspace;
5283 dst.invert <- src.invert;
5284 dst.colorscale <- src.colorscale;
5285 dst.redirectstderr <- src.redirectstderr;
5286 dst.ghyllscroll <- src.ghyllscroll;
5287 dst.columns <- src.columns;
5288 dst.beyecolumns <- src.beyecolumns;
5291 let get s =
5292 let h = Hashtbl.create 10 in
5293 let dc = { defconf with angle = defconf.angle } in
5294 let rec toplevel v t spos _ =
5295 match t with
5296 | Vdata | Vcdata | Vend -> v
5297 | Vopen ("llppconfig", _, closed) ->
5298 if closed
5299 then v
5300 else { v with f = llppconfig }
5301 | Vopen _ ->
5302 error "unexpected subelement at top level" s spos
5303 | Vclose _ -> error "unexpected close at top level" s spos
5305 and llppconfig v t spos _ =
5306 match t with
5307 | Vdata | Vcdata -> v
5308 | Vend -> error "unexpected end of input in llppconfig" s spos
5309 | Vopen ("defaults", attrs, closed) ->
5310 let c = config_of dc attrs in
5311 setconf dc c;
5312 if closed
5313 then v
5314 else { v with f = skip "defaults" (fun () -> v) }
5316 | Vopen ("ui-font", attrs, closed) ->
5317 let rec getsize size = function
5318 | [] -> size
5319 | ("size", v) :: rest ->
5320 let size =
5321 fromstring int_of_string spos "size" v fstate.fontsize in
5322 getsize size rest
5323 | l -> getsize size l
5325 fstate.fontsize <- getsize fstate.fontsize attrs;
5326 if closed
5327 then v
5328 else { v with f = uifont (Buffer.create 10) }
5330 | Vopen ("doc", attrs, closed) ->
5331 let pathent, spage, srely, span = doc_of attrs in
5332 let path = unent pathent
5333 and pageno = fromstring int_of_string spos "page" spage 0
5334 and rely = fromstring float_of_string spos "rely" srely 0.0
5335 and pan = fromstring int_of_string spos "pan" span 0 in
5336 let c = config_of dc attrs in
5337 let anchor = (pageno, rely) in
5338 if closed
5339 then (Hashtbl.add h path (c, [], pan, anchor); v)
5340 else { v with f = doc path pan anchor c [] }
5342 | Vopen _ ->
5343 error "unexpected subelement in llppconfig" s spos
5345 | Vclose "llppconfig" -> { v with f = toplevel }
5346 | Vclose _ -> error "unexpected close in llppconfig" s spos
5348 and uifont b v t spos epos =
5349 match t with
5350 | Vdata | Vcdata ->
5351 Buffer.add_substring b s spos (epos - spos);
5353 | Vopen (_, _, _) ->
5354 error "unexpected subelement in ui-font" s spos
5355 | Vclose "ui-font" ->
5356 if String.length !fontpath = 0
5357 then fontpath := Buffer.contents b;
5358 { v with f = llppconfig }
5359 | Vclose _ -> error "unexpected close in ui-font" s spos
5360 | Vend -> error "unexpected end of input in ui-font" s spos
5362 and doc path pan anchor c bookmarks v t spos _ =
5363 match t with
5364 | Vdata | Vcdata -> v
5365 | Vend -> error "unexpected end of input in doc" s spos
5366 | Vopen ("bookmarks", _, closed) ->
5367 if closed
5368 then v
5369 else { v with f = pbookmarks path pan anchor c bookmarks }
5371 | Vopen (_, _, _) ->
5372 error "unexpected subelement in doc" s spos
5374 | Vclose "doc" ->
5375 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
5376 { v with f = llppconfig }
5378 | Vclose _ -> error "unexpected close in doc" s spos
5380 and pbookmarks path pan anchor c bookmarks v t spos _ =
5381 match t with
5382 | Vdata | Vcdata -> v
5383 | Vend -> error "unexpected end of input in bookmarks" s spos
5384 | Vopen ("item", attrs, closed) ->
5385 let titleent, spage, srely = bookmark_of attrs in
5386 let page = fromstring int_of_string spos "page" spage 0
5387 and rely = fromstring float_of_string spos "rely" srely 0.0 in
5388 let bookmarks = (unent titleent, 0, (page, rely)) :: bookmarks in
5389 if closed
5390 then { v with f = pbookmarks path pan anchor c bookmarks }
5391 else
5392 let f () = v in
5393 { v with f = skip "item" f }
5395 | Vopen _ ->
5396 error "unexpected subelement in bookmarks" s spos
5398 | Vclose "bookmarks" ->
5399 { v with f = doc path pan anchor c bookmarks }
5401 | Vclose _ -> error "unexpected close in bookmarks" s spos
5403 and skip tag f v t spos _ =
5404 match t with
5405 | Vdata | Vcdata -> v
5406 | Vend ->
5407 error ("unexpected end of input in skipped " ^ tag) s spos
5408 | Vopen (tag', _, closed) ->
5409 if closed
5410 then v
5411 else
5412 let f' () = { v with f = skip tag f } in
5413 { v with f = skip tag' f' }
5414 | Vclose ctag ->
5415 if tag = ctag
5416 then f ()
5417 else error ("unexpected close in skipped " ^ tag) s spos
5420 parse { f = toplevel; accu = () } s;
5421 h, dc;
5424 let do_load f ic =
5426 let len = in_channel_length ic in
5427 let s = String.create len in
5428 really_input ic s 0 len;
5429 f s;
5430 with
5431 | Parse_error (msg, s, pos) ->
5432 let subs = subs s pos in
5433 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
5434 failwith ("parse error: " ^ s)
5436 | exn ->
5437 failwith ("config load error: " ^ Printexc.to_string exn)
5440 let defconfpath =
5441 let dir =
5443 let dir = Filename.concat home ".config" in
5444 if Sys.is_directory dir then dir else home
5445 with _ -> home
5447 Filename.concat dir "llpp.conf"
5450 let confpath = ref defconfpath;;
5452 let load1 f =
5453 if Sys.file_exists !confpath
5454 then
5455 match
5456 (try Some (open_in_bin !confpath)
5457 with exn ->
5458 prerr_endline
5459 ("Error opening configuation file `" ^ !confpath ^ "': " ^
5460 Printexc.to_string exn);
5461 None
5463 with
5464 | Some ic ->
5465 begin try
5466 f (do_load get ic)
5467 with exn ->
5468 prerr_endline
5469 ("Error loading configuation from `" ^ !confpath ^ "': " ^
5470 Printexc.to_string exn);
5471 end;
5472 close_in ic;
5474 | None -> ()
5475 else
5476 f (Hashtbl.create 0, defconf)
5479 let load () =
5480 let f (h, dc) =
5481 let pc, pb, px, pa =
5483 Hashtbl.find h (Filename.basename state.path)
5484 with Not_found -> dc, [], 0, (0, 0.0)
5486 setconf defconf dc;
5487 setconf conf pc;
5488 state.bookmarks <- pb;
5489 state.x <- px;
5490 state.scrollw <- conf.scrollbw;
5491 if conf.jumpback
5492 then state.anchor <- pa;
5493 cbput state.hists.nav pa;
5495 load1 f
5498 let add_attrs bb always dc c =
5499 let ob s a b =
5500 if always || a != b
5501 then Printf.bprintf bb "\n %s='%b'" s a
5502 and oi s a b =
5503 if always || a != b
5504 then Printf.bprintf bb "\n %s='%d'" s a
5505 and oI s a b =
5506 if always || a != b
5507 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
5508 and oz s a b =
5509 if always || a <> b
5510 then Printf.bprintf bb "\n %s='%d'" s (truncate (a*.100.))
5511 and oF s a b =
5512 if always || a <> b
5513 then Printf.bprintf bb "\n %s='%f'" s a
5514 and oc s a b =
5515 if always || a <> b
5516 then
5517 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
5518 and oC s a b =
5519 if always || a <> b
5520 then
5521 Printf.bprintf bb "\n %s='%s'" s (colorspace_to_string a)
5522 and oR s a b =
5523 if always || a <> b
5524 then
5525 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
5526 and os s a b =
5527 if always || a <> b
5528 then
5529 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
5530 and og s a b =
5531 if always || a <> b
5532 then
5533 match a with
5534 | None -> ()
5535 | Some (_N, _A, _B) ->
5536 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
5537 and oW s a b =
5538 if always || a <> b
5539 then
5540 let v =
5541 match a with
5542 | None -> "false"
5543 | Some f ->
5544 if f = infinity
5545 then "true"
5546 else string_of_float f
5548 Printf.bprintf bb "\n %s='%s'" s v
5549 and oco s a b =
5550 if always || a <> b
5551 then
5552 match a with
5553 | Some ((n, a, b), _) when n > 1 ->
5554 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
5555 | _ -> ()
5556 and obeco s a b =
5557 if always || a <> b
5558 then
5559 match a with
5560 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
5561 | _ -> ()
5563 let w, h =
5564 if always
5565 then dc.winw, dc.winh
5566 else
5567 match state.fullscreen with
5568 | Some wh -> wh
5569 | None -> c.winw, c.winh
5571 let zoom, presentation, interpagespace, maxwait =
5572 if always
5573 then dc.zoom, dc.presentation, dc.interpagespace, dc.maxwait
5574 else
5575 match state.mode with
5576 | Birdseye (bc, _, _, _, _) ->
5577 bc.zoom, bc.presentation, bc.interpagespace, bc.maxwait
5578 | _ -> c.zoom, c.presentation, c.interpagespace, c.maxwait
5580 oi "width" w dc.winw;
5581 oi "height" h dc.winh;
5582 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
5583 oi "scroll-handle-height" c.scrollh dc.scrollh;
5584 ob "case-insensitive-search" c.icase dc.icase;
5585 ob "preload" c.preload dc.preload;
5586 oi "page-bias" c.pagebias dc.pagebias;
5587 oi "scroll-step" c.scrollstep dc.scrollstep;
5588 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
5589 ob "max-height-fit" c.maxhfit dc.maxhfit;
5590 ob "crop-hack" c.crophack dc.crophack;
5591 oW "throttle" maxwait dc.maxwait;
5592 ob "highlight-links" c.hlinks dc.hlinks;
5593 ob "under-cursor-info" c.underinfo dc.underinfo;
5594 oi "vertical-margin" interpagespace dc.interpagespace;
5595 oz "zoom" zoom dc.zoom;
5596 ob "presentation" presentation dc.presentation;
5597 oi "rotation-angle" c.angle dc.angle;
5598 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
5599 ob "proportional-display" c.proportional dc.proportional;
5600 oI "pixmap-cache-size" c.memlimit dc.memlimit;
5601 oi "tex-count" c.texcount dc.texcount;
5602 oi "slice-height" c.sliceheight dc.sliceheight;
5603 oi "thumbnail-width" c.thumbw dc.thumbw;
5604 ob "persistent-location" c.jumpback dc.jumpback;
5605 oc "background-color" c.bgcolor dc.bgcolor;
5606 ob "scrollbar-in-presentation" c.scrollbarinpm dc.scrollbarinpm;
5607 oi "tile-width" c.tilew dc.tilew;
5608 oi "tile-height" c.tileh dc.tileh;
5609 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
5610 ob "checkers" c.checkers dc.checkers;
5611 oi "aalevel" c.aalevel dc.aalevel;
5612 ob "trim-margins" c.trimmargins dc.trimmargins;
5613 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
5614 os "uri-launcher" c.urilauncher dc.urilauncher;
5615 oC "color-space" c.colorspace dc.colorspace;
5616 ob "invert-colors" c.invert dc.invert;
5617 oF "brightness" c.colorscale dc.colorscale;
5618 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
5619 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
5620 oco "columns" c.columns dc.columns;
5621 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
5622 if always
5623 then ob "wmclass-hack" !wmclasshack false;
5626 let save () =
5627 let uifontsize = fstate.fontsize in
5628 let bb = Buffer.create 32768 in
5629 let f (h, dc) =
5630 let dc = if conf.bedefault then conf else dc in
5631 Buffer.add_string bb "<llppconfig>\n";
5633 if String.length !fontpath > 0
5634 then
5635 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
5636 uifontsize
5637 !fontpath
5638 else (
5639 if uifontsize <> 14
5640 then
5641 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
5644 Buffer.add_string bb "<defaults ";
5645 add_attrs bb true dc dc;
5646 Buffer.add_string bb "/>\n";
5648 let adddoc path pan anchor c bookmarks =
5649 if bookmarks == [] && c = dc && anchor = emptyanchor
5650 then ()
5651 else (
5652 Printf.bprintf bb "<doc path='%s'"
5653 (enent path 0 (String.length path));
5655 if anchor <> emptyanchor
5656 then (
5657 let n, y = anchor in
5658 Printf.bprintf bb " page='%d'" n;
5659 if y > 1e-6
5660 then
5661 Printf.bprintf bb " rely='%f'" y
5665 if pan != 0
5666 then Printf.bprintf bb " pan='%d'" pan;
5668 add_attrs bb false dc c;
5670 begin match bookmarks with
5671 | [] -> Buffer.add_string bb "/>\n"
5672 | _ ->
5673 Buffer.add_string bb ">\n<bookmarks>\n";
5674 List.iter (fun (title, _level, (page, rely)) ->
5675 Printf.bprintf bb
5676 "<item title='%s' page='%d'"
5677 (enent title 0 (String.length title))
5678 page
5680 if rely > 1e-6
5681 then
5682 Printf.bprintf bb " rely='%f'" rely
5684 Buffer.add_string bb "/>\n";
5685 ) bookmarks;
5686 Buffer.add_string bb "</bookmarks>\n</doc>\n";
5687 end;
5691 let pan =
5692 match state.mode with
5693 | Birdseye (_, pan, _, _, _) -> pan
5694 | _ -> state.x
5696 let basename = Filename.basename state.path in
5697 adddoc basename pan (getanchor ())
5698 { conf with
5699 autoscrollstep =
5700 match state.autoscroll with
5701 | Some step -> step
5702 | None -> conf.autoscrollstep }
5703 (if conf.savebmarks then state.bookmarks else []);
5705 Hashtbl.iter (fun path (c, bookmarks, x, y) ->
5706 if basename <> path
5707 then adddoc path x y c bookmarks
5708 ) h;
5709 Buffer.add_string bb "</llppconfig>";
5711 load1 f;
5712 if Buffer.length bb > 0
5713 then
5715 let tmp = !confpath ^ ".tmp" in
5716 let oc = open_out_bin tmp in
5717 Buffer.output_buffer oc bb;
5718 close_out oc;
5719 Unix.rename tmp !confpath;
5720 with exn ->
5721 prerr_endline
5722 ("error while saving configuration: " ^ Printexc.to_string exn)
5724 end;;
5726 let () =
5727 Arg.parse
5728 (Arg.align
5729 [("-p", Arg.String (fun s -> state.password <- s) ,
5730 "<password> Set password");
5732 ("-f", Arg.String (fun s -> Config.fontpath := s),
5733 "<path> Set path to the user interface font");
5735 ("-c", Arg.String (fun s -> Config.confpath := s),
5736 "<path> Set path to the configuration file");
5738 ("-v", Arg.Unit (fun () ->
5739 Printf.printf
5740 "%s\nconfiguration path: %s\n"
5741 (version ())
5742 Config.defconfpath
5744 exit 0), " Print version and exit");
5747 (fun s -> state.path <- s)
5748 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
5750 if String.length state.path = 0
5751 then (prerr_endline "file name missing"; exit 1);
5753 Config.load ();
5755 let _ = Glut.init Sys.argv in
5756 let () = Glut.initDisplayMode ~depth:false ~double_buffer:true () in
5757 let () = Glut.initWindowSize conf.winw conf.winh in
5758 let _ = Glut.createWindow ("llpp " ^ Filename.basename state.path) in
5760 if not (Glut.extensionSupported "GL_ARB_texture_rectangle"
5761 || Glut.extensionSupported "GL_EXT_texture_rectangle")
5762 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
5764 let csock, ssock =
5765 if not is_windows
5766 then
5767 Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0
5768 else
5769 let addr = Unix.ADDR_INET (Unix.inet_addr_loopback, 1337) in
5770 let sock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
5771 Unix.setsockopt sock Unix.SO_REUSEADDR true;
5772 Unix.bind sock addr;
5773 Unix.listen sock 1;
5774 let csock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
5775 Unix.connect csock addr;
5776 let ssock, _ = Unix.accept sock in
5777 Unix.close sock;
5778 let opts sock =
5779 Unix.setsockopt sock Unix.TCP_NODELAY true;
5780 Unix.setsockopt_optint sock Unix.SO_LINGER None;
5782 opts ssock;
5783 opts csock;
5784 ssock, csock
5787 let () = Glut.displayFunc display in
5788 let () = Glut.reshapeFunc reshape in
5789 let () = Glut.keyboardFunc keyboard in
5790 let () = Glut.specialFunc special in
5791 let () = Glut.idleFunc (Some idle) in
5792 let () = Glut.mouseFunc mouse in
5793 let () = Glut.motionFunc motion in
5794 let () = Glut.passiveMotionFunc pmotion in
5796 setcheckers conf.checkers;
5797 init ssock (
5798 conf.angle, conf.proportional, (conf.trimmargins, conf.trimfuzz),
5799 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
5800 !Config.wmclasshack, !Config.fontpath
5802 state.csock <- csock;
5803 state.ssock <- ssock;
5804 state.text <- "Opening " ^ state.path;
5805 setaalevel conf.aalevel;
5806 writeopen state.path state.password;
5807 state.uioh <- uioh;
5808 setfontsize fstate.fontsize;
5810 redirectstderr ();
5812 while true do
5814 Glut.mainLoop ();
5815 with
5816 | Glut.BadEnum "key in special_of_int" ->
5817 showtext '!' " LablGlut bug: special key not recognized";
5819 | Quit ->
5820 wcmd "quit" [];
5821 Config.save ();
5822 exit 0
5824 | exn when conf.redirectstderr ->
5825 let s =
5826 Printf.sprintf "exception %s\n%s"
5827 (Printexc.to_string exn)
5828 (Printexc.get_backtrace ())
5830 ignore (try
5831 Unix.single_write state.stderr s 0 (String.length s);
5832 with _ -> 0);
5833 exit 1
5834 done;