9 external cloexec
: Unix.file_descr
-> unit = "ml_cloexec";;
10 external glx
: int -> unit = "ml_glx";;
11 external glxsync
: unit -> unit = "ml_glxsync";;
12 external swapb
: unit -> unit = "ml_swapb";;
13 external hasdata
: Unix.file_descr
-> bool = "ml_hasdata";;
14 external toutf8
: int -> string = "ml_keysymtoutf8";;
16 let dolog fmt
= Format.kprintf prerr_endline fmt
;;
17 let vlog fmt
= Format.kprintf ignore fmt
;;
22 method reshape _ _
= ()
23 method mouse _ _ _ _ _
= ()
24 method motion _ _
= ()
25 method pmotion _ _
= ()
35 method reshape
: int -> int -> unit
36 method mouse
: int -> bool -> int -> int -> int -> unit
37 method motion
: int -> int -> unit
38 method pmotion
: int -> int -> unit
39 method key
: int -> int -> unit
40 method enter
: int -> int -> unit
48 ; mutable keymap
: int array array
49 ; fifo
: (string -> unit) Queue.t
51 ; mutable protoatom
: int
52 ; mutable deleatom
: int
53 ; mutable idbase
: int
54 ; mutable fullscreen
: (int -> unit)
55 ; mutable setwmname
: (string -> unit)
56 ; mutable stringatom
: int
58 ; mutable sock
: Unix.file_descr
64 ; mutable curcurs
: cursor
65 ; mutable capslmask
: int
66 ; mutable levl3mask
: int
67 ; mutable levl5mask
: int
71 | Fs
of (int * int * int * int)
78 ; fifo
= Queue.create
()
83 ; fullscreen
= (fun _
-> ())
84 ; setwmname
= (fun _
-> ())
93 ; curcurs
= CURSOR_INHERIT
100 let w8 s pos i
= String.set s pos
(Char.chr
(i
land 0xff));;
104 w8 s
(pos
+1) (i
lsr 8);
109 w16 s
(pos
+2) (i
lsr 16);
113 let rb pos1
= Char.code
(String.get s
(pos
+ pos1
)) in
114 (rb 0) lor ((rb 1) lsl 8)
119 i - ((i land 0x8000) lsl 1);
122 let r8 s pos
= Char.code
(String.get s pos
);;
125 let rb pos1
= Char.code
(String.get s
(pos
+ pos1
)) in
126 let l = (rb 0) lor ((rb 1) lsl 8)
127 and u
= (rb 2) lor ((rb 3) lsl 8) in
131 let exntos = function
132 | Unix.Unix_error
(e
, s
, a
) -> Printf.sprintf
"%s(%s):%s(%d)"
133 s a
(Unix.error_message e
) (Obj.magic e
)
134 | exn
-> Printexc.to_string exn
;
137 let error fmt
= Printf.kprintf failwith fmt
;;
140 let s = String.create n
in
142 let m = Unix.read sock
s pos n
in
147 ignore
(Unix.select
[sock
] [] [] 0.01);
148 loop (pos
+ m) (n
- m)
155 let sendstr1 s pos len sock
=
156 vlog "%d => %S" state.seq
s;
157 state.seq
<- state.seq
+ 1;
158 let n = Unix.send sock
s pos len
[] in
160 then error "send %d returned %d" len
n;
163 let updkmap sock resp
=
164 let syms = r8 resp
1 in
165 let len = r32 resp
4 in
168 then readstr sock
(4*len)
171 let m = len / syms in
172 state.keymap
<- Array.make_matrix
173 (state.maxk
- state.mink
) syms 0xffffff;
174 let rec loop i = if i = m then () else
176 let rec loop2 k l = if l = syms then () else
177 let v = r32 data k in
178 state.keymap
.(i).(l) <- v;
187 let updmodmap sock resp
=
189 let len = r16 resp
4 in
192 then readstr sock
(len*4)
195 let modmap = Array.make_matrix
8 n 0xffffff in
196 let rec loop l = if l = 8 then () else
198 let rec loop1 m = if m = n then () else
200 let code = r8 data p in
201 modmap.(l).(m) <- code;
204 let ki = code - state.mink
in
207 let a = state.keymap
.(ki) in
208 let rec capsloop i = if i = Array.length
a || i > 3 then () else
211 then state.capslmask
<- 2
219 let ki = code - state.mink
in
222 let a = state.keymap
.(ki) in
223 let rec lloop i = if i = Array.length
a || i > 3 then () else
226 | 0xfe03 -> state.levl3mask
<- 0x80
227 | 0xfe11 -> state.levl5mask
<- 0x20
243 let sendwithrep sock
s f
=
244 Queue.push f
state.fifo
;
245 sendstr1 s 0 (String.length
s) sock
;
249 let b = Buffer.create
16 in
250 List.iter
(Buffer.add_string
b) ss
;
251 let bl = Buffer.length
b in
252 let pl = bl land 3 in
256 Buffer.add_substring
b pad 0 (4 - pl);
261 let padcat s1 s2
= padcatl [s1
; s2
];;
263 let internreq name onlyifexists
=
264 let s = "\016\000\000\000\000\000\000\000" in
265 let s = padcat s name
in
266 if onlyifexists
then w8 s 1 1;
267 w16 s 2 (String.length
s / 4);
268 w16 s 4 (String.length name
);
272 let sendintern sock
s onlyifexists f
=
273 let s = internreq s onlyifexists
in
274 sendwithrep sock
s f
;
277 let createwindowreq wid parent x y w h bw mask
=
278 let s = "\001\000\009\000wwwwppppxxyywwhhbwccvvvvmmmmeeee" in
288 w32 s 28 0x800; (* eventmask *)
293 let getgeometryreq wid
=
294 let s = "\014u\002\000dddd" in
300 let s = "\008u\002\000wwww" in
305 let getkeymapreq first count
=
306 let s = "\101u\002\000fcuu" in
312 let changepropreq wid prop typ format props
=
313 let s = "\018\000llwwwwppppttttfuuuLLLL" in
314 let s = padcat s props
in
315 w16 s 2 (String.length
s / 4);
320 let ful = String.length props
/ (match format
with
324 | n -> error "no idea what %d means" n)
330 let openfontreq fid name
=
331 let s = "\045ullffffnnuu" in
332 let s = padcat s name
in
333 w16 s 2 (String.length
s / 4);
335 w16 s 8 (String.length name
);
339 let createglyphcursorreq fid cid cindex
=
340 let s = "\094u\008\000ccccffffffffssmmrrggbbRRGGBB" in
355 let changewindowattributesreq wid mask attrs
=
356 let s = "\002ullwwwwmmmm" in
357 let s = padcat s attrs
in
358 w16 s 2 (String.length
s / 4);
364 let configurewindowreq wid mask values
=
365 let s = "\012ullwwwwmmuu" in
366 let s = padcat s values
in
367 w16 s 2 (String.length
s / 4);
379 let clientmessage format seq wid typ
data =
380 let s = "\033fsswwwwtttt" in
381 let s = padcat s data in
389 let sendeventreq propagate destwid mask
data =
390 let s = "\025p\011\000wwwwmmmm" in
391 let s = padcat s data in
398 let getmodifiermappingreq () =
399 let s = "\119u\001\000" in
403 let getkeysym code mask
=
404 let shift = (mask
land 1) lxor ((mask
land state.capslmask
) lsr 1) in
405 let mod3 = (mask
land state.levl3mask
) != 0 in
406 let mod4 = (mask
land state.levl5mask
) != 0 in
408 if mod3 then (if mod4 then 8 else 4) else (if mod4 then 6 else 0)
410 let keysym = state.keymap
.(code-state.mink
).(index) in
411 if index land 1 = 1 && keysym = 0
412 then state.keymap
.(code-state.mink
).(index - 1)
417 let resp = readstr sock
32 in
418 let opcode = r8 resp 0 in
419 match opcode land lnot
0x80 with
424 and resid
= r32 resp 4
427 error "code=%d serial=%d resid=%#x min=%d maj=%d\n%S"
428 code serial resid min maj
resp;
430 | 1 -> (* response *)
431 let rep = Queue.pop
state.fifo
in
434 | 2 -> (* key press *)
435 if Array.length
state.keymap
> 0
437 let code = r8 resp 1 in
438 let mask = r16 resp 28 in
439 let keysym = getkeysym code mask in
440 vlog "keysym = %x %c mask %#x code %d"
441 keysym (Char.unsafe_chr
keysym) mask code;
442 state.t#key
keysym mask;
444 | 3 -> (* key release *)
445 if Array.length
state.keymap
> 0
447 let code = r8 resp 1 in
448 let mask = r16 resp 28 in
449 let keysym = getkeysym code mask in
450 vlog "release keysym = %x %c mask %#x code %#d"
451 keysym (Char.unsafe_chr
keysym) mask code
453 | 4 -> (* buttonpress *)
457 and m = r16 resp 28 in
458 state.t#mouse
n true x y
m;
461 | 5 -> (* buttonrelease *)
465 and m = r16 resp 28 in
466 state.t#mouse
n false x y
m;
467 vlog "release %d %d %d" n x y
470 let x = r16s resp 24 in
471 let y = r16s resp 26 in
472 let m = r16 resp 28 in
474 then state.t#pmotion
x y
475 else state.t#motion
x y;
476 vlog "move %dx%d => %d" x y m
480 and y = r16s resp 26 in
482 vlog "enter %d %d" x y
487 | 18 -> vlog "unmap";
492 | 12 -> (* exposure *)
496 | 15 -> (* visibility *)
497 let vis = r8 resp 8 in
498 if vis != 2 then state.t#expose
;
499 vlog "visibility %d" vis;
501 | 34 -> (* mapping *)
502 state.keymap
<- [||];
503 let s = getkeymapreq state.mink
(state.maxk
-state.mink
-1) in
504 sendwithrep sock
s (updkmap sock
);
505 state.capslmask
<- 0;
506 state.levl3mask
<- 0;
507 state.levl5mask
<- 0;
508 let s = getmodifiermappingreq () in
509 sendwithrep sock
s (updmodmap sock
);
512 | 33 -> (* clientmessage *)
513 let atom = r32 resp 8 in
514 if atom = state.protoatom
516 let atom = r32 resp 12 in
517 if atom = state.deleatom
522 | 21 -> (* reparent *)
525 | 22 -> (* configure *)
529 and h
= r16 resp 22 in
530 vlog "configure cur [%d %d %d %d] conf [%d %d %d %d]"
531 state.x state.y state.w
state.h
535 if w
!= state.w
|| h
!= state.h
546 dolog "event %d %S" n resp
552 if hasdata sock
then loop ();
557 let sendstr s ?
(pos
=0) ?
(len=String.length
s) sock
=
558 sendstr1 s pos
len sock
;
559 if hasdata sock
then readresp sock
;
565 let s = "wwuuhhuu" in
568 let s = configurewindowreq state.idbase
0x000c s in
569 sendstr s state.sock
;
570 else state.fullscreen
state.idbase
573 let syncsendwithrep sock secstowait
s f
=
574 let completed = ref false in
575 sendwithrep sock
s (fun resp -> f
resp; completed := true);
576 let now = Unix.gettimeofday
in
577 let deadline = now () +. secstowait
in
578 let rec readtillcompletion () =
579 let r, _
, _
= Unix.select
[sock
] [] [] (deadline -. now ()) in
581 | [] -> error "didn't get X response in %f seconds, aborting" secstowait
585 then readtillcompletion ()
587 readtillcompletion ();
590 let syncsendintern sock secstowait
s onlyifexists f
=
591 let s = internreq s onlyifexists
in
592 syncsendwithrep sock secstowait
s f
;
595 let setup sock screennum w h
=
596 let s = readstr sock
2 in
597 let n = String.length
s in
599 then error "failed to read X connection setup response n=%d" n;
602 let reasonlen = r8 s 1 in
603 let s = readstr sock
6 in
608 let data = readstr sock
len in
609 let reason = String.sub
data 0 reasonlen in
610 error "X connection failed maj=%d min=%d reason=%S"
613 | '
\002'
-> failwith
"X connection setup failed: authentication required";
616 let s = readstr sock
38 in
620 and idbase
= r32 s 10
621 and idmask
= r32 s 14
623 and screens
= r8 s 26
624 and formats
= r8 s 27
626 and maxkk
= r8 s 33 in
627 let data = readstr sock
(4*add
-32) in
628 let vendor = String.sub
data 0 vlen
in
629 let pos = ((vlen
+3) land lnot
3) + formats
*8 in
631 if screennum
>= screens
632 then error "invalid screen %d, max %d" screennum
(screens
-1);
636 let rec findscreen n pos = if n = screennum
then pos else
638 let ndepths = r8 s (pos+39) in
639 let rec skipdepths n pos = if n = ndepths then pos else
641 let nvisiuals = r16 s (pos+2) in
642 pos + nvisiuals*24 + 8
646 skipdepths n (pos+40)
652 let root = r32 data pos in
653 let rootw = r16 data (pos+20)
654 and rooth
= r16 data (pos+22) in
657 state.idbase
<- idbase
;
658 vlog "vendor = %S, maj=%d min=%d" vendor maj min
;
659 vlog "screens = %d formats = %d" screens formats
;
660 vlog "minkk = %d maxkk = %d" minkk maxkk
;
661 vlog "idbase = %#x idmask = %#x" idbase idmask
;
662 vlog "root=%#x %dx%d" root rootw rooth
;
665 + 0x00000001 (* KeyPress *)
666 (* + 0x00000002 *) (* KeyRelease *)
667 + 0x00000004 (* ButtonPress *)
668 + 0x00000008 (* ButtonRelease *)
669 + 0x00000010 (* EnterWindow *)
670 + 0x00000020 (* LeaveWindow *)
671 + 0x00000040 (* PointerMotion *)
672 (* + 0x00000080 *) (* PointerMotionHint *)
673 (* + 0x00000100 *) (* Button1Motion *)
674 (* + 0x00000200 *) (* Button2Motion *)
675 (* + 0x00000400 *) (* Button3Motion *)
676 (* + 0x00000800 *) (* Button4Motion *)
677 (* + 0x00001000 *) (* Button5Motion *)
678 + 0x00002000 (* ButtonMotion *)
679 (* + 0x00004000 *) (* KeymapState *)
680 + 0x00008000 (* Exposure *)
681 + 0x00010000 (* VisibilityChange *)
682 + 0x00020000 (* StructureNotify *)
683 (* + 0x00040000 *) (* ResizeRedirect *)
684 (* + 0x00080000 *) (* SubstructureNotify *)
685 (* + 0x00100000 *) (* SubstructureRedirect *)
686 (* + 0x00200000 *) (* FocusChange *)
687 (* + 0x00400000 *) (* PropertyChange *)
688 (* + 0x00800000 *) (* ColormapChange *)
689 (* + 0x01000000 *) (* OwnerGrabButton *)
691 let wid = state.idbase
in
692 let s = createwindowreq wid root 0 0 w h
0 mask in
695 sendintern sock
"WM_PROTOCOLS" false (fun resp ->
696 state.protoatom
<- r32 resp 8;
697 sendintern sock
"WM_DELETE_WINDOW" false (fun resp ->
698 state.deleatom
<- r32 resp 8;
699 let s = s32 state.deleatom
in
700 let s = changepropreq wid state.protoatom
4 32 s in
705 syncsendintern sock
2.0 "WM_CLASS" false (fun resp ->
706 let atom = r32 resp 8 in
707 let llpp = "llpp\000llpp\000" in
708 let s = changepropreq wid atom 31 8 llpp in
712 let s = mapreq wid in
715 let s = getkeymapreq state.mink
(state.maxk
-state.mink
) in
716 sendwithrep sock
s (updkmap sock
);
718 let s = getmodifiermappingreq () in
719 sendwithrep sock
s (updmodmap sock
);
721 let s = openfontreq (wid+1) "cursor" in
724 Array.iteri
(fun i glyphindex
->
725 let s = createglyphcursorreq (wid+1) (wid+2+i) glyphindex
in
727 ) [|34;48;50;58;128;152|];
729 sendintern sock
"UTF8_STRING" true (fun resp ->
730 let atom = r32 resp 8 in
732 then state.stringatom
<- atom;
736 let s = changepropreq state.idbase
39 state.stringatom
8 s in
737 sendstr s state.sock
;
739 state.setwmname <- setwmname;
740 sendintern sock
"_NET_WM_NAME" true (fun resp ->
741 let atom = r32 resp 8 in
743 then state.setwmname <- (fun s ->
745 let s = changepropreq state.idbase
atom state.stringatom
8 s in
746 sendstr s state.sock
;
750 state.fullscreen
<- (fun wid ->
751 let s = "xxuuyyuuwwuuhhuu" in
758 let s = configurewindowreq wid 0x000f s in
759 sendstr s state.sock
;
760 state.fs
<- Fs
(state.x, state.y, state.w
, state.h
);
767 let s = configurewindowreq wid 0x000f s in
768 sendstr s state.sock
;
772 sendintern sock
"_NET_WM_STATE" true (fun resp ->
773 let nwmsatom = r32 resp 8 in
776 sendintern sock
"_NET_WM_STATE_FULLSCREEN" true (fun resp ->
777 let fsatom = r32 resp 8 in
782 let data = String.make
20 '
\000'
in
785 | NoFs
-> Fs
(-1, -1, -1, -1), 1
791 let cm = clientmessage 32 0 wid nwmsatom data in
792 let s = sendeventreq 0 root 0x180000 cm in
798 let s = getgeometryreq wid in
799 syncsendwithrep sock
2.0 s (fun resp ->
802 and h
= r16 resp 18 in
808 error "unknown conection setup response %d" (Char.code c
)
811 let getauth haddr dnum
=
813 if haddr = "localhost" || String.length
haddr = 0
815 try Unix.gethostname
()
817 dolog "failed to resolve `%S': %s" haddr (exntos exn
);
822 try Sys.getenv
"XAUTHORITY"
824 try Filename.concat
(Sys.getenv
"HOME") ".Xauthority"
828 let input_string ic
len =
829 let s = String.create
len in
830 really_input ic
s 0 len;
834 let rb pos = Char.code (String.get
s pos) in
835 (rb 1) lor ((rb 0) lsl 8)
839 let s = input_string ic
2 in
843 let family = input_string ic
2 in
847 try Some
(int_of_string
nums)
850 "display number(%S) is not an integer (corrupt %S?): %s"
851 nums path (exntos exn
);
857 vlog "family %S addr %S(%S) num %S(%d) name %S data %S"
858 family addr haddr nums dnum
name data;
860 | Some num
when addr = haddr && num
= dnum
->
867 | End_of_file
-> "", ""
869 dolog "exception while reading X authority data (%S): %s"
878 if String.length
path = 0
880 else Some
(open_in_bin
path)
882 if Sys.file_exists
path
884 dolog "failed to open X authority file `%S' : %s"
890 | Some ic
-> readauth ic
895 try Sys.getenv
"DISPLAY"
897 error "could not get DISPLAY evironment variable: %s"
902 then error "invalid DISPLAY(%s) %S" w d
904 let s = String.sub
d b (e
- b) in
907 error "invalid DISPLAY %S can not parse %s(%S): %s"
911 if pos = String.length
d
912 then error "invalid DISPLAY %S no display number specified" d
916 let rec pdispnum pos1
=
917 if pos1
= String.length
d
918 then getnum "display number" (pos+1) pos1
, 0
922 let dispnum = getnum "display number" (pos+1) pos1
in
923 let rec pscreennum pos2
=
924 if pos2
= String.length
d
925 then getnum "screen number" (pos1
+1) pos2
928 | '
0'
.. '
9'
-> pscreennum (pos2
+1)
930 error "invalid DISPLAY %S, cannot parse screen number" d
932 dispnum, pscreennum (pos1
+1)
933 | '
0'
.. '
9'
-> pdispnum (pos1
+1)
935 error "invalid DISPLAY %S, cannot parse display number" d
937 String.sub
d 0 pos, pdispnum (pos+1)
941 let host, (dispnum, screennum
) = phost 0 in
942 let aname, adata
= getauth host dispnum in
945 if osx
|| String.length
host = 0 || host = "unix"
949 then Unix.ADDR_UNIX
d
950 else Unix.ADDR_UNIX
("/tmp/.X11-unix/X" ^ string_of_int
dispnum)
952 let fd = Unix.socket
Unix.PF_UNIX
Unix.SOCK_STREAM
0 in
956 try Unix.gethostbyname
host
958 error "cannot resolve %S: %s" host (exntos exn
)
960 let addr = h.Unix.h_addr_list
.(0) in
961 let port = 6000 + dispnum in
962 let fd = Unix.socket
Unix.PF_INET
Unix.SOCK_STREAM
0 in
963 fd, (Unix.ADDR_INET
(addr, port))
965 try Unix.connect
fd addr; fd
967 error "failed to connect to X: %s" (exntos exn
)
970 let s = "luMMmmnndduu" in
971 let s = padcat s aname in
972 let s = padcat s adata
in
975 w16 s 6 (String.length
aname);
976 w16 s 8 (String.length adata
);
977 sendstr1 s 0 (String.length
s) fd;
979 setup fd screennum
w h;
981 fd, state.w, state.h;
988 let setcursor cursor
=
989 if cursor
!= state.curcurs
993 | CURSOR_INHERIT
-> -1
996 | CURSOR_CROSSHAIR
-> 0
999 let s = s32 (if n = -1 then 0 else state.idbase
+2+n) in
1000 let s = changewindowattributesreq state.idbase
(*cursor*)0x4000 s in
1001 sendstr s state.sock
;
1002 state.curcurs
<- cursor
;
1006 state.fullscreen state.idbase
;
1009 let metamask = 0x40;;
1014 let withalt mask = mask land altmask != 0;;
1015 let withctrl mask = mask land ctrlmask != 0;;
1016 let withshift mask = mask land shiftmask != 0;;
1017 let withmeta mask = mask land metamask != 0;;
1018 let withnone mask = mask land (altmask + ctrlmask + shiftmask + metamask) = 0;;
1021 let t = Hashtbl.create
20
1022 and f
= Hashtbl.create
20 in
1024 List.iter
(fun s -> Hashtbl.add t s k) (n::nl
);
1028 let s = String.create
1 in
1030 add s [] (Char.code c
)
1033 let an = Char.code a and bn
= Char.code b in
1034 for i = an to bn
do addc (Char.chr
i) done;
1039 String.iter
addc "`~!@#$%^&*()-_=+\\|[{]};:,./<>?";
1040 for i = 0 to 29 do add ("f" ^ string_of_int
(i+1)) [] (0xffbe + i) done;
1041 add "space" [] 0x20;
1042 add "ret" ["return"; "enter"] 0xff0d;
1043 add "tab" [] 0xff09;
1044 add "left" [] 0xff51;
1045 add "right" [] 0xff53;
1046 add "home" [] 0xff50;
1047 add "end" [] 0xff57;
1048 add "ins" ["insert"] 0xff63;
1049 add "del" ["delete"] 0xffff;
1050 add "esc" ["escape"] 0xff1b;
1051 add "pgup" ["pageup"] 0xff55;
1052 add "pgdown" ["pagedown"] 0xff56;
1053 add "backspace" [] 0xff08;
1055 add "down" [] 0xff54;
1060 try Hashtbl.find xlatf
k
1061 with Not_found
-> Printf.sprintf
"%#x" k;
1065 try Hashtbl.find xlatt name
1067 if String.length
name = 1
1068 then Char.code name.[0]
1069 else int_of_string
name;