3 let (~
>) = Bytes.unsafe_of_string
;;
25 type wid
= int and screenno
= int and vid
= int and atom
= int;;
27 external glxinit
: string -> wid
-> screenno
-> vid
= "ml_glxinit";;
28 external glxcompleteinit
: unit -> unit = "ml_glxcompleteinit";;
29 external swapb
: unit -> unit = "ml_swapb";;
30 external setcursor
: cursor
-> unit = "ml_setcursor";;
31 external setwinbgcol
: int -> unit = "ml_setbgcol";;
33 let vlog fmt
= Format.ksprintf ignore fmt
;;
40 method reshape _ _
= ()
41 method mouse _ _ _ _ _
= ()
42 method motion _ _
= ()
43 method pmotion _ _
= ()
47 method winstate _
= ()
53 method map
: bool -> unit
55 method visible
: visiblestate
-> unit
56 method reshape
: int -> int -> unit
57 method mouse
: int -> bool -> int -> int -> int -> unit
58 method motion
: int -> int -> unit
59 method pmotion
: int -> int -> unit
60 method key
: int -> int -> unit
61 method enter
: int -> int -> unit
63 method winstate
: winstate list
-> unit
70 ; mutable keymap
: int array array
71 ; fifo
: (bytes
-> unit) Queue.t
73 ; mutable protoatom
: atom
74 ; mutable deleatom
: atom
75 ; mutable nwmsatom
: atom
76 ; mutable maxvatom
: atom
77 ; mutable maxhatom
: atom
78 ; mutable fulsatom
: atom
79 ; mutable idbase
: int
82 ; mutable fullscreen
: (int -> unit)
83 ; mutable setwmname
: (bytes
-> unit)
84 ; mutable actwin
: (unit -> unit)
85 ; mutable stringatom
: int
87 ; mutable sock
: Unix.file_descr
93 ; mutable curcurs
: cursor
94 ; mutable capslmask
: int
95 ; mutable numlmask
: int
96 ; mutable levl3mask
: int
97 ; mutable levl5mask
: int
102 | Fs
of (int * int * int * int)
109 ; fifo
= Queue.create
()
120 ; fullscreen
= (fun _
-> ())
121 ; setwmname
= (fun _
-> ())
122 ; actwin
= (fun _
-> ())
131 ; curcurs
= CURSOR_TEXT
140 let w8 s pos i
= Bytes.set s pos
(Char.chr
(i
land 0xff));;
141 let r8 s pos
= Char.code
(Bytes.get s pos
);;
143 let ordermagic = 'l'
;;
147 w8 s
(pos
+1) (i
lsr 8)
152 w16 s
(pos
+2) (i
lsr 16)
156 let rb pos1
= Char.code
(Bytes.get s
(pos
+ pos1
)) in
157 (rb 0) lor ((rb 1) lsl 8)
162 i - ((i land 0x8000) lsl 1)
166 let rb pos1
= Char.code
(Bytes.get s
(pos
+ pos1
)) in
167 let l = (rb 0) lor ((rb 1) lsl 8)
168 and u
= (rb 2) lor ((rb 3) lsl 8) in
172 let makereq opcode len reqlen
=
173 let s = Bytes.create len
in
180 let s = Bytes.create n
in
182 let m = tempfailureretry
(Unix.read sock
s pos
) n
in
187 ignore
(tempfailureretry
(Unix.select
[sock
] [] []) 0.01);
188 loop (pos
+ m) (n
- m)
195 let sendstr1 s pos len sock
=
196 let s = Bytes.unsafe_to_string
s in
197 vlog "%d <= %S" state.seq
s;
198 state.seq
<- state.seq
+ 1;
199 let n = tempfailureretry
(Unix.write_substring sock
s pos
) len
in
201 then error
"send %d returned %d" len
n;
204 let updkmap sock resp
=
205 let syms = r8 resp
1 in
206 let len = r32 resp
4 in
209 then readstr sock
(4*len)
212 let m = len / syms in
213 state.keymap
<- Array.make_matrix
214 (state.maxk
- state.mink
) syms 0xffffff;
215 let rec loop i = if i = m then () else
217 let rec loop2 k l = if l = syms then () else
218 let v = r32 data k in
219 state.keymap
.(i).(l) <- v;
228 let updmodmap sock resp
=
230 let len = r16 resp
4 in
233 then readstr sock
(len*4)
236 if len > 0 then (*???*)
237 let modmap = Array.make_matrix
8 n 0xffffff in
238 let rec loop l = if l = 8 then () else
240 let rec loop1 m = if m = n then () else
242 let code = r8 data p in
243 modmap.(l).(m) <- code;
246 let ki = code - state.mink
in
249 let a = state.keymap
.(ki) in
250 let rec capsloop i = if i = Array.length
a || i > 3 then () else
253 then state.capslmask
<- 2
261 let ki = code - state.mink
in
264 let a = state.keymap
.(ki) in
265 let rec lloop i = if i = Array.length
a || i > 3 then () else
268 | 0xfe03 -> state.levl3mask
<- 1 lsl l
269 | 0xfe11 -> state.levl5mask
<- 1 lsl l
270 | 0xff7f -> state.numlmask
<- 1 lsl l
284 let sendwithrep sock
s f
=
285 Queue.push f
state.fifo
;
286 sendstr1 s 0 (Bytes.length
s) sock
;
292 let b = Buffer.create
16 in
293 List.iter
(Buffer.add_bytes
b) ss
;
294 let bl = Buffer.length
b in
295 let pl = bl land 3 in
298 Buffer.add_substring
b pad 0 (4 - pl);
303 let padcat s1 s2
= padcatl [s1
; s2
];;
305 let internreq name onlyifexists
=
306 let s = makereq 16 8 8 in
307 let s = padcat s name
in
308 w8 s 1 (if onlyifexists
then 1 else 0);
309 w16 s 2 (Bytes.length
s / 4);
310 w16 s 4 (Bytes.length name
);
314 let sendintern sock
s onlyifexists f
=
315 let s = internreq s onlyifexists
in
316 sendwithrep sock
s f
;
319 let createwindowreq wid parent x y w h bw eventmask vid depth mid
=
320 let s = makereq 1 48 12 in
329 w16 s 22 0; (* copyfromparent *)
330 w32 s 24 vid
; (* visual *)
331 w32 s 28 0x280a; (* valuemask =
336 w32 s 32 0; (* background pixel *)
337 w32 s 36 0; (* border pixel*)
343 let createcolormapreq mid wid vid
=
344 let s = makereq 78 16 4 in
352 let getgeometryreq wid
=
353 let s = makereq 14 8 2 in
359 let s = makereq 8 8 2 in
364 let getkeymapreq first count
=
365 let s = makereq 101 8 2 in
371 let changepropreq wid prop typ format props
=
372 let s = makereq 18 24 0 in
373 let s = padcat s props
in
375 w16 s 2 (Bytes.length
s / 4);
380 let ful = Bytes.length props
/ (match format
with
384 | n -> error
"no idea what %d means" n)
390 let getpropreq delete wid prop typ
=
391 let s = makereq 20 24 6 in
392 w8 s 1 (if delete
then 1 else 0);
401 let configurewindowreq wid mask values
=
402 let s = makereq 12 12 0 in
403 let s = padcat s values
in
404 w16 s 2 (Bytes.length
s / 4);
411 let s = Bytes.create
4 in
416 let clientmessage format seq wid typ
data =
417 let s = makereq 33 12 0 in
418 let s = padcat s data in
426 let sendeventreq propagate destwid mask
data =
427 let s = makereq 25 12 11 in
428 let s = padcat s data in
436 let getmodifiermappingreq () =
440 let queryextensionreq name
=
441 let s = makereq 98 8 0 in
442 let s = padcat s name
in
443 w16 s 2 (Bytes.length
s / 4);
444 w16 s 4 (Bytes.length name
);
448 let getkeysym pkpk
code mask
=
449 if (pkpk
>= 0xff80 && pkpk
<= 0xffbd)
450 || (pkpk
>= 0x11000000 && pkpk
<= 0x1100ffff)
452 if mask
land state.numlmask
!= 0
454 let keysym = state.keymap
.(code-state.mink
).(1) in
455 if keysym = 0 then pkpk
else keysym
460 if pkpk
land 0xf000 = 0xf000
462 else (mask
land 1) lxor ((mask
land state.capslmask
) lsr 1)
465 if state.xkb
&& mask
land 0x2000 != 0
469 let l3 = (mask
land state.levl3mask
) != 0 in
470 let l4 = (mask
land state.levl5mask
) != 0 in
472 if l3 then (if l4 then 8 else 4) else (if l4 then 6 else 0)
474 let keysym = state.keymap
.(code-state.mink
).(index) in
475 if index land 1 = 1 && keysym = 0
476 then state.keymap
.(code-state.mink
).(index - 1)
481 let getkeysym code mask
=
482 let pkpk = state.keymap
.(code-state.mink
).(0) in
483 if state.xkb
&& pkpk lsr 8 = 0xfe (* XKB *)
485 else getkeysym pkpk code mask
489 let resp = readstr sock
32 in
490 let opcode = r8 resp 0 in
491 match opcode land lnot
0x80 with
496 and resid
= r32 resp 4
499 error
"code=%d serial=%d resid=%#x min=%d maj=%d\n%S"
500 code serial resid min maj
(Bytes.unsafe_to_string
resp);
502 | 1 -> (* response *)
503 let rep = Queue.pop
state.fifo
in
506 | 2 -> (* key press *)
507 if Array.length
state.keymap
> 0
509 let code = r8 resp 1 in
510 let mask = r16 resp 28 in
511 let keysym = getkeysym code mask in
512 vlog "keysym = %x %c mask %#x code %d"
513 keysym (Char.unsafe_chr
keysym) mask code;
514 state.t#key
keysym mask;
516 | 3 -> (* key release *)
517 if Array.length
state.keymap
> 0
519 let code = r8 resp 1 in
520 let mask = r16 resp 28 in
521 let keysym = getkeysym code mask in
522 vlog "release keysym = %x %c mask %#x code %#d"
523 keysym (Char.unsafe_chr
keysym) mask code
525 | 4 -> (* buttonpress *)
529 and m = r16 resp 28 in
530 state.t#mouse
n true x y
m;
533 | 5 -> (* buttonrelease *)
537 and m = r16 resp 28 in
538 state.t#mouse
n false x y
m;
539 vlog "release %d %d %d" n x y
542 let x = r16s resp 24 in
543 let y = r16s resp 26 in
544 let m = r16 resp 28 in
546 then state.t#pmotion
x y
547 else state.t#motion
x y;
548 vlog "move %dx%d => %d" x y m
552 and y = r16s resp 26 in
554 vlog "enter %d %d" x y
567 | 12 -> (* exposure *)
571 | 15 -> (* visibility *)
576 | 1 -> PartiallyObscured
579 dolog
"unknown visibility %d" v;
583 vlog "visibility %d" v;
585 | 34 -> (* mapping *)
587 let s = getkeymapreq state.mink
(state.maxk
-state.mink
-1) in
588 sendwithrep sock
s (updkmap sock
);
589 state.capslmask
<- 0;
590 state.levl3mask
<- 0;
591 state.levl5mask
<- 0;
593 let s = getmodifiermappingreq () in
594 sendwithrep sock
s (updmodmap sock
);
596 | 33 -> (* clientmessage *)
597 let atom = r32 resp 8 in
598 if atom = state.protoatom
600 let atom = r32 resp 12 in
601 if atom = state.deleatom
606 | 21 -> (* reparent *)
609 | 22 -> (* configure *)
613 and h
= r16 resp 22 in
614 vlog "configure cur [%d %d %d %d] conf [%d %d %d %d]"
615 state.x state.y state.w
state.h
618 if w
!= state.w
|| h
!= state.h
629 let atom = r32 resp 8 in
630 if atom = state.nwmsatom
632 let s = getpropreq false state.wid
atom 4 in
633 sendwithrep sock
s (fun resp ->
634 let len = r32 resp 4 in
635 let nitems = r32 resp 16 in
640 let s = readstr sock
(len*4) in
641 let rec loop wsl i = if i = nitems then wsl else
642 let atom = r32 s (i*4) in
644 if atom = state.maxhatom
647 if atom = state.maxvatom
650 if atom = state.fulsatom
652 state.fs
<- Fs
(state.x, state.y, state.w
, state.h
);
662 state.t#winstate
(List.sort compare
wsl)
666 dolog
"event %d %S" n (Bytes.unsafe_to_string
resp)
672 if hasdata sock
then loop ();
677 let sendstr s ?
(pos
=0) ?
(len=Bytes.length
s) sock
=
678 sendstr1 s pos
len sock
;
679 if hasdata sock
then readresp sock
;
685 let s = Bytes.create
8 in
688 let s = configurewindowreq state.wid
0x000c s in
689 sendstr s state.sock
;
690 else state.fullscreen
state.wid
697 let syncsendwithrep sock secstowait
s f
=
698 let completed = ref false in
699 sendwithrep sock
s (fun resp -> f
resp; completed := true);
700 let now = Unix.gettimeofday
in
701 let deadline = now () +. secstowait
in
702 let rec readtillcompletion () =
704 let timeout = deadline -. now () in
707 else Unix.select
[sock
] [] [] timeout
709 let r, _
, _
= tempfailureretry
sf deadline in
711 | [] -> error
"didn't get X response in %f seconds, aborting" secstowait
715 then readtillcompletion ()
717 readtillcompletion ();
721 let s = mapreq state.wid
in
722 sendstr s state.sock
;
725 let syncsendintern sock secstowait
s onlyifexists f
=
726 let s = internreq s onlyifexists
in
727 syncsendwithrep sock secstowait
s f
;
730 let setup disp sock rootwid screennum w h
=
731 let s = readstr sock
2 in
732 let n = Bytes.length
s in
734 then error
"failed to read X connection setup response n=%d" n;
735 match Bytes.get
s 0 with
737 let reasonlen = r8 s 1 in
738 let s = readstr sock
6 in
743 let data = readstr sock
len in
744 let reason = Bytes.sub
data 0 reasonlen in
745 error
"X connection failed maj=%d min=%d reason=%S"
746 maj min
(Bytes.unsafe_to_string
reason)
748 | '
\002'
-> failwith
"X connection setup failed: authentication required";
751 let s = readstr sock
38 in
755 and idbase
= r32 s 10
756 and idmask
= r32 s 14
758 and screens
= r8 s 26
759 and formats
= r8 s 27
761 and maxkk
= r8 s 33 in
762 let data = readstr sock
(4*add
-32) in
763 let vendor = Bytes.sub
data 0 vlen
in
764 let pos = ((vlen
+3) land lnot
3) + formats
*8 in
766 if screennum
>= screens
767 then error
"invalid screen %d, max %d" screennum
(screens
-1);
770 let rec findscreen n pos = if n = screennum
then pos else
772 let ndepths = r8 data (pos+39) in
773 let rec skipdepths n pos = if n = ndepths then pos else
775 let nvisiuals = r16 data (pos+2) in
776 pos + nvisiuals*24 + 8
780 skipdepths n (pos+40)
786 let root = if rootwid
= 0 then r32 data pos else rootwid
in
787 let rootw = r16 data (pos+20)
788 and rooth
= r16 data (pos+22)
789 and rootdepth
= r8 data (pos+38)in
793 state.idbase
<- idbase
;
794 vlog "vendor = %S, maj=%d min=%d" (Bytes.unsafe_to_string
vendor) maj min
;
795 vlog "screens = %d formats = %d" screens formats
;
796 vlog "minkk = %d maxkk = %d" minkk maxkk
;
797 vlog "idbase = %#x idmask = %#x" idbase idmask
;
798 vlog "root=%#x %dx%d" root rootw rooth
;
799 vlog "wmm = %d, hmm = %d" (r16 data (pos+24)) (r16 data (pos+26));
800 vlog "visualid = %#x" (r32 data (pos+32));
801 vlog "root depth = %d" rootdepth
;
803 let wid = state.idbase
in
810 let vid = glxinit disp
wid screennum
in
811 let ndepths = r8 data (pos+39) in
812 let rec finddepth n'
pos =
814 then error
"cannot find depth for visual %#x" vid;
815 let depth = r8 data pos in
816 let nvisuals = r16 data (pos+2) in
817 let rec findvisual n pos =
819 then finddepth (n'
+1) pos
821 let id = r32 data pos in
824 else findvisual (n+1) (pos+24)
828 let depth = finddepth 0 (pos+40) in
830 let s = createcolormapreq mid root vid in
834 + 0x00000001 (* KeyPress *)
835 (* + 0x00000002 *) (* KeyRelease *)
836 + 0x00000004 (* ButtonPress *)
837 + 0x00000008 (* ButtonRelease *)
838 + 0x00000010 (* EnterWindow *)
839 + 0x00000020 (* LeaveWindow *)
840 + 0x00000040 (* PointerMotion *)
841 (* + 0x00000080 *) (* PointerMotionHint *)
842 (* + 0x00000100 *) (* Button1Motion *)
843 (* + 0x00000200 *) (* Button2Motion *)
844 (* + 0x00000400 *) (* Button3Motion *)
845 (* + 0x00000800 *) (* Button4Motion *)
846 (* + 0x00001000 *) (* Button5Motion *)
847 + 0x00002000 (* ButtonMotion *)
848 (* + 0x00004000 *) (* KeymapState *)
849 + 0x00008000 (* Exposure *)
850 + 0x00010000 (* VisibilityChange *)
851 + 0x00020000 (* StructureNotify *)
852 (* + 0x00040000 *) (* ResizeRedirect *)
853 (* + 0x00080000 *) (* SubstructureNotify *)
854 (* + 0x00100000 *) (* SubstructureRedirect *)
855 (* + 0x00200000 *) (* FocusChange *)
856 + 0x00400000 (* PropertyChange *)
857 (* + 0x00800000 *) (* ColormapChange *)
858 (* + 0x01000000 *) (* OwnerGrabButton *)
861 let s = createwindowreq wid root 0 0 w h
0 mask vid depth mid in
864 sendintern sock
(~
> "WM_PROTOCOLS") false (fun resp ->
865 state.protoatom
<- r32 resp 8;
867 sock
(~
> "WM_DELETE_WINDOW") false (fun resp ->
868 state.deleatom
<- r32 resp 8;
869 let s = s32 state.deleatom
in
870 let s = changepropreq wid state.protoatom
4 32 s in
875 sendintern sock
(~
> "WM_CLIENT_MACHINE") false (fun resp ->
876 let atom = r32 resp 8 in
879 try Unix.gethostname
()
881 dolog
"error getting host name: %s" @@ exntos exn
;
886 let s = changepropreq wid atom state.stringatom
8
889 sendintern sock
(~
> "_NET_WM_PID") false (fun resp ->
890 let atom = r32 resp 8 in
891 let pid = Unix.getpid
() in
893 let s = changepropreq wid atom 6(*cardinal*) 32 s in
898 state.actwin
<- (fun () ->
899 let s = Bytes.create
4 in
900 let s = configurewindowreq wid 0x40 s in
901 sendstr s state.sock
;
902 let s = mapreq wid in
903 sendstr s state.sock
;
906 sendintern sock
(~
> "_NET_ACTIVE_WINDOW") true (fun resp ->
907 let atom = r32 resp 8 in
908 state.actwin
<- (fun () ->
909 let data = Bytes.make
20 '
\000'
in
910 let cm = clientmessage 32 0 wid atom data in
911 let s = sendeventreq 0 root 0x180000 cm in
912 sendstr s state.sock
;
916 syncsendintern sock
2.0 (~
> "WM_CLASS") false (fun resp ->
917 let atom = r32 resp 8 in
918 let llpp = ~
> "llpp\000llpp\000" in
919 let s = changepropreq wid atom 31 8 llpp in
923 let s = getkeymapreq state.mink
(state.maxk
-state.mink
) in
924 sendwithrep sock
s (updkmap sock
);
926 let s = getmodifiermappingreq () in
927 sendwithrep sock
s (updmodmap sock
);
929 sendintern sock
(~
> "UTF8_STRING") true (fun resp ->
930 let atom = r32 resp 8 in
932 then state.stringatom
<- atom;
936 let s = changepropreq wid 39 state.stringatom
8 s in
937 sendstr s state.sock
;
939 state.setwmname <- setwmname;
940 sendintern sock
(~
> "_NET_WM_NAME") true (fun resp ->
941 let atom = r32 resp 8 in
943 then state.setwmname <- (fun s ->
945 let s = changepropreq wid atom state.stringatom
8 s in
946 sendstr s state.sock
;
950 state.fullscreen
<- (fun wid ->
951 let s = Bytes.create
16 in
958 let s = configurewindowreq wid 0x000f s in
959 sendstr s state.sock
;
960 state.fs
<- Fs
(state.x, state.y, state.w
, state.h
);
967 let s = configurewindowreq wid 0x000f s in
968 sendstr s state.sock
;
972 sendintern sock
(~
> "_NET_WM_STATE") true (fun resp ->
973 state.nwmsatom
<- r32 resp 8;
974 if state.nwmsatom
!= 0
976 sendintern sock
(~
> "_NET_WM_STATE_MAXIMIZED_VERT") true (fun resp ->
977 state.maxvatom
<- r32 resp 8;
979 sendintern sock
(~
> "_NET_WM_STATE_MAXIMIZED_HORZ") true (fun resp ->
980 state.maxhatom
<- r32 resp 8;
982 sendintern sock
(~
> "_NET_WM_STATE_FULLSCREEN") true (fun resp ->
983 state.fulsatom
<- r32 resp 8;
984 if state.fulsatom
!= 0
988 let data = Bytes.make
20 '
\000'
in
991 | NoFs
-> Fs
(-1, -1, -1, -1), 1
995 w32 data 4 state.fulsatom
;
997 let cm = clientmessage 32 0 wid state.nwmsatom
data in
998 let s = sendeventreq 0 root 0x180000 cm in
1005 let s = queryextensionreq (~
> "XKEYBOARD") in
1008 let present = r8 resp 8 in
1011 let maj = r8 resp 9 in
1012 let s = Bytes.create
8 in
1013 w8 s 0 maj; (* XKB *)
1014 w8 s 1 0; (* XKBUseExtension *)
1015 w16 s 2 2; (* request-length *)
1016 w16 s 4 1; (* wantedMajor *)
1017 w16 s 6 0; (* watnedMinor *)
1021 let supported = r8 resp 1 in
1022 state.xkb
<- supported != 0
1026 let s = getgeometryreq wid in
1027 syncsendwithrep sock
2.0 s (fun resp ->
1030 and h
= r16 resp 18 in
1036 error
"unknown conection setup response %d" (Char.code c
)
1039 let getauth haddr dnum
=
1041 if emptystr
haddr || haddr = "localhost"
1043 try Unix.gethostname
()
1045 dolog
"failed to resolve `%S': %s" haddr @@ exntos exn
;
1050 try Sys.getenv
"XAUTHORITY"
1052 try Filename.concat
(Sys.getenv
"HOME") ".Xauthority"
1053 with Not_found
-> E.s
1057 let rb pos = Char.code (Bytes.get
s pos) in
1058 (rb 1) lor ((rb 0) lsl 8)
1062 let s = really_input_string ic
2 in
1063 let n = r16be (~
> s) in
1064 really_input_string ic
n
1066 let family = really_input_string ic
2 in
1070 try Some
(int_of_string
nums)
1073 "display number(%S) is not an integer (corrupt %S?): %s"
1074 nums path @@ exntos exn
;
1080 vlog "family %S addr %S(%S) num %S(%d) name %S data %S"
1081 family addr haddr nums dnum
name data;
1083 | Some num
when addr = haddr && num
= dnum
->
1090 | End_of_file
-> E.s, E.s
1092 dolog
"exception while reading X authority data (%S): %s"
1102 match open_in_bin
path with
1104 | (exception exn
) ->
1105 dolog
"failed to open X authority file `%S' : %s" path @@ exntos exn
;
1109 let init t rootwid
w h platform
=
1111 try Sys.getenv
"DISPLAY"
1113 error
"cannot get DISPLAY evironment variable: %s" @@ exntos exn
1117 then error
"invalid DISPLAY(%s) %S" w d
1119 let s = String.sub
d b (e
- b) in
1122 error
"invalid DISPLAY %S can not parse %s(%S): %s"
1126 if pos = String.length
d
1127 then error
"invalid DISPLAY %S no display number specified" d
1131 let rec pdispnum pos1
=
1132 if pos1
= String.length
d
1133 then getnum "display number" (pos+1) pos1
, 0
1137 let dispnum = getnum "display number" (pos+1) pos1
in
1138 let rec pscreennum pos2
=
1139 if pos2
= String.length
d
1140 then getnum "screen number" (pos1
+1) pos2
1143 | '
0'
.. '
9'
-> pscreennum (pos2
+1)
1145 error
"invalid DISPLAY %S, cannot parse screen number" d
1147 dispnum, pscreennum (pos1
+1)
1148 | '
0'
.. '
9'
-> pdispnum (pos1
+1)
1150 error
"invalid DISPLAY %S, cannot parse display number" d
1152 String.sub
d 0 pos, pdispnum (pos+1)
1156 let host, (dispnum, screennum
) = phost 0 in
1157 let aname, adata
= getauth host dispnum in
1160 if emptystr
host || host = "unix"
1164 | Utils.Posx
-> Unix.ADDR_UNIX
d
1166 Unix.ADDR_UNIX
("\000/tmp/.X11-unix/X" ^ string_of_int
dispnum)
1167 | Utils.Punknown
| Utils.Psun
| Utils.Pbsd
| Utils.Pcygwin
->
1168 Unix.ADDR_UNIX
("/tmp/.X11-unix/X" ^ string_of_int
dispnum)
1170 Unix.socket
Unix.PF_UNIX
Unix.SOCK_STREAM
0, addr
1173 try Unix.gethostbyname
host
1174 with exn
-> error
"cannot resolve %S: %s" host @@ exntos exn
1176 let addr = h.Unix.h_addr_list
.(0) in
1177 let port = 6000 + dispnum in
1178 let fd = Unix.socket
Unix.PF_INET
Unix.SOCK_STREAM
0 in
1179 fd, (Unix.ADDR_INET
(addr, port))
1181 try Unix.connect
fd addr; fd
1182 with exn
-> error
"failed to connect to X: %s" @@ exntos exn
1185 let s = Bytes.create
12 in
1186 let s = padcat s (~
> aname) in
1187 let s = padcat s (~
> adata
) in
1188 Bytes.set
s 0 ordermagic;
1191 w16 s 6 (String.length
aname);
1192 w16 s 8 (String.length adata
);
1193 sendstr1 s 0 (Bytes.length
s) fd;
1195 setup d fd rootwid screennum
w h;
1197 fd, state.w, state.h;
1201 state.setwmname (~
> s);
1204 let setcursor cursor
=
1205 if cursor
!= state.curcurs
1208 state.curcurs
<- cursor
;
1213 state.fullscreen state.wid;
1216 let metamask = 0x40;;
1221 let withalt mask = mask land altmask != 0;;
1222 let withctrl mask = mask land ctrlmask != 0;;
1223 let withshift mask = mask land shiftmask != 0;;
1224 let withmeta mask = mask land metamask != 0;;
1225 let withnone mask = mask land (altmask + ctrlmask + shiftmask + metamask) = 0;;
1228 let t = Hashtbl.create
20
1229 and f
= Hashtbl.create
20 in
1231 List.iter
(fun s -> Hashtbl.add t s k) (n::nl
);
1235 let s = String.make
1 c
in
1236 add s [] (Char.code c
)
1239 let an = Char.code a and bn
= Char.code b in
1240 for i = an to bn
do addc (Char.chr
i) done;
1245 String.iter
addc "`~!@#$%^&*()-_=+\\|[{]};:,./<>?";
1246 for i = 0 to 29 do add ("f" ^ string_of_int
(i+1)) [] (0xffbe + i) done;
1247 add "space" [] 0x20;
1248 add "ret" ["return"; "enter"] 0xff0d;
1249 add "tab" [] 0xff09;
1250 add "left" [] 0xff51;
1251 add "right" [] 0xff53;
1252 add "home" [] 0xff50;
1253 add "end" [] 0xff57;
1254 add "ins" ["insert"] 0xff63;
1255 add "del" ["delete"] 0xffff;
1256 add "esc" ["escape"] 0xff1b;
1257 add "pgup" ["pageup"] 0xff55;
1258 add "pgdown" ["pagedown"] 0xff56;
1259 add "backspace" [] 0xff08;
1261 add "down" [] 0xff54;
1262 add "menu" [] 0xff67;
1267 try Hashtbl.find xlatf
k
1268 with Not_found
-> Printf.sprintf
"%#x" k;
1272 try Hashtbl.find xlatt name
1274 if String.length
name = 1
1275 then Char.code name.[0]
1276 else int_of_string
name;