9 external cloexec
: Unix.file_descr
-> unit = "ml_cloexec";;
10 external glx
: int -> unit = "ml_glx";;
11 external swapb
: unit -> unit = "ml_swapb";;
12 external hasdata
: Unix.file_descr
-> bool = "ml_hasdata";;
13 external toutf8
: int -> string = "ml_keysymtoutf8";;
15 let dolog fmt
= Format.kprintf prerr_endline fmt
;;
16 let vlog fmt
= Format.kprintf ignore fmt
;;
21 method reshape _ _
= ()
22 method mouse _ _ _ _ _
= ()
23 method motion _ _
= ()
24 method pmotion _ _
= ()
34 method reshape
: int -> int -> unit
35 method mouse
: int -> bool -> int -> int -> int -> unit
36 method motion
: int -> int -> unit
37 method pmotion
: int -> int -> unit
38 method key
: int -> int -> unit
39 method enter
: int -> int -> unit
47 ; mutable keymap
: int array array
48 ; fifo
: (string -> unit) Queue.t
50 ; mutable protoatom
: int
51 ; mutable deleatom
: int
52 ; mutable idbase
: int
53 ; mutable fullscreen
: (int -> unit)
54 ; mutable setwmname
: (string -> unit)
55 ; mutable stringatom
: int
57 ; mutable sock
: Unix.file_descr
63 ; mutable curcurs
: cursor
67 | Fs
of (int * int * int * int)
74 ; fifo
= Queue.create
()
79 ; fullscreen
= (fun _
-> ())
80 ; setwmname
= (fun _
-> ())
89 ; curcurs
= CURSOR_INHERIT
93 let w8 s pos i
= String.set s pos
(Char.chr
(i
land 0xff));;
97 w8 s
(pos
+1) (i
lsr 8);
102 w16 s
(pos
+2) (i
lsr 16);
106 let rb pos1
= Char.code
(String.get s
(pos
+ pos1
)) in
107 (rb 0) lor ((rb 1) lsl 8)
112 i - ((i land 0x8000) lsl 1);
115 let r8 s pos
= Char.code
(String.get s pos
);;
118 let rb pos1
= Char.code
(String.get s
(pos
+ pos1
)) in
119 let l = (rb 0) lor ((rb 1) lsl 8)
120 and u
= (rb 2) lor ((rb 3) lsl 8) in
124 let error fmt
= Printf.kprintf failwith fmt
;;
127 let s = String.create n
in
129 let m = Unix.read sock
s pos n
in
132 ignore
(Unix.select
[sock
] [] [] 0.01);
133 loop (pos
+ m) (n
- m)
140 let sendstr1 s pos len sock
=
141 vlog "%d => %S" state.seq
s;
142 state.seq
<- state.seq
+ 1;
143 let n = Unix.send sock
s pos len
[] in
145 then error "send %d returned %d" len
n;
148 let updkmap sock resp
=
149 let syms = r8 resp
1 in
150 let len = r32 resp
4 in
153 then readstr sock
(4*len)
156 let m = len / syms in
157 state.keymap
<- Array.make_matrix
158 (state.maxk
- state.mink
) syms 0xffffff;
159 let rec loop i = if i = m then () else
161 let rec loop2 k l = if l = syms then () else
162 let v = r32 data k in
163 state.keymap
.(i).(l) <- v;
172 let sendwithrep sock
s f
=
173 Queue.push f
state.fifo
;
174 sendstr1 s 0 (String.length
s) sock
;
178 let b = Buffer.create
16 in
179 List.iter
(Buffer.add_string
b) ss
;
180 let bl = Buffer.length
b in
181 let pl = bl land 3 in
185 Buffer.add_substring
b pad 0 (4 - pl);
190 let padcat s1 s2
= padcatl [s1
; s2
];;
192 let internreq name onlyifexists
=
193 let s = "\016\000\000\000\000\000\000\000" in
194 let s = padcat s name
in
195 w8 s 1 (if onlyifexists
then 1 else 0);
196 w16 s 2 (String.length
s / 4);
197 w16 s 4 (String.length name
);
201 let sendintern sock
s onlyifexists f
=
202 let s = internreq s onlyifexists
in
203 sendwithrep sock
s f
;
206 let createwindowreq wid parent x y w h bw mask
=
207 let s = "\001\000\009\000wwwwppppxxyywwhhbwccvvvvmmmmeeee" in
217 w32 s 28 0x800; (* eventmask *)
222 let getgeometryreq wid
=
223 let s = "\014u\002\000dddd" in
229 let s = "\008u\002\000wwww" in
234 let getkeymapreq first count
=
235 let s = "\101u\002\000fcuu" in
241 let changepropreq wid prop typ format props
=
242 let s = "\018\000llwwwwppppttttfuuuLLLL" in
243 let s = padcat s props
in
244 w16 s 2 (String.length
s / 4);
249 let ful = String.length props
/ (match format
with
253 | n -> error "no idea what %d means" n)
259 let openfontreq fid name
=
260 let s = "\045ullffffnnuu" in
261 let s = padcat s name
in
262 w16 s 2 (String.length
s / 4);
264 w16 s 8 (String.length name
);
268 let createglyphcursorreq fid cid cindex
=
269 let s = "\094u\008\000ccccffffffffssmmrrggbbRRGGBB" in
284 let mapwindowreq wid
=
285 let s = "\008u\002\000wwww" in
290 let changewindowattributesreq wid mask attrs
=
291 let s = "\002ullwwwwmmmm" in
292 let s = padcat s attrs
in
293 w16 s 2 (String.length
s / 4);
299 let configurewindowreq wid mask values
=
300 let s = "\012ullwwwwmmuu" in
301 let s = padcat s values
in
302 w16 s 2 (String.length
s / 4);
314 let clientmessage format seq wid typ
data =
315 let s = "\033fsswwwwtttt" in
316 let s = padcat s data in
324 let sendeventreq propagate destwid mask
data =
325 let s = "\025p\011\000wwwwmmmm" in
326 let s = padcat s data in
333 let getkeysym code mask
=
334 let index = (mask
land 1) lxor ((mask
land 2) lsr 1) in
335 let keysym = state.keymap
.(code
-state.mink
).(index) in
336 if index = 1 && keysym = 0
337 then state.keymap
.(code
-state.mink
).(0)
341 let rec readresp sock
=
342 let resp = readstr sock
32 in
343 let opcode = r8 resp 0 in
344 match opcode land lnot
0x80 with
349 and resid
= r32 resp 4
352 error "code=%d serial=%d resid=%#x min=%d maj=%d\n%S"
353 code serial resid min maj
resp;
355 | 1 -> (* response *)
356 let rep = Queue.pop
state.fifo
in
359 | 2 -> (* key press *)
360 if Array.length
state.keymap
> 0
362 let code = r8 resp 1 in
363 let mask = r16 resp 28 in
364 let keysym = getkeysym code mask in
365 vlog "keysym = %x %c" keysym (Char.unsafe_chr
keysym);
366 state.t#key
keysym mask;
368 | 3 -> (* key release *)
369 if Array.length
state.keymap
> 0
371 let code = r8 resp 1 in
372 let mask = r16 resp 28 in
373 let keysym = getkeysym code mask in
374 vlog "release keysym = %x %c mask %#x"
375 keysym (Char.unsafe_chr
keysym) mask
377 | 4 -> (* buttonpress *)
381 and m = r16 resp 28 in
382 state.t#mouse
n true x y
m;
385 | 5 -> (* buttonrelease *)
389 and m = r16 resp 28 in
390 state.t#mouse
n false x y
m;
391 vlog "release %d %d %d" n x y
394 let x = r16s resp 24 in
395 let y = r16s resp 26 in
396 let m = r16 resp 28 in
398 then state.t#pmotion
x y
399 else state.t#motion
x y;
400 vlog "move %dx%d => %d" x y m
404 and y = r16s resp 26 in
406 vlog "enter %d %d" x y
411 | 18 -> vlog "unmap";
416 | 12 -> (* exposure *)
420 | 15 -> (* visibility *)
421 let vis = r8 resp 8 in
422 if vis != 2 then state.t#expose
;
423 vlog "visibility %d" vis;
425 | 34 -> (* mapping *)
426 state.keymap
<- [||];
427 let s = getkeymapreq state.mink
(state.maxk
-state.mink
-1) in
428 sendwithrep sock
s (updkmap sock
);
430 | 33 -> (* clientmessage *)
431 let atom = r32 resp 8 in
432 if atom = state.protoatom
434 let atom = r32 resp 12 in
435 if atom = state.deleatom
440 | 21 -> (* reparent *)
443 | 22 -> (* configure *)
447 and h
= r16 resp 22 in
448 vlog "configure cur [%d %d %d %d] conf [%d %d %d %d]"
449 state.x state.y state.w
state.h
452 if w
!= state.w
|| h
!= state.h
463 dolog "event %d %S" n resp
469 if hasdata sock
then loop ();
474 let sendstr s ?
(pos
=0) ?
(len=String.length
s) sock
=
475 sendstr1 s pos
len sock
;
476 if hasdata sock
then readresp sock
;
480 let b = Buffer.create
16 in
481 String.iter
(fun c
->
482 Buffer.add_string
b (Printf.sprintf
"%02x" (Char.code c
))) s;
489 let s = "wwuuhhuu" in
492 let s = configurewindowreq state.idbase
0x000c s in
493 sendstr s state.sock
;
494 else state.fullscreen
state.idbase
497 let setup sock screennum w h
=
498 let s = readstr sock
2 in
499 let n = String.length
s in
501 then error "failed to read X connection setup response n=%d" n;
504 let reasonlen = r8 s 1 in
505 let s = readstr sock
6 in
510 let data = readstr sock
len in
511 let reason = String.sub
data 0 reasonlen in
512 error "X connection failed maj=%d min=%d reason=%S"
515 | '
\002'
-> failwith
"X connection setup failed: authentication required";
518 let s = readstr sock
38 in
522 and idbase
= r32 s 10
523 and idmask
= r32 s 14
525 and screens
= r8 s 26
526 and formats
= r8 s 27
528 and maxkk
= r8 s 33 in
529 let data = readstr sock
(4*add
-32) in
530 let vendor = String.sub
data 0 vlen
in
531 let pos = ((vlen
+3) land lnot
3) + formats
*8 in
533 if screennum
>= screens
534 then error "invalid screen %d, max %d" screennum
(screens
-1);
538 let rec findscreen n pos = if n = screennum
then pos else
540 let ndepths = r8 s (pos+39) in
541 let rec skipdepths n pos = if n = ndepths then pos else
543 let nvisiuals = r16 s (pos+2) in
544 pos + nvisiuals*24 + 8
548 skipdepths n (pos+40)
554 let root = r32 data pos in
555 let rootw = r16 data (pos+20)
556 and rooth
= r16 data (pos+22) in
559 state.idbase
<- idbase
;
560 vlog "vendor = %S, maj=%d min=%d" vendor maj min
;
561 vlog "screens = %d formats = %d" screens formats
;
562 vlog "minkk = %d maxkk = %d" minkk maxkk
;
563 vlog "idbase = %#x idmask = %#x" idbase idmask
;
564 vlog "root=%#x %dx%d" root rootw rooth
;
567 + 0x00000001 (* KeyPress *)
568 (* + 0x00000002 *) (* KeyRelease *)
569 + 0x00000004 (* ButtonPress *)
570 + 0x00000008 (* ButtonRelease *)
571 + 0x00000010 (* EnterWindow *)
572 + 0x00000020 (* LeaveWindow *)
573 + 0x00000040 (* PointerMotion *)
574 (* + 0x00000080 *) (* PointerMotionHint *)
575 (* + 0x00000100 *) (* Button1Motion *)
576 (* + 0x00000200 *) (* Button2Motion *)
577 (* + 0x00000400 *) (* Button3Motion *)
578 (* + 0x00000800 *) (* Button4Motion *)
579 (* + 0x00001000 *) (* Button5Motion *)
580 + 0x00002000 (* ButtonMotion *)
581 (* + 0x00004000 *) (* KeymapState *)
582 + 0x00008000 (* Exposure *)
583 + 0x00010000 (* VisibilityChange *)
584 + 0x00020000 (* StructureNotify *)
585 (* + 0x00040000 *) (* ResizeRedirect *)
586 (* + 0x00080000 *) (* SubstructureNotify *)
587 (* + 0x00100000 *) (* SubstructureRedirect *)
588 (* + 0x00200000 *) (* FocusChange *)
589 (* + 0x00400000 *) (* PropertyChange *)
590 (* + 0x00800000 *) (* ColormapChange *)
591 (* + 0x01000000 *) (* OwnerGrabButton *)
593 let wid = state.idbase
in
594 let s = createwindowreq wid root 0 0 w h
0 mask in
597 let s = mapreq wid in
600 let s = getkeymapreq state.mink
(state.maxk
-state.mink
) in
601 sendwithrep sock
s (updkmap sock
);
603 sendintern sock
"WM_PROTOCOLS" false (fun resp ->
604 state.protoatom
<- r32 resp 8;
605 sendintern sock
"WM_DELETE_WINDOW" false (fun resp ->
606 state.deleatom
<- r32 resp 8;
607 let s = s32 state.deleatom
in
608 let s = changepropreq wid state.protoatom
4 32 s in
613 sendintern sock
"WM_CLASS" false (fun resp ->
614 let atom = r32 resp 8 in
615 let llpp = "llpp\000llpp\000" in
616 let s = changepropreq wid atom 31 8 llpp in
620 let s = openfontreq (wid+1) "cursor" in
623 Array.iteri
(fun i glyphindex
->
624 let s = createglyphcursorreq (wid+1) (wid+2+i) glyphindex
in
626 ) [|34;48;50;58;128;152|];
628 sendintern sock
"UTF8_STRING" true (fun resp ->
629 let atom = r32 resp 8 in
631 then state.stringatom
<- atom;
635 let s = changepropreq state.idbase
39 state.stringatom
8 s in
636 sendstr s state.sock
;
638 state.setwmname <- setwmname;
639 sendintern sock
"_NET_WM_NAME" true (fun resp ->
640 let atom = r32 resp 8 in
642 then state.setwmname <- (fun s ->
644 let s = changepropreq state.idbase
atom state.stringatom
8 s in
645 sendstr s state.sock
;
649 state.fullscreen
<- (fun wid ->
650 let s = "xxuuyyuuwwuuhhuu" in
657 let s = configurewindowreq wid 0x000f s in
658 sendstr s state.sock
;
659 state.fs
<- Fs
(state.x, state.y, state.w
, state.h
);
666 let s = configurewindowreq wid 0x000f s in
667 sendstr s state.sock
;
671 sendintern sock
"_NET_WM_STATE" true (fun resp ->
672 let nwmsatom = r32 resp 8 in
675 sendintern sock
"_NET_WM_STATE_FULLSCREEN" true (fun resp ->
676 let fsatom = r32 resp 8 in
681 let data = String.make
20 '
\000'
in
684 | NoFs
-> Fs
(-1, -1, -1, -1), 1
690 let cm = clientmessage 32 0 wid nwmsatom data in
691 let s = sendeventreq 0 root 0x180000 cm in
697 let s = getgeometryreq wid in
698 let completed = ref false in
699 sendwithrep sock
s (fun resp ->
702 and h
= r16 resp 18 in
707 let now = Unix.gettimeofday
in
708 let deadline = now () +. 2.0 in
709 let rec readtillcompletion () =
710 let r, _
, _
= Unix.select
[sock
] [] [] (deadline -. now ()) in
712 | [] -> readtillcompletion ()
716 then readtillcompletion ()
718 readtillcompletion ();
721 error "unknown conection setup response %d" (Char.code c
)
724 let getauth haddr dnum
=
726 if haddr = "localhost" || String.length
haddr = 0
728 try Unix.gethostname
()
730 dolog "failed to resolve `%S': %s" haddr (Printexc.to_string exn
);
735 let input_string ic
len =
736 let s = String.create
len in
737 really_input ic
s 0 len;
741 let rb pos = Char.code (String.get
s pos) in
742 (rb 1) lor ((rb 0) lsl 8)
746 let s = input_string ic
2 in
750 let family = input_string ic
2 in
753 let num = int_of_string
nums in
757 vlog "family %S addr %S(%S) num %d(%d) name %S data %S"
758 family addr haddr num dnum
name data;
759 if addr = haddr && num = dnum
764 try find () with _
-> "", ""
770 try Sys.getenv
"XAUTHORITY"
772 try Filename.concat
(Sys.getenv
"HOME") ".Xauthority"
777 if String.length
path = 0
779 else Some
(open_in_bin
path)
781 if Sys.file_exists
path
783 dolog "failed to open X authority file `%S' : %s"
784 path (Printexc.to_string exn
);
789 | Some ic
-> readauth ic
794 try Sys.getenv
"DISPLAY"
796 error "Could not get DISPLAY evironment variable: %s"
797 (Printexc.to_string exn
)
799 let colonpos = String.index d '
:'
in
800 let host = String.sub
d 0 colonpos in
801 let dispnum, screennum
=
803 let dotpos = String.index_from
d (colonpos + 1) '
.'
in
804 let disp = String.sub
d (colonpos + 1) (dotpos - colonpos - 1) in
805 let screen = String.sub
d (dotpos + 1) (String.length
d - dotpos - 1) in
806 int_of_string
disp, int_of_string
screen
808 let disp = String.sub
d (colonpos + 1) (String.length
d - colonpos - 1) in
809 int_of_string
disp, 0
811 let aname, adata
= getauth host dispnum in
813 if osx
|| String.length
host = 0 || host = "unix"
817 then Unix.ADDR_UNIX
d
818 else Unix.ADDR_UNIX
("/tmp/.X11-unix/X" ^ string_of_int
dispnum)
820 let fd = Unix.socket
Unix.PF_UNIX
Unix.SOCK_STREAM
0 in
821 Unix.connect
fd addr;
824 let h = Unix.gethostbyname
host in
825 let addr = h.Unix.h_addr_list
.(0) in
826 let port = 6000 + dispnum in
827 let fd = Unix.socket
Unix.PF_INET
Unix.SOCK_STREAM
0 in
828 Unix.connect
fd (Unix.ADDR_INET
(addr, port));
832 let s = "luMMmmnndduu" in
833 let s = padcat s aname in
834 let s = padcat s adata
in
837 w16 s 6 (String.length
aname);
838 w16 s 8 (String.length adata
);
839 sendstr1 s 0 (String.length
s) fd;
841 setup fd screennum
w h;
843 fd, state.w, state.h;
850 let setcursor cursor
=
851 if cursor
!= state.curcurs
855 | CURSOR_INHERIT
-> -1
858 | CURSOR_CROSSHAIR
-> 0
861 let s = s32 (if n = -1 then 0 else state.idbase
+2+n) in
862 let s = changewindowattributesreq state.idbase
(*cursor*)0x4000 s in
863 sendstr s state.sock
;
864 state.curcurs
<- cursor
;
868 state.fullscreen state.idbase
;
871 let metamask = 0x40;;
876 let withalt mask = mask land altmask != 0;;
877 let withctrl mask = mask land ctrlmask != 0;;
878 let withshift mask = mask land shiftmask != 0;;
879 let withmeta mask = mask land metamask != 0;;
880 let withnone mask = mask land (altmask + ctrlmask + shiftmask + metamask) = 0;;
883 let t = Hashtbl.create
20
884 and f
= Hashtbl.create
20 in
886 List.iter
(fun s -> Hashtbl.add t s k) (n::nl
);
890 let s = String.create
1 in
892 add s [] (Char.code c
)
895 let an = Char.code a
and bn
= Char.code b in
896 for i = an to bn
do addc (Char.chr
i) done;
901 String.iter
addc "`~!@#$%^&*()-_=+\\|[{]};:,./<>?";
902 for i = 0 to 29 do add ("f" ^ string_of_int
(i+1)) [] (0xffbe + i) done;
904 add "ret" ["return"; "enter"] 0xff0d;
906 add "left" [] 0xff51;
907 add "right" [] 0xff53;
908 add "home" [] 0xff50;
910 add "ins" ["insert"] 0xff63;
911 add "del" ["delete"] 0xffff;
912 add "esc" ["escape"] 0xff1b;
913 add "pgup" ["pageup"] 0xff55;
914 add "pgdown" ["pagedown"] 0xff56;
915 add "backspace" [] 0xff08;
917 add "down" [] 0xff54;
922 try Hashtbl.find xlatf
k
923 with Not_found
-> Printf.sprintf
"%#x" k;
927 try Hashtbl.find xlatt name
929 if String.length
name = 1
930 then Char.code name.[0]
931 else int_of_string
name;