9 let tempfailureretry f a
=
11 try f a
with Unix.Unix_error
(Unix.EINTR
, _
, _
) -> g ()
15 external cloexec
: Unix.file_descr
-> unit = "ml_cloexec";;
16 external glx
: int -> unit = "ml_glx";;
17 external glxsync
: unit -> unit = "ml_glxsync";;
18 external swapb
: unit -> unit = "ml_swapb";;
19 external hasdata
: Unix.file_descr
-> bool = "ml_hasdata";;
20 external toutf8
: int -> string = "ml_keysymtoutf8";;
22 let dolog fmt
= Format.kprintf prerr_endline fmt
;;
23 let vlog fmt
= Format.kprintf ignore fmt
;;
28 method reshape _ _
= ()
29 method mouse _ _ _ _ _
= ()
30 method motion _ _
= ()
31 method pmotion _ _
= ()
41 method reshape
: int -> int -> unit
42 method mouse
: int -> bool -> int -> int -> int -> unit
43 method motion
: int -> int -> unit
44 method pmotion
: int -> int -> unit
45 method key
: int -> int -> unit
46 method enter
: int -> int -> unit
54 ; mutable keymap
: int array array
55 ; fifo
: (string -> unit) Queue.t
57 ; mutable protoatom
: int
58 ; mutable deleatom
: int
59 ; mutable idbase
: int
60 ; mutable fullscreen
: (int -> unit)
61 ; mutable setwmname
: (string -> unit)
62 ; mutable stringatom
: int
64 ; mutable sock
: Unix.file_descr
70 ; mutable curcurs
: cursor
71 ; mutable capslmask
: int
72 ; mutable numlmask
: int
73 ; mutable levl3mask
: int
74 ; mutable levl5mask
: int
78 | Fs
of (int * int * int * int)
85 ; fifo
= Queue.create
()
90 ; fullscreen
= (fun _
-> ())
91 ; setwmname
= (fun _
-> ())
100 ; curcurs
= CURSOR_INHERIT
108 let w8 s pos i
= String.set s pos
(Char.chr
(i
land 0xff));;
112 w8 s
(pos
+1) (i
lsr 8);
117 w16 s
(pos
+2) (i
lsr 16);
121 let rb pos1
= Char.code
(String.get s
(pos
+ pos1
)) in
122 (rb 0) lor ((rb 1) lsl 8)
127 i - ((i land 0x8000) lsl 1);
130 let r8 s pos
= Char.code
(String.get s pos
);;
133 let rb pos1
= Char.code
(String.get s
(pos
+ pos1
)) in
134 let l = (rb 0) lor ((rb 1) lsl 8)
135 and u
= (rb 2) lor ((rb 3) lsl 8) in
139 let exntos = function
140 | Unix.Unix_error
(e
, s
, a
) -> Printf.sprintf
"%s(%s):%s(%d)"
141 s a
(Unix.error_message e
) (Obj.magic e
)
142 | exn
-> Printexc.to_string exn
;
145 let error fmt
= Printf.kprintf failwith fmt
;;
148 let s = String.create n
in
150 let m = tempfailureretry (Unix.read sock
s pos
) n
in
155 ignore
(tempfailureretry (Unix.select
[sock
] [] []) 0.01);
156 loop (pos
+ m) (n
- m)
163 let sendstr1 s pos len sock
=
164 vlog "%d => %S" state.seq
s;
165 state.seq
<- state.seq
+ 1;
166 let n = tempfailureretry (Unix.send sock
s pos len
) [] in
168 then error "send %d returned %d" len
n;
171 let updkmap sock resp
=
172 let syms = r8 resp
1 in
173 let len = r32 resp
4 in
176 then readstr sock
(4*len)
179 let m = len / syms in
180 state.keymap
<- Array.make_matrix
181 (state.maxk
- state.mink
) syms 0xffffff;
182 let rec loop i = if i = m then () else
184 let rec loop2 k l = if l = syms then () else
185 let v = r32 data k in
186 state.keymap
.(i).(l) <- v;
195 let updmodmap sock resp
=
197 let len = r16 resp
4 in
200 then readstr sock
(len*4)
203 let modmap = Array.make_matrix
8 n 0xffffff in
204 let rec loop l = if l = 8 then () else
206 let rec loop1 m = if m = n then () else
208 let code = r8 data p in
209 modmap.(l).(m) <- code;
212 let ki = code - state.mink
in
215 let a = state.keymap
.(ki) in
216 let rec capsloop i = if i = Array.length
a || i > 3 then () else
219 then state.capslmask
<- 2
227 let ki = code - state.mink
in
230 let a = state.keymap
.(ki) in
231 let rec lloop i = if i = Array.length
a || i > 3 then () else
234 | 0xfe03 -> state.levl3mask
<- 1 lsl l
235 | 0xfe11 -> state.levl5mask
<- 1 lsl l
236 | 0xff7f -> state.numlmask
<- 1 lsl l
250 let sendwithrep sock
s f
=
251 Queue.push f
state.fifo
;
252 sendstr1 s 0 (String.length
s) sock
;
256 let b = Buffer.create
16 in
257 List.iter
(Buffer.add_string
b) ss
;
258 let bl = Buffer.length
b in
259 let pl = bl land 3 in
263 Buffer.add_substring
b pad 0 (4 - pl);
268 let padcat s1 s2
= padcatl [s1
; s2
];;
270 let internreq name onlyifexists
=
271 let s = "\016\000\000\000\000\000\000\000" in
272 let s = padcat s name
in
273 if onlyifexists
then w8 s 1 1;
274 w16 s 2 (String.length
s / 4);
275 w16 s 4 (String.length name
);
279 let sendintern sock
s onlyifexists f
=
280 let s = internreq s onlyifexists
in
281 sendwithrep sock
s f
;
284 let createwindowreq wid parent x y w h bw mask
=
285 let s = "\001\000\009\000wwwwppppxxyywwhhbwccvvvvmmmmeeee" in
295 w32 s 28 0x800; (* eventmask *)
300 let getgeometryreq wid
=
301 let s = "\014u\002\000dddd" in
307 let s = "\008u\002\000wwww" in
312 let getkeymapreq first count
=
313 let s = "\101u\002\000fcuu" in
319 let changepropreq wid prop typ format props
=
320 let s = "\018\000llwwwwppppttttfuuuLLLL" in
321 let s = padcat s props
in
322 w16 s 2 (String.length
s / 4);
327 let ful = String.length props
/ (match format
with
331 | n -> error "no idea what %d means" n)
337 let openfontreq fid name
=
338 let s = "\045ullffffnnuu" in
339 let s = padcat s name
in
340 w16 s 2 (String.length
s / 4);
342 w16 s 8 (String.length name
);
346 let createglyphcursorreq fid cid cindex
=
347 let s = "\094u\008\000ccccffffffffssmmrrggbbRRGGBB" in
362 let changewindowattributesreq wid mask attrs
=
363 let s = "\002ullwwwwmmmm" in
364 let s = padcat s attrs
in
365 w16 s 2 (String.length
s / 4);
371 let configurewindowreq wid mask values
=
372 let s = "\012ullwwwwmmuu" in
373 let s = padcat s values
in
374 w16 s 2 (String.length
s / 4);
386 let clientmessage format seq wid typ
data =
387 let s = "\033fsswwwwtttt" in
388 let s = padcat s data in
396 let sendeventreq propagate destwid mask
data =
397 let s = "\025p\011\000wwwwmmmm" in
398 let s = padcat s data in
405 let getmodifiermappingreq () =
406 let s = "\119u\001\000" in
410 let getkeysym code mask
=
411 let pkpk = state.keymap
.(code-state.mink
).(0) in
412 if (pkpk >= 0xff80 && pkpk <= 0xffbd)
413 || (pkpk >= 0x11000000 && pkpk <= 0x1100ffff)
415 if mask
land state.numlmask
!= 0
417 let keysym = state.keymap
.(code-state.mink
).(1) in
418 if keysym = 0 then pkpk else keysym
422 let shift = (mask
land 1) lxor ((mask
land state.capslmask
) lsr 1) in
424 let l3 = (mask
land state.levl3mask
) != 0 in
425 let l4 = (mask
land state.levl5mask
) != 0 in
427 if l3 then (if l4 then 8 else 4) else (if l4 then 6 else 0)
429 let keysym = state.keymap
.(code-state.mink
).(index) in
430 if index land 1 = 1 && keysym = 0
431 then state.keymap
.(code-state.mink
).(index - 1)
437 let resp = readstr sock
32 in
438 let opcode = r8 resp 0 in
439 match opcode land lnot
0x80 with
444 and resid
= r32 resp 4
447 error "code=%d serial=%d resid=%#x min=%d maj=%d\n%S"
448 code serial resid min maj
resp;
450 | 1 -> (* response *)
451 let rep = Queue.pop
state.fifo
in
454 | 2 -> (* key press *)
455 if Array.length
state.keymap
> 0
457 let code = r8 resp 1 in
458 let mask = r16 resp 28 in
459 let keysym = getkeysym code mask in
460 vlog "keysym = %x %c mask %#x code %d"
461 keysym (Char.unsafe_chr
keysym) mask code;
462 state.t#key
keysym mask;
464 | 3 -> (* key release *)
465 if Array.length
state.keymap
> 0
467 let code = r8 resp 1 in
468 let mask = r16 resp 28 in
469 let keysym = getkeysym code mask in
470 vlog "release keysym = %x %c mask %#x code %#d"
471 keysym (Char.unsafe_chr
keysym) mask code
473 | 4 -> (* buttonpress *)
477 and m = r16 resp 28 in
478 state.t#mouse
n true x y
m;
481 | 5 -> (* buttonrelease *)
485 and m = r16 resp 28 in
486 state.t#mouse
n false x y
m;
487 vlog "release %d %d %d" n x y
490 let x = r16s resp 24 in
491 let y = r16s resp 26 in
492 let m = r16 resp 28 in
494 then state.t#pmotion
x y
495 else state.t#motion
x y;
496 vlog "move %dx%d => %d" x y m
500 and y = r16s resp 26 in
502 vlog "enter %d %d" x y
507 | 18 -> vlog "unmap";
512 | 12 -> (* exposure *)
516 | 15 -> (* visibility *)
517 let vis = r8 resp 8 in
518 if vis != 2 then state.t#expose
;
519 vlog "visibility %d" vis;
521 | 34 -> (* mapping *)
522 state.keymap
<- [||];
523 let s = getkeymapreq state.mink
(state.maxk
-state.mink
-1) in
524 sendwithrep sock
s (updkmap sock
);
525 state.capslmask
<- 0;
526 state.levl3mask
<- 0;
527 state.levl5mask
<- 0;
529 let s = getmodifiermappingreq () in
530 sendwithrep sock
s (updmodmap sock
);
533 | 33 -> (* clientmessage *)
534 let atom = r32 resp 8 in
535 if atom = state.protoatom
537 let atom = r32 resp 12 in
538 if atom = state.deleatom
543 | 21 -> (* reparent *)
546 | 22 -> (* configure *)
550 and h
= r16 resp 22 in
551 vlog "configure cur [%d %d %d %d] conf [%d %d %d %d]"
552 state.x state.y state.w
state.h
556 if w
!= state.w
|| h
!= state.h
567 dolog "event %d %S" n resp
573 if hasdata sock
then loop ();
578 let sendstr s ?
(pos
=0) ?
(len=String.length
s) sock
=
579 sendstr1 s pos
len sock
;
580 if hasdata sock
then readresp sock
;
586 let s = "wwuuhhuu" in
589 let s = configurewindowreq state.idbase
0x000c s in
590 sendstr s state.sock
;
591 else state.fullscreen
state.idbase
594 let syncsendwithrep sock secstowait
s f
=
595 let completed = ref false in
596 sendwithrep sock
s (fun resp -> f
resp; completed := true);
597 let now = Unix.gettimeofday
in
598 let deadline = now () +. secstowait
in
599 let rec readtillcompletion () =
601 let timeout = deadline -. now () in
604 else Unix.select
[sock
] [] [] timeout
606 let r, _
, _
= tempfailureretry sf deadline in
608 | [] -> error "didn't get X response in %f seconds, aborting" secstowait
612 then readtillcompletion ()
614 readtillcompletion ();
617 let syncsendintern sock secstowait
s onlyifexists f
=
618 let s = internreq s onlyifexists
in
619 syncsendwithrep sock secstowait
s f
;
622 let setup sock screennum w h
=
623 let s = readstr sock
2 in
624 let n = String.length
s in
626 then error "failed to read X connection setup response n=%d" n;
629 let reasonlen = r8 s 1 in
630 let s = readstr sock
6 in
635 let data = readstr sock
len in
636 let reason = String.sub
data 0 reasonlen in
637 error "X connection failed maj=%d min=%d reason=%S"
640 | '
\002'
-> failwith
"X connection setup failed: authentication required";
643 let s = readstr sock
38 in
647 and idbase
= r32 s 10
648 and idmask
= r32 s 14
650 and screens
= r8 s 26
651 and formats
= r8 s 27
653 and maxkk
= r8 s 33 in
654 let data = readstr sock
(4*add
-32) in
655 let vendor = String.sub
data 0 vlen
in
656 let pos = ((vlen
+3) land lnot
3) + formats
*8 in
658 if screennum
>= screens
659 then error "invalid screen %d, max %d" screennum
(screens
-1);
663 let rec findscreen n pos = if n = screennum
then pos else
665 let ndepths = r8 s (pos+39) in
666 let rec skipdepths n pos = if n = ndepths then pos else
668 let nvisiuals = r16 s (pos+2) in
669 pos + nvisiuals*24 + 8
673 skipdepths n (pos+40)
679 let root = r32 data pos in
680 let rootw = r16 data (pos+20)
681 and rooth
= r16 data (pos+22) in
684 state.idbase
<- idbase
;
685 vlog "vendor = %S, maj=%d min=%d" vendor maj min
;
686 vlog "screens = %d formats = %d" screens formats
;
687 vlog "minkk = %d maxkk = %d" minkk maxkk
;
688 vlog "idbase = %#x idmask = %#x" idbase idmask
;
689 vlog "root=%#x %dx%d" root rootw rooth
;
692 + 0x00000001 (* KeyPress *)
693 (* + 0x00000002 *) (* KeyRelease *)
694 + 0x00000004 (* ButtonPress *)
695 + 0x00000008 (* ButtonRelease *)
696 + 0x00000010 (* EnterWindow *)
697 + 0x00000020 (* LeaveWindow *)
698 + 0x00000040 (* PointerMotion *)
699 (* + 0x00000080 *) (* PointerMotionHint *)
700 (* + 0x00000100 *) (* Button1Motion *)
701 (* + 0x00000200 *) (* Button2Motion *)
702 (* + 0x00000400 *) (* Button3Motion *)
703 (* + 0x00000800 *) (* Button4Motion *)
704 (* + 0x00001000 *) (* Button5Motion *)
705 + 0x00002000 (* ButtonMotion *)
706 (* + 0x00004000 *) (* KeymapState *)
707 + 0x00008000 (* Exposure *)
708 + 0x00010000 (* VisibilityChange *)
709 + 0x00020000 (* StructureNotify *)
710 (* + 0x00040000 *) (* ResizeRedirect *)
711 (* + 0x00080000 *) (* SubstructureNotify *)
712 (* + 0x00100000 *) (* SubstructureRedirect *)
713 (* + 0x00200000 *) (* FocusChange *)
714 (* + 0x00400000 *) (* PropertyChange *)
715 (* + 0x00800000 *) (* ColormapChange *)
716 (* + 0x01000000 *) (* OwnerGrabButton *)
718 let wid = state.idbase
in
719 let s = createwindowreq wid root 0 0 w h
0 mask in
722 sendintern sock
"WM_PROTOCOLS" false (fun resp ->
723 state.protoatom
<- r32 resp 8;
724 sendintern sock
"WM_DELETE_WINDOW" false (fun resp ->
725 state.deleatom
<- r32 resp 8;
726 let s = s32 state.deleatom
in
727 let s = changepropreq wid state.protoatom
4 32 s in
732 sendintern sock
"WM_CLIENT_MACHINE" false (fun resp ->
733 let atom = r32 resp 8 in
736 try Unix.gethostname
()
738 dolog "error getting host name: %s" (exntos exn
);
744 let s = changepropreq wid atom state.stringatom
8 hostname in
746 sendintern sock
"_NET_WM_PID" false (fun resp ->
747 let atom = r32 resp 8 in
748 let pid = Unix.getpid
() in
750 let s = changepropreq wid atom 6 (* cardinal *) 8 s in
756 syncsendintern sock
2.0 "WM_CLASS" false (fun resp ->
757 let atom = r32 resp 8 in
758 let llpp = "llpp\000llpp\000" in
759 let s = changepropreq wid atom 31 8 llpp in
763 let s = mapreq wid in
766 let s = getkeymapreq state.mink
(state.maxk
-state.mink
) in
767 sendwithrep sock
s (updkmap sock
);
769 let s = getmodifiermappingreq () in
770 sendwithrep sock
s (updmodmap sock
);
772 let s = openfontreq (wid+1) "cursor" in
775 Array.iteri
(fun i glyphindex
->
776 let s = createglyphcursorreq (wid+1) (wid+2+i) glyphindex
in
778 ) [|34;48;50;58;128;152|];
780 sendintern sock
"UTF8_STRING" true (fun resp ->
781 let atom = r32 resp 8 in
783 then state.stringatom
<- atom;
787 let s = changepropreq state.idbase
39 state.stringatom
8 s in
788 sendstr s state.sock
;
790 state.setwmname <- setwmname;
791 sendintern sock
"_NET_WM_NAME" true (fun resp ->
792 let atom = r32 resp 8 in
794 then state.setwmname <- (fun s ->
796 let s = changepropreq state.idbase
atom state.stringatom
8 s in
797 sendstr s state.sock
;
801 state.fullscreen
<- (fun wid ->
802 let s = "xxuuyyuuwwuuhhuu" in
809 let s = configurewindowreq wid 0x000f s in
810 sendstr s state.sock
;
811 state.fs
<- Fs
(state.x, state.y, state.w
, state.h
);
818 let s = configurewindowreq wid 0x000f s in
819 sendstr s state.sock
;
823 sendintern sock
"_NET_WM_STATE" true (fun resp ->
824 let nwmsatom = r32 resp 8 in
827 sendintern sock
"_NET_WM_STATE_FULLSCREEN" true (fun resp ->
828 let fsatom = r32 resp 8 in
833 let data = String.make
20 '
\000'
in
836 | NoFs
-> Fs
(-1, -1, -1, -1), 1
842 let cm = clientmessage 32 0 wid nwmsatom data in
843 let s = sendeventreq 0 root 0x180000 cm in
849 let s = getgeometryreq wid in
850 syncsendwithrep sock
2.0 s (fun resp ->
853 and h
= r16 resp 18 in
859 error "unknown conection setup response %d" (Char.code c
)
862 let getauth haddr dnum
=
864 if haddr = "localhost" || String.length
haddr = 0
866 try Unix.gethostname
()
868 dolog "failed to resolve `%S': %s" haddr (exntos exn
);
873 try Sys.getenv
"XAUTHORITY"
875 try Filename.concat
(Sys.getenv
"HOME") ".Xauthority"
879 let input_string ic
len =
880 let s = String.create
len in
881 really_input ic
s 0 len;
885 let rb pos = Char.code (String.get
s pos) in
886 (rb 1) lor ((rb 0) lsl 8)
890 let s = input_string ic
2 in
894 let family = input_string ic
2 in
898 try Some
(int_of_string
nums)
901 "display number(%S) is not an integer (corrupt %S?): %s"
902 nums path (exntos exn
);
908 vlog "family %S addr %S(%S) num %S(%d) name %S data %S"
909 family addr haddr nums dnum
name data;
911 | Some num
when addr = haddr && num
= dnum
->
918 | End_of_file
-> "", ""
920 dolog "exception while reading X authority data (%S): %s"
929 if String.length
path = 0
931 else Some
(open_in_bin
path)
933 if Sys.file_exists
path
935 dolog "failed to open X authority file `%S' : %s"
941 | Some ic
-> readauth ic
946 try Sys.getenv
"DISPLAY"
948 error "could not get DISPLAY evironment variable: %s"
953 then error "invalid DISPLAY(%s) %S" w d
955 let s = String.sub
d b (e
- b) in
958 error "invalid DISPLAY %S can not parse %s(%S): %s"
962 if pos = String.length
d
963 then error "invalid DISPLAY %S no display number specified" d
967 let rec pdispnum pos1
=
968 if pos1
= String.length
d
969 then getnum "display number" (pos+1) pos1
, 0
973 let dispnum = getnum "display number" (pos+1) pos1
in
974 let rec pscreennum pos2
=
975 if pos2
= String.length
d
976 then getnum "screen number" (pos1
+1) pos2
979 | '
0'
.. '
9'
-> pscreennum (pos2
+1)
981 error "invalid DISPLAY %S, cannot parse screen number" d
983 dispnum, pscreennum (pos1
+1)
984 | '
0'
.. '
9'
-> pdispnum (pos1
+1)
986 error "invalid DISPLAY %S, cannot parse display number" d
988 String.sub
d 0 pos, pdispnum (pos+1)
992 let host, (dispnum, screennum
) = phost 0 in
993 let aname, adata
= getauth host dispnum in
996 if osx
|| String.length
host = 0 || host = "unix"
1000 then Unix.ADDR_UNIX
d
1001 else Unix.ADDR_UNIX
("/tmp/.X11-unix/X" ^ string_of_int
dispnum)
1003 let fd = Unix.socket
Unix.PF_UNIX
Unix.SOCK_STREAM
0 in
1007 try Unix.gethostbyname
host
1009 error "cannot resolve %S: %s" host (exntos exn
)
1011 let addr = h.Unix.h_addr_list
.(0) in
1012 let port = 6000 + dispnum in
1013 let fd = Unix.socket
Unix.PF_INET
Unix.SOCK_STREAM
0 in
1014 fd, (Unix.ADDR_INET
(addr, port))
1016 try Unix.connect
fd addr; fd
1018 error "failed to connect to X: %s" (exntos exn
)
1021 let s = "luMMmmnndduu" in
1022 let s = padcat s aname in
1023 let s = padcat s adata
in
1026 w16 s 6 (String.length
aname);
1027 w16 s 8 (String.length adata
);
1028 sendstr1 s 0 (String.length
s) fd;
1030 setup fd screennum
w h;
1032 fd, state.w, state.h;
1039 let setcursor cursor
=
1040 if cursor
!= state.curcurs
1044 | CURSOR_INHERIT
-> -1
1047 | CURSOR_CROSSHAIR
-> 0
1050 let s = s32 (if n = -1 then 0 else state.idbase
+2+n) in
1051 let s = changewindowattributesreq state.idbase
(*cursor*)0x4000 s in
1052 sendstr s state.sock
;
1053 state.curcurs
<- cursor
;
1057 state.fullscreen state.idbase
;
1060 let metamask = 0x40;;
1065 let withalt mask = mask land altmask != 0;;
1066 let withctrl mask = mask land ctrlmask != 0;;
1067 let withshift mask = mask land shiftmask != 0;;
1068 let withmeta mask = mask land metamask != 0;;
1069 let withnone mask = mask land (altmask + ctrlmask + shiftmask + metamask) = 0;;
1072 let t = Hashtbl.create
20
1073 and f
= Hashtbl.create
20 in
1075 List.iter
(fun s -> Hashtbl.add t s k) (n::nl
);
1079 let s = String.create
1 in
1081 add s [] (Char.code c
)
1084 let an = Char.code a and bn
= Char.code b in
1085 for i = an to bn
do addc (Char.chr
i) done;
1090 String.iter
addc "`~!@#$%^&*()-_=+\\|[{]};:,./<>?";
1091 for i = 0 to 29 do add ("f" ^ string_of_int
(i+1)) [] (0xffbe + i) done;
1092 add "space" [] 0x20;
1093 add "ret" ["return"; "enter"] 0xff0d;
1094 add "tab" [] 0xff09;
1095 add "left" [] 0xff51;
1096 add "right" [] 0xff53;
1097 add "home" [] 0xff50;
1098 add "end" [] 0xff57;
1099 add "ins" ["insert"] 0xff63;
1100 add "del" ["delete"] 0xffff;
1101 add "esc" ["escape"] 0xff1b;
1102 add "pgup" ["pageup"] 0xff55;
1103 add "pgdown" ["pagedown"] 0xff56;
1104 add "backspace" [] 0xff08;
1106 add "down" [] 0xff54;
1111 try Hashtbl.find xlatf
k
1112 with Not_found
-> Printf.sprintf
"%#x" k;
1116 try Hashtbl.find xlatt name
1118 if String.length
name = 1
1119 then Char.code name.[0]
1120 else int_of_string
name;