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
66 | Fs
of (int * int * int * int)
73 ; fifo
= Queue.create
()
78 ; fullscreen
= (fun _
-> ())
79 ; setwmname
= (fun _
-> ())
91 let w8 s pos i
= String.set s pos
(Char.chr
(i
land 0xff));;
95 w8 s
(pos
+1) (i
lsr 8);
100 w16 s
(pos
+2) (i
lsr 16);
104 let rb pos1
= Char.code
(String.get s
(pos
+ pos1
)) in
105 (rb 0) lor ((rb 1) lsl 8)
110 i - ((i land 0x8000) lsl 1);
113 let r8 s pos
= Char.code
(String.get s pos
);;
116 let rb pos1
= Char.code
(String.get s
(pos
+ pos1
)) in
117 let l = (rb 0) lor ((rb 1) lsl 8)
118 and u
= (rb 2) lor ((rb 3) lsl 8) in
122 let error fmt
= Printf.kprintf failwith fmt
;;
125 let s = String.create n
in
127 let m = Unix.read sock
s pos n
in
130 ignore
(Unix.select
[sock
] [] [] 0.01);
131 loop (pos
+ m) (n
- m)
138 let sendstr1 s pos len sock
=
139 vlog "%d => %S" state.seq
s;
140 state.seq
<- state.seq
+ 1;
141 let n = Unix.send sock
s pos len
[] in
143 then error "send %d returned %d" len
n;
146 let updkmap sock resp
=
147 let syms = r8 resp
1 in
148 let len = r32 resp
4 in
151 then readstr sock
(4*len)
154 let m = len / syms in
155 state.keymap
<- Array.make_matrix
156 (state.maxk
- state.mink
) syms 0xffffff;
157 let rec loop i = if i = m then () else
159 let rec loop2 k l = if l = syms then () else
160 let v = r32 data k in
161 state.keymap
.(i).(l) <- v;
170 let sendwithrep sock
s f
=
171 Queue.push f
state.fifo
;
172 sendstr1 s 0 (String.length
s) sock
;
176 let b = Buffer.create
16 in
177 List.iter
(Buffer.add_string
b) ss
;
178 let bl = Buffer.length
b in
179 let pl = bl land 3 in
183 Buffer.add_substring
b pad 0 (4 - pl);
188 let padcat s1 s2
= padcatl [s1
; s2
];;
190 let internreq name onlyifexists
=
191 let s = "\016\000\000\000\000\000\000\000" in
192 let s = padcat s name
in
193 w8 s 1 (if onlyifexists
then 1 else 0);
194 w16 s 2 (String.length
s / 4);
195 w16 s 4 (String.length name
);
199 let sendintern sock
s onlyifexists f
=
200 let s = internreq s onlyifexists
in
201 sendwithrep sock
s f
;
204 let createwindowreq wid parent x y w h bw mask
=
205 let s = "\001\000\009\000wwwwppppxxyywwhhbwccvvvvmmmmeeee" in
215 w32 s 28 0x800; (* eventmask *)
220 let getgeometryreq wid
=
221 let s = "\014u\002\000dddd" in
227 let s = "\008u\002\000wwww" in
232 let getkeymapreq first count
=
233 let s = "\101u\002\000fcuu" in
239 let changepropreq wid prop typ format props
=
240 let s = "\018\000llwwwwppppttttfuuuLLLL" in
241 let s = padcat s props
in
242 w16 s 2 (String.length
s / 4);
247 let ful = String.length props
/ (match format
with
251 | n -> error "no idea what %d means" n)
257 let openfontreq fid name
=
258 let s = "\045ullffffnnuu" in
259 let s = padcat s name
in
260 w16 s 2 (String.length
s / 4);
262 w16 s 8 (String.length name
);
266 let createglyphcursorreq fid cid cindex
=
267 let s = "\094u\008\000ccccffffffffssmmrrggbbRRGGBB" in
282 let mapwindowreq wid
=
283 let s = "\008u\002\000wwww" in
288 let changewindowattributesreq wid mask attrs
=
289 let s = "\002ullwwwwmmmm" in
290 let s = padcat s attrs
in
291 w16 s 2 (String.length
s / 4);
297 let configurewindowreq wid mask values
=
298 let s = "\012ullwwwwmmuu" in
299 let s = padcat s values
in
300 w16 s 2 (String.length
s / 4);
312 let clientmessage format seq wid typ
data =
313 let s = "\033fsswwwwtttt" in
314 let s = padcat s data in
322 let sendeventreq propagate destwid mask
data =
323 let s = "\025p\011\000wwwwmmmm" in
324 let s = padcat s data in
331 let getkeysym code mask
=
332 let index = (mask
land 1) lxor ((mask
land 2) lsr 1) in
333 let keysym = state.keymap
.(code
-state.mink
).(index) in
334 if index = 1 && keysym = 0
335 then state.keymap
.(code
-state.mink
).(0)
339 let rec readresp sock
=
340 let resp = readstr sock
32 in
341 let opcode = r8 resp 0 in
342 match opcode land lnot
0x80 with
347 and resid
= r32 resp 4
350 error "code=%d serial=%d resid=%#x min=%d maj=%d\n%S"
351 code serial resid min maj
resp;
353 | 1 -> (* response *)
354 let rep = Queue.pop
state.fifo
in
357 | 2 -> (* key press *)
358 if Array.length
state.keymap
> 0
360 let code = r8 resp 1 in
361 let mask = r16 resp 28 in
362 let keysym = getkeysym code mask in
363 vlog "keysym = %x %c" keysym (Char.unsafe_chr
keysym);
364 state.t#key
keysym mask;
366 | 3 -> (* key release *)
367 if Array.length
state.keymap
> 0
369 let code = r8 resp 1 in
370 let mask = r16 resp 28 in
371 let keysym = getkeysym code mask in
372 vlog "release keysym = %x %c mask %#x"
373 keysym (Char.unsafe_chr
keysym) mask
375 | 4 -> (* buttonpress *)
379 and m = r16 resp 28 in
380 state.t#mouse
n true x y
m;
383 | 5 -> (* buttonrelease *)
387 and m = r16 resp 28 in
388 state.t#mouse
n false x y
m;
389 vlog "release %d %d %d" n x y
392 let x = r16s resp 24 in
393 let y = r16s resp 26 in
394 let m = r16 resp 28 in
396 then state.t#pmotion
x y
397 else state.t#motion
x y;
398 vlog "move %dx%d => %d" x y m
402 and y = r16s resp 26 in
404 vlog "enter %d %d" x y
409 | 18 -> vlog "unmap";
414 | 12 -> (* exposure *)
418 | 15 -> (* visibility *)
419 let vis = r8 resp 8 in
420 if vis != 2 then state.t#display
;
421 vlog "visibility %d" vis;
423 | 34 -> (* mapping *)
424 state.keymap
<- [||];
425 let s = getkeymapreq state.mink
(state.maxk
-state.mink
-1) in
426 sendwithrep sock
s (updkmap sock
);
428 | 33 -> (* clientmessage *)
429 let atom = r32 resp 8 in
430 if atom = state.protoatom
432 let atom = r32 resp 12 in
433 if atom = state.deleatom
438 | 21 -> (* reparent *)
441 | 22 -> (* configure *)
445 and h
= r16 resp 22 in
446 vlog "configure cur [%d %d %d %d] conf [%d %d %d %d]"
447 state.x state.y state.w
state.h
450 if w
!= state.w
|| h
!= state.h
461 dolog "event %d %S" n resp
467 if hasdata sock
then loop ();
472 let sendstr s ?
(pos
=0) ?
(len=String.length
s) sock
=
473 sendstr1 s pos
len sock
;
474 if hasdata sock
then readresp sock
;
478 let b = Buffer.create
16 in
479 String.iter
(fun c
->
480 Buffer.add_string
b (Printf.sprintf
"%02x" (Char.code c
))) s;
487 let s = "wwuuhhuu" in
490 let s = configurewindowreq state.idbase
0x000c s in
491 sendstr s state.sock
;
492 else state.fullscreen
state.idbase
495 let setup sock screennum w h
=
496 let s = readstr sock
2 in
497 let n = String.length
s in
499 then error "failed to read X connection setup response n=%d" n;
502 let reasonlen = r8 s 1 in
503 let s = readstr sock
6 in
508 let data = readstr sock
len in
509 let reason = String.sub
data 0 reasonlen in
510 error "X connection failed maj=%d min=%d reason=%S"
513 | '
\002'
-> failwith
"X connection setup failed: authentication required";
516 let s = readstr sock
38 in
520 and idbase
= r32 s 10
521 and idmask
= r32 s 14
523 and screens
= r8 s 26
524 and formats
= r8 s 27
526 and maxkk
= r8 s 33 in
527 let data = readstr sock
(4*add
-32) in
528 let vendor = String.sub
data 0 vlen
in
529 let pos = ((vlen
+3) land lnot
3) + formats
*8 in
531 if screennum
>= screens
532 then error "invalid screen %d, max %d" screennum
(screens
-1);
536 let rec findscreen n pos = if n = screennum
then pos else
538 let ndepths = r8 s (pos+39) in
539 let rec skipdepths n pos = if n = ndepths then pos else
541 let nvisiuals = r16 s (pos+2) in
542 pos + nvisiuals*24 + 8
546 skipdepths n (pos+40)
552 let root = r32 data pos in
553 let rootw = r16 data (pos+20)
554 and rooth
= r16 data (pos+22) in
557 state.idbase
<- idbase
;
558 vlog "vendor = %S, maj=%d min=%d" vendor maj min
;
559 vlog "screens = %d formats = %d" screens formats
;
560 vlog "minkk = %d maxkk = %d" minkk maxkk
;
561 vlog "idbase = %#x idmask = %#x" idbase idmask
;
562 vlog "root=%#x %dx%d" root rootw rooth
;
565 + 0x00000001 (* KeyPress *)
566 (* + 0x00000002 *) (* KeyRelease *)
567 + 0x00000004 (* ButtonPress *)
568 + 0x00000008 (* ButtonRelease *)
569 + 0x00000010 (* EnterWindow *)
570 + 0x00000020 (* LeaveWindow *)
571 + 0x00000040 (* PointerMotion *)
572 (* + 0x00000080 *) (* PointerMotionHint *)
573 (* + 0x00000100 *) (* Button1Motion *)
574 (* + 0x00000200 *) (* Button2Motion *)
575 (* + 0x00000400 *) (* Button3Motion *)
576 (* + 0x00000800 *) (* Button4Motion *)
577 (* + 0x00001000 *) (* Button5Motion *)
578 + 0x00002000 (* ButtonMotion *)
579 (* + 0x00004000 *) (* KeymapState *)
580 + 0x00008000 (* Exposure *)
581 + 0x00010000 (* VisibilityChange *)
582 + 0x00020000 (* StructureNotify *)
583 (* + 0x00040000 *) (* ResizeRedirect *)
584 (* + 0x00080000 *) (* SubstructureNotify *)
585 (* + 0x00100000 *) (* SubstructureRedirect *)
586 (* + 0x00200000 *) (* FocusChange *)
587 (* + 0x00400000 *) (* PropertyChange *)
588 (* + 0x00800000 *) (* ColormapChange *)
589 (* + 0x01000000 *) (* OwnerGrabButton *)
591 let wid = state.idbase
in
592 let s = createwindowreq wid root 0 0 w h
0 mask in
595 let s = mapreq wid in
598 let s = getkeymapreq state.mink
(state.maxk
-state.mink
) in
599 sendwithrep sock
s (updkmap sock
);
601 sendintern sock
"WM_PROTOCOLS" false (fun resp ->
602 state.protoatom
<- r32 resp 8;
603 sendintern sock
"WM_DELETE_WINDOW" false (fun resp ->
604 state.deleatom
<- r32 resp 8;
605 let s = s32 state.deleatom
in
606 let s = changepropreq wid state.protoatom
4 32 s in
611 sendintern sock
"WM_CLASS" false (fun resp ->
612 let atom = r32 resp 8 in
613 let llpp = "llpp\000llpp\000" in
614 let s = changepropreq wid atom 31 8 llpp in
618 let s = openfontreq (wid+1) "cursor" in
621 Array.iteri
(fun i glyphindex
->
622 let s = createglyphcursorreq (wid+1) (wid+2+i) glyphindex
in
624 ) [|34;48;50;58;128;152|];
626 sendintern sock
"UTF8_STRING" true (fun resp ->
627 let atom = r32 resp 8 in
629 then state.stringatom
<- atom;
633 let s = changepropreq state.idbase
39 state.stringatom
8 s in
634 sendstr s state.sock
;
636 state.setwmname <- setwmname;
637 sendintern sock
"_NET_WM_NAME" true (fun resp ->
638 let atom = r32 resp 8 in
640 then state.setwmname <- (fun s ->
642 let s = changepropreq state.idbase
atom state.stringatom
8 s in
643 sendstr s state.sock
;
647 state.fullscreen
<- (fun wid ->
648 let s = "xxuuyyuuwwuuhhuu" in
655 let s = configurewindowreq wid 0x000f s in
656 sendstr s state.sock
;
657 state.fs
<- Fs
(state.x, state.y, state.w
, state.h
);
664 let s = configurewindowreq wid 0x000f s in
665 sendstr s state.sock
;
669 sendintern sock
"_NET_WM_STATE" true (fun resp ->
670 let nwmsatom = r32 resp 8 in
673 sendintern sock
"_NET_WM_STATE_FULLSCREEN" true (fun resp ->
674 let fsatom = r32 resp 8 in
679 let data = String.make
20 '
\000'
in
682 | NoFs
-> Fs
(-1, -1, -1, -1), 1
688 let cm = clientmessage 32 0 wid nwmsatom data in
689 let s = sendeventreq 0 root 0x180000 cm in
695 let s = getgeometryreq wid in
696 let completed = ref false in
697 sendwithrep sock
s (fun resp ->
700 and h
= r16 resp 18 in
705 let now = Unix.gettimeofday
in
706 let deadline = now () +. 2.0 in
707 let rec readtillcompletion () =
708 let r, _
, _
= Unix.select
[sock
] [] [] (deadline -. now ()) in
710 | [] -> readtillcompletion ()
714 then readtillcompletion ()
716 readtillcompletion ();
719 error "unknown conection setup response %d" (Char.code c
)
722 let getauth haddr dnum
=
724 if haddr = "localhost" || String.length
haddr = 0
726 try Unix.gethostname
()
728 dolog "failed to resolve `%S': %s" haddr (Printexc.to_string exn
);
733 let input_string ic
len =
734 let s = String.create
len in
735 really_input ic
s 0 len;
739 let rb pos = Char.code (String.get
s pos) in
740 (rb 1) lor ((rb 0) lsl 8)
744 let s = input_string ic
2 in
748 let family = input_string ic
2 in
751 let num = int_of_string
nums in
755 vlog "family %S addr %S(%S) num %d(%d) name %S data %S"
756 family addr haddr num dnum
name data;
757 if addr = haddr && num = dnum
762 try find () with _
-> "", ""
768 try Sys.getenv
"XAUTHORITY"
770 try Filename.concat
(Sys.getenv
"HOME") ".Xauthority"
775 if String.length
path = 0
777 else Some
(open_in_bin
path)
779 dolog "failed to open X authority file `%S' : %s"
781 (Printexc.to_string exn
);
786 | Some ic
-> readauth ic
791 try Sys.getenv
"DISPLAY"
793 error "Could not get DISPLAY evironment variable: %s"
794 (Printexc.to_string exn
)
796 let colonpos = String.index d '
:'
in
797 let host = String.sub
d 0 colonpos in
798 let dispnum, screennum
=
800 let dotpos = String.index_from
d (colonpos + 1) '
.'
in
801 let disp = String.sub
d (colonpos + 1) (dotpos - colonpos - 1) in
802 let screen = String.sub
d (dotpos + 1) (String.length
d - dotpos - 1) in
803 int_of_string
disp, int_of_string
screen
805 let disp = String.sub
d (colonpos + 1) (String.length
d - colonpos - 1) in
806 int_of_string
disp, 0
808 let aname, adata
= getauth host dispnum in
810 if osx
|| String.length
host = 0 || host = "unix"
814 then Unix.ADDR_UNIX
d
815 else Unix.ADDR_UNIX
("/tmp/.X11-unix/X" ^ string_of_int
dispnum)
817 let fd = Unix.socket
Unix.PF_UNIX
Unix.SOCK_STREAM
0 in
818 Unix.connect
fd addr;
821 let h = Unix.gethostbyname
host in
822 let addr = h.Unix.h_addr_list
.(0) in
823 let port = 6000 + dispnum in
824 let fd = Unix.socket
Unix.PF_INET
Unix.SOCK_STREAM
0 in
825 Unix.connect
fd (Unix.ADDR_INET
(addr, port));
829 let s = "luMMmmnndduu" in
830 let s = padcat s aname in
831 let s = padcat s adata
in
834 w16 s 6 (String.length
aname);
835 w16 s 8 (String.length adata
);
836 sendstr1 s 0 (String.length
s) fd;
838 setup fd screennum
w h;
840 fd, state.w, state.h;
847 let setcursor cursor
=
850 | CURSOR_INHERIT
-> -1
853 | CURSOR_CROSSHAIR
-> 0
856 let s = s32 (if n = -1 then 0 else state.idbase
+2+n) in
857 let s = changewindowattributesreq state.idbase
(*cursor*)0x4000 s in
858 sendstr s state.sock
;
862 state.fullscreen state.idbase
;
865 let metamask = 0x40;;
870 let withalt mask = mask land altmask != 0;;
871 let withctrl mask = mask land ctrlmask != 0;;
872 let withshift mask = mask land shiftmask != 0;;
873 let withmeta mask = mask land metamask != 0;;
874 let withnone mask = mask land (altmask + ctrlmask + shiftmask + metamask) = 0;;
877 let t = Hashtbl.create
20
878 and f
= Hashtbl.create
20 in
880 List.iter
(fun s -> Hashtbl.add t s k) (n::nl
);
884 let s = String.create
1 in
886 add s [] (Char.code c
)
889 let an = Char.code a
and bn
= Char.code b in
890 for i = an to bn
do addc (Char.chr
i) done;
895 String.iter
addc "`~!@#$%^&*()-_=+\\|[{]};:,./<>?";
896 for i = 0 to 29 do add ("f" ^ string_of_int
(i+1)) [] (0xffbe + i) done;
898 add "ret" ["return"; "enter"] 0xff0d;
900 add "left" [] 0xff51;
901 add "right" [] 0xff53;
902 add "home" [] 0xff50;
904 add "ins" ["insert"] 0xff63;
905 add "del" ["delete"] 0xffff;
906 add "esc" ["escape"] 0xff1b;
907 add "pgup" ["pageup"] 0xff55;
908 add "pgdown" ["pagedown"] 0xff56;
909 add "backspace" [] 0xff08;
911 add "down" [] 0xff54;
916 try Hashtbl.find xlatf
k
917 with Not_found
-> Printf.sprintf
"%#x" k;
921 try Hashtbl.find xlatt name
923 if String.length
name = 1
924 then Char.code name.[0]
925 else int_of_string
name;