Support cover pages
[llpp.git] / main.ml
blob0c9e0e741d143a416d2afbb2b59fd64d3d55742b
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 writecmd fd s =
650 let len = String.length s in
651 let n = 4 + len in
652 let b = Buffer.create n in
653 Buffer.add_char b (Char.chr ((len lsr 24) land 0xff));
654 Buffer.add_char b (Char.chr ((len lsr 16) land 0xff));
655 Buffer.add_char b (Char.chr ((len lsr 8) land 0xff));
656 Buffer.add_char b (Char.chr ((len lsr 0) land 0xff));
657 Buffer.add_string b s;
658 let s' = Buffer.contents b in
659 let n' = Unix.write fd s' 0 n in
660 if n' != n then failwith "write failed";
663 let readcmd fd =
664 let s = "xxxx" in
665 let n = Unix.read fd s 0 4 in
666 if n != 4 then failwith "incomplete read(len)";
667 let len = 0
668 lor (Char.code s.[0] lsl 24)
669 lor (Char.code s.[1] lsl 16)
670 lor (Char.code s.[2] lsl 8)
671 lor (Char.code s.[3] lsl 0)
673 let s = String.create len in
674 let n = Unix.read fd s 0 len in
675 if n != len then failwith "incomplete read(data)";
679 let makecmd s l =
680 let b = Buffer.create 10 in
681 Buffer.add_string b s;
682 let rec combine = function
683 | [] -> b
684 | x :: xs ->
685 Buffer.add_char b ' ';
686 let s =
687 match x with
688 | `b b -> if b then "1" else "0"
689 | `s s -> s
690 | `i i -> string_of_int i
691 | `f f -> string_of_float f
692 | `I f -> string_of_int (truncate f)
694 Buffer.add_string b s;
695 combine xs;
697 combine l;
700 let wcmd s l =
701 let cmd = Buffer.contents (makecmd s l) in
702 writecmd state.csock cmd;
705 let calcips h =
706 if conf.presentation
707 then
708 let d = conf.winh - h in
709 max 0 ((d + 1) / 2)
710 else
711 conf.interpagespace
714 let calcheight () =
715 let rec f pn ph pi fh l =
716 match l with
717 | (n, _, h, _) :: rest ->
718 let ips = calcips h in
719 let fh =
720 if conf.presentation
721 then fh+ips
722 else (
723 if isbirdseye state.mode && pn = 0
724 then fh + ips
725 else fh
728 let fh = fh + ((n - pn) * (ph + pi)) in
729 f n h ips fh rest;
731 | [] ->
732 let inc =
733 if conf.presentation || (isbirdseye state.mode && pn = 0)
734 then 0
735 else -pi
737 let fh = fh + ((state.pagecount - pn) * (ph + pi)) + inc in
738 max 0 fh
740 let fh = f 0 0 0 0 state.pdims in
744 let calcheight () =
745 match conf.columns with
746 | None -> calcheight ()
747 | Some (_, b) ->
748 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
749 y + h
752 let getpageyh pageno =
753 let rec f pn ph pi y l =
754 match l with
755 | (n, _, h, _) :: rest ->
756 let ips = calcips h in
757 if n >= pageno
758 then
759 let h = if n = pageno then h else ph in
760 if conf.presentation && n = pageno
761 then
762 y + (pageno - pn) * (ph + pi) + pi, h
763 else
764 y + (pageno - pn) * (ph + pi), h
765 else
766 let y = y + (if conf.presentation then pi else 0) in
767 let y = y + (n - pn) * (ph + pi) in
768 f n h ips y rest
770 | [] ->
771 y + (pageno - pn) * (ph + pi), ph
773 f 0 0 0 0 state.pdims
776 let getpageyh pageno =
777 match conf.columns with
778 | None -> getpageyh pageno
779 | Some (_, b) ->
780 let (_, _, y, (_, _, h, _)) = b.(pageno) in
781 y, h
784 let getpagedim pageno =
785 let rec f ppdim l =
786 match l with
787 | (n, _, _, _) as pdim :: rest ->
788 if n >= pageno
789 then (if n = pageno then pdim else ppdim)
790 else f pdim rest
792 | [] -> ppdim
794 f (-1, -1, -1, -1) state.pdims
797 let getpagey pageno = fst (getpageyh pageno);;
799 let layout1 y sh =
800 let sh = sh - state.hscrollh in
801 let rec f ~pageno ~pdimno ~prev ~py ~dy ~pdims ~accu =
802 let ((w, h, ips, xoff) as curr), rest, pdimno, yinc =
803 match pdims with
804 | (pageno', w, h, xoff) :: rest when pageno' = pageno ->
805 let ips = calcips h in
806 let yinc =
807 if conf.presentation || (isbirdseye state.mode && pageno = 0)
808 then ips
809 else 0
811 (w, h, ips, xoff), rest, pdimno + 1, yinc
812 | _ ->
813 prev, pdims, pdimno, 0
815 let dy = dy + yinc in
816 let py = py + yinc in
817 if pageno = state.pagecount || dy >= sh
818 then
819 accu
820 else
821 let vy = y + dy in
822 if py + h <= vy - yinc
823 then
824 let py = py + h + ips in
825 let dy = max 0 (py - y) in
826 f ~pageno:(pageno+1)
827 ~pdimno
828 ~prev:curr
831 ~pdims:rest
832 ~accu
833 else
834 let pagey = vy - py in
835 let pagevh = h - pagey in
836 let pagevh = min (sh - dy) pagevh in
837 let off = if yinc > 0 then py - vy else 0 in
838 let py = py + h + ips in
839 let pagex, dx =
840 let xoff = xoff +
841 if state.w < conf.winw - state.scrollw
842 then (conf.winw - state.scrollw - state.w) / 2
843 else 0
845 let dispx = xoff + state.x in
846 if dispx < 0
847 then (-dispx, 0)
848 else (0, dispx)
850 let pagevw =
851 let lw = w - pagex in
852 min lw (conf.winw - state.scrollw)
854 let e =
855 { pageno = pageno
856 ; pagedimno = pdimno
857 ; pagew = w
858 ; pageh = h
859 ; pagex = pagex
860 ; pagey = pagey + off
861 ; pagevw = pagevw
862 ; pagevh = pagevh - off
863 ; pagedispx = dx
864 ; pagedispy = dy + off
867 let accu = e :: accu in
868 f ~pageno:(pageno+1)
869 ~pdimno
870 ~prev:curr
872 ~dy:(dy+pagevh+ips)
873 ~pdims:rest
874 ~accu
876 if state.invalidated = 0
877 then (
878 let accu =
880 ~pageno:0
881 ~pdimno:~-1
882 ~prev:(0,0,0,0)
883 ~py:0
884 ~dy:0
885 ~pdims:state.pdims
886 ~accu:[]
888 List.rev accu
890 else
894 let layoutN (_, b) y sh =
895 let sh = sh - state.hscrollh in
896 let rec fold accu n =
897 if n = Array.length b
898 then accu
899 else
900 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
901 if (vy - y) > sh
902 then accu
903 else
904 let accu =
905 if vy + h > y
906 then
907 let pagey = max 0 (y - vy) in
908 let e =
909 { pageno = n
910 ; pagedimno = pdimno
911 ; pagew = w
912 ; pageh = h
913 ; pagex = 0
914 ; pagey = pagey
915 ; pagevw = w
916 ; pagevh = h - pagey
917 ; pagedispx = dx + xoff + state.x
918 ; pagedispy = if pagey > 0 then 0 else vy - y
921 e :: accu
922 else
923 accu
925 fold accu (n+1)
927 if state.invalidated = 0
928 then List.rev (fold [] 0)
929 else []
932 let layout y sh =
933 match conf.columns with
934 | None -> layout1 y sh
935 | Some c -> layoutN c y sh
938 let clamp incr =
939 let y = state.y + incr in
940 let y = max 0 y in
941 let y = min y (state.maxy - (if conf.maxhfit then conf.winh else 0)) in
945 let getopaque pageno =
946 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
947 with Not_found -> None
950 let putopaque pageno opaque =
951 Hashtbl.replace state.pagemap (pageno, state.gen) opaque
954 let itertiles l f =
955 let tilex = l.pagex mod conf.tilew in
956 let tiley = l.pagey mod conf.tileh in
958 let col = l.pagex / conf.tilew in
959 let row = l.pagey / conf.tileh in
961 let vw =
962 let a = l.pagew - l.pagex in
963 let b = conf.winw - state.scrollw in
964 min a b
965 and vh = l.pagevh in
967 let rec rowloop row y0 dispy h =
968 if h = 0
969 then ()
970 else (
971 let dh = conf.tileh - y0 in
972 let dh = min h dh in
973 let rec colloop col x0 dispx w =
974 if w = 0
975 then ()
976 else (
977 let dw = conf.tilew - x0 in
978 let dw = min w dw in
980 f col row dispx dispy x0 y0 dw dh;
981 colloop (col+1) 0 (dispx+dw) (w-dw)
984 colloop col tilex l.pagedispx vw;
985 rowloop (row+1) 0 (dispy+dh) (h-dh)
988 if vw > 0 && vh > 0
989 then rowloop row tiley l.pagedispy vh;
992 let gettileopaque l col row =
993 let key =
994 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
996 try Some (Hashtbl.find state.tilemap key)
997 with Not_found -> None
1000 let puttileopaque l col row gen colorspace angle opaque size elapsed =
1001 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
1002 Hashtbl.add state.tilemap key (opaque, size, elapsed)
1005 let drawtiles l color =
1006 GlDraw.color color;
1007 let f col row x y tilex tiley w h =
1008 match gettileopaque l col row with
1009 | Some (opaque, _, t) ->
1010 let params = x, y, w, h, tilex, tiley in
1011 if conf.invert
1012 then (
1013 Gl.enable `blend;
1014 GlFunc.blend_func `zero `one_minus_src_color;
1016 drawtile params opaque;
1017 if conf.invert
1018 then Gl.disable `blend;
1019 if conf.debug
1020 then (
1021 let s = Printf.sprintf
1022 "%d[%d,%d] %f sec"
1023 l.pageno col row t
1025 let w = measurestr fstate.fontsize s in
1026 GlMisc.push_attrib [`current];
1027 GlDraw.color (0.0, 0.0, 0.0);
1028 GlDraw.rect
1029 (float (x-2), float (y-2))
1030 (float (x+2) +. w, float (y + fstate.fontsize + 2));
1031 GlDraw.color (1.0, 1.0, 1.0);
1032 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
1033 GlMisc.pop_attrib ();
1036 | _ ->
1037 let w =
1038 let lw = conf.winw - state.scrollw - x in
1039 min lw w
1040 and h =
1041 let lh = conf.winh - y in
1042 min lh h
1044 Gl.enable `texture_2d;
1045 begin match state.texid with
1046 | Some id ->
1047 GlTex.bind_texture `texture_2d id;
1048 let x0 = float x
1049 and y0 = float y
1050 and x1 = float (x+w)
1051 and y1 = float (y+h) in
1053 let tw = float w /. 64.0
1054 and th = float h /. 64.0 in
1055 let tx0 = float tilex /. 64.0
1056 and ty0 = float tiley /. 64.0 in
1057 let tx1 = tx0 +. tw
1058 and ty1 = ty0 +. th in
1059 GlDraw.begins `quads;
1060 GlTex.coord2 (tx0, ty0); GlDraw.vertex2 (x0, y0);
1061 GlTex.coord2 (tx0, ty1); GlDraw.vertex2 (x0, y1);
1062 GlTex.coord2 (tx1, ty1); GlDraw.vertex2 (x1, y1);
1063 GlTex.coord2 (tx1, ty0); GlDraw.vertex2 (x1, y0);
1064 GlDraw.ends ();
1066 Gl.disable `texture_2d;
1067 | None ->
1068 GlDraw.color (1.0, 1.0, 1.0);
1069 GlDraw.rect
1070 (float x, float y)
1071 (float (x+w), float (y+h));
1072 end;
1073 if w > 128 && h > fstate.fontsize + 10
1074 then (
1075 GlDraw.color (0.0, 0.0, 0.0);
1076 let c, r =
1077 if conf.verbose
1078 then (col*conf.tilew, row*conf.tileh)
1079 else col, row
1081 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
1083 GlDraw.color color;
1085 itertiles l f
1088 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
1090 let tilevisible1 l x y =
1091 let ax0 = l.pagex
1092 and ax1 = l.pagex + l.pagevw
1093 and ay0 = l.pagey
1094 and ay1 = l.pagey + l.pagevh in
1096 let bx0 = x
1097 and by0 = y in
1098 let bx1 = min (bx0 + conf.tilew) l.pagew
1099 and by1 = min (by0 + conf.tileh) l.pageh in
1101 let rx0 = max ax0 bx0
1102 and ry0 = max ay0 by0
1103 and rx1 = min ax1 bx1
1104 and ry1 = min ay1 by1 in
1106 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
1107 nonemptyintersection
1110 let tilevisible layout n x y =
1111 let rec findpageinlayout = function
1112 | l :: _ when l.pageno = n -> tilevisible1 l x y
1113 | _ :: rest -> findpageinlayout rest
1114 | [] -> false
1116 findpageinlayout layout
1119 let tileready l x y =
1120 tilevisible1 l x y &&
1121 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
1124 let tilepage n p layout =
1125 let rec loop = function
1126 | l :: rest ->
1127 if l.pageno = n
1128 then
1129 let f col row _ _ _ _ _ _ =
1130 if state.currently = Idle
1131 then
1132 match gettileopaque l col row with
1133 | Some _ -> ()
1134 | None ->
1135 let x = col*conf.tilew
1136 and y = row*conf.tileh in
1137 let w =
1138 let w = l.pagew - x in
1139 min w conf.tilew
1141 let h =
1142 let h = l.pageh - y in
1143 min h conf.tileh
1145 wcmd "tile"
1146 [`s p
1147 ;`i x
1148 ;`i y
1149 ;`i w
1150 ;`i h
1152 state.currently <-
1153 Tiling (
1154 l, p, conf.colorspace, conf.angle, state.gen, col, row,
1155 conf.tilew, conf.tileh
1158 itertiles l f;
1159 else
1160 loop rest
1162 | [] -> ()
1164 if state.invalidated = 0 then loop layout;
1167 let preloadlayout visiblepages =
1168 let presentation = conf.presentation in
1169 let interpagespace = conf.interpagespace in
1170 let maxy = state.maxy in
1171 conf.presentation <- false;
1172 conf.interpagespace <- 0;
1173 state.maxy <- calcheight ();
1174 let y =
1175 match visiblepages with
1176 | [] -> 0
1177 | l :: _ -> getpagey l.pageno + l.pagey
1179 let y = if y < conf.winh then 0 else y - conf.winh in
1180 let h = state.y - y + conf.winh*3 in
1181 let pages = layout y h in
1182 conf.presentation <- presentation;
1183 conf.interpagespace <- interpagespace;
1184 state.maxy <- maxy;
1185 pages;
1188 let load pages =
1189 let rec loop pages =
1190 if state.currently != Idle
1191 then ()
1192 else
1193 match pages with
1194 | l :: rest ->
1195 begin match getopaque l.pageno with
1196 | None ->
1197 wcmd "page" [`i l.pageno; `i l.pagedimno];
1198 state.currently <- Loading (l, state.gen);
1199 | Some opaque ->
1200 tilepage l.pageno opaque pages;
1201 loop rest
1202 end;
1203 | _ -> ()
1205 if state.invalidated = 0 then loop pages
1208 let preload pages =
1209 load pages;
1210 if conf.preload && state.currently = Idle
1211 then load (preloadlayout pages);
1214 let layoutready layout =
1215 let rec fold all ls =
1216 all && match ls with
1217 | l :: rest ->
1218 let seen = ref false in
1219 let allvisible = ref true in
1220 let foo col row _ _ _ _ _ _ =
1221 seen := true;
1222 allvisible := !allvisible &&
1223 begin match gettileopaque l col row with
1224 | Some _ -> true
1225 | None -> false
1228 itertiles l foo;
1229 fold (!seen && !allvisible) rest
1230 | [] -> true
1232 let alltilesvisible = fold true layout in
1233 alltilesvisible;
1236 let gotoy y =
1237 let y = bound y 0 state.maxy in
1238 let y, layout, proceed =
1239 match conf.maxwait with
1240 | Some time when state.ghyll == noghyll ->
1241 begin match state.throttle with
1242 | None ->
1243 let layout = layout y conf.winh in
1244 let ready = layoutready layout in
1245 if not ready
1246 then (
1247 load layout;
1248 state.throttle <- Some (layout, y, now ());
1250 else G.postRedisplay "gotoy showall (None)";
1251 y, layout, ready
1252 | Some (_, _, started) ->
1253 let dt = now () -. started in
1254 if dt > time
1255 then (
1256 state.throttle <- None;
1257 let layout = layout y conf.winh in
1258 load layout;
1259 G.postRedisplay "maxwait";
1260 y, layout, true
1262 else -1, [], false
1265 | _ ->
1266 let layout = layout y conf.winh in
1267 if true || layoutready layout
1268 then G.postRedisplay "gotoy ready";
1269 y, layout, true
1271 if proceed
1272 then (
1273 state.y <- y;
1274 state.layout <- layout;
1275 begin match state.mode with
1276 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
1277 if not (pagevisible layout pageno)
1278 then (
1279 match state.layout with
1280 | [] -> ()
1281 | l :: _ ->
1282 state.mode <- Birdseye (
1283 conf, leftx, l.pageno, hooverpageno, anchor
1286 | _ -> ()
1287 end;
1288 preload layout;
1290 state.ghyll <- noghyll;
1293 let conttiling pageno opaque =
1294 tilepage pageno opaque
1295 (if conf.preload then preloadlayout state.layout else state.layout)
1298 let gotoy_and_clear_text y =
1299 gotoy y;
1300 if not conf.verbose then state.text <- "";
1303 let getanchor () =
1304 match state.layout with
1305 | [] -> emptyanchor
1306 | l :: _ -> (l.pageno, float l.pagey /. float l.pageh)
1309 let getanchory (n, top) =
1310 let y, h = getpageyh n in
1311 y + (truncate (top *. float h));
1314 let gotoanchor anchor =
1315 gotoy (getanchory anchor);
1318 let addnav () =
1319 cbput state.hists.nav (getanchor ());
1322 let getnav dir =
1323 let anchor = cbgetc state.hists.nav dir in
1324 getanchory anchor;
1327 let gotoghyll y =
1328 let rec scroll f n a b =
1329 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
1330 let snake f a b =
1331 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
1332 if f < a
1333 then s (float f /. float a)
1334 else (
1335 if f > b
1336 then 1.0 -. s ((float (f-b) /. float (n-b)))
1337 else 1.0
1340 snake f a b
1341 and summa f n a b =
1342 (* courtesy:
1343 http://integrals.wolfram.com/index.jsp?expr=3x%5E2-2x%5E3&random=false *)
1344 let iv x = -.((-.2.0 +. x)*.x**3.0)/.2.0 in
1345 let iv1 = iv f in
1346 let ins = float a *. iv1
1347 and outs = float (n-b) *. iv1 in
1348 let ones = b - a in
1349 ins +. outs +. float ones
1351 let rec set (_N, _A, _B) y sy =
1352 let sum = summa 1.0 _N _A _B in
1353 let dy = float (y - sy) in
1354 state.ghyll <- (
1355 let rec gf n y1 o =
1356 if n >= _N
1357 then state.ghyll <- noghyll
1358 else
1359 let go n =
1360 let s = scroll n _N _A _B in
1361 let y1 = y1 +. ((s *. dy) /. sum) in
1362 gotoy_and_clear_text (truncate y1);
1363 state.ghyll <- gf (n+1) y1;
1365 match o with
1366 | None -> go n
1367 | Some y' -> set (_N/2, 0, 0) y' state.y
1369 gf 0 (float state.y)
1372 match conf.ghyllscroll with
1373 | None ->
1374 gotoy_and_clear_text y
1375 | Some nab ->
1376 if state.ghyll == noghyll
1377 then set nab y state.y
1378 else state.ghyll (Some y)
1381 let gotopage n top =
1382 let y, h = getpageyh n in
1383 let y = y + (truncate (top *. float h)) in
1384 gotoghyll y
1387 let gotopage1 n top =
1388 let y = getpagey n in
1389 let y = y + top in
1390 gotoghyll y
1393 let invalidate () =
1394 state.layout <- [];
1395 state.pdims <- [];
1396 state.rects <- [];
1397 state.rects1 <- [];
1398 state.invalidated <- state.invalidated + 1;
1401 let writeopen path password =
1402 writecmd state.csock ("open " ^ path ^ "\000" ^ password ^ "\000");
1405 let opendoc path password =
1406 invalidate ();
1407 state.path <- path;
1408 state.password <- password;
1409 state.gen <- state.gen + 1;
1410 state.docinfo <- [];
1412 setaalevel conf.aalevel;
1413 writeopen path password;
1414 Glut.setWindowTitle ("llpp " ^ Filename.basename path);
1415 wcmd "geometry" [`i state.w; `i conf.winh];
1418 let scalecolor c =
1419 let c = c *. conf.colorscale in
1420 (c, c, c);
1423 let scalecolor2 (r, g, b) =
1424 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
1427 let represent () =
1428 let docolumns = function
1429 | None -> ()
1430 | Some ((columns, coverA, coverB), _) ->
1431 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1432 let rec loop pageno pdimno pdim x y rowh pdims =
1433 if pageno = state.pagecount
1434 then ()
1435 else
1436 let pdimno, ((_, w, h, _) as pdim), pdims =
1437 match pdims with
1438 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1439 pdimno+1, pdim, rest
1440 | _ ->
1441 pdimno, pdim, pdims
1443 let x, y, rowh =
1444 if pageno = coverA - 1 || pageno = state.pagecount - coverB
1445 then (
1446 (conf.winw - state.scrollw - w) / 2,
1447 y + rowh + conf.interpagespace, h
1449 else (
1450 if (pageno - coverA) mod columns = 0
1451 then 0, y + rowh + conf.interpagespace, h
1452 else x + w + conf.interpagespace, y, max rowh h
1455 a.(pageno) <- (pdimno, x, y, pdim);
1456 loop (pageno+1) pdimno pdim x y rowh pdims
1458 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
1459 let rec fix n rowh = if n = Array.length a then () else
1460 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(n) in
1461 let rowh =
1462 if n mod columns = 0
1463 then h
1464 else max h rowh
1466 if h < rowh
1467 then (
1468 let y = y + (rowh - h) / 2 in
1469 a.(n) <- (pdimno, x, y, pdim);
1471 fix (n+1) rowh
1473 fix 0 0;
1474 conf.columns <- Some ((columns, coverA, coverB), a);
1476 docolumns conf.columns;
1477 state.maxy <- calcheight ();
1478 state.hscrollh <-
1479 if state.w <= conf.winw - state.scrollw
1480 then 0
1481 else state.scrollw
1483 match state.mode with
1484 | Birdseye (_, _, pageno, _, _) ->
1485 let y, h = getpageyh pageno in
1486 let top = (conf.winh - h) / 2 in
1487 gotoy (max 0 (y - top))
1488 | _ -> gotoanchor state.anchor
1491 let reshape =
1492 let firsttime = ref true in
1493 fun ~w ~h ->
1494 GlDraw.viewport 0 0 w h;
1495 if state.invalidated = 0 && not !firsttime
1496 then state.anchor <- getanchor ();
1498 firsttime := false;
1499 conf.winw <- w;
1500 let w = truncate (float w *. conf.zoom) - state.scrollw in
1501 let w = max w 2 in
1502 state.w <- w;
1503 conf.winh <- h;
1504 setfontsize fstate.fontsize;
1505 GlMat.mode `modelview;
1506 GlMat.load_identity ();
1508 GlMat.mode `projection;
1509 GlMat.load_identity ();
1510 GlMat.rotate ~x:1.0 ~angle:180.0 ();
1511 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
1512 GlMat.scale3 (2.0 /. float conf.winw, 2.0 /. float conf.winh, 1.0);
1514 let w =
1515 match conf.columns with
1516 | None -> w
1517 | Some ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
1519 invalidate ();
1520 wcmd "geometry" [`i w; `i h];
1523 let enttext () =
1524 let len = String.length state.text in
1525 let drawstring s =
1526 let hscrollh =
1527 match state.mode with
1528 | View -> state.hscrollh
1529 | _ -> 0
1531 let rect x w =
1532 GlDraw.rect
1533 (x, float (conf.winh - (fstate.fontsize + 4) - hscrollh))
1534 (x+.w, float (conf.winh - hscrollh))
1537 let w = float (conf.winw - state.scrollw - 1) in
1538 if state.progress >= 0.0 && state.progress < 1.0
1539 then (
1540 GlDraw.color (0.3, 0.3, 0.3);
1541 let w1 = w *. state.progress in
1542 rect 0.0 w1;
1543 GlDraw.color (0.0, 0.0, 0.0);
1544 rect w1 (w-.w1)
1546 else (
1547 GlDraw.color (0.0, 0.0, 0.0);
1548 rect 0.0 w;
1551 GlDraw.color (1.0, 1.0, 1.0);
1552 drawstring fstate.fontsize
1553 (if len > 0 then 8 else 2) (conf.winh - hscrollh - 5) s;
1555 let s =
1556 match state.mode with
1557 | Textentry ((prefix, text, _, _, _), _) ->
1558 let s =
1559 if len > 0
1560 then
1561 Printf.sprintf "%s%s_ [%s]" prefix text state.text
1562 else
1563 Printf.sprintf "%s%s_" prefix text
1567 | _ -> state.text
1569 let s =
1570 if state.newerrmsgs
1571 then (
1572 if not (istextentry state.mode)
1573 then
1574 let s1 = "(press 'e' to review error messasges)" in
1575 if String.length s > 0 then s ^ " " ^ s1 else s1
1576 else s
1578 else s
1580 if String.length s > 0
1581 then drawstring s
1584 let showtext c s =
1585 state.text <- Printf.sprintf "%c%s" c s;
1586 G.postRedisplay "showtext";
1589 let gctiles () =
1590 let len = Queue.length state.tilelru in
1591 let rec loop qpos =
1592 if state.memused <= conf.memlimit
1593 then ()
1594 else (
1595 if qpos < len
1596 then
1597 let (k, p, s) as lruitem = Queue.pop state.tilelru in
1598 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
1599 let (_, pw, ph, _) = getpagedim n in
1601 gen = state.gen
1602 && colorspace = conf.colorspace
1603 && angle = conf.angle
1604 && pagew = pw
1605 && pageh = ph
1606 && (
1607 let layout =
1608 match state.throttle with
1609 | None ->
1610 if conf.preload
1611 then preloadlayout state.layout
1612 else state.layout
1613 | Some (layout, _, _) ->
1614 layout
1616 let x = col*conf.tilew
1617 and y = row*conf.tileh in
1618 tilevisible layout n x y
1620 then Queue.push lruitem state.tilelru
1621 else (
1622 wcmd "freetile" [`s p];
1623 state.memused <- state.memused - s;
1624 state.uioh#infochanged Memused;
1625 Hashtbl.remove state.tilemap k;
1627 loop (qpos+1)
1630 loop 0
1633 let flushtiles () =
1634 Queue.iter (fun (k, p, s) ->
1635 wcmd "freetile" [`s p];
1636 state.memused <- state.memused - s;
1637 state.uioh#infochanged Memused;
1638 Hashtbl.remove state.tilemap k;
1639 ) state.tilelru;
1640 Queue.clear state.tilelru;
1641 load state.layout;
1644 let logcurrently = function
1645 | Idle -> dolog "Idle"
1646 | Loading (l, gen) ->
1647 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
1648 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
1649 dolog
1650 "Tiling %d[%d,%d] page=%s cs=%s angle"
1651 l.pageno col row pageopaque
1652 (colorspace_to_string colorspace)
1654 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
1655 angle gen conf.angle state.gen
1656 tilew tileh
1657 conf.tilew conf.tileh
1659 | Outlining _ ->
1660 dolog "outlining"
1663 let act cmds =
1664 (* dolog "%S" cmds; *)
1665 let op, args =
1666 let spacepos =
1667 try String.index cmds ' '
1668 with Not_found -> -1
1670 if spacepos = -1
1671 then cmds, ""
1672 else
1673 let l = String.length cmds in
1674 let op = String.sub cmds 0 spacepos in
1675 op, begin
1676 if l - spacepos < 2 then ""
1677 else String.sub cmds (spacepos+1) (l-spacepos-1)
1680 match op with
1681 | "clear" ->
1682 state.uioh#infochanged Pdim;
1683 state.pdims <- [];
1685 | "clearrects" ->
1686 state.rects <- state.rects1;
1687 G.postRedisplay "clearrects";
1689 | "continue" ->
1690 let n =
1691 try Scanf.sscanf args "%u" (fun n -> n)
1692 with exn ->
1693 dolog "error processing 'continue' %S: %s"
1694 cmds (Printexc.to_string exn);
1695 exit 1;
1697 state.pagecount <- n;
1698 state.invalidated <- state.invalidated - 1;
1699 begin match state.currently with
1700 | Outlining l ->
1701 state.currently <- Idle;
1702 state.outlines <- Array.of_list (List.rev l)
1703 | _ -> ()
1704 end;
1705 if state.invalidated = 0
1706 then represent ();
1707 if conf.maxwait = None
1708 then G.postRedisplay "continue";
1710 | "title" ->
1711 Glut.setWindowTitle args
1713 | "msg" ->
1714 showtext ' ' args
1716 | "vmsg" ->
1717 if conf.verbose
1718 then showtext ' ' args
1720 | "progress" ->
1721 let progress, text =
1723 Scanf.sscanf args "%f %n"
1724 (fun f pos ->
1725 f, String.sub args pos (String.length args - pos))
1726 with exn ->
1727 dolog "error processing 'progress' %S: %s"
1728 cmds (Printexc.to_string exn);
1729 exit 1;
1731 state.text <- text;
1732 state.progress <- progress;
1733 G.postRedisplay "progress"
1735 | "firstmatch" ->
1736 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1738 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
1739 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1740 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1741 with exn ->
1742 dolog "error processing 'firstmatch' %S: %s"
1743 cmds (Printexc.to_string exn);
1744 exit 1;
1746 let y = (getpagey pageno) + truncate y0 in
1747 addnav ();
1748 gotoy y;
1749 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
1751 | "match" ->
1752 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1754 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
1755 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1756 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1757 with exn ->
1758 dolog "error processing 'match' %S: %s"
1759 cmds (Printexc.to_string exn);
1760 exit 1;
1762 state.rects1 <-
1763 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
1765 | "page" ->
1766 let pageopaque, t =
1768 Scanf.sscanf args "%s %f" (fun p t -> p, t)
1769 with exn ->
1770 dolog "error processing 'page' %S: %s"
1771 cmds (Printexc.to_string exn);
1772 exit 1;
1774 begin match state.currently with
1775 | Loading (l, gen) ->
1776 vlog "page %d took %f sec" l.pageno t;
1777 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
1778 begin match state.throttle with
1779 | None ->
1780 let preloadedpages =
1781 if conf.preload
1782 then preloadlayout state.layout
1783 else state.layout
1785 let evict () =
1786 let module IntSet =
1787 Set.Make (struct type t = int let compare = (-) end) in
1788 let set =
1789 List.fold_left (fun s l -> IntSet.add l.pageno s)
1790 IntSet.empty preloadedpages
1792 let evictedpages =
1793 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
1794 if not (IntSet.mem pageno set)
1795 then (
1796 wcmd "freepage" [`s opaque];
1797 key :: accu
1799 else accu
1800 ) state.pagemap []
1802 List.iter (Hashtbl.remove state.pagemap) evictedpages;
1804 evict ();
1805 state.currently <- Idle;
1806 if gen = state.gen
1807 then (
1808 tilepage l.pageno pageopaque state.layout;
1809 load state.layout;
1810 load preloadedpages;
1811 if pagevisible state.layout l.pageno
1812 && layoutready state.layout
1813 then G.postRedisplay "page";
1816 | Some (layout, _, _) ->
1817 state.currently <- Idle;
1818 tilepage l.pageno pageopaque layout;
1819 load state.layout
1820 end;
1822 | _ ->
1823 dolog "Inconsistent loading state";
1824 logcurrently state.currently;
1825 raise Quit;
1828 | "tile" ->
1829 let (x, y, opaque, size, t) =
1831 Scanf.sscanf args "%u %u %s %u %f"
1832 (fun x y p size t -> (x, y, p, size, t))
1833 with exn ->
1834 dolog "error processing 'tile' %S: %s"
1835 cmds (Printexc.to_string exn);
1836 exit 1;
1838 begin match state.currently with
1839 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
1840 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
1842 if tilew != conf.tilew || tileh != conf.tileh
1843 then (
1844 wcmd "freetile" [`s opaque];
1845 state.currently <- Idle;
1846 load state.layout;
1848 else (
1849 puttileopaque l col row gen cs angle opaque size t;
1850 state.memused <- state.memused + size;
1851 state.uioh#infochanged Memused;
1852 gctiles ();
1853 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
1854 opaque, size) state.tilelru;
1856 let layout =
1857 match state.throttle with
1858 | None -> state.layout
1859 | Some (layout, _, _) -> layout
1862 state.currently <- Idle;
1863 if gen = state.gen
1864 && conf.colorspace = cs
1865 && conf.angle = angle
1866 && tilevisible layout l.pageno x y
1867 then conttiling l.pageno pageopaque;
1869 begin match state.throttle with
1870 | None ->
1871 preload state.layout;
1872 if gen = state.gen
1873 && conf.colorspace = cs
1874 && conf.angle = angle
1875 && tilevisible state.layout l.pageno x y
1876 then G.postRedisplay "tile nothrottle";
1878 | Some (layout, y, _) ->
1879 let ready = layoutready layout in
1880 if ready
1881 then (
1882 state.y <- y;
1883 state.layout <- layout;
1884 state.throttle <- None;
1885 G.postRedisplay "throttle";
1887 else load layout;
1888 end;
1891 | _ ->
1892 dolog "Inconsistent tiling state";
1893 logcurrently state.currently;
1894 raise Quit;
1897 | "pdim" ->
1898 let pdim =
1900 Scanf.sscanf args "%u %u %u %u" (fun n w h x -> n, w, h, x)
1901 with exn ->
1902 dolog "error processing 'pdim' %S: %s"
1903 cmds (Printexc.to_string exn);
1904 exit 1;
1906 state.uioh#infochanged Pdim;
1907 state.pdims <- pdim :: state.pdims
1909 | "o" ->
1910 let (l, n, t, h, pos) =
1912 Scanf.sscanf args "%u %u %d %u %n"
1913 (fun l n t h pos -> l, n, t, h, pos)
1914 with exn ->
1915 dolog "error processing 'o' %S: %s"
1916 cmds (Printexc.to_string exn);
1917 exit 1;
1919 let s = String.sub args pos (String.length args - pos) in
1920 let outline = (s, l, (n, float t /. float h)) in
1921 begin match state.currently with
1922 | Outlining outlines ->
1923 state.currently <- Outlining (outline :: outlines)
1924 | Idle ->
1925 state.currently <- Outlining [outline]
1926 | currently ->
1927 dolog "invalid outlining state";
1928 logcurrently currently
1931 | "info" ->
1932 state.docinfo <- (1, args) :: state.docinfo
1934 | "infoend" ->
1935 state.uioh#infochanged Docinfo;
1936 state.docinfo <- List.rev state.docinfo
1938 | _ ->
1939 dolog "unknown cmd `%S'" cmds
1942 let idle () =
1943 if state.deadline == nan then state.deadline <- now ();
1944 let r =
1945 match state.errfd with
1946 | None -> [state.csock]
1947 | Some fd -> [state.csock; fd]
1949 let rec loop delay =
1950 let deadline =
1951 if state.ghyll == noghyll
1952 then state.deadline
1953 else now () +. 0.02
1955 let timeout =
1956 if delay > 0.0
1957 then max 0.0 (deadline -. now ())
1958 else 0.0
1960 let r, _, _ = Unix.select r [] [] timeout in
1961 begin match r with
1962 | [] ->
1963 state.ghyll None;
1964 begin match state.autoscroll with
1965 | Some step when step != 0 ->
1966 let y = state.y + step in
1967 let y =
1968 if y < 0
1969 then state.maxy
1970 else if y >= state.maxy then 0 else y
1972 gotoy y;
1973 if state.mode = View
1974 then state.text <- "";
1975 state.deadline <- state.deadline +. 0.005;
1977 | _ ->
1978 state.deadline <- state.deadline +. delay;
1979 end;
1981 | l ->
1982 let rec checkfds c = function
1983 | [] -> c
1984 | fd :: rest when fd = state.csock ->
1985 let cmd = readcmd state.csock in
1986 act cmd;
1987 checkfds true rest
1988 | fd :: rest ->
1989 let s = String.create 80 in
1990 let n = Unix.read fd s 0 80 in
1991 if conf.redirectstderr
1992 then (
1993 Buffer.add_substring state.errmsgs s 0 n;
1994 state.newerrmsgs <- true;
1995 Glut.postRedisplay ();
1997 else (
1998 prerr_string (String.sub s 0 n);
1999 flush stderr;
2001 checkfds c rest
2003 if checkfds false l
2004 then loop 0.0
2005 end;
2006 in loop 0.007
2009 let onhist cb =
2010 let rc = cb.rc in
2011 let action = function
2012 | HCprev -> cbget cb ~-1
2013 | HCnext -> cbget cb 1
2014 | HCfirst -> cbget cb ~-(cb.rc)
2015 | HClast -> cbget cb (cb.len - 1 - cb.rc)
2016 and cancel () = cb.rc <- rc
2017 in (action, cancel)
2020 let search pattern forward =
2021 if String.length pattern > 0
2022 then
2023 let pn, py =
2024 match state.layout with
2025 | [] -> 0, 0
2026 | l :: _ ->
2027 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
2029 let cmd =
2030 let b = makecmd "search"
2031 [`b conf.icase; `i pn; `i py; `i (if forward then 1 else 0)]
2033 Buffer.add_char b ',';
2034 Buffer.add_string b pattern;
2035 Buffer.add_char b '\000';
2036 Buffer.contents b;
2038 writecmd state.csock cmd;
2041 let intentry text key =
2042 let c = Char.unsafe_chr key in
2043 match c with
2044 | '0' .. '9' ->
2045 let text = addchar text c in
2046 TEcont text
2048 | _ ->
2049 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2050 TEcont text
2053 let textentry text key =
2054 let c = Char.unsafe_chr key in
2055 match c with
2056 | _ when key >= 32 && key < 127 ->
2057 let text = addchar text c in
2058 TEcont text
2060 | _ ->
2061 dolog "unhandled key %d char `%c'" key (Char.unsafe_chr key);
2062 TEcont text
2065 let reqlayout angle proportional =
2066 match state.throttle with
2067 | None ->
2068 if state.invalidated = 0 then state.anchor <- getanchor ();
2069 conf.angle <- angle mod 360;
2070 conf.proportional <- proportional;
2071 invalidate ();
2072 wcmd "reqlayout" [`i conf.angle; `b proportional];
2073 | _ -> ()
2076 let settrim trimmargins trimfuzz =
2077 if state.invalidated = 0 then state.anchor <- getanchor ();
2078 conf.trimmargins <- trimmargins;
2079 conf.trimfuzz <- trimfuzz;
2080 let x0, y0, x1, y1 = trimfuzz in
2081 invalidate ();
2082 wcmd "settrim" [
2083 `b conf.trimmargins;
2084 `i x0;
2085 `i y0;
2086 `i x1;
2087 `i y1;
2089 Hashtbl.iter (fun _ opaque ->
2090 wcmd "freepage" [`s opaque];
2091 ) state.pagemap;
2092 Hashtbl.clear state.pagemap;
2095 let setzoom zoom =
2096 match state.throttle with
2097 | None ->
2098 let zoom = max 0.01 zoom in
2099 if zoom <> conf.zoom
2100 then (
2101 state.prevzoom <- conf.zoom;
2102 let relx =
2103 if zoom <= 1.0
2104 then (state.x <- 0; 0.0)
2105 else float state.x /. float state.w
2107 conf.zoom <- zoom;
2108 reshape conf.winw conf.winh;
2109 if zoom > 1.0
2110 then (
2111 let x = relx *. float state.w in
2112 state.x <- truncate x;
2114 state.text <- Printf.sprintf "zoom is now %-5.1f" (zoom *. 100.0);
2117 | Some (layout, y, started) ->
2118 let time =
2119 match conf.maxwait with
2120 | None -> 0.0
2121 | Some t -> t
2123 let dt = now () -. started in
2124 if dt > time
2125 then (
2126 state.y <- y;
2127 load layout;
2131 let setcolumns columns coverA coverB =
2132 if columns < 2
2133 then (
2134 conf.columns <- None;
2135 state.x <- 0;
2136 setzoom 1.0;
2138 else (
2139 conf.columns <- Some ((columns, coverA, coverB), [||]);
2140 conf.zoom <- 1.0;
2142 reshape conf.winw conf.winh;
2145 let enterbirdseye () =
2146 let zoom = float conf.thumbw /. float conf.winw in
2147 let birdseyepageno =
2148 let cy = conf.winh / 2 in
2149 let fold = function
2150 | [] -> 0
2151 | l :: rest ->
2152 let rec fold best = function
2153 | [] -> best.pageno
2154 | l :: rest ->
2155 let d = cy - (l.pagedispy + l.pagevh/2)
2156 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2157 if abs d < abs dbest
2158 then fold l rest
2159 else best.pageno
2160 in fold l rest
2162 fold state.layout
2164 state.mode <- Birdseye (
2165 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2167 conf.zoom <- zoom;
2168 conf.presentation <- false;
2169 conf.interpagespace <- 10;
2170 conf.hlinks <- false;
2171 state.x <- 0;
2172 state.mstate <- Mnone;
2173 conf.maxwait <- None;
2174 conf.columns <- (
2175 match conf.beyecolumns with
2176 | Some c ->
2177 conf.zoom <- 1.0;
2178 Some ((c, 0, 0), [||])
2179 | None -> None
2181 Glut.setCursor Glut.CURSOR_INHERIT;
2182 if conf.verbose
2183 then
2184 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2185 (100.0*.zoom)
2186 else
2187 state.text <- ""
2189 reshape conf.winw conf.winh;
2192 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2193 state.mode <- View;
2194 conf.zoom <- c.zoom;
2195 conf.presentation <- c.presentation;
2196 conf.interpagespace <- c.interpagespace;
2197 conf.maxwait <- c.maxwait;
2198 conf.hlinks <- c.hlinks;
2199 conf.beyecolumns <- (
2200 match conf.columns with
2201 | Some ((c, _, _), _) -> Some c
2202 | None -> None
2204 conf.columns <- (
2205 match c.columns with
2206 | Some (c, _) -> Some (c, [||])
2207 | None -> None
2209 state.x <- leftx;
2210 if conf.verbose
2211 then
2212 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2213 (100.0*.conf.zoom)
2215 reshape conf.winw conf.winh;
2216 state.anchor <- if goback then anchor else (pageno, 0.0);
2219 let togglebirdseye () =
2220 match state.mode with
2221 | Birdseye vals -> leavebirdseye vals true
2222 | View -> enterbirdseye ()
2223 | _ -> ()
2226 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2227 let pageno = max 0 (pageno - incr) in
2228 let rec loop = function
2229 | [] -> gotopage1 pageno 0
2230 | l :: _ when l.pageno = pageno ->
2231 if l.pagedispy >= 0 && l.pagey = 0
2232 then G.postRedisplay "upbirdseye"
2233 else gotopage1 pageno 0
2234 | _ :: rest -> loop rest
2236 loop state.layout;
2237 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2240 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2241 let pageno = min (state.pagecount - 1) (pageno + incr) in
2242 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2243 let rec loop = function
2244 | [] ->
2245 let y, h = getpageyh pageno in
2246 let dy = (y - state.y) - (conf.winh - h - conf.interpagespace) in
2247 gotoy (clamp dy)
2248 | l :: _ when l.pageno = pageno ->
2249 if l.pagevh != l.pageh
2250 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
2251 else G.postRedisplay "downbirdseye"
2252 | _ :: rest -> loop rest
2254 loop state.layout
2257 let optentry mode _ key =
2258 let btos b = if b then "on" else "off" in
2259 let c = Char.unsafe_chr key in
2260 match c with
2261 | 's' ->
2262 let ondone s =
2263 try conf.scrollstep <- int_of_string s with exc ->
2264 state.text <- Printf.sprintf "bad integer `%s': %s"
2265 s (Printexc.to_string exc)
2267 TEswitch ("scroll step: ", "", None, intentry, ondone)
2269 | 'A' ->
2270 let ondone s =
2272 conf.autoscrollstep <- int_of_string s;
2273 if state.autoscroll <> None
2274 then state.autoscroll <- Some conf.autoscrollstep
2275 with exc ->
2276 state.text <- Printf.sprintf "bad integer `%s': %s"
2277 s (Printexc.to_string exc)
2279 TEswitch ("auto scroll step: ", "", None, intentry, ondone)
2281 | 'Z' ->
2282 let ondone s =
2284 let zoom = float (int_of_string s) /. 100.0 in
2285 setzoom zoom
2286 with exc ->
2287 state.text <- Printf.sprintf "bad integer `%s': %s"
2288 s (Printexc.to_string exc)
2290 TEswitch ("zoom: ", "", None, intentry, ondone)
2292 | 't' ->
2293 let ondone s =
2295 conf.thumbw <- bound (int_of_string s) 2 4096;
2296 state.text <-
2297 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
2298 begin match mode with
2299 | Birdseye beye ->
2300 leavebirdseye beye false;
2301 enterbirdseye ();
2302 | _ -> ();
2304 with exc ->
2305 state.text <- Printf.sprintf "bad integer `%s': %s"
2306 s (Printexc.to_string exc)
2308 TEswitch ("thumbnail width: ", "", None, intentry, ondone)
2310 | 'R' ->
2311 let ondone s =
2312 match try
2313 Some (int_of_string s)
2314 with exc ->
2315 state.text <- Printf.sprintf "bad integer `%s': %s"
2316 s (Printexc.to_string exc);
2317 None
2318 with
2319 | Some angle -> reqlayout angle conf.proportional
2320 | None -> ()
2322 TEswitch ("rotation: ", "", None, intentry, ondone)
2324 | 'i' ->
2325 conf.icase <- not conf.icase;
2326 TEdone ("case insensitive search " ^ (btos conf.icase))
2328 | 'p' ->
2329 conf.preload <- not conf.preload;
2330 gotoy state.y;
2331 TEdone ("preload " ^ (btos conf.preload))
2333 | 'v' ->
2334 conf.verbose <- not conf.verbose;
2335 TEdone ("verbose " ^ (btos conf.verbose))
2337 | 'd' ->
2338 conf.debug <- not conf.debug;
2339 TEdone ("debug " ^ (btos conf.debug))
2341 | 'h' ->
2342 conf.maxhfit <- not conf.maxhfit;
2343 state.maxy <-
2344 state.maxy + (if conf.maxhfit then -conf.winh else conf.winh);
2345 TEdone ("maxhfit " ^ (btos conf.maxhfit))
2347 | 'c' ->
2348 conf.crophack <- not conf.crophack;
2349 TEdone ("crophack " ^ btos conf.crophack)
2351 | 'a' ->
2352 let s =
2353 match conf.maxwait with
2354 | None ->
2355 conf.maxwait <- Some infinity;
2356 "always wait for page to complete"
2357 | Some _ ->
2358 conf.maxwait <- None;
2359 "show placeholder if page is not ready"
2361 TEdone s
2363 | 'f' ->
2364 conf.underinfo <- not conf.underinfo;
2365 TEdone ("underinfo " ^ btos conf.underinfo)
2367 | 'P' ->
2368 conf.savebmarks <- not conf.savebmarks;
2369 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
2371 | 'S' ->
2372 let ondone s =
2374 let pageno, py =
2375 match state.layout with
2376 | [] -> 0, 0
2377 | l :: _ ->
2378 l.pageno, l.pagey
2380 conf.interpagespace <- int_of_string s;
2381 state.maxy <- calcheight ();
2382 let y = getpagey pageno in
2383 gotoy (y + py)
2384 with exc ->
2385 state.text <- Printf.sprintf "bad integer `%s': %s"
2386 s (Printexc.to_string exc)
2388 TEswitch ("vertical margin: ", "", None, intentry, ondone)
2390 | 'l' ->
2391 reqlayout conf.angle (not conf.proportional);
2392 TEdone ("proportional display " ^ btos conf.proportional)
2394 | 'T' ->
2395 settrim (not conf.trimmargins) conf.trimfuzz;
2396 TEdone ("trim margins " ^ btos conf.trimmargins)
2398 | 'I' ->
2399 conf.invert <- not conf.invert;
2400 TEdone ("invert colors " ^ btos conf.invert)
2402 | _ ->
2403 state.text <- Printf.sprintf "bad option %d `%c'" key c;
2404 TEstop
2407 class type lvsource = object
2408 method getitemcount : int
2409 method getitem : int -> (string * int)
2410 method hasaction : int -> bool
2411 method exit :
2412 uioh:uioh ->
2413 cancel:bool ->
2414 active:int ->
2415 first:int ->
2416 pan:int ->
2417 qsearch:string ->
2418 uioh option
2419 method getactive : int
2420 method getfirst : int
2421 method getqsearch : string
2422 method setqsearch : string -> unit
2423 method getpan : int
2424 end;;
2426 class virtual lvsourcebase = object
2427 val mutable m_active = 0
2428 val mutable m_first = 0
2429 val mutable m_qsearch = ""
2430 val mutable m_pan = 0
2431 method getactive = m_active
2432 method getfirst = m_first
2433 method getqsearch = m_qsearch
2434 method getpan = m_pan
2435 method setqsearch s = m_qsearch <- s
2436 end;;
2438 let textentryspecial key = function
2439 | ((c, _, (Some (action, _) as onhist), onkey, ondone), mode) ->
2440 let s =
2441 match key with
2442 | Glut.KEY_UP -> action HCprev
2443 | Glut.KEY_DOWN -> action HCnext
2444 | Glut.KEY_HOME -> action HCfirst
2445 | Glut.KEY_END -> action HClast
2446 | _ -> state.text
2448 state.mode <- Textentry ((c, s, onhist, onkey, ondone), mode);
2449 G.postRedisplay "special textentry";
2450 | _ -> ()
2453 let textentrykeyboard key ((c, text, opthist, onkey, ondone), onleave) =
2454 let enttext te =
2455 state.mode <- Textentry (te, onleave);
2456 state.text <- "";
2457 enttext ();
2458 G.postRedisplay "textentrykeyboard enttext";
2460 match Char.unsafe_chr key with
2461 | '\008' -> (* backspace *)
2462 let len = String.length text in
2463 if len = 0
2464 then (
2465 onleave Cancel;
2466 G.postRedisplay "textentrykeyboard after cancel";
2468 else (
2469 let s = String.sub text 0 (len - 1) in
2470 enttext (c, s, opthist, onkey, ondone)
2473 | '\r' | '\n' ->
2474 ondone text;
2475 onleave Confirm;
2476 G.postRedisplay "textentrykeyboard after confirm"
2478 | '\007' (* ctrl-g *)
2479 | '\027' -> (* escape *)
2480 if String.length text = 0
2481 then (
2482 begin match opthist with
2483 | None -> ()
2484 | Some (_, onhistcancel) -> onhistcancel ()
2485 end;
2486 onleave Cancel;
2487 state.text <- "";
2488 G.postRedisplay "textentrykeyboard after cancel2"
2490 else (
2491 enttext (c, "", opthist, onkey, ondone)
2494 | '\127' -> () (* delete *)
2496 | _ ->
2497 begin match onkey text key with
2498 | TEdone text ->
2499 ondone text;
2500 onleave Confirm;
2501 G.postRedisplay "textentrykeyboard after confirm2";
2503 | TEcont text ->
2504 enttext (c, text, opthist, onkey, ondone);
2506 | TEstop ->
2507 onleave Cancel;
2508 G.postRedisplay "textentrykeyboard after cancel3"
2510 | TEswitch te ->
2511 state.mode <- Textentry (te, onleave);
2512 G.postRedisplay "textentrykeyboard switch";
2513 end;
2516 let firstof first active =
2517 if first > active || abs (first - active) > fstate.maxrows - 1
2518 then max 0 (active - (fstate.maxrows/2))
2519 else first
2522 let calcfirst first active =
2523 if active > first
2524 then
2525 let rows = active - first in
2526 if rows > fstate.maxrows then active - fstate.maxrows else first
2527 else active
2530 let scrollph y maxy =
2531 let sh = (float (maxy + conf.winh) /. float conf.winh) in
2532 let sh = float conf.winh /. sh in
2533 let sh = max sh (float conf.scrollh) in
2535 let percent =
2536 if y = state.maxy
2537 then 1.0
2538 else float y /. float maxy
2540 let position = (float conf.winh -. sh) *. percent in
2542 let position =
2543 if position +. sh > float conf.winh
2544 then float conf.winh -. sh
2545 else position
2547 position, sh;
2550 let coe s = (s :> uioh);;
2552 class listview ~(source:lvsource) ~trusted =
2553 object (self)
2554 val m_pan = source#getpan
2555 val m_first = source#getfirst
2556 val m_active = source#getactive
2557 val m_qsearch = source#getqsearch
2558 val m_prev_uioh = state.uioh
2560 method private elemunder y =
2561 let n = y / (fstate.fontsize+1) in
2562 if m_first + n < source#getitemcount
2563 then (
2564 if source#hasaction (m_first + n)
2565 then Some (m_first + n)
2566 else None
2568 else None
2570 method display =
2571 Gl.enable `blend;
2572 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2573 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2574 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
2575 GlDraw.color (1., 1., 1.);
2576 Gl.enable `texture_2d;
2577 let fs = fstate.fontsize in
2578 let nfs = fs + 1 in
2579 let ww = fstate.wwidth in
2580 let tabw = 30.0*.ww in
2581 let itemcount = source#getitemcount in
2582 let rec loop row =
2583 if (row - m_first) * nfs > conf.winh
2584 then ()
2585 else (
2586 if row >= 0 && row < itemcount
2587 then (
2588 let (s, level) = source#getitem row in
2589 let y = (row - m_first) * nfs in
2590 let x = 5.0 +. float (level + m_pan) *. ww in
2591 if row = m_active
2592 then (
2593 Gl.disable `texture_2d;
2594 GlDraw.polygon_mode `both `line;
2595 GlDraw.color (1., 1., 1.) ~alpha:0.9;
2596 GlDraw.rect (1., float (y + 1))
2597 (float (conf.winw - conf.scrollbw - 1), float (y + fs + 3));
2598 GlDraw.polygon_mode `both `fill;
2599 GlDraw.color (1., 1., 1.);
2600 Gl.enable `texture_2d;
2603 let drawtabularstring s =
2604 let drawstr x s = drawstring1 fs (truncate x) (y+nfs) s in
2605 if trusted
2606 then
2607 let tabpos = try String.index s '\t' with Not_found -> -1 in
2608 if tabpos > 0
2609 then
2610 let len = String.length s - tabpos - 1 in
2611 let s1 = String.sub s 0 tabpos
2612 and s2 = String.sub s (tabpos + 1) len in
2613 let nx = drawstr x s1 in
2614 let sw = nx -. x in
2615 let x = x +. (max tabw sw) in
2616 drawstr x s2
2617 else
2618 drawstr x s
2619 else
2620 drawstr x s
2622 let _ = drawtabularstring s in
2623 loop (row+1)
2627 loop m_first;
2628 Gl.disable `blend;
2629 Gl.disable `texture_2d;
2631 method updownlevel incr =
2632 let len = source#getitemcount in
2633 let curlevel =
2634 if m_active >= 0 && m_active < len
2635 then snd (source#getitem m_active)
2636 else -1
2638 let rec flow i =
2639 if i = len then i-1 else if i = -1 then 0 else
2640 let _, l = source#getitem i in
2641 if l != curlevel then i else flow (i+incr)
2643 let active = flow m_active in
2644 let first = calcfirst m_first active in
2645 G.postRedisplay "special outline updownlevel";
2646 {< m_active = active; m_first = first >}
2648 method private key1 key =
2649 let set active first qsearch =
2650 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
2652 let search active pattern incr =
2653 let dosearch re =
2654 let rec loop n =
2655 if n >= 0 && n < source#getitemcount
2656 then (
2657 let s, _ = source#getitem n in
2659 (try ignore (Str.search_forward re s 0); true
2660 with Not_found -> false)
2661 then Some n
2662 else loop (n + incr)
2664 else None
2666 loop active
2669 let re = Str.regexp_case_fold pattern in
2670 dosearch re
2671 with Failure s ->
2672 state.text <- s;
2673 None
2675 match key with
2676 | 18 | 19 -> (* ctrl-r/ctlr-s *)
2677 let incr = if key = 18 then -1 else 1 in
2678 let active, first =
2679 match search (m_active + incr) m_qsearch incr with
2680 | None ->
2681 state.text <- m_qsearch ^ " [not found]";
2682 m_active, m_first
2683 | Some active ->
2684 state.text <- m_qsearch;
2685 active, firstof m_first active
2687 G.postRedisplay "listview ctrl-r/s";
2688 set active first m_qsearch;
2690 | 8 -> (* backspace *)
2691 let len = String.length m_qsearch in
2692 if len = 0
2693 then coe self
2694 else (
2695 if len = 1
2696 then (
2697 state.text <- "";
2698 G.postRedisplay "listview empty qsearch";
2699 set m_active m_first "";
2701 else
2702 let qsearch = String.sub m_qsearch 0 (len - 1) in
2703 let active, first =
2704 match search m_active qsearch ~-1 with
2705 | None ->
2706 state.text <- qsearch ^ " [not found]";
2707 m_active, m_first
2708 | Some active ->
2709 state.text <- qsearch;
2710 active, firstof m_first active
2712 G.postRedisplay "listview backspace qsearch";
2713 set active first qsearch
2716 | _ when key >= 32 && key < 127 ->
2717 let pattern = addchar m_qsearch (Char.chr key) in
2718 let active, first =
2719 match search m_active pattern 1 with
2720 | None ->
2721 state.text <- pattern ^ " [not found]";
2722 m_active, m_first
2723 | Some active ->
2724 state.text <- pattern;
2725 active, firstof m_first active
2727 G.postRedisplay "listview qsearch add";
2728 set active first pattern;
2730 | 27 -> (* escape *)
2731 state.text <- "";
2732 if String.length m_qsearch = 0
2733 then (
2734 G.postRedisplay "list view escape";
2735 begin
2736 match
2737 source#exit (coe self) true m_active m_first m_pan m_qsearch
2738 with
2739 | None -> m_prev_uioh
2740 | Some uioh -> uioh
2743 else (
2744 G.postRedisplay "list view kill qsearch";
2745 source#setqsearch "";
2746 coe {< m_qsearch = "" >}
2749 | 13 -> (* enter *)
2750 state.text <- "";
2751 let self = {< m_qsearch = "" >} in
2752 source#setqsearch "";
2753 let opt =
2754 G.postRedisplay "listview enter";
2755 if m_active >= 0 && m_active < source#getitemcount
2756 then (
2757 source#exit (coe self) false m_active m_first m_pan "";
2759 else (
2760 source#exit (coe self) true m_active m_first m_pan "";
2763 begin match opt with
2764 | None -> m_prev_uioh
2765 | Some uioh -> uioh
2768 | 127 -> (* delete *)
2769 coe self
2771 | _ -> dolog "unknown key %d" key; coe self
2773 method private special1 key =
2774 let itemcount = source#getitemcount in
2775 let find start incr =
2776 let rec find i =
2777 if i = -1 || i = itemcount
2778 then -1
2779 else (
2780 if source#hasaction i
2781 then i
2782 else find (i + incr)
2785 find start
2787 let set active first =
2788 let first = bound first 0 (itemcount - fstate.maxrows) in
2789 state.text <- "";
2790 coe {< m_active = active; m_first = first >}
2792 let navigate incr =
2793 let isvisible first n = n >= first && n - first <= fstate.maxrows in
2794 let active, first =
2795 let incr1 = if incr > 0 then 1 else -1 in
2796 if isvisible m_first m_active
2797 then
2798 let next =
2799 let next = m_active + incr in
2800 let next =
2801 if next < 0 || next >= itemcount
2802 then -1
2803 else find next incr1
2805 if next = -1 || abs (m_active - next) > fstate.maxrows
2806 then -1
2807 else next
2809 if next = -1
2810 then
2811 let first = m_first + incr in
2812 let first = bound first 0 (itemcount - 1) in
2813 let next =
2814 let next = m_active + incr in
2815 let next = bound next 0 (itemcount - 1) in
2816 find next ~-incr1
2818 let active = if next = -1 then m_active else next in
2819 active, first
2820 else
2821 let first = min next m_first in
2822 let first =
2823 if abs (next - first) > fstate.maxrows
2824 then first + incr
2825 else first
2827 next, first
2828 else
2829 let first = m_first + incr in
2830 let first = bound first 0 (itemcount - 1) in
2831 let active =
2832 let next = m_active + incr in
2833 let next = bound next 0 (itemcount - 1) in
2834 let next = find next incr1 in
2835 let active =
2836 if next = -1 || abs (m_active - first) > fstate.maxrows
2837 then (
2838 let active = if m_active = -1 then next else m_active in
2839 active
2841 else next
2843 if isvisible first active
2844 then active
2845 else -1
2847 active, first
2849 G.postRedisplay "listview navigate";
2850 set active first;
2852 begin match key with
2853 | Glut.KEY_UP -> navigate ~-1
2854 | Glut.KEY_DOWN -> navigate 1
2855 | Glut.KEY_PAGE_UP -> navigate ~-(fstate.maxrows)
2856 | Glut.KEY_PAGE_DOWN -> navigate fstate.maxrows
2858 | Glut.KEY_RIGHT ->
2859 state.text <- "";
2860 G.postRedisplay "listview right";
2861 coe {< m_pan = m_pan - 1 >}
2863 | Glut.KEY_LEFT ->
2864 state.text <- "";
2865 G.postRedisplay "listview left";
2866 coe {< m_pan = m_pan + 1 >}
2868 | Glut.KEY_HOME ->
2869 let active = find 0 1 in
2870 G.postRedisplay "listview home";
2871 set active 0;
2873 | Glut.KEY_END ->
2874 let first = max 0 (itemcount - fstate.maxrows) in
2875 let active = find (itemcount - 1) ~-1 in
2876 G.postRedisplay "listview end";
2877 set active first;
2879 | _ -> coe self
2880 end;
2882 method key key =
2883 match state.mode with
2884 | Textentry te -> textentrykeyboard key te; coe self
2885 | _ -> self#key1 key
2887 method special key =
2888 match state.mode with
2889 | Textentry te -> textentryspecial key te; coe self
2890 | _ -> self#special1 key
2892 method button button bstate x y =
2893 let opt =
2894 match button with
2895 | Glut.LEFT_BUTTON when x > conf.winw - conf.scrollbw ->
2896 G.postRedisplay "listview scroll";
2897 if bstate = Glut.DOWN
2898 then
2899 let _, position, sh = self#scrollph in
2900 if y > truncate position && y < truncate (position +. sh)
2901 then (
2902 state.mstate <- Mscrolly;
2903 Some (coe self)
2905 else
2906 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
2907 let first = truncate (s *. float source#getitemcount) in
2908 let first = min source#getitemcount first in
2909 Some (coe {< m_first = first; m_active = first >})
2910 else (
2911 state.mstate <- Mnone;
2912 Some (coe self);
2914 | Glut.LEFT_BUTTON when bstate = Glut.UP ->
2915 begin match self#elemunder y with
2916 | Some n ->
2917 G.postRedisplay "listview click";
2918 source#exit
2919 (coe {< m_active = n >}) false n m_first m_pan m_qsearch
2920 | _ ->
2921 Some (coe self)
2923 | Glut.OTHER_BUTTON n when (n == 3 || n == 4) && bstate = Glut.UP ->
2924 let len = source#getitemcount in
2925 let first =
2926 if n = 4 && m_first + fstate.maxrows >= len
2927 then
2928 m_first
2929 else
2930 let first = m_first + (if n == 3 then -1 else 1) in
2931 bound first 0 (len - 1)
2933 G.postRedisplay "listview wheel";
2934 Some (coe {< m_first = first >})
2935 | _ ->
2936 Some (coe self)
2938 match opt with
2939 | None -> m_prev_uioh
2940 | Some uioh -> uioh
2942 method motion _ y =
2943 match state.mstate with
2944 | Mscrolly ->
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 G.postRedisplay "listview motion";
2949 coe {< m_first = first; m_active = first >}
2950 | _ -> coe self
2952 method pmotion x y =
2953 if x < conf.winw - conf.scrollbw
2954 then
2955 let n =
2956 match self#elemunder y with
2957 | None -> Glut.setCursor Glut.CURSOR_INHERIT; m_active
2958 | Some n -> Glut.setCursor Glut.CURSOR_INFO; n
2960 let o =
2961 if n != m_active
2962 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
2963 else self
2965 coe o
2966 else (
2967 Glut.setCursor Glut.CURSOR_INHERIT;
2968 coe self
2971 method infochanged _ = ()
2973 method scrollpw = (0, 0.0, 0.0)
2974 method scrollph =
2975 let nfs = fstate.fontsize + 1 in
2976 let y = m_first * nfs in
2977 let itemcount = source#getitemcount in
2978 let maxi = max 0 (itemcount - fstate.maxrows) in
2979 let maxy = maxi * nfs in
2980 let p, h = scrollph y maxy in
2981 conf.scrollbw, p, h
2982 end;;
2984 class outlinelistview ~source =
2985 object (self)
2986 inherit listview ~source:(source :> lvsource) ~trusted:false as super
2988 method key key =
2989 match key with
2990 | 14 -> (* ctrl-n *)
2991 source#narrow m_qsearch;
2992 G.postRedisplay "outline ctrl-n";
2993 coe {< m_first = 0; m_active = 0 >}
2995 | 21 -> (* ctrl-u *)
2996 source#denarrow;
2997 G.postRedisplay "outline ctrl-u";
2998 state.text <- "";
2999 coe {< m_first = 0; m_active = 0 >}
3001 | 12 -> (* ctrl-l *)
3002 let first = m_active - (fstate.maxrows / 2) in
3003 G.postRedisplay "outline ctrl-l";
3004 coe {< m_first = first >}
3006 | 127 -> (* delete *)
3007 source#remove m_active;
3008 G.postRedisplay "outline delete";
3009 let active = max 0 (m_active-1) in
3010 coe {< m_first = firstof m_first active;
3011 m_active = active >}
3013 | key -> super#key key
3015 method special key =
3016 let calcfirst first active =
3017 if active > first
3018 then
3019 let rows = active - first in
3020 if rows > fstate.maxrows then active - fstate.maxrows else first
3021 else active
3023 let navigate incr =
3024 let active = m_active + incr in
3025 let active = bound active 0 (source#getitemcount - 1) in
3026 let first = calcfirst m_first active in
3027 G.postRedisplay "special outline navigate";
3028 coe {< m_active = active; m_first = first >}
3030 match key with
3031 | Glut.KEY_UP -> navigate ~-1
3032 | Glut.KEY_DOWN -> navigate 1
3033 | Glut.KEY_PAGE_UP -> navigate ~-(fstate.maxrows)
3034 | Glut.KEY_PAGE_DOWN -> navigate fstate.maxrows
3036 | Glut.KEY_RIGHT ->
3037 let o =
3038 if Glut.getModifiers () land Glut.active_ctrl != 0
3039 then (
3040 G.postRedisplay "special outline right";
3041 {< m_pan = m_pan + 1 >}
3043 else self#updownlevel 1
3045 coe o
3047 | Glut.KEY_LEFT ->
3048 let o =
3049 if Glut.getModifiers () land Glut.active_ctrl != 0
3050 then (
3051 G.postRedisplay "special outline left";
3052 {< m_pan = m_pan - 1 >}
3054 else self#updownlevel ~-1
3056 coe o
3058 | Glut.KEY_HOME ->
3059 G.postRedisplay "special outline home";
3060 coe {< m_first = 0; m_active = 0 >}
3062 | Glut.KEY_END ->
3063 let active = source#getitemcount - 1 in
3064 let first = max 0 (active - fstate.maxrows) in
3065 G.postRedisplay "special outline end";
3066 coe {< m_active = active; m_first = first >}
3068 | _ -> super#special key
3071 let outlinesource usebookmarks =
3072 let empty = [||] in
3073 (object
3074 inherit lvsourcebase
3075 val mutable m_items = empty
3076 val mutable m_orig_items = empty
3077 val mutable m_prev_items = empty
3078 val mutable m_narrow_pattern = ""
3079 val mutable m_hadremovals = false
3081 method getitemcount =
3082 Array.length m_items + (if m_hadremovals then 1 else 0)
3084 method getitem n =
3085 if n == Array.length m_items && m_hadremovals
3086 then
3087 ("[Confirm removal]", 0)
3088 else
3089 let s, n, _ = m_items.(n) in
3090 (s, n)
3092 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3093 ignore (uioh, first, pan, qsearch);
3094 let confrimremoval = m_hadremovals && active = Array.length m_items in
3095 let items =
3096 if String.length m_narrow_pattern = 0
3097 then m_orig_items
3098 else m_items
3100 if not cancel
3101 then (
3102 if not confrimremoval
3103 then(
3104 let _, _, anchor = m_items.(active) in
3105 gotoanchor anchor;
3106 m_items <- items;
3108 else (
3109 state.bookmarks <- Array.to_list m_items;
3110 m_orig_items <- m_items;
3113 else m_items <- items;
3114 None
3116 method hasaction _ = true
3118 method greetmsg =
3119 if Array.length m_items != Array.length m_orig_items
3120 then "Narrowed to " ^ m_narrow_pattern ^ " (ctrl-u to restore)"
3121 else ""
3123 method narrow pattern =
3124 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
3125 match reopt with
3126 | None -> ()
3127 | Some re ->
3128 let rec loop accu n =
3129 if n = -1
3130 then (
3131 m_narrow_pattern <- pattern;
3132 m_items <- Array.of_list accu
3134 else
3135 let (s, _, _) as o = m_items.(n) in
3136 let accu =
3137 if (try ignore (Str.search_forward re s 0); true
3138 with Not_found -> false)
3139 then o :: accu
3140 else accu
3142 loop accu (n-1)
3144 loop [] (Array.length m_items - 1)
3146 method denarrow =
3147 m_orig_items <- (
3148 if usebookmarks
3149 then Array.of_list state.bookmarks
3150 else state.outlines
3152 m_items <- m_orig_items
3154 method remove m =
3155 if usebookmarks
3156 then
3157 if m >= 0 && m < Array.length m_items
3158 then (
3159 m_hadremovals <- true;
3160 m_items <- Array.init (Array.length m_items - 1) (fun n ->
3161 let n = if n >= m then n+1 else n in
3162 m_items.(n)
3166 method reset anchor items =
3167 m_hadremovals <- false;
3168 if m_orig_items == empty || m_prev_items != items
3169 then (
3170 m_orig_items <- items;
3171 if String.length m_narrow_pattern = 0
3172 then m_items <- items;
3174 m_prev_items <- items;
3175 let rely = getanchory anchor in
3176 let active =
3177 let rec loop n best bestd =
3178 if n = Array.length m_items
3179 then best
3180 else
3181 let (_, _, anchor) = m_items.(n) in
3182 let orely = getanchory anchor in
3183 let d = abs (orely - rely) in
3184 if d < bestd
3185 then loop (n+1) n d
3186 else loop (n+1) best bestd
3188 loop 0 ~-1 max_int
3190 m_active <- active;
3191 m_first <- firstof m_first active
3192 end)
3195 let enterselector usebookmarks =
3196 let source = outlinesource usebookmarks in
3197 fun errmsg ->
3198 let outlines =
3199 if usebookmarks
3200 then Array.of_list state.bookmarks
3201 else state.outlines
3203 if Array.length outlines = 0
3204 then (
3205 showtext ' ' errmsg;
3207 else (
3208 state.text <- source#greetmsg;
3209 Glut.setCursor Glut.CURSOR_INHERIT;
3210 let anchor = getanchor () in
3211 source#reset anchor outlines;
3212 state.uioh <- coe (new outlinelistview ~source);
3213 G.postRedisplay "enter selector";
3217 let enteroutlinemode =
3218 let f = enterselector false in
3219 fun ()-> f "Document has no outline";
3222 let enterbookmarkmode =
3223 let f = enterselector true in
3224 fun () -> f "Document has no bookmarks (yet)";
3227 let color_of_string s =
3228 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
3229 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
3233 let color_to_string (r, g, b) =
3234 let r = truncate (r *. 256.0)
3235 and g = truncate (g *. 256.0)
3236 and b = truncate (b *. 256.0) in
3237 Printf.sprintf "%d/%d/%d" r g b
3240 let irect_of_string s =
3241 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
3244 let irect_to_string (x0,y0,x1,y1) =
3245 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
3248 let makecheckers () =
3249 (* Appropriated from lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
3250 following to say:
3251 converted by Issac Trotts. July 25, 2002 *)
3252 let image_height = 64
3253 and image_width = 64 in
3255 let make_image () =
3256 let image =
3257 GlPix.create `ubyte ~format:`rgb ~width:image_width ~height:image_height
3259 for i = 0 to image_width - 1 do
3260 for j = 0 to image_height - 1 do
3261 Raw.sets (GlPix.to_raw image) ~pos:(3*(i*image_height+j))
3262 (if (i land 8 ) lxor (j land 8) = 0
3263 then [|255;255;255|] else [|200;200;200|])
3264 done
3265 done;
3266 image
3268 let image = make_image () in
3269 let id = GlTex.gen_texture () in
3270 GlTex.bind_texture `texture_2d id;
3271 GlPix.store (`unpack_alignment 1);
3272 GlTex.image2d image;
3273 List.iter (GlTex.parameter ~target:`texture_2d)
3274 [ `wrap_s `repeat;
3275 `wrap_t `repeat;
3276 `mag_filter `nearest;
3277 `min_filter `nearest ];
3281 let setcheckers enabled =
3282 match state.texid with
3283 | None ->
3284 if enabled then state.texid <- Some (makecheckers ())
3286 | Some texid ->
3287 if not enabled
3288 then (
3289 GlTex.delete_texture texid;
3290 state.texid <- None;
3294 let int_of_string_with_suffix s =
3295 let l = String.length s in
3296 let s1, shift =
3297 if l > 1
3298 then
3299 let suffix = Char.lowercase s.[l-1] in
3300 match suffix with
3301 | 'k' -> String.sub s 0 (l-1), 10
3302 | 'm' -> String.sub s 0 (l-1), 20
3303 | 'g' -> String.sub s 0 (l-1), 30
3304 | _ -> s, 0
3305 else s, 0
3307 let n = int_of_string s1 in
3308 let m = n lsl shift in
3309 if m < 0 || m < n
3310 then raise (Failure "value too large")
3311 else m
3314 let string_with_suffix_of_int n =
3315 if n = 0
3316 then "0"
3317 else
3318 let n, s =
3319 if n = 0
3320 then 0, ""
3321 else (
3322 if n land ((1 lsl 20) - 1) = 0
3323 then n lsr 20, "M"
3324 else (
3325 if n land ((1 lsl 10) - 1) = 0
3326 then n lsr 10, "K"
3327 else n, ""
3331 let rec loop s n =
3332 let h = n mod 1000 in
3333 let n = n / 1000 in
3334 if n = 0
3335 then string_of_int h ^ s
3336 else (
3337 let s = Printf.sprintf "_%03d%s" h s in
3338 loop s n
3341 loop "" n ^ s;
3344 let defghyllscroll = (40, 8, 32);;
3345 let ghyllscroll_of_string s =
3346 let (n, a, b) as nab =
3347 if s = "default"
3348 then defghyllscroll
3349 else Scanf.sscanf s "%u,%u,%u" (fun n a b -> n, a, b)
3351 if n <= a || n <= b || a >= b
3352 then failwith "invalid ghyll N,A,B (N <= A, A < B, N <= B)";
3353 nab;
3356 let ghyllscroll_to_string ((n, a, b) as nab) =
3357 if nab = defghyllscroll
3358 then "default"
3359 else Printf.sprintf "%d,%d,%d" n a b;
3362 let describe_location () =
3363 let f (fn, _) l =
3364 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
3366 let fn, ln = List.fold_left f (-1, -1) state.layout in
3367 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
3368 let percent =
3369 if maxy <= 0
3370 then 100.
3371 else (100. *. (float state.y /. float maxy))
3373 if fn = ln
3374 then
3375 Printf.sprintf "page %d of %d [%.2f%%]"
3376 (fn+1) state.pagecount percent
3377 else
3378 Printf.sprintf
3379 "pages %d-%d of %d [%.2f%%]"
3380 (fn+1) (ln+1) state.pagecount percent
3383 let columns_to_string (n, a, b) =
3384 if a = 0 && b = 0
3385 then Printf.sprintf "%d" n
3386 else Printf.sprintf "%d,%d,%d" n a b;
3389 let columns_of_string s =
3391 (int_of_string s, 0, 0)
3392 with _ ->
3393 Scanf.sscanf s "%u,%u,%u" (fun n a b -> (n, a, b));
3396 let enterinfomode =
3397 let btos b = if b then "\xe2\x88\x9a" else "" in
3398 let showextended = ref false in
3399 let leave mode = function
3400 | Confirm -> state.mode <- mode
3401 | Cancel -> state.mode <- mode in
3402 let src =
3403 (object
3404 val mutable m_first_time = true
3405 val mutable m_l = []
3406 val mutable m_a = [||]
3407 val mutable m_prev_uioh = nouioh
3408 val mutable m_prev_mode = View
3410 inherit lvsourcebase
3412 method reset prev_mode prev_uioh =
3413 m_a <- Array.of_list (List.rev m_l);
3414 m_l <- [];
3415 m_prev_mode <- prev_mode;
3416 m_prev_uioh <- prev_uioh;
3417 if m_first_time
3418 then (
3419 let rec loop n =
3420 if n >= Array.length m_a
3421 then ()
3422 else
3423 match m_a.(n) with
3424 | _, _, _, Action _ -> m_active <- n
3425 | _ -> loop (n+1)
3427 loop 0;
3428 m_first_time <- false;
3431 method int name get set =
3432 m_l <-
3433 (name, `int get, 1, Action (
3434 fun u ->
3435 let ondone s =
3436 try set (int_of_string s)
3437 with exn ->
3438 state.text <- Printf.sprintf "bad integer `%s': %s"
3439 s (Printexc.to_string exn)
3441 state.text <- "";
3442 let te = name ^ ": ", "", None, intentry, ondone in
3443 state.mode <- Textentry (te, leave m_prev_mode);
3445 )) :: m_l
3447 method int_with_suffix name get set =
3448 m_l <-
3449 (name, `intws get, 1, Action (
3450 fun u ->
3451 let ondone s =
3452 try set (int_of_string_with_suffix s)
3453 with exn ->
3454 state.text <- Printf.sprintf "bad integer `%s': %s"
3455 s (Printexc.to_string exn)
3457 state.text <- "";
3458 let te =
3459 name ^ ": ", "", None, intentry_with_suffix, ondone
3461 state.mode <- Textentry (te, leave m_prev_mode);
3463 )) :: m_l
3465 method bool ?(offset=1) ?(btos=btos) name get set =
3466 m_l <-
3467 (name, `bool (btos, get), offset, Action (
3468 fun u ->
3469 let v = get () in
3470 set (not v);
3472 )) :: m_l
3474 method color name get set =
3475 m_l <-
3476 (name, `color get, 1, Action (
3477 fun u ->
3478 let invalid = (nan, nan, nan) in
3479 let ondone s =
3480 let c =
3481 try color_of_string s
3482 with exn ->
3483 state.text <- Printf.sprintf "bad color `%s': %s"
3484 s (Printexc.to_string exn);
3485 invalid
3487 if c <> invalid
3488 then set c;
3490 let te = name ^ ": ", "", None, textentry, ondone in
3491 state.text <- color_to_string (get ());
3492 state.mode <- Textentry (te, leave m_prev_mode);
3494 )) :: m_l
3496 method string name get set =
3497 m_l <-
3498 (name, `string get, 1, Action (
3499 fun u ->
3500 let ondone s = set s in
3501 let te = name ^ ": ", "", None, textentry, ondone in
3502 state.mode <- Textentry (te, leave m_prev_mode);
3504 )) :: m_l
3506 method colorspace name get set =
3507 m_l <-
3508 (name, `string get, 1, Action (
3509 fun _ ->
3510 let source =
3511 let vals = [| "rgb"; "bgr"; "gray" |] in
3512 (object
3513 inherit lvsourcebase
3515 initializer
3516 m_active <- int_of_colorspace conf.colorspace;
3517 m_first <- 0;
3519 method getitemcount = Array.length vals
3520 method getitem n = (vals.(n), 0)
3521 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3522 ignore (uioh, first, pan, qsearch);
3523 if not cancel then set active;
3524 None
3525 method hasaction _ = true
3526 end)
3528 state.text <- "";
3529 coe (new listview ~source ~trusted:true)
3530 )) :: m_l
3532 method caption s offset =
3533 m_l <- (s, `empty, offset, Noaction) :: m_l
3535 method caption2 s f offset =
3536 m_l <- (s, `string f, offset, Noaction) :: m_l
3538 method getitemcount = Array.length m_a
3540 method getitem n =
3541 let tostr = function
3542 | `int f -> string_of_int (f ())
3543 | `intws f -> string_with_suffix_of_int (f ())
3544 | `string f -> f ()
3545 | `color f -> color_to_string (f ())
3546 | `bool (btos, f) -> btos (f ())
3547 | `empty -> ""
3549 let name, t, offset, _ = m_a.(n) in
3550 ((let s = tostr t in
3551 if String.length s > 0
3552 then Printf.sprintf "%s\t%s" name s
3553 else name),
3554 offset)
3556 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3557 let uiohopt =
3558 if not cancel
3559 then (
3560 m_qsearch <- qsearch;
3561 let uioh =
3562 match m_a.(active) with
3563 | _, _, _, Action f -> f uioh
3564 | _ -> uioh
3566 Some uioh
3568 else None
3570 m_active <- active;
3571 m_first <- first;
3572 m_pan <- pan;
3573 uiohopt
3575 method hasaction n =
3576 match m_a.(n) with
3577 | _, _, _, Action _ -> true
3578 | _ -> false
3579 end)
3581 let rec fillsrc prevmode prevuioh =
3582 let sep () = src#caption "" 0 in
3583 let colorp name get set =
3584 src#string name
3585 (fun () -> color_to_string (get ()))
3586 (fun v ->
3588 let c = color_of_string v in
3589 set c
3590 with exn ->
3591 state.text <- Printf.sprintf "bad color `%s': %s"
3592 v (Printexc.to_string exn);
3595 let oldmode = state.mode in
3596 let birdseye = isbirdseye state.mode in
3598 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
3600 src#bool "presentation mode"
3601 (fun () -> conf.presentation)
3602 (fun v ->
3603 conf.presentation <- v;
3604 state.anchor <- getanchor ();
3605 represent ());
3607 src#bool "ignore case in searches"
3608 (fun () -> conf.icase)
3609 (fun v -> conf.icase <- v);
3611 src#bool "preload"
3612 (fun () -> conf.preload)
3613 (fun v -> conf.preload <- v);
3615 src#bool "highlight links"
3616 (fun () -> conf.hlinks)
3617 (fun v -> conf.hlinks <- v);
3619 src#bool "under info"
3620 (fun () -> conf.underinfo)
3621 (fun v -> conf.underinfo <- v);
3623 src#bool "persistent bookmarks"
3624 (fun () -> conf.savebmarks)
3625 (fun v -> conf.savebmarks <- v);
3627 src#bool "proportional display"
3628 (fun () -> conf.proportional)
3629 (fun v -> reqlayout conf.angle v);
3631 src#bool "trim margins"
3632 (fun () -> conf.trimmargins)
3633 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
3635 src#bool "persistent location"
3636 (fun () -> conf.jumpback)
3637 (fun v -> conf.jumpback <- v);
3639 sep ();
3640 src#int "inter-page space"
3641 (fun () -> conf.interpagespace)
3642 (fun n ->
3643 conf.interpagespace <- n;
3644 let pageno, py =
3645 match state.layout with
3646 | [] -> 0, 0
3647 | l :: _ ->
3648 l.pageno, l.pagey
3650 state.maxy <- calcheight ();
3651 let y = getpagey pageno in
3652 gotoy (y + py)
3655 src#int "page bias"
3656 (fun () -> conf.pagebias)
3657 (fun v -> conf.pagebias <- v);
3659 src#int "scroll step"
3660 (fun () -> conf.scrollstep)
3661 (fun n -> conf.scrollstep <- n);
3663 src#int "auto scroll step"
3664 (fun () ->
3665 match state.autoscroll with
3666 | Some step -> step
3667 | _ -> conf.autoscrollstep)
3668 (fun n ->
3669 if state.autoscroll <> None
3670 then state.autoscroll <- Some n;
3671 conf.autoscrollstep <- n);
3673 src#int "zoom"
3674 (fun () -> truncate (conf.zoom *. 100.))
3675 (fun v -> setzoom ((float v) /. 100.));
3677 src#int "rotation"
3678 (fun () -> conf.angle)
3679 (fun v -> reqlayout v conf.proportional);
3681 src#int "scroll bar width"
3682 (fun () -> state.scrollw)
3683 (fun v ->
3684 state.scrollw <- v;
3685 conf.scrollbw <- v;
3686 reshape conf.winw conf.winh;
3689 src#int "scroll handle height"
3690 (fun () -> conf.scrollh)
3691 (fun v -> conf.scrollh <- v;);
3693 src#int "thumbnail width"
3694 (fun () -> conf.thumbw)
3695 (fun v ->
3696 conf.thumbw <- min 4096 v;
3697 match oldmode with
3698 | Birdseye beye ->
3699 leavebirdseye beye false;
3700 enterbirdseye ()
3701 | _ -> ()
3704 src#string "columns"
3705 (fun () ->
3706 match conf.columns with
3707 | None -> "1"
3708 | Some (multicol, _) -> columns_to_string multicol)
3709 (fun v ->
3710 let n, a, b = columns_of_string v in
3711 setcolumns n a b);
3713 sep ();
3714 src#caption "Presentation mode" 0;
3715 src#bool "scrollbar visible"
3716 (fun () -> conf.scrollbarinpm)
3717 (fun v ->
3718 if v != conf.scrollbarinpm
3719 then (
3720 conf.scrollbarinpm <- v;
3721 if conf.presentation
3722 then (
3723 state.scrollw <- if v then conf.scrollbw else 0;
3724 reshape conf.winw conf.winh;
3729 sep ();
3730 src#caption "Pixmap cache" 0;
3731 src#int_with_suffix "size (advisory)"
3732 (fun () -> conf.memlimit)
3733 (fun v -> conf.memlimit <- v);
3735 src#caption2 "used"
3736 (fun () -> Printf.sprintf "%s bytes, %d tiles"
3737 (string_with_suffix_of_int state.memused)
3738 (Hashtbl.length state.tilemap)) 1;
3740 sep ();
3741 src#caption "Layout" 0;
3742 src#caption2 "Dimension"
3743 (fun () ->
3744 Printf.sprintf "%dx%d (virtual %dx%d)"
3745 conf.winw conf.winh
3746 state.w state.maxy)
3748 if conf.debug
3749 then
3750 src#caption2 "Position" (fun () ->
3751 Printf.sprintf "%dx%d" state.x state.y
3753 else
3754 src#caption2 "Visible" (fun () -> describe_location ()) 1
3757 sep ();
3758 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
3759 "Save these parameters as global defaults at exit"
3760 (fun () -> conf.bedefault)
3761 (fun v -> conf.bedefault <- v)
3764 sep ();
3765 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
3766 src#bool ~offset:0 ~btos "Extended parameters"
3767 (fun () -> !showextended)
3768 (fun v -> showextended := v; fillsrc prevmode prevuioh);
3769 if !showextended
3770 then (
3771 src#bool "checkers"
3772 (fun () -> conf.checkers)
3773 (fun v -> conf.checkers <- v; setcheckers v);
3774 src#bool "verbose"
3775 (fun () -> conf.verbose)
3776 (fun v -> conf.verbose <- v);
3777 src#bool "invert colors"
3778 (fun () -> conf.invert)
3779 (fun v -> conf.invert <- v);
3780 src#bool "max fit"
3781 (fun () -> conf.maxhfit)
3782 (fun v -> conf.maxhfit <- v);
3783 src#bool "redirect stderr"
3784 (fun () -> conf.redirectstderr)
3785 (fun v -> conf.redirectstderr <- v; redirectstderr ());
3786 src#string "uri launcher"
3787 (fun () -> conf.urilauncher)
3788 (fun v -> conf.urilauncher <- v);
3789 src#string "tile size"
3790 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
3791 (fun v ->
3793 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
3794 conf.tileh <- max 64 w;
3795 conf.tilew <- max 64 h;
3796 flushtiles ();
3797 with exn ->
3798 state.text <- Printf.sprintf "bad tile size `%s': %s"
3799 v (Printexc.to_string exn));
3800 src#int "texture count"
3801 (fun () -> conf.texcount)
3802 (fun v ->
3803 if realloctexts v
3804 then conf.texcount <- v
3805 else showtext '!' " Failed to set texture count please retry later"
3807 src#int "slice height"
3808 (fun () -> conf.sliceheight)
3809 (fun v ->
3810 conf.sliceheight <- v;
3811 wcmd "sliceh" [`i conf.sliceheight];
3813 src#int "anti-aliasing level"
3814 (fun () -> conf.aalevel)
3815 (fun v ->
3816 conf.aalevel <- bound v 0 8;
3817 state.anchor <- getanchor ();
3818 opendoc state.path state.password;
3820 src#int "ui font size"
3821 (fun () -> fstate.fontsize)
3822 (fun v -> setfontsize (bound v 5 100));
3823 colorp "background color"
3824 (fun () -> conf.bgcolor)
3825 (fun v -> conf.bgcolor <- v);
3826 src#bool "crop hack"
3827 (fun () -> conf.crophack)
3828 (fun v -> conf.crophack <- v);
3829 src#string "trim fuzz"
3830 (fun () -> irect_to_string conf.trimfuzz)
3831 (fun v ->
3833 conf.trimfuzz <- irect_of_string v;
3834 if conf.trimmargins
3835 then settrim true conf.trimfuzz;
3836 with exn ->
3837 state.text <- Printf.sprintf "bad irect `%s': %s"
3838 v (Printexc.to_string exn)
3840 src#string "throttle"
3841 (fun () ->
3842 match conf.maxwait with
3843 | None -> "show place holder if page is not ready"
3844 | Some time ->
3845 if time = infinity
3846 then "wait for page to fully render"
3847 else
3848 "wait " ^ string_of_float time
3849 ^ " seconds before showing placeholder"
3851 (fun v ->
3853 let f = float_of_string v in
3854 if f <= 0.0
3855 then conf.maxwait <- None
3856 else conf.maxwait <- Some f
3857 with exn ->
3858 state.text <- Printf.sprintf "bad time `%s': %s"
3859 v (Printexc.to_string exn)
3861 src#string "ghyll scroll"
3862 (fun () ->
3863 match conf.ghyllscroll with
3864 | None -> ""
3865 | Some nab -> ghyllscroll_to_string nab
3867 (fun v ->
3869 let gs =
3870 if String.length v = 0
3871 then None
3872 else Some (ghyllscroll_of_string v)
3874 conf.ghyllscroll <- gs
3875 with exn ->
3876 state.text <- Printf.sprintf "bad ghyll `%s': %s"
3877 v (Printexc.to_string exn)
3879 src#colorspace "color space"
3880 (fun () -> colorspace_to_string conf.colorspace)
3881 (fun v ->
3882 conf.colorspace <- colorspace_of_int v;
3883 wcmd "cs" [`i v];
3884 load state.layout;
3888 sep ();
3889 src#caption "Document" 0;
3890 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
3891 src#caption2 "Pages"
3892 (fun () -> string_of_int state.pagecount) 1;
3893 src#caption2 "Dimensions"
3894 (fun () -> string_of_int (List.length state.pdims)) 1;
3895 if conf.trimmargins
3896 then (
3897 sep ();
3898 src#caption "Trimmed margins" 0;
3899 src#caption2 "Dimensions"
3900 (fun () -> string_of_int (List.length state.pdims)) 1;
3903 src#reset prevmode prevuioh;
3905 fun () ->
3906 state.text <- "";
3907 let prevmode = state.mode
3908 and prevuioh = state.uioh in
3909 fillsrc prevmode prevuioh;
3910 let source = (src :> lvsource) in
3911 state.uioh <- coe (object (self)
3912 inherit listview ~source ~trusted:true as super
3913 val mutable m_prevmemused = 0
3914 method infochanged = function
3915 | Memused ->
3916 if m_prevmemused != state.memused
3917 then (
3918 m_prevmemused <- state.memused;
3919 G.postRedisplay "memusedchanged";
3921 | Pdim -> G.postRedisplay "pdimchanged"
3922 | Docinfo -> fillsrc prevmode prevuioh
3924 method special key =
3925 if Glut.getModifiers () land Glut.active_ctrl = 0
3926 then
3927 match key with
3928 | Glut.KEY_LEFT -> coe (self#updownlevel ~-1)
3929 | Glut.KEY_RIGHT -> coe (self#updownlevel 1)
3930 | _ -> super#special key
3931 else super#special key
3932 end);
3933 G.postRedisplay "info";
3936 let enterhelpmode =
3937 let source =
3938 (object
3939 inherit lvsourcebase
3940 method getitemcount = Array.length state.help
3941 method getitem n =
3942 let s, n, _ = state.help.(n) in
3943 (s, n)
3945 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3946 let optuioh =
3947 if not cancel
3948 then (
3949 m_qsearch <- qsearch;
3950 match state.help.(active) with
3951 | _, _, Action f -> Some (f uioh)
3952 | _ -> Some (uioh)
3954 else None
3956 m_active <- active;
3957 m_first <- first;
3958 m_pan <- pan;
3959 optuioh
3961 method hasaction n =
3962 match state.help.(n) with
3963 | _, _, Action _ -> true
3964 | _ -> false
3966 initializer
3967 m_active <- -1
3968 end)
3969 in fun () ->
3970 state.uioh <- coe (new listview ~source ~trusted:true);
3971 G.postRedisplay "help";
3974 let entermsgsmode =
3975 let msgsource =
3976 let re = Str.regexp "[\r\n]" in
3977 (object
3978 inherit lvsourcebase
3979 val mutable m_items = [||]
3981 method getitemcount = 1 + Array.length m_items
3983 method getitem n =
3984 if n = 0
3985 then "[Clear]", 0
3986 else m_items.(n-1), 0
3988 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3989 ignore uioh;
3990 if not cancel
3991 then (
3992 if active = 0
3993 then Buffer.clear state.errmsgs;
3994 m_qsearch <- qsearch;
3996 m_active <- active;
3997 m_first <- first;
3998 m_pan <- pan;
3999 None
4001 method hasaction n =
4002 n = 0
4004 method reset =
4005 state.newerrmsgs <- false;
4006 let l = Str.split re (Buffer.contents state.errmsgs) in
4007 m_items <- Array.of_list l
4009 initializer
4010 m_active <- 0
4011 end)
4012 in fun () ->
4013 state.text <- "";
4014 msgsource#reset;
4015 let source = (msgsource :> lvsource) in
4016 state.uioh <- coe (object
4017 inherit listview ~source ~trusted:false as super
4018 method display =
4019 if state.newerrmsgs
4020 then msgsource#reset;
4021 super#display
4022 end);
4023 G.postRedisplay "msgs";
4026 let quickbookmark ?title () =
4027 match state.layout with
4028 | [] -> ()
4029 | l :: _ ->
4030 let title =
4031 match title with
4032 | None ->
4033 let sec = Unix.gettimeofday () in
4034 let tm = Unix.localtime sec in
4035 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
4036 (l.pageno+1)
4037 tm.Unix.tm_mday
4038 tm.Unix.tm_mon
4039 (tm.Unix.tm_year + 1900)
4040 tm.Unix.tm_hour
4041 tm.Unix.tm_min
4042 | Some title -> title
4044 state.bookmarks <-
4045 (title, 0, (l.pageno, float l.pagey /. float l.pageh))
4046 :: state.bookmarks
4049 let doreshape w h =
4050 state.fullscreen <- None;
4051 Glut.reshapeWindow w h;
4054 let viewkeyboard key =
4055 let enttext te =
4056 let mode = state.mode in
4057 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
4058 state.text <- "";
4059 enttext ();
4060 G.postRedisplay "view:enttext"
4062 let c = Char.chr key in
4063 match c with
4064 | '\027' | 'q' -> (* escape *)
4065 begin match state.mstate with
4066 | Mzoomrect _ ->
4067 state.mstate <- Mnone;
4068 Glut.setCursor Glut.CURSOR_INHERIT;
4069 G.postRedisplay "kill zoom rect";
4070 | _ ->
4071 raise Quit
4072 end;
4074 | '\008' -> (* backspace *)
4075 let y = getnav ~-1 in
4076 gotoy_and_clear_text y
4078 | 'o' ->
4079 enteroutlinemode ()
4081 | 'u' ->
4082 state.rects <- [];
4083 state.text <- "";
4084 G.postRedisplay "dehighlight";
4086 | '/' | '?' ->
4087 let ondone isforw s =
4088 cbput state.hists.pat s;
4089 state.searchpattern <- s;
4090 search s isforw
4092 let s = String.create 1 in
4093 s.[0] <- c;
4094 enttext (s, "", Some (onhist state.hists.pat),
4095 textentry, ondone (c ='/'))
4097 | '+' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
4098 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
4099 setzoom (conf.zoom +. incr)
4101 | '+' ->
4102 let ondone s =
4103 let n =
4104 try int_of_string s with exc ->
4105 state.text <- Printf.sprintf "bad integer `%s': %s"
4106 s (Printexc.to_string exc);
4107 max_int
4109 if n != max_int
4110 then (
4111 conf.pagebias <- n;
4112 state.text <- "page bias is now " ^ string_of_int n;
4115 enttext ("page bias: ", "", None, intentry, ondone)
4117 | '-' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
4118 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
4119 setzoom (max 0.01 (conf.zoom -. decr))
4121 | '-' ->
4122 let ondone msg = state.text <- msg in
4123 enttext (
4124 "option [acfhilpstvAPRSZTI]: ", "", None,
4125 optentry state.mode, ondone
4128 | '0' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
4129 setzoom 1.0
4131 | '1' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
4132 let zoom = zoomforh conf.winw conf.winh state.scrollw in
4133 if zoom < 1.0
4134 then setzoom zoom
4136 | '9' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
4137 togglebirdseye ()
4139 | '0' .. '9' ->
4140 let ondone s =
4141 let n =
4142 try int_of_string s with exc ->
4143 state.text <- Printf.sprintf "bad integer `%s': %s"
4144 s (Printexc.to_string exc);
4147 if n >= 0
4148 then (
4149 addnav ();
4150 cbput state.hists.pag (string_of_int n);
4151 gotopage1 (n + conf.pagebias - 1) 0;
4154 let pageentry text key =
4155 match Char.unsafe_chr key with
4156 | 'g' -> TEdone text
4157 | _ -> intentry text key
4159 let text = "x" in text.[0] <- c;
4160 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone)
4162 | 'b' ->
4163 state.scrollw <- if state.scrollw > 0 then 0 else conf.scrollbw;
4164 reshape conf.winw conf.winh;
4166 | 'l' ->
4167 conf.hlinks <- not conf.hlinks;
4168 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
4169 G.postRedisplay "toggle highlightlinks";
4171 | 'a' ->
4172 begin match state.autoscroll with
4173 | Some step ->
4174 conf.autoscrollstep <- step;
4175 state.autoscroll <- None
4176 | None ->
4177 if conf.autoscrollstep = 0
4178 then state.autoscroll <- Some 1
4179 else state.autoscroll <- Some conf.autoscrollstep
4182 | 'P' ->
4183 conf.presentation <- not conf.presentation;
4184 if conf.presentation
4185 then (
4186 if not conf.scrollbarinpm
4187 then state.scrollw <- 0;
4189 else
4190 state.scrollw <- conf.scrollbw;
4192 showtext ' ' ("presentation mode " ^
4193 if conf.presentation then "on" else "off");
4194 state.anchor <- getanchor ();
4195 represent ()
4197 | 'f' ->
4198 begin match state.fullscreen with
4199 | None ->
4200 state.fullscreen <- Some (conf.winw, conf.winh);
4201 Glut.fullScreen ()
4202 | Some (w, h) ->
4203 state.fullscreen <- None;
4204 doreshape w h
4207 | 'g' ->
4208 gotoy_and_clear_text 0
4210 | 'G' ->
4211 gotopage1 (state.pagecount - 1) 0
4213 | 'n' ->
4214 search state.searchpattern true
4216 | 'p' | 'N' ->
4217 search state.searchpattern false
4219 | 't' ->
4220 begin match state.layout with
4221 | [] -> ()
4222 | l :: _ ->
4223 gotoy_and_clear_text (getpagey l.pageno)
4226 | ' ' ->
4227 begin match List.rev state.layout with
4228 | [] -> ()
4229 | l :: _ ->
4230 let pageno = min (l.pageno+1) (state.pagecount-1) in
4231 gotoy_and_clear_text (getpagey pageno)
4234 | '\127' -> (* del *)
4235 begin match state.layout with
4236 | [] -> ()
4237 | l :: _ ->
4238 let pageno = max 0 (l.pageno-1) in
4239 gotoy_and_clear_text (getpagey pageno)
4242 | '=' ->
4243 showtext ' ' (describe_location ());
4245 | 'w' ->
4246 begin match state.layout with
4247 | [] -> ()
4248 | l :: _ ->
4249 doreshape (l.pagew + state.scrollw) l.pageh;
4250 G.postRedisplay "w"
4253 | '\'' ->
4254 enterbookmarkmode ()
4256 | 'h' ->
4257 enterhelpmode ()
4259 | 'i' ->
4260 enterinfomode ()
4262 | 'e' when conf.redirectstderr ->
4263 entermsgsmode ()
4265 | 'm' ->
4266 let ondone s =
4267 match state.layout with
4268 | l :: _ ->
4269 state.bookmarks <-
4270 (s, 0, (l.pageno, float l.pagey /. float l.pageh))
4271 :: state.bookmarks
4272 | _ -> ()
4274 enttext ("bookmark: ", "", None, textentry, ondone)
4276 | '~' ->
4277 quickbookmark ();
4278 showtext ' ' "Quick bookmark added";
4280 | 'z' ->
4281 begin match state.layout with
4282 | l :: _ ->
4283 let rect = getpdimrect l.pagedimno in
4284 let w, h =
4285 if conf.crophack
4286 then
4287 (truncate (1.8 *. (rect.(1) -. rect.(0))),
4288 truncate (1.2 *. (rect.(3) -. rect.(0))))
4289 else
4290 (truncate (rect.(1) -. rect.(0)),
4291 truncate (rect.(3) -. rect.(0)))
4293 let w = truncate ((float w)*.conf.zoom)
4294 and h = truncate ((float h)*.conf.zoom) in
4295 if w != 0 && h != 0
4296 then (
4297 state.anchor <- getanchor ();
4298 doreshape (w + state.scrollw) (h + conf.interpagespace)
4300 G.postRedisplay "z";
4302 | [] -> ()
4305 | '\000' -> (* ctrl-2 *)
4306 let maxw = getmaxw () in
4307 if maxw > 0.0
4308 then setzoom (maxw /. float conf.winw)
4310 | '<' | '>' ->
4311 reqlayout (conf.angle + (if c = '>' then 30 else -30)) conf.proportional
4313 | '[' | ']' ->
4314 conf.colorscale <-
4315 bound (conf.colorscale +. (if c = ']' then 0.1 else -0.1)) 0.0 1.0
4317 G.postRedisplay "brightness";
4319 | 'k' ->
4320 begin match state.mode with
4321 | Birdseye beye -> upbirdseye 1 beye
4322 | _ -> gotoy (clamp (-conf.scrollstep))
4325 | 'j' ->
4326 begin match state.mode with
4327 | Birdseye beye -> downbirdseye 1 beye
4328 | _ -> gotoy (clamp conf.scrollstep)
4331 | 'r' ->
4332 state.anchor <- getanchor ();
4333 opendoc state.path state.password
4335 | 'v' when not conf.debug ->
4336 List.iter debugl state.layout;
4338 | 'v' when conf.debug ->
4339 state.rects <- [];
4340 List.iter (fun l ->
4341 match getopaque l.pageno with
4342 | None -> ()
4343 | Some opaque ->
4344 let x0, y0, x1, y1 = pagebbox opaque in
4345 let a,b = float x0, float y0 in
4346 let c,d = float x1, float y0 in
4347 let e,f = float x1, float y1 in
4348 let h,j = float x0, float y1 in
4349 let rect = (a,b,c,d,e,f,h,j) in
4350 debugrect rect;
4351 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
4352 ) state.layout;
4353 G.postRedisplay "v";
4355 | _ ->
4356 vlog "huh? %d %c" key (Char.chr key);
4359 let birdseyekeyboard key ((_, _, pageno, _, _) as beye) =
4360 match key with
4361 | 27 -> (* escape *)
4362 leavebirdseye beye true
4364 | 12 -> (* ctrl-l *)
4365 let y, h = getpageyh pageno in
4366 let top = (conf.winh - h) / 2 in
4367 gotoy (max 0 (y - top))
4369 | 13 -> (* enter *)
4370 leavebirdseye beye false
4372 | _ ->
4373 viewkeyboard key
4376 let keyboard ~key ~x ~y =
4377 ignore x;
4378 ignore y;
4379 if key = 7 && not (istextentry state.mode) (* ctrl-g *)
4380 then wcmd "interrupt" []
4381 else state.uioh <- state.uioh#key key
4384 let birdseyespecial key ((oconf, leftx, _, hooverpageno, anchor) as beye) =
4385 let incr =
4386 match conf.columns with
4387 | None -> 1
4388 | Some ((c, _, _), _) -> c
4390 match key with
4391 | Glut.KEY_UP -> upbirdseye incr beye
4392 | Glut.KEY_DOWN -> downbirdseye incr beye
4393 | Glut.KEY_LEFT -> upbirdseye 1 beye
4394 | Glut.KEY_RIGHT -> downbirdseye 1 beye
4396 | Glut.KEY_PAGE_UP ->
4397 begin match state.layout with
4398 | l :: _ ->
4399 if l.pagey != 0
4400 then (
4401 state.mode <- Birdseye (
4402 oconf, leftx, l.pageno, hooverpageno, anchor
4404 gotopage1 l.pageno 0;
4406 else (
4407 let layout = layout (state.y-conf.winh) conf.winh in
4408 match layout with
4409 | [] -> gotoy (clamp (-conf.winh))
4410 | l :: _ ->
4411 state.mode <- Birdseye (
4412 oconf, leftx, l.pageno, hooverpageno, anchor
4414 gotopage1 l.pageno 0
4417 | [] -> gotoy (clamp (-conf.winh))
4418 end;
4420 | Glut.KEY_PAGE_DOWN ->
4421 begin match List.rev state.layout with
4422 | l :: _ ->
4423 let layout = layout (state.y + conf.winh) conf.winh in
4424 begin match layout with
4425 | [] ->
4426 let incr = l.pageh - l.pagevh in
4427 if incr = 0
4428 then (
4429 state.mode <-
4430 Birdseye (
4431 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
4433 G.postRedisplay "birdseye pagedown";
4435 else gotoy (clamp (incr + conf.interpagespace*2));
4437 | l :: _ ->
4438 state.mode <-
4439 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
4440 gotopage1 l.pageno 0;
4443 | [] -> gotoy (clamp conf.winh)
4444 end;
4446 | Glut.KEY_HOME ->
4447 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
4448 gotopage1 0 0
4450 | Glut.KEY_END ->
4451 let pageno = state.pagecount - 1 in
4452 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
4453 if not (pagevisible state.layout pageno)
4454 then
4455 let h =
4456 match List.rev state.pdims with
4457 | [] -> conf.winh
4458 | (_, _, h, _) :: _ -> h
4460 gotoy (max 0 (getpagey pageno - (conf.winh - h - conf.interpagespace)))
4461 else G.postRedisplay "birdseye end";
4462 | _ -> ()
4465 let setautoscrollspeed step goingdown =
4466 let incr = max 1 ((abs step) / 2) in
4467 let incr = if goingdown then incr else -incr in
4468 let astep = step + incr in
4469 state.autoscroll <- Some astep;
4472 let special ~key ~x ~y =
4473 ignore x;
4474 ignore y;
4475 state.uioh <- state.uioh#special key
4478 let drawpage l =
4479 let color =
4480 match state.mode with
4481 | Textentry _ -> scalecolor 0.4
4482 | View -> scalecolor 1.0
4483 | Birdseye (_, _, pageno, hooverpageno, _) ->
4484 if l.pageno = hooverpageno
4485 then scalecolor 0.9
4486 else (
4487 if l.pageno = pageno
4488 then scalecolor 1.0
4489 else scalecolor 0.8
4492 drawtiles l color;
4493 begin match getopaque l.pageno with
4494 | Some opaque ->
4495 if tileready l l.pagex l.pagey
4496 then
4497 let x = l.pagedispx - l.pagex
4498 and y = l.pagedispy - l.pagey in
4499 postprocess opaque conf.hlinks x y;
4501 | _ -> ()
4502 end;
4505 let scrollindicator () =
4506 let sbw, ph, sh = state.uioh#scrollph in
4507 let sbh, pw, sw = state.uioh#scrollpw in
4509 GlDraw.color (0.64, 0.64, 0.64);
4510 GlDraw.rect
4511 (float (conf.winw - sbw), 0.)
4512 (float conf.winw, float conf.winh)
4514 GlDraw.rect
4515 (0., float (conf.winh - sbh))
4516 (float (conf.winw - state.scrollw - 1), float conf.winh)
4518 GlDraw.color (0.0, 0.0, 0.0);
4520 GlDraw.rect
4521 (float (conf.winw - sbw), ph)
4522 (float conf.winw, ph +. sh)
4524 GlDraw.rect
4525 (pw, float (conf.winh - sbh))
4526 (pw +. sw, float conf.winh)
4530 let pagetranslatepoint l x y =
4531 let dy = y - l.pagedispy in
4532 let y = dy + l.pagey in
4533 let dx = x - l.pagedispx in
4534 let x = dx + l.pagex in
4535 (x, y);
4538 let showsel () =
4539 match state.mstate with
4540 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
4543 | Msel ((x0, y0), (x1, y1)) ->
4544 let rec loop = function
4545 | l :: ls ->
4546 if ((y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
4547 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh))))
4548 && ((x0 >= l.pagedispx && x0 <= (l.pagedispx + l.pagevw))
4549 || ((x1 >= l.pagedispx && x1 <= (l.pagedispx + l.pagevw))))
4550 then
4551 match getopaque l.pageno with
4552 | Some opaque ->
4553 let dx, dy = pagetranslatepoint l 0 0 in
4554 let x0 = x0 + dx
4555 and y0 = y0 + dy
4556 and x1 = x1 + dx
4557 and y1 = y1 + dy in
4558 GlMat.mode `modelview;
4559 GlMat.push ();
4560 GlMat.translate ~x:(float ~-dx) ~y:(float ~-dy) ();
4561 seltext opaque (x0, y0, x1, y1);
4562 GlMat.pop ();
4563 | _ -> ()
4564 else loop ls
4565 | [] -> ()
4567 loop state.layout
4570 let showrects () =
4571 Gl.enable `blend;
4572 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
4573 GlDraw.polygon_mode `both `fill;
4574 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
4575 List.iter
4576 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
4577 List.iter (fun l ->
4578 if l.pageno = pageno
4579 then (
4580 let dx = float (l.pagedispx - l.pagex) in
4581 let dy = float (l.pagedispy - l.pagey) in
4582 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
4583 GlDraw.begins `quads;
4585 GlDraw.vertex2 (x0+.dx, y0+.dy);
4586 GlDraw.vertex2 (x1+.dx, y1+.dy);
4587 GlDraw.vertex2 (x2+.dx, y2+.dy);
4588 GlDraw.vertex2 (x3+.dx, y3+.dy);
4590 GlDraw.ends ();
4592 ) state.layout
4593 ) state.rects
4595 Gl.disable `blend;
4598 let display () =
4599 GlClear.color (scalecolor2 conf.bgcolor);
4600 GlClear.clear [`color];
4601 List.iter drawpage state.layout;
4602 showrects ();
4603 showsel ();
4604 state.uioh#display;
4605 scrollindicator ();
4606 begin match state.mstate with
4607 | Mzoomrect ((x0, y0), (x1, y1)) ->
4608 Gl.enable `blend;
4609 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
4610 GlDraw.polygon_mode `both `fill;
4611 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
4612 GlDraw.rect (float x0, float y0)
4613 (float x1, float y1);
4614 Gl.disable `blend;
4615 | _ -> ()
4616 end;
4617 enttext ();
4618 Glut.swapBuffers ();
4621 let getunder x y =
4622 let rec f = function
4623 | l :: rest ->
4624 begin match getopaque l.pageno with
4625 | Some opaque ->
4626 let x0 = l.pagedispx in
4627 let x1 = x0 + l.pagevw in
4628 let y0 = l.pagedispy in
4629 let y1 = y0 + l.pagevh in
4630 if y >= y0 && y <= y1 && x >= x0 && x <= x1
4631 then
4632 let px, py = pagetranslatepoint l x y in
4633 match whatsunder opaque px py with
4634 | Unone -> f rest
4635 | under -> under
4636 else f rest
4637 | _ ->
4638 f rest
4640 | [] -> Unone
4642 f state.layout
4645 let zoomrect x y x1 y1 =
4646 let x0 = min x x1
4647 and x1 = max x x1
4648 and y0 = min y y1 in
4649 gotoy (state.y + y0);
4650 state.anchor <- getanchor ();
4651 let zoom = (float conf.winw *. conf.zoom) /. float (x1 - x0) in
4652 let margin =
4653 if state.w < conf.winw - state.scrollw
4654 then (conf.winw - state.scrollw - state.w) / 2
4655 else 0
4657 state.x <- (state.x + margin) - x0;
4658 setzoom zoom;
4659 Glut.setCursor Glut.CURSOR_INHERIT;
4660 state.mstate <- Mnone;
4663 let scrollx x =
4664 let winw = conf.winw - state.scrollw - 1 in
4665 let s = float x /. float winw in
4666 let destx = truncate (float (state.w + winw) *. s) in
4667 state.x <- winw - destx;
4668 gotoy_and_clear_text state.y;
4669 state.mstate <- Mscrollx;
4672 let scrolly y =
4673 let s = float y /. float conf.winh in
4674 let desty = truncate (float (state.maxy - conf.winh) *. s) in
4675 gotoy_and_clear_text desty;
4676 state.mstate <- Mscrolly;
4679 let viewmouse button bstate x y =
4680 match button with
4681 | Glut.OTHER_BUTTON n when (n == 3 || n == 4) && bstate = Glut.UP ->
4682 if Glut.getModifiers () land Glut.active_ctrl != 0
4683 then (
4684 match state.mstate with
4685 | Mzoom (oldn, i) ->
4686 if oldn = n
4687 then (
4688 if i = 2
4689 then
4690 let incr =
4691 match n with
4692 | 4 ->
4693 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
4694 | _ ->
4695 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
4697 let zoom = conf.zoom -. incr in
4698 setzoom zoom;
4699 state.mstate <- Mzoom (n, 0);
4700 else
4701 state.mstate <- Mzoom (n, i+1);
4703 else state.mstate <- Mzoom (n, 0)
4705 | _ -> state.mstate <- Mzoom (n, 0)
4707 else (
4708 match state.autoscroll with
4709 | Some step -> setautoscrollspeed step (n=4)
4710 | None ->
4711 let incr =
4712 if n = 3
4713 then -conf.scrollstep
4714 else conf.scrollstep
4716 let incr = incr * 2 in
4717 let y = clamp incr in
4718 gotoy_and_clear_text y
4721 | Glut.LEFT_BUTTON when Glut.getModifiers () land Glut.active_ctrl != 0 ->
4722 if bstate = Glut.DOWN
4723 then (
4724 Glut.setCursor Glut.CURSOR_CROSSHAIR;
4725 state.mstate <- Mpan (x, y)
4727 else
4728 state.mstate <- Mnone
4730 | Glut.RIGHT_BUTTON ->
4731 if bstate = Glut.DOWN
4732 then (
4733 Glut.setCursor Glut.CURSOR_CYCLE;
4734 let p = (x, y) in
4735 state.mstate <- Mzoomrect (p, p)
4737 else (
4738 match state.mstate with
4739 | Mzoomrect ((x0, y0), _) ->
4740 if abs (x-x0) > 10 && abs (y - y0) > 10
4741 then zoomrect x0 y0 x y
4742 else (
4743 state.mstate <- Mnone;
4744 Glut.setCursor Glut.CURSOR_INHERIT;
4745 G.postRedisplay "kill accidental zoom rect";
4747 | _ ->
4748 Glut.setCursor Glut.CURSOR_INHERIT;
4749 state.mstate <- Mnone
4752 | Glut.LEFT_BUTTON when x > conf.winw - state.scrollw ->
4753 if bstate = Glut.DOWN
4754 then
4755 let _, position, sh = state.uioh#scrollph in
4756 if y > truncate position && y < truncate (position +. sh)
4757 then state.mstate <- Mscrolly
4758 else scrolly y
4759 else
4760 state.mstate <- Mnone
4762 | Glut.LEFT_BUTTON when y > conf.winh - state.hscrollh ->
4763 if bstate = Glut.DOWN
4764 then
4765 let _, position, sw = state.uioh#scrollpw in
4766 if x > truncate position && x < truncate (position +. sw)
4767 then state.mstate <- Mscrollx
4768 else scrollx x
4769 else
4770 state.mstate <- Mnone
4772 | Glut.LEFT_BUTTON ->
4773 let dest = if bstate = Glut.DOWN then getunder x y else Unone in
4774 begin match dest with
4775 | Ulinkgoto (pageno, top) ->
4776 if pageno >= 0
4777 then (
4778 addnav ();
4779 gotopage1 pageno top;
4782 | Ulinkuri s ->
4783 gotouri s
4785 | Unone when bstate = Glut.DOWN ->
4786 Glut.setCursor Glut.CURSOR_CROSSHAIR;
4787 state.mstate <- Mpan (x, y);
4789 | Unone | Utext _ ->
4790 if bstate = Glut.DOWN
4791 then (
4792 if conf.angle mod 360 = 0
4793 then (
4794 state.mstate <- Msel ((x, y), (x, y));
4795 G.postRedisplay "mouse select";
4798 else (
4799 match state.mstate with
4800 | Mnone -> ()
4802 | Mzoom _ | Mscrollx | Mscrolly ->
4803 state.mstate <- Mnone
4805 | Mzoomrect ((x0, y0), _) ->
4806 zoomrect x0 y0 x y
4808 | Mpan _ ->
4809 Glut.setCursor Glut.CURSOR_INHERIT;
4810 state.mstate <- Mnone
4812 | Msel ((_, y0), (_, y1)) ->
4813 let f l =
4814 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
4815 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
4816 then
4817 match getopaque l.pageno with
4818 | Some opaque ->
4819 copysel opaque
4820 | _ -> ()
4822 List.iter f state.layout;
4823 copysel ""; (* ugly *)
4824 Glut.setCursor Glut.CURSOR_INHERIT;
4825 state.mstate <- Mnone;
4829 | _ -> ()
4832 let birdseyemouse button bstate x y
4833 (conf, leftx, _, hooverpageno, anchor) =
4834 match button with
4835 | Glut.LEFT_BUTTON when bstate = Glut.UP ->
4836 let rec loop = function
4837 | [] -> ()
4838 | l :: rest ->
4839 if y > l.pagedispy && y < l.pagedispy + l.pagevh
4840 && x > l.pagedispx && x < l.pagedispx + l.pagevw
4841 then (
4842 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
4844 else loop rest
4846 loop state.layout
4847 | Glut.OTHER_BUTTON _ -> viewmouse button bstate x y
4848 | _ -> ()
4851 let mouse bstate button x y =
4852 state.uioh <- state.uioh#button button bstate x y;
4855 let mouse ~button ~state ~x ~y = mouse state button x y;;
4857 let motion ~x ~y =
4858 state.uioh <- state.uioh#motion x y
4861 let pmotion ~x ~y =
4862 state.uioh <- state.uioh#pmotion x y;
4865 let uioh = object
4866 method display = ()
4868 method key key =
4869 begin match state.mode with
4870 | Textentry textentry -> textentrykeyboard key textentry
4871 | Birdseye birdseye -> birdseyekeyboard key birdseye
4872 | View -> viewkeyboard key
4873 end;
4874 state.uioh
4876 method special key =
4877 begin match state.mode with
4878 | View | (Birdseye _) when key = Glut.KEY_F9 ->
4879 togglebirdseye ()
4881 | Birdseye vals ->
4882 birdseyespecial key vals
4884 | View when key = Glut.KEY_F1 ->
4885 enterhelpmode ()
4887 | View ->
4888 begin match state.autoscroll with
4889 | Some step when key = Glut.KEY_DOWN || key = Glut.KEY_UP ->
4890 setautoscrollspeed step (key = Glut.KEY_DOWN)
4892 | _ ->
4893 let y =
4894 match key with
4895 | Glut.KEY_F3 -> search state.searchpattern true; state.y
4896 | Glut.KEY_UP ->
4897 if Glut.getModifiers () land Glut.active_ctrl != 0
4898 then
4899 if Glut.getModifiers () land Glut.active_shift != 0
4900 then (setzoom state.prevzoom; state.y)
4901 else clamp (-conf.winh/2)
4902 else clamp (-conf.scrollstep)
4903 | Glut.KEY_DOWN ->
4904 if Glut.getModifiers () land Glut.active_ctrl != 0
4905 then
4906 if Glut.getModifiers () land Glut.active_shift != 0
4907 then (setzoom state.prevzoom; state.y)
4908 else clamp (conf.winh/2)
4909 else clamp (conf.scrollstep)
4910 | Glut.KEY_PAGE_UP ->
4911 if Glut.getModifiers () land Glut.active_ctrl != 0
4912 then
4913 match state.layout with
4914 | [] -> state.y
4915 | l :: _ -> state.y - l.pagey
4916 else
4917 clamp (-conf.winh)
4918 | Glut.KEY_PAGE_DOWN ->
4919 if Glut.getModifiers () land Glut.active_ctrl != 0
4920 then
4921 match List.rev state.layout with
4922 | [] -> state.y
4923 | l :: _ -> getpagey l.pageno
4924 else
4925 clamp conf.winh
4926 | Glut.KEY_HOME ->
4927 addnav ();
4929 | Glut.KEY_END ->
4930 addnav ();
4931 state.maxy - (if conf.maxhfit then conf.winh else 0)
4933 | (Glut.KEY_RIGHT | Glut.KEY_LEFT) when
4934 Glut.getModifiers () land Glut.active_alt != 0 ->
4935 getnav (if key = Glut.KEY_LEFT then 1 else -1)
4937 | Glut.KEY_RIGHT when conf.zoom > 1.0 ->
4938 let dx =
4939 if Glut.getModifiers () land Glut.active_ctrl != 0
4940 then (conf.winw / 2)
4941 else 10
4943 state.x <- state.x - dx;
4944 state.y
4945 | Glut.KEY_LEFT when conf.zoom > 1.0 ->
4946 let dx =
4947 if Glut.getModifiers () land Glut.active_ctrl != 0
4948 then (conf.winw / 2)
4949 else 10
4951 state.x <- state.x + dx;
4952 state.y
4954 | _ -> state.y
4956 if abs (state.y - y) > conf.scrollstep*2
4957 then gotoghyll y
4958 else gotoy_and_clear_text y
4961 | Textentry te -> textentryspecial key te
4962 end;
4963 state.uioh
4965 method button button bstate x y =
4966 begin match state.mode with
4967 | View -> viewmouse button bstate x y
4968 | Birdseye beye -> birdseyemouse button bstate x y beye
4969 | Textentry _ -> ()
4970 end;
4971 state.uioh
4973 method motion x y =
4974 begin match state.mode with
4975 | Textentry _ -> ()
4976 | View | Birdseye _ ->
4977 match state.mstate with
4978 | Mzoom _ | Mnone -> ()
4980 | Mpan (x0, y0) ->
4981 let dx = x - x0
4982 and dy = y0 - y in
4983 state.mstate <- Mpan (x, y);
4984 if conf.zoom > 1.0 then state.x <- state.x + dx;
4985 let y = clamp dy in
4986 gotoy_and_clear_text y
4988 | Msel (a, _) ->
4989 state.mstate <- Msel (a, (x, y));
4990 G.postRedisplay "motion select";
4992 | Mscrolly ->
4993 let y = min conf.winh (max 0 y) in
4994 scrolly y
4996 | Mscrollx ->
4997 let x = min conf.winw (max 0 x) in
4998 scrollx x
5000 | Mzoomrect (p0, _) ->
5001 state.mstate <- Mzoomrect (p0, (x, y));
5002 G.postRedisplay "motion zoomrect";
5003 end;
5004 state.uioh
5006 method pmotion x y =
5007 begin match state.mode with
5008 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
5009 let rec loop = function
5010 | [] ->
5011 if hooverpageno != -1
5012 then (
5013 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
5014 G.postRedisplay "pmotion birdseye no hoover";
5016 | l :: rest ->
5017 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5018 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5019 then (
5020 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
5021 G.postRedisplay "pmotion birdseye hoover";
5023 else loop rest
5025 loop state.layout
5027 | Textentry _ -> ()
5029 | View ->
5030 match state.mstate with
5031 | Mnone ->
5032 begin match getunder x y with
5033 | Unone -> Glut.setCursor Glut.CURSOR_INHERIT
5034 | Ulinkuri uri ->
5035 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
5036 Glut.setCursor Glut.CURSOR_INFO
5037 | Ulinkgoto (page, _) ->
5038 if conf.underinfo
5039 then showtext 'p' ("age: " ^ string_of_int (page+1));
5040 Glut.setCursor Glut.CURSOR_INFO
5041 | Utext s ->
5042 if conf.underinfo then showtext 'f' ("ont: " ^ s);
5043 Glut.setCursor Glut.CURSOR_TEXT
5046 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
5048 end;
5049 state.uioh
5051 method infochanged _ = ()
5053 method scrollph =
5054 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
5055 let p, h = scrollph state.y maxy in
5056 state.scrollw, p, h
5058 method scrollpw =
5059 let winw = conf.winw - state.scrollw - 1 in
5060 let fwinw = float winw in
5061 let sw =
5062 let sw = fwinw /. float state.w in
5063 let sw = fwinw *. sw in
5064 max sw (float conf.scrollh)
5066 let position, sw =
5067 let f = state.w+winw in
5068 let r = float (winw-state.x) /. float f in
5069 let p = fwinw *. r in
5070 p-.sw/.2., sw
5072 let sw =
5073 if position +. sw > fwinw
5074 then fwinw -. position
5075 else sw
5077 state.hscrollh, position, sw
5078 end;;
5080 module Config =
5081 struct
5082 open Parser
5084 let fontpath = ref "";;
5085 let wmclasshack = ref false;;
5087 let unent s =
5088 let l = String.length s in
5089 let b = Buffer.create l in
5090 unent b s 0 l;
5091 Buffer.contents b;
5094 let home =
5096 match platform with
5097 | Pwindows | Pmingw -> Sys.getenv "HOMEPATH"
5098 | _ -> Sys.getenv "HOME"
5099 with exn ->
5100 prerr_endline
5101 ("Can not determine home directory location: " ^
5102 Printexc.to_string exn);
5106 let config_of c attrs =
5107 let apply c k v =
5109 match k with
5110 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
5111 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
5112 | "case-insensitive-search" -> { c with icase = bool_of_string v }
5113 | "preload" -> { c with preload = bool_of_string v }
5114 | "page-bias" -> { c with pagebias = int_of_string v }
5115 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
5116 | "auto-scroll-step" ->
5117 { c with autoscrollstep = max 0 (int_of_string v) }
5118 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
5119 | "crop-hack" -> { c with crophack = bool_of_string v }
5120 | "throttle" ->
5121 let mw =
5122 match String.lowercase v with
5123 | "true" -> Some infinity
5124 | "false" -> None
5125 | f -> Some (float_of_string f)
5127 { c with maxwait = mw}
5128 | "highlight-links" -> { c with hlinks = bool_of_string v }
5129 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
5130 | "vertical-margin" ->
5131 { c with interpagespace = max 0 (int_of_string v) }
5132 | "zoom" ->
5133 let zoom = float_of_string v /. 100. in
5134 let zoom = max zoom 0.0 in
5135 { c with zoom = zoom }
5136 | "presentation" -> { c with presentation = bool_of_string v }
5137 | "rotation-angle" -> { c with angle = int_of_string v }
5138 | "width" -> { c with winw = max 20 (int_of_string v) }
5139 | "height" -> { c with winh = max 20 (int_of_string v) }
5140 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
5141 | "proportional-display" -> { c with proportional = bool_of_string v }
5142 | "pixmap-cache-size" ->
5143 { c with memlimit = max 2 (int_of_string_with_suffix v) }
5144 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
5145 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
5146 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
5147 | "persistent-location" -> { c with jumpback = bool_of_string v }
5148 | "background-color" -> { c with bgcolor = color_of_string v }
5149 | "scrollbar-in-presentation" ->
5150 { c with scrollbarinpm = bool_of_string v }
5151 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
5152 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
5153 | "mupdf-store-size" ->
5154 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
5155 | "checkers" -> { c with checkers = bool_of_string v }
5156 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
5157 | "trim-margins" -> { c with trimmargins = bool_of_string v }
5158 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
5159 | "wmclass-hack" -> wmclasshack := bool_of_string v; c
5160 | "uri-launcher" -> { c with urilauncher = unent v }
5161 | "color-space" -> { c with colorspace = colorspace_of_string v }
5162 | "invert-colors" -> { c with invert = bool_of_string v }
5163 | "brightness" -> { c with colorscale = float_of_string v }
5164 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
5165 | "ghyllscroll" ->
5166 { c with ghyllscroll = Some (ghyllscroll_of_string v) }
5167 | "columns" ->
5168 let nab = columns_of_string v in
5169 { c with columns = Some (nab, [||]) }
5170 | "birds-eye-columns" ->
5171 { c with beyecolumns = Some (max (int_of_string v) 2) }
5172 | _ -> c
5173 with exn ->
5174 prerr_endline ("Error processing attribute (`" ^
5175 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
5178 let rec fold c = function
5179 | [] -> c
5180 | (k, v) :: rest ->
5181 let c = apply c k v in
5182 fold c rest
5184 fold c attrs;
5187 let fromstring f pos n v d =
5188 try f v
5189 with exn ->
5190 dolog "Error processing attribute (%S=%S) at %d\n%s"
5191 n v pos (Printexc.to_string exn)
5196 let bookmark_of attrs =
5197 let rec fold title page rely = function
5198 | ("title", v) :: rest -> fold v page rely rest
5199 | ("page", v) :: rest -> fold title v rely rest
5200 | ("rely", v) :: rest -> fold title page v rest
5201 | _ :: rest -> fold title page rely rest
5202 | [] -> title, page, rely
5204 fold "invalid" "0" "0" attrs
5207 let doc_of attrs =
5208 let rec fold path page rely pan = function
5209 | ("path", v) :: rest -> fold v page rely pan rest
5210 | ("page", v) :: rest -> fold path v rely pan rest
5211 | ("rely", v) :: rest -> fold path page v pan rest
5212 | ("pan", v) :: rest -> fold path page rely v rest
5213 | _ :: rest -> fold path page rely pan rest
5214 | [] -> path, page, rely, pan
5216 fold "" "0" "0" "0" attrs
5219 let setconf dst src =
5220 dst.scrollbw <- src.scrollbw;
5221 dst.scrollh <- src.scrollh;
5222 dst.icase <- src.icase;
5223 dst.preload <- src.preload;
5224 dst.pagebias <- src.pagebias;
5225 dst.verbose <- src.verbose;
5226 dst.scrollstep <- src.scrollstep;
5227 dst.maxhfit <- src.maxhfit;
5228 dst.crophack <- src.crophack;
5229 dst.autoscrollstep <- src.autoscrollstep;
5230 dst.maxwait <- src.maxwait;
5231 dst.hlinks <- src.hlinks;
5232 dst.underinfo <- src.underinfo;
5233 dst.interpagespace <- src.interpagespace;
5234 dst.zoom <- src.zoom;
5235 dst.presentation <- src.presentation;
5236 dst.angle <- src.angle;
5237 dst.winw <- src.winw;
5238 dst.winh <- src.winh;
5239 dst.savebmarks <- src.savebmarks;
5240 dst.memlimit <- src.memlimit;
5241 dst.proportional <- src.proportional;
5242 dst.texcount <- src.texcount;
5243 dst.sliceheight <- src.sliceheight;
5244 dst.thumbw <- src.thumbw;
5245 dst.jumpback <- src.jumpback;
5246 dst.bgcolor <- src.bgcolor;
5247 dst.scrollbarinpm <- src.scrollbarinpm;
5248 dst.tilew <- src.tilew;
5249 dst.tileh <- src.tileh;
5250 dst.mustoresize <- src.mustoresize;
5251 dst.checkers <- src.checkers;
5252 dst.aalevel <- src.aalevel;
5253 dst.trimmargins <- src.trimmargins;
5254 dst.trimfuzz <- src.trimfuzz;
5255 dst.urilauncher <- src.urilauncher;
5256 dst.colorspace <- src.colorspace;
5257 dst.invert <- src.invert;
5258 dst.colorscale <- src.colorscale;
5259 dst.redirectstderr <- src.redirectstderr;
5260 dst.ghyllscroll <- src.ghyllscroll;
5261 dst.columns <- src.columns;
5262 dst.beyecolumns <- src.beyecolumns;
5265 let get s =
5266 let h = Hashtbl.create 10 in
5267 let dc = { defconf with angle = defconf.angle } in
5268 let rec toplevel v t spos _ =
5269 match t with
5270 | Vdata | Vcdata | Vend -> v
5271 | Vopen ("llppconfig", _, closed) ->
5272 if closed
5273 then v
5274 else { v with f = llppconfig }
5275 | Vopen _ ->
5276 error "unexpected subelement at top level" s spos
5277 | Vclose _ -> error "unexpected close at top level" s spos
5279 and llppconfig v t spos _ =
5280 match t with
5281 | Vdata | Vcdata -> v
5282 | Vend -> error "unexpected end of input in llppconfig" s spos
5283 | Vopen ("defaults", attrs, closed) ->
5284 let c = config_of dc attrs in
5285 setconf dc c;
5286 if closed
5287 then v
5288 else { v with f = skip "defaults" (fun () -> v) }
5290 | Vopen ("ui-font", attrs, closed) ->
5291 let rec getsize size = function
5292 | [] -> size
5293 | ("size", v) :: rest ->
5294 let size =
5295 fromstring int_of_string spos "size" v fstate.fontsize in
5296 getsize size rest
5297 | l -> getsize size l
5299 fstate.fontsize <- getsize fstate.fontsize attrs;
5300 if closed
5301 then v
5302 else { v with f = uifont (Buffer.create 10) }
5304 | Vopen ("doc", attrs, closed) ->
5305 let pathent, spage, srely, span = doc_of attrs in
5306 let path = unent pathent
5307 and pageno = fromstring int_of_string spos "page" spage 0
5308 and rely = fromstring float_of_string spos "rely" srely 0.0
5309 and pan = fromstring int_of_string spos "pan" span 0 in
5310 let c = config_of dc attrs in
5311 let anchor = (pageno, rely) in
5312 if closed
5313 then (Hashtbl.add h path (c, [], pan, anchor); v)
5314 else { v with f = doc path pan anchor c [] }
5316 | Vopen _ ->
5317 error "unexpected subelement in llppconfig" s spos
5319 | Vclose "llppconfig" -> { v with f = toplevel }
5320 | Vclose _ -> error "unexpected close in llppconfig" s spos
5322 and uifont b v t spos epos =
5323 match t with
5324 | Vdata | Vcdata ->
5325 Buffer.add_substring b s spos (epos - spos);
5327 | Vopen (_, _, _) ->
5328 error "unexpected subelement in ui-font" s spos
5329 | Vclose "ui-font" ->
5330 if String.length !fontpath = 0
5331 then fontpath := Buffer.contents b;
5332 { v with f = llppconfig }
5333 | Vclose _ -> error "unexpected close in ui-font" s spos
5334 | Vend -> error "unexpected end of input in ui-font" s spos
5336 and doc path pan anchor c bookmarks v t spos _ =
5337 match t with
5338 | Vdata | Vcdata -> v
5339 | Vend -> error "unexpected end of input in doc" s spos
5340 | Vopen ("bookmarks", _, closed) ->
5341 if closed
5342 then v
5343 else { v with f = pbookmarks path pan anchor c bookmarks }
5345 | Vopen (_, _, _) ->
5346 error "unexpected subelement in doc" s spos
5348 | Vclose "doc" ->
5349 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
5350 { v with f = llppconfig }
5352 | Vclose _ -> error "unexpected close in doc" s spos
5354 and pbookmarks path pan anchor c bookmarks v t spos _ =
5355 match t with
5356 | Vdata | Vcdata -> v
5357 | Vend -> error "unexpected end of input in bookmarks" s spos
5358 | Vopen ("item", attrs, closed) ->
5359 let titleent, spage, srely = bookmark_of attrs in
5360 let page = fromstring int_of_string spos "page" spage 0
5361 and rely = fromstring float_of_string spos "rely" srely 0.0 in
5362 let bookmarks = (unent titleent, 0, (page, rely)) :: bookmarks in
5363 if closed
5364 then { v with f = pbookmarks path pan anchor c bookmarks }
5365 else
5366 let f () = v in
5367 { v with f = skip "item" f }
5369 | Vopen _ ->
5370 error "unexpected subelement in bookmarks" s spos
5372 | Vclose "bookmarks" ->
5373 { v with f = doc path pan anchor c bookmarks }
5375 | Vclose _ -> error "unexpected close in bookmarks" s spos
5377 and skip tag f v t spos _ =
5378 match t with
5379 | Vdata | Vcdata -> v
5380 | Vend ->
5381 error ("unexpected end of input in skipped " ^ tag) s spos
5382 | Vopen (tag', _, closed) ->
5383 if closed
5384 then v
5385 else
5386 let f' () = { v with f = skip tag f } in
5387 { v with f = skip tag' f' }
5388 | Vclose ctag ->
5389 if tag = ctag
5390 then f ()
5391 else error ("unexpected close in skipped " ^ tag) s spos
5394 parse { f = toplevel; accu = () } s;
5395 h, dc;
5398 let do_load f ic =
5400 let len = in_channel_length ic in
5401 let s = String.create len in
5402 really_input ic s 0 len;
5403 f s;
5404 with
5405 | Parse_error (msg, s, pos) ->
5406 let subs = subs s pos in
5407 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
5408 failwith ("parse error: " ^ s)
5410 | exn ->
5411 failwith ("config load error: " ^ Printexc.to_string exn)
5414 let defconfpath =
5415 let dir =
5417 let dir = Filename.concat home ".config" in
5418 if Sys.is_directory dir then dir else home
5419 with _ -> home
5421 Filename.concat dir "llpp.conf"
5424 let confpath = ref defconfpath;;
5426 let load1 f =
5427 if Sys.file_exists !confpath
5428 then
5429 match
5430 (try Some (open_in_bin !confpath)
5431 with exn ->
5432 prerr_endline
5433 ("Error opening configuation file `" ^ !confpath ^ "': " ^
5434 Printexc.to_string exn);
5435 None
5437 with
5438 | Some ic ->
5439 begin try
5440 f (do_load get ic)
5441 with exn ->
5442 prerr_endline
5443 ("Error loading configuation from `" ^ !confpath ^ "': " ^
5444 Printexc.to_string exn);
5445 end;
5446 close_in ic;
5448 | None -> ()
5449 else
5450 f (Hashtbl.create 0, defconf)
5453 let load () =
5454 let f (h, dc) =
5455 let pc, pb, px, pa =
5457 Hashtbl.find h (Filename.basename state.path)
5458 with Not_found -> dc, [], 0, (0, 0.0)
5460 setconf defconf dc;
5461 setconf conf pc;
5462 state.bookmarks <- pb;
5463 state.x <- px;
5464 state.scrollw <- conf.scrollbw;
5465 if conf.jumpback
5466 then state.anchor <- pa;
5467 cbput state.hists.nav pa;
5469 load1 f
5472 let add_attrs bb always dc c =
5473 let ob s a b =
5474 if always || a != b
5475 then Printf.bprintf bb "\n %s='%b'" s a
5476 and oi s a b =
5477 if always || a != b
5478 then Printf.bprintf bb "\n %s='%d'" s a
5479 and oI s a b =
5480 if always || a != b
5481 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
5482 and oz s a b =
5483 if always || a <> b
5484 then Printf.bprintf bb "\n %s='%d'" s (truncate (a*.100.))
5485 and oF s a b =
5486 if always || a <> b
5487 then Printf.bprintf bb "\n %s='%f'" s a
5488 and oc s a b =
5489 if always || a <> b
5490 then
5491 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
5492 and oC s a b =
5493 if always || a <> b
5494 then
5495 Printf.bprintf bb "\n %s='%s'" s (colorspace_to_string a)
5496 and oR s a b =
5497 if always || a <> b
5498 then
5499 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
5500 and os s a b =
5501 if always || a <> b
5502 then
5503 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
5504 and og s a b =
5505 if always || a <> b
5506 then
5507 match a with
5508 | None -> ()
5509 | Some (_N, _A, _B) ->
5510 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
5511 and oW s a b =
5512 if always || a <> b
5513 then
5514 let v =
5515 match a with
5516 | None -> "false"
5517 | Some f ->
5518 if f = infinity
5519 then "true"
5520 else string_of_float f
5522 Printf.bprintf bb "\n %s='%s'" s v
5523 and oco s a b =
5524 if always || a <> b
5525 then
5526 match a with
5527 | Some ((n, a, b), _) when n > 1 ->
5528 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
5529 | _ -> ()
5530 and obeco s a b =
5531 if always || a <> b
5532 then
5533 match a with
5534 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
5535 | _ -> ()
5537 let w, h =
5538 if always
5539 then dc.winw, dc.winh
5540 else
5541 match state.fullscreen with
5542 | Some wh -> wh
5543 | None -> c.winw, c.winh
5545 let zoom, presentation, interpagespace, maxwait =
5546 if always
5547 then dc.zoom, dc.presentation, dc.interpagespace, dc.maxwait
5548 else
5549 match state.mode with
5550 | Birdseye (bc, _, _, _, _) ->
5551 bc.zoom, bc.presentation, bc.interpagespace, bc.maxwait
5552 | _ -> c.zoom, c.presentation, c.interpagespace, c.maxwait
5554 oi "width" w dc.winw;
5555 oi "height" h dc.winh;
5556 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
5557 oi "scroll-handle-height" c.scrollh dc.scrollh;
5558 ob "case-insensitive-search" c.icase dc.icase;
5559 ob "preload" c.preload dc.preload;
5560 oi "page-bias" c.pagebias dc.pagebias;
5561 oi "scroll-step" c.scrollstep dc.scrollstep;
5562 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
5563 ob "max-height-fit" c.maxhfit dc.maxhfit;
5564 ob "crop-hack" c.crophack dc.crophack;
5565 oW "throttle" maxwait dc.maxwait;
5566 ob "highlight-links" c.hlinks dc.hlinks;
5567 ob "under-cursor-info" c.underinfo dc.underinfo;
5568 oi "vertical-margin" interpagespace dc.interpagespace;
5569 oz "zoom" zoom dc.zoom;
5570 ob "presentation" presentation dc.presentation;
5571 oi "rotation-angle" c.angle dc.angle;
5572 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
5573 ob "proportional-display" c.proportional dc.proportional;
5574 oI "pixmap-cache-size" c.memlimit dc.memlimit;
5575 oi "tex-count" c.texcount dc.texcount;
5576 oi "slice-height" c.sliceheight dc.sliceheight;
5577 oi "thumbnail-width" c.thumbw dc.thumbw;
5578 ob "persistent-location" c.jumpback dc.jumpback;
5579 oc "background-color" c.bgcolor dc.bgcolor;
5580 ob "scrollbar-in-presentation" c.scrollbarinpm dc.scrollbarinpm;
5581 oi "tile-width" c.tilew dc.tilew;
5582 oi "tile-height" c.tileh dc.tileh;
5583 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
5584 ob "checkers" c.checkers dc.checkers;
5585 oi "aalevel" c.aalevel dc.aalevel;
5586 ob "trim-margins" c.trimmargins dc.trimmargins;
5587 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
5588 os "uri-launcher" c.urilauncher dc.urilauncher;
5589 oC "color-space" c.colorspace dc.colorspace;
5590 ob "invert-colors" c.invert dc.invert;
5591 oF "brightness" c.colorscale dc.colorscale;
5592 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
5593 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
5594 oco "columns" c.columns dc.columns;
5595 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
5596 if always
5597 then ob "wmclass-hack" !wmclasshack false;
5600 let save () =
5601 let uifontsize = fstate.fontsize in
5602 let bb = Buffer.create 32768 in
5603 let f (h, dc) =
5604 let dc = if conf.bedefault then conf else dc in
5605 Buffer.add_string bb "<llppconfig>\n";
5607 if String.length !fontpath > 0
5608 then
5609 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
5610 uifontsize
5611 !fontpath
5612 else (
5613 if uifontsize <> 14
5614 then
5615 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
5618 Buffer.add_string bb "<defaults ";
5619 add_attrs bb true dc dc;
5620 Buffer.add_string bb "/>\n";
5622 let adddoc path pan anchor c bookmarks =
5623 if bookmarks == [] && c = dc && anchor = emptyanchor
5624 then ()
5625 else (
5626 Printf.bprintf bb "<doc path='%s'"
5627 (enent path 0 (String.length path));
5629 if anchor <> emptyanchor
5630 then (
5631 let n, y = anchor in
5632 Printf.bprintf bb " page='%d'" n;
5633 if y > 1e-6
5634 then
5635 Printf.bprintf bb " rely='%f'" y
5639 if pan != 0
5640 then Printf.bprintf bb " pan='%d'" pan;
5642 add_attrs bb false dc c;
5644 begin match bookmarks with
5645 | [] -> Buffer.add_string bb "/>\n"
5646 | _ ->
5647 Buffer.add_string bb ">\n<bookmarks>\n";
5648 List.iter (fun (title, _level, (page, rely)) ->
5649 Printf.bprintf bb
5650 "<item title='%s' page='%d'"
5651 (enent title 0 (String.length title))
5652 page
5654 if rely > 1e-6
5655 then
5656 Printf.bprintf bb " rely='%f'" rely
5658 Buffer.add_string bb "/>\n";
5659 ) bookmarks;
5660 Buffer.add_string bb "</bookmarks>\n</doc>\n";
5661 end;
5665 let pan =
5666 match state.mode with
5667 | Birdseye (_, pan, _, _, _) -> pan
5668 | _ -> state.x
5670 let basename = Filename.basename state.path in
5671 adddoc basename pan (getanchor ())
5672 { conf with
5673 autoscrollstep =
5674 match state.autoscroll with
5675 | Some step -> step
5676 | None -> conf.autoscrollstep }
5677 (if conf.savebmarks then state.bookmarks else []);
5679 Hashtbl.iter (fun path (c, bookmarks, x, y) ->
5680 if basename <> path
5681 then adddoc path x y c bookmarks
5682 ) h;
5683 Buffer.add_string bb "</llppconfig>";
5685 load1 f;
5686 if Buffer.length bb > 0
5687 then
5689 let tmp = !confpath ^ ".tmp" in
5690 let oc = open_out_bin tmp in
5691 Buffer.output_buffer oc bb;
5692 close_out oc;
5693 Unix.rename tmp !confpath;
5694 with exn ->
5695 prerr_endline
5696 ("error while saving configuration: " ^ Printexc.to_string exn)
5698 end;;
5700 let () =
5701 Arg.parse
5702 (Arg.align
5703 [("-p", Arg.String (fun s -> state.password <- s) ,
5704 "<password> Set password");
5706 ("-f", Arg.String (fun s -> Config.fontpath := s),
5707 "<path> Set path to the user interface font");
5709 ("-c", Arg.String (fun s -> Config.confpath := s),
5710 "<path> Set path to the configuration file");
5712 ("-v", Arg.Unit (fun () ->
5713 Printf.printf
5714 "%s\nconfiguration path: %s\n"
5715 (version ())
5716 Config.defconfpath
5718 exit 0), " Print version and exit");
5721 (fun s -> state.path <- s)
5722 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
5724 if String.length state.path = 0
5725 then (prerr_endline "file name missing"; exit 1);
5727 Config.load ();
5729 let _ = Glut.init Sys.argv in
5730 let () = Glut.initDisplayMode ~depth:false ~double_buffer:true () in
5731 let () = Glut.initWindowSize conf.winw conf.winh in
5732 let _ = Glut.createWindow ("llpp " ^ Filename.basename state.path) in
5734 if not (Glut.extensionSupported "GL_ARB_texture_rectangle"
5735 || Glut.extensionSupported "GL_EXT_texture_rectangle")
5736 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
5738 let csock, ssock =
5739 if not is_windows
5740 then
5741 Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0
5742 else
5743 let addr = Unix.ADDR_INET (Unix.inet_addr_loopback, 1337) in
5744 let sock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
5745 Unix.setsockopt sock Unix.SO_REUSEADDR true;
5746 Unix.bind sock addr;
5747 Unix.listen sock 1;
5748 let csock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
5749 Unix.connect csock addr;
5750 let ssock, _ = Unix.accept sock in
5751 Unix.close sock;
5752 let opts sock =
5753 Unix.setsockopt sock Unix.TCP_NODELAY true;
5754 Unix.setsockopt_optint sock Unix.SO_LINGER None;
5756 opts ssock;
5757 opts csock;
5758 ssock, csock
5761 let () = Glut.displayFunc display in
5762 let () = Glut.reshapeFunc reshape in
5763 let () = Glut.keyboardFunc keyboard in
5764 let () = Glut.specialFunc special in
5765 let () = Glut.idleFunc (Some idle) in
5766 let () = Glut.mouseFunc mouse in
5767 let () = Glut.motionFunc motion in
5768 let () = Glut.passiveMotionFunc pmotion in
5770 setcheckers conf.checkers;
5771 init ssock (
5772 conf.angle, conf.proportional, (conf.trimmargins, conf.trimfuzz),
5773 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
5774 !Config.wmclasshack, !Config.fontpath
5776 state.csock <- csock;
5777 state.ssock <- ssock;
5778 state.text <- "Opening " ^ state.path;
5779 setaalevel conf.aalevel;
5780 writeopen state.path state.password;
5781 state.uioh <- uioh;
5782 setfontsize fstate.fontsize;
5784 redirectstderr ();
5786 while true do
5788 Glut.mainLoop ();
5789 with
5790 | Glut.BadEnum "key in special_of_int" ->
5791 showtext '!' " LablGlut bug: special key not recognized";
5793 | Quit ->
5794 wcmd "quit" [];
5795 Config.save ();
5796 exit 0
5798 | exn when conf.redirectstderr ->
5799 let s =
5800 Printf.sprintf "exception %s\n%s"
5801 (Printexc.to_string exn)
5802 (Printexc.get_backtrace ())
5804 ignore (try
5805 Unix.single_write state.stderr s 0 (String.length s);
5806 with _ -> 0);
5807 exit 1
5808 done;