Working but non-portable
[llpp.git] / wsi.ml
blob97e71034747fe15a881025e3f906161f9922bccc
1 open Utils;;
3 let (~>) = Bytes.unsafe_of_string;;
5 type cursor =
6 | CURSOR_INHERIT
7 | CURSOR_INFO
8 | CURSOR_CYCLE
9 | CURSOR_FLEUR
10 | CURSOR_TEXT
13 type winstate =
14 | MaxVert
15 | MaxHorz
16 | Fullscreen
19 type visiblestate =
20 | Unobscured
21 | PartiallyObscured
22 | FullyObscured
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;;
35 let onot = object
36 method display = ()
37 method map _ = ()
38 method expose = ()
39 method visible _ = ()
40 method reshape _ _ = ()
41 method mouse _ _ _ _ _ = ()
42 method motion _ _ = ()
43 method pmotion _ _ = ()
44 method key _ _ = ()
45 method enter _ _ = ()
46 method leave = ()
47 method winstate _ = ()
48 method quit = exit 0
49 end;;
51 class type t = object
52 method display : unit
53 method map : bool -> unit
54 method expose : 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
62 method leave : unit
63 method winstate : winstate list -> unit
64 method quit : unit
65 end;;
67 type state =
68 { mutable mink : int
69 ; mutable maxk : int
70 ; mutable keymap : int array array
71 ; fifo : (bytes -> unit) Queue.t
72 ; mutable seq : int
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
80 ; mutable wid : int
81 ; mutable fid : int
82 ; mutable fullscreen : (int -> unit)
83 ; mutable setwmname : (bytes -> unit)
84 ; mutable actwin : (unit -> unit)
85 ; mutable stringatom : int
86 ; mutable t : t
87 ; mutable sock : Unix.file_descr
88 ; mutable x : int
89 ; mutable y : int
90 ; mutable w : int
91 ; mutable h : int
92 ; mutable fs : fs
93 ; mutable curcurs : cursor
94 ; mutable capslmask : int
95 ; mutable numlmask : int
96 ; mutable levl3mask : int
97 ; mutable levl5mask : int
98 ; mutable xkb : bool
100 and fs =
101 | NoFs
102 | Fs of (int * int * int * int)
105 let state =
106 { mink = max_int
107 ; maxk = min_int
108 ; keymap = E.a
109 ; fifo = Queue.create ()
110 ; seq = 0
111 ; protoatom = -1
112 ; deleatom = -1
113 ; nwmsatom = -1
114 ; maxvatom = -1
115 ; maxhatom = -1
116 ; fulsatom = -1
117 ; idbase = -1
118 ; wid = -1
119 ; fid = -1
120 ; fullscreen = (fun _ -> ())
121 ; setwmname = (fun _ -> ())
122 ; actwin = (fun _ -> ())
123 ; sock = Unix.stdin
124 ; t = onot
125 ; x = -1
126 ; y = -1
127 ; w = -1
128 ; h = -1
129 ; fs = NoFs
130 ; stringatom = 31
131 ; curcurs = CURSOR_TEXT
132 ; capslmask = 0
133 ; numlmask = 0
134 ; levl3mask = 0
135 ; levl5mask = 0
136 ; xkb = false
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';;
145 let w16 s pos i =
146 w8 s pos i;
147 w8 s (pos+1) (i lsr 8)
150 let w32 s pos i =
151 w16 s pos i;
152 w16 s (pos+2) (i lsr 16)
155 let r16 s pos =
156 let rb pos1 = Char.code (Bytes.get s (pos + pos1)) in
157 (rb 0) lor ((rb 1) lsl 8)
160 let r16s s pos =
161 let i = r16 s pos in
162 i - ((i land 0x8000) lsl 1)
165 let r32 s pos =
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
169 (u lsl 16) lor l
172 let makereq opcode len reqlen =
173 let s = Bytes.create len in
174 w8 s 0 opcode;
175 w16 s 2 reqlen;
179 let readstr sock n =
180 let s = Bytes.create n in
181 let rec loop pos n =
182 let m = tempfailureretry (Unix.read sock s pos) n in
183 if m = 0
184 then state.t#quit;
185 if n != m
186 then (
187 ignore (tempfailureretry (Unix.select [sock] [] []) 0.01);
188 loop (pos + m) (n - m)
191 loop 0 n;
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
200 if n != len
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
207 let data =
208 if len > 0
209 then readstr sock (4*len)
210 else E.b
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
216 let k = i*4*syms in
217 let rec loop2 k l = if l = syms then () else
218 let v = r32 data k in
219 state.keymap.(i).(l) <- v;
220 loop2 (k+4) (l+1)
222 loop2 k 0;
223 loop (i+1);
225 loop 0;
228 let updmodmap sock resp =
229 let n = r8 resp 1 in
230 let len = r16 resp 4 in
231 let data =
232 if len > 0
233 then readstr sock (len*4)
234 else E.b
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
239 let p = l*n in
240 let rec loop1 m = if m = n then () else
241 let p = p+m in
242 let code = r8 data p in
243 modmap.(l).(m) <- code;
244 if l = 1
245 then (
246 let ki = code - state.mink in
247 if ki >= 0
248 then
249 let a = state.keymap.(ki) in
250 let rec capsloop i = if i = Array.length a || i > 3 then () else
251 let s = a.(i) in
252 if s = 0xffe5
253 then state.capslmask <- 2
254 else capsloop (i+1)
256 capsloop 0;
258 else (
259 if l > 3
260 then (
261 let ki = code - state.mink in
262 if ki >= 0
263 then
264 let a = state.keymap.(ki) in
265 let rec lloop i = if i = Array.length a || i > 3 then () else
266 let s = a.(i) in
267 match s with
268 | 0xfe03 -> state.levl3mask <- 1 lsl l
269 | 0xfe11 -> state.levl5mask <- 1 lsl l
270 | 0xff7f -> state.numlmask <- 1 lsl l
271 | _ -> lloop (i+1)
273 lloop 0;
276 loop1 (m+1)
278 loop1 0;
279 loop (l+1)
281 loop 0;
284 let sendwithrep sock s f =
285 Queue.push f state.fifo;
286 sendstr1 s 0 (Bytes.length s) sock;
289 let padcatl =
290 let pad = "123" in
291 fun ss ->
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
296 if pl != 0
297 then (
298 Buffer.add_substring b pad 0 (4 - pl);
300 Buffer.to_bytes b;
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
321 w8 s 1 depth;
322 w32 s 4 wid;
323 w32 s 8 parent;
324 w16 s 12 x;
325 w16 s 14 y;
326 w16 s 16 w;
327 w16 s 18 h;
328 w16 s 20 bw;
329 w16 s 22 0; (* copyfromparent *)
330 w32 s 24 vid; (* visual *)
331 w32 s 28 0x280a; (* valuemask =
332 | background pixel
333 | border pixel
334 | event mask
335 | colormap *)
336 w32 s 32 0; (* background pixel *)
337 w32 s 36 0; (* border pixel*)
338 w32 s 40 eventmask;
339 w32 s 44 mid;
343 let createcolormapreq mid wid vid =
344 let s = makereq 78 16 4 in
345 w8 s 1 0;
346 w32 s 4 mid;
347 w32 s 8 wid;
348 w32 s 12 vid;
352 let getgeometryreq wid =
353 let s = makereq 14 8 2 in
354 w32 s 4 wid;
358 let mapreq wid =
359 let s = makereq 8 8 2 in
360 w32 s 4 wid;
364 let getkeymapreq first count =
365 let s = makereq 101 8 2 in
366 w8 s 4 first;
367 w8 s 5 count;
371 let changepropreq wid prop typ format props =
372 let s = makereq 18 24 0 in
373 let s = padcat s props in
374 w8 s 1 0;
375 w16 s 2 (Bytes.length s / 4);
376 w32 s 4 wid;
377 w32 s 8 prop;
378 w32 s 12 typ;
379 w8 s 16 format;
380 let ful = Bytes.length props / (match format with
381 | 8 -> 1
382 | 16 -> 2
383 | 32 -> 4
384 | n -> error "no idea what %d means" n)
386 w32 s 20 ful;
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);
393 w32 s 4 wid;
394 w32 s 8 prop;
395 w32 s 12 typ;
396 w32 s 16 0;
397 w32 s 20 2;
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);
405 w32 s 4 wid;
406 w16 s 8 mask;
410 let s32 n =
411 let s = Bytes.create 4 in
412 w32 s 0 n;
416 let clientmessage format seq wid typ data =
417 let s = makereq 33 12 0 in
418 let s = padcat s data in
419 w8 s 1 format;
420 w16 s 2 seq;
421 w32 s 4 wid;
422 w32 s 8 typ;
426 let sendeventreq propagate destwid mask data =
427 let s = makereq 25 12 11 in
428 let s = padcat s data in
429 w8 s 1 propagate;
430 w16 s 2 11;
431 w32 s 4 destwid;
432 w32 s 8 mask;
436 let getmodifiermappingreq () =
437 makereq 119 4 1;
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)
451 then (
452 if mask land state.numlmask != 0
453 then
454 let keysym = state.keymap.(code-state.mink).(1) in
455 if keysym = 0 then pkpk else keysym
456 else pkpk
458 else (
459 let shift =
460 if pkpk land 0xf000 = 0xf000
461 then 0
462 else (mask land 1) lxor ((mask land state.capslmask) lsr 1)
464 let index =
465 if state.xkb && mask land 0x2000 != 0
466 then
467 shift + 2
468 else
469 let l3 = (mask land state.levl3mask) != 0 in
470 let l4 = (mask land state.levl5mask) != 0 in
471 shift +
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)
477 else keysym
481 let getkeysym code mask =
482 let pkpk = state.keymap.(code-state.mink).(0) in
483 if state.xkb && pkpk lsr 8 = 0xfe (* XKB *)
484 then 0
485 else getkeysym pkpk code mask
488 let readresp sock =
489 let resp = readstr sock 32 in
490 let opcode = r8 resp 0 in
491 match opcode land lnot 0x80 with
492 | 0 -> (* error *)
493 let s = resp in
494 let code = r8 s 1
495 and serial = r16 s 2
496 and resid = r32 resp 4
497 and min = r16 s 8
498 and maj = r8 s 10 in
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
504 rep resp;
506 | 2 -> (* key press *)
507 if Array.length state.keymap > 0
508 then
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
518 then
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 *)
526 let n = r8 resp 1
527 and x = r16s resp 24
528 and y = r16s resp 26
529 and m = r16 resp 28 in
530 state.t#mouse n true x y m;
531 vlog "press %d" n
533 | 5 -> (* buttonrelease *)
534 let n = r8 resp 1
535 and x = r16s resp 24
536 and y = r16s resp 26
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
541 | 6 -> (* motion *)
542 let x = r16s resp 24 in
543 let y = r16s resp 26 in
544 let m = r16 resp 28 in
545 if m land 0x1f00 = 0
546 then state.t#pmotion x y
547 else state.t#motion x y;
548 vlog "move %dx%d => %d" x y m
550 | 7 -> (* enter *)
551 let x = r16s resp 24
552 and y = r16s resp 26 in
553 state.t#enter x y;
554 vlog "enter %d %d" x y
556 | 8 -> (* leave *)
557 state.t#leave
559 | 18 -> (* unmap *)
560 state.t#map false;
561 vlog "unmap";
563 | 19 -> (* map *)
564 state.t#map true;
565 vlog "map";
567 | 12 -> (* exposure *)
568 vlog "exposure";
569 state.t#expose
571 | 15 -> (* visibility *)
572 let v = r8 resp 8 in
573 let vis =
574 match v with
575 | 0 -> Unobscured
576 | 1 -> PartiallyObscured
577 | 2 -> FullyObscured
578 | _ ->
579 dolog "unknown visibility %d" v;
580 Unobscured
582 state.t#visible vis;
583 vlog "visibility %d" v;
585 | 34 -> (* mapping *)
586 state.keymap <- E.a;
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;
592 state.numlmask <- 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
599 then (
600 let atom = r32 resp 12 in
601 if atom = state.deleatom
602 then state.t#quit;
604 vlog "atom %#x" atom
606 | 21 -> (* reparent *)
607 vlog "reparent"
609 | 22 -> (* configure *)
610 let x = r16s resp 16
611 and y = r16s resp 18
612 and w = r16 resp 20
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
616 x y w h
618 if w != state.w || h != state.h
619 then (
620 state.t#reshape w h;
622 state.w <- w;
623 state.h <- h;
624 state.x <- x;
625 state.y <- y;
626 state.t#expose
628 | 28 ->
629 let atom = r32 resp 8 in
630 if atom = state.nwmsatom
631 then
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
636 let wsl =
637 if len = 0
638 then []
639 else
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
643 let wsl =
644 if atom = state.maxhatom
645 then MaxHorz::wsl
646 else (
647 if atom = state.maxvatom
648 then MaxVert::wsl
649 else (
650 if atom = state.fulsatom
651 then (
652 state.fs <- Fs (state.x, state.y, state.w, state.h);
653 Fullscreen::wsl
655 else wsl
658 in loop wsl (i+1)
660 loop [] 0
662 state.t#winstate (List.sort compare wsl)
665 | n ->
666 dolog "event %d %S" n (Bytes.unsafe_to_string resp)
669 let readresp sock =
670 let rec loop () =
671 readresp sock;
672 if hasdata sock then loop ();
674 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;
682 let reshape w h =
683 if state.fs = NoFs
684 then
685 let s = Bytes.create 8 in
686 w32 s 0 w;
687 w32 s 4 h;
688 let s = configurewindowreq state.wid 0x000c s in
689 sendstr s state.sock;
690 else state.fullscreen state.wid
693 let activatewin () =
694 state.actwin ();
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 () =
703 let sf deadline =
704 let timeout = deadline -. now () in
705 if timeout <= 0.0
706 then [], [], []
707 else Unix.select [sock] [] [] timeout
709 let r, _, _ = tempfailureretry sf deadline in
710 match r with
711 | [] -> error "didn't get X response in %f seconds, aborting" secstowait
712 | _ ->
713 readresp sock;
714 if not !completed
715 then readtillcompletion ()
717 readtillcompletion ();
720 let mapwin () =
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
733 if n != 2
734 then error "failed to read X connection setup response n=%d" n;
735 match Bytes.get s 0 with
736 | '\000' ->
737 let reasonlen = r8 s 1 in
738 let s = readstr sock 6 in
739 let maj = r16 s 0
740 and min = r16 s 2
741 and add = r16 s 4 in
742 let len = add*4 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";
750 | '\001' ->
751 let s = readstr sock 38 in
752 let maj = r16 s 0
753 and min = r16 s 2
754 and add = r16 s 4
755 and idbase = r32 s 10
756 and idmask = r32 s 14
757 and vlen = r16 s 22
758 and screens = r8 s 26
759 and formats = r8 s 27
760 and minkk = r8 s 32
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);
769 let pos =
770 let rec findscreen n pos = if n = screennum then pos else
771 let pos =
772 let ndepths = r8 data (pos+39) in
773 let rec skipdepths n pos = if n = ndepths then pos else
774 let pos =
775 let nvisiuals = r16 data (pos+2) in
776 pos + nvisiuals*24 + 8
778 skipdepths (n+1) pos
780 skipdepths n (pos+40)
782 findscreen (n+1) pos
784 findscreen 0 pos
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
791 state.mink <- minkk;
792 state.maxk <- maxkk;
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
804 let mid = wid+1 in
805 let fid = mid+1 in
807 state.wid <- wid;
808 state.fid <- fid;
810 let vid = glxinit disp wid screennum in
811 let ndepths = r8 data (pos+39) in
812 let rec finddepth n' pos =
813 if n' = ndepths
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 =
818 if n = nvisuals
819 then finddepth (n'+1) pos
820 else
821 let id = r32 data pos in
822 if id = vid
823 then depth
824 else findvisual (n+1) (pos+24)
826 findvisual 0 (pos+8)
828 let depth = finddepth 0 (pos+40) in
830 let s = createcolormapreq mid root vid in
831 sendstr s sock;
833 let mask = 0
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
862 sendstr s sock;
864 sendintern sock (~> "WM_PROTOCOLS") false (fun resp ->
865 state.protoatom <- r32 resp 8;
866 sendintern
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
871 sendstr s sock;
875 sendintern sock (~> "WM_CLIENT_MACHINE") false (fun resp ->
876 let atom = r32 resp 8 in
877 let empty = E.s in
878 let hostname =
879 try Unix.gethostname ()
880 with exn ->
881 dolog "error getting host name: %s" @@ exntos exn;
882 empty
884 if hostname != empty
885 then
886 let s = changepropreq wid atom state.stringatom 8
887 (~> hostname) in
888 sendstr s sock;
889 sendintern sock (~> "_NET_WM_PID") false (fun resp ->
890 let atom = r32 resp 8 in
891 let pid = Unix.getpid () in
892 let s = s32 pid in
893 let s = changepropreq wid atom 6(*cardinal*) 32 s in
894 sendstr s sock;
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
920 sendstr s sock;
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
931 if atom != 0
932 then state.stringatom <- atom;
935 let setwmname s =
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
942 if atom != 0
943 then state.setwmname <- (fun s ->
944 setwmname 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
952 match state.fs with
953 | NoFs ->
954 w32 s 0 0;
955 w32 s 4 0;
956 w32 s 8 rootw;
957 w32 s 12 rooth;
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);
962 | Fs (x, y, w, h) ->
963 w32 s 0 x;
964 w32 s 4 y;
965 w32 s 8 w;
966 w32 s 12 h;
967 let s = configurewindowreq wid 0x000f s in
968 sendstr s state.sock;
969 state.fs <- NoFs;
972 sendintern sock (~> "_NET_WM_STATE") true (fun resp ->
973 state.nwmsatom <- r32 resp 8;
974 if state.nwmsatom != 0
975 then (
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
985 then
986 state.fullscreen <-
987 (fun wid ->
988 let data = Bytes.make 20 '\000' in
989 let fs, f =
990 match state.fs with
991 | NoFs -> Fs (-1, -1, -1, -1), 1
992 | Fs _ -> NoFs, 0
994 w32 data 0 f;
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
999 sendstr s sock;
1000 state.fs <- fs;
1005 let s = queryextensionreq (~> "XKEYBOARD") in
1006 sendwithrep
1007 sock s (fun resp ->
1008 let present = r8 resp 8 in
1009 if present != 0
1010 then (
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 *)
1018 sendwithrep
1019 sock s
1020 (fun resp ->
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 ->
1028 glxcompleteinit ();
1029 let w = r16 resp 16
1030 and h = r16 resp 18 in
1031 state.w <- w;
1032 state.h <- h;
1035 | c ->
1036 error "unknown conection setup response %d" (Char.code c)
1039 let getauth haddr dnum =
1040 let haddr =
1041 if emptystr haddr || haddr = "localhost"
1042 then
1043 try Unix.gethostname ()
1044 with exn ->
1045 dolog "failed to resolve `%S': %s" haddr @@ exntos exn;
1046 haddr
1047 else haddr
1049 let path =
1050 try Sys.getenv "XAUTHORITY"
1051 with Not_found ->
1052 try Filename.concat (Sys.getenv "HOME") ".Xauthority"
1053 with Not_found -> E.s
1055 let readauth ic =
1056 let r16be s =
1057 let rb pos = Char.code (Bytes.get s pos) in
1058 (rb 1) lor ((rb 0) lsl 8)
1060 let rec find () =
1061 let rs () =
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
1067 let addr = rs () in
1068 let nums = rs () in
1069 let optnum =
1070 try Some (int_of_string nums)
1071 with exn ->
1072 dolog
1073 "display number(%S) is not an integer (corrupt %S?): %s"
1074 nums path @@ exntos exn;
1075 None
1077 let name = rs () in
1078 let data = rs () in
1080 vlog "family %S addr %S(%S) num %S(%d) name %S data %S"
1081 family addr haddr nums dnum name data;
1082 match optnum with
1083 | Some num when addr = haddr && num = dnum ->
1084 name, data
1085 | _ -> find ()
1087 let name, data =
1088 try find ()
1089 with
1090 | End_of_file -> E.s, E.s
1091 | exn ->
1092 dolog "exception while reading X authority data (%S): %s"
1093 path @@ exntos exn;
1094 E.s, E.s
1096 close_in ic;
1097 name, data;
1099 if emptystr path
1100 then E.s, E.s
1101 else
1102 match open_in_bin path with
1103 | ic -> readauth ic
1104 | (exception exn) ->
1105 dolog "failed to open X authority file `%S' : %s" path @@ exntos exn;
1106 E.s, E.s
1109 let init t rootwid w h platform =
1110 let d =
1111 try Sys.getenv "DISPLAY"
1112 with exn ->
1113 error "cannot get DISPLAY evironment variable: %s" @@ exntos exn
1115 let getnum w b e =
1116 if b = e
1117 then error "invalid DISPLAY(%s) %S" w d
1118 else
1119 let s = String.sub d b (e - b) in
1120 try int_of_string s
1121 with exn ->
1122 error "invalid DISPLAY %S can not parse %s(%S): %s"
1123 d w s @@ exntos exn
1125 let rec phost pos =
1126 if pos = String.length d
1127 then error "invalid DISPLAY %S no display number specified" d
1128 else (
1129 if d.[pos] = ':'
1130 then
1131 let rec pdispnum pos1 =
1132 if pos1 = String.length d
1133 then getnum "display number" (pos+1) pos1, 0
1134 else
1135 match d.[pos1] with
1136 | '.' ->
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
1141 else
1142 match d.[pos2] with
1143 | '0' .. '9' -> pscreennum (pos2+1)
1144 | _ ->
1145 error "invalid DISPLAY %S, cannot parse screen number" d
1147 dispnum, pscreennum (pos1+1)
1148 | '0' .. '9' -> pdispnum (pos1+1)
1149 | _ ->
1150 error "invalid DISPLAY %S, cannot parse display number" d
1152 String.sub d 0 pos, pdispnum (pos+1)
1153 else phost (pos+1)
1156 let host, (dispnum, screennum) = phost 0 in
1157 let aname, adata = getauth host dispnum in
1158 let fd =
1159 let fd, addr =
1160 if emptystr host || host = "unix"
1161 then
1162 let addr =
1163 match platform with
1164 | Utils.Posx -> Unix.ADDR_UNIX d
1165 | Utils.Plinux ->
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
1171 else
1172 let h =
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
1184 cloexec fd;
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;
1189 w16 s 2 11;
1190 w16 s 4 0;
1191 w16 s 6 (String.length aname);
1192 w16 s 8 (String.length adata);
1193 sendstr1 s 0 (Bytes.length s) fd;
1194 state.sock <- fd;
1195 setup d fd rootwid screennum w h;
1196 state.t <- t;
1197 fd, state.w, state.h;
1200 let settitle s =
1201 state.setwmname (~> s);
1204 let setcursor cursor =
1205 if cursor != state.curcurs
1206 then (
1207 setcursor cursor;
1208 state.curcurs <- cursor;
1212 let fullscreen () =
1213 state.fullscreen state.wid;
1216 let metamask = 0x40;;
1217 let altmask = 8;;
1218 let shiftmask = 1;;
1219 let ctrlmask = 4;;
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;;
1227 let xlatt, xlatf =
1228 let t = Hashtbl.create 20
1229 and f = Hashtbl.create 20 in
1230 let add n nl k =
1231 List.iter (fun s -> Hashtbl.add t s k) (n::nl);
1232 Hashtbl.add f k n
1234 let addc c =
1235 let s = String.make 1 c in
1236 add s [] (Char.code c)
1238 let addcr a b =
1239 let an = Char.code a and bn = Char.code b in
1240 for i = an to bn do addc (Char.chr i) done;
1242 addcr '0' '9';
1243 addcr 'a' 'z';
1244 addcr 'A' 'Z';
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;
1260 add "up" [] 0xff52;
1261 add "down" [] 0xff54;
1262 add "menu" [] 0xff67;
1263 t, f;
1266 let keyname k =
1267 try Hashtbl.find xlatf k
1268 with Not_found -> Printf.sprintf "%#x" k;
1271 let namekey name =
1272 try Hashtbl.find xlatt name
1273 with Not_found ->
1274 if String.length name = 1
1275 then Char.code name.[0]
1276 else int_of_string name;