Bring some sanity into key handling
[llpp.git] / wsi.ml
blob798940a226cb7f6fb0842c6f4e7022f7f52914ad
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 =
52 object
53 method display : unit
54 method map : bool -> unit
55 method expose : unit
56 method visible : visiblestate -> unit
57 method reshape : int -> int -> unit
58 method mouse : int -> bool -> int -> int -> int -> unit
59 method motion : int -> int -> unit
60 method pmotion : int -> int -> unit
61 method key : int -> int -> unit
62 method enter : int -> int -> unit
63 method leave : unit
64 method winstate : winstate list -> unit
65 method quit : unit
66 end;;
68 type state =
69 { mutable mink : int
70 ; mutable maxk : int
71 ; mutable keymap : int array array
72 ; fifo : (bytes -> unit) Queue.t
73 ; mutable seq : int
74 ; mutable protoatom : atom
75 ; mutable deleatom : atom
76 ; mutable nwmsatom : atom
77 ; mutable maxvatom : atom
78 ; mutable maxhatom : atom
79 ; mutable fulsatom : atom
80 ; mutable idbase : int
81 ; mutable wid : int
82 ; mutable fid : int
83 ; mutable fullscreen : (int -> unit)
84 ; mutable setwmname : (bytes -> unit)
85 ; mutable actwin : (unit -> unit)
86 ; mutable stringatom : int
87 ; mutable t : t
88 ; mutable sock : Unix.file_descr
89 ; mutable x : int
90 ; mutable y : int
91 ; mutable w : int
92 ; mutable h : int
93 ; mutable fs : fs
94 ; mutable curcurs : cursor
95 ; mutable capslmask : int
96 ; mutable numlmask : int
97 ; mutable levl3mask : int
98 ; mutable levl5mask : int
99 ; mutable xkb : bool
101 and fs =
102 | NoFs
103 | Fs of (int * int * int * int)
106 let state =
107 { mink = max_int
108 ; maxk = min_int
109 ; keymap = E.a
110 ; fifo = Queue.create ()
111 ; seq = 0
112 ; protoatom = -1
113 ; deleatom = -1
114 ; nwmsatom = -1
115 ; maxvatom = -1
116 ; maxhatom = -1
117 ; fulsatom = -1
118 ; idbase = -1
119 ; wid = -1
120 ; fid = -1
121 ; fullscreen = (fun _ -> ())
122 ; setwmname = (fun _ -> ())
123 ; actwin = (fun _ -> ())
124 ; sock = Unix.stdin
125 ; t = onot
126 ; x = -1
127 ; y = -1
128 ; w = -1
129 ; h = -1
130 ; fs = NoFs
131 ; stringatom = 31
132 ; curcurs = CURSOR_TEXT
133 ; capslmask = 0
134 ; numlmask = 0
135 ; levl3mask = 0
136 ; levl5mask = 0
137 ; xkb = false
141 let w8 s pos i = Bytes.set s pos (Char.chr (i land 0xff));;
142 let r8 s pos = Char.code (Bytes.get s pos);;
144 let ordermagic = 'l';;
146 let w16 s pos i =
147 w8 s pos i;
148 w8 s (pos+1) (i lsr 8)
151 let w32 s pos i =
152 w16 s pos i;
153 w16 s (pos+2) (i lsr 16)
156 let r16 s pos =
157 let rb pos1 = Char.code (Bytes.get s (pos + pos1)) in
158 (rb 0) lor ((rb 1) lsl 8)
161 let r16s s pos =
162 let i = r16 s pos in
163 i - ((i land 0x8000) lsl 1)
166 let r32 s pos =
167 let rb pos1 = Char.code (Bytes.get s (pos + pos1)) in
168 let l = (rb 0) lor ((rb 1) lsl 8)
169 and u = (rb 2) lor ((rb 3) lsl 8) in
170 (u lsl 16) lor l
173 let makereq opcode len reqlen =
174 let s = Bytes.create len in
175 w8 s 0 opcode;
176 w16 s 2 reqlen;
180 let readstr sock n =
181 let s = Bytes.create n in
182 let rec loop pos n =
183 let m = tempfailureretry (Unix.read sock s pos) n in
184 if m = 0
185 then state.t#quit;
186 if n != m
187 then (
188 ignore (tempfailureretry (Unix.select [sock] [] []) 0.01);
189 loop (pos + m) (n - m)
192 loop 0 n;
196 let sendstr1 s pos len sock =
197 let s = Bytes.unsafe_to_string s in
198 vlog "%d <= %S" state.seq s;
199 state.seq <- state.seq + 1;
200 let n = tempfailureretry (Unix.write_substring sock s pos) len in
201 if n != len
202 then error "send %d returned %d" len n;
205 let updkmap sock resp =
206 let syms = r8 resp 1 in
207 let len = r32 resp 4 in
208 let data =
209 if len > 0
210 then readstr sock (4*len)
211 else E.b
213 let m = len / syms in
214 state.keymap <- Array.make_matrix
215 (state.maxk - state.mink) syms 0xffffff;
216 let rec loop i = if i = m then () else
217 let k = i*4*syms in
218 let rec loop2 k l = if l = syms then () else
219 let v = r32 data k in
220 state.keymap.(i).(l) <- v;
221 loop2 (k+4) (l+1)
223 loop2 k 0;
224 loop (i+1);
226 loop 0;
229 let updmodmap sock resp =
230 let n = r8 resp 1 in
231 let len = r16 resp 4 in
232 let data =
233 if len > 0
234 then readstr sock (len*4)
235 else E.b
237 if len > 0 then (*???*)
238 let modmap = Array.make_matrix 8 n 0xffffff in
239 let rec loop l =
240 if l = 8
241 then ()
242 else
243 let p = l*n in
244 let rec loop1 m =
245 if m = n then ()
246 else
247 let p = p+m in
248 let code = r8 data p in
249 modmap.(l).(m) <- code;
250 if l = 1
251 then (
252 let ki = code - state.mink in
253 if ki >= 0
254 then
255 let a = state.keymap.(ki) in
256 let rec capsloop i =
257 if i = Array.length a || i > 3
258 then ()
259 else
260 let s = a.(i) in
261 if s = 0xffe5
262 then state.capslmask <- 2
263 else capsloop (i+1)
265 capsloop 0;
267 else (
268 if l > 3
269 then (
270 let ki = code - state.mink in
271 if ki >= 0
272 then
273 let a = state.keymap.(ki) in
274 let rec lloop i =
275 if i = Array.length a || i > 3
276 then ()
277 else
278 let s = a.(i) in
279 match s with
280 | 0xfe03 -> state.levl3mask <- 1 lsl l
281 | 0xfe11 -> state.levl5mask <- 1 lsl l
282 | 0xff7f -> state.numlmask <- 1 lsl l
283 | _ -> lloop (i+1)
285 lloop 0;
288 loop1 (m+1)
290 loop1 0;
291 loop (l+1)
293 loop 0;
296 let sendwithrep sock s f =
297 Queue.push f state.fifo;
298 sendstr1 s 0 (Bytes.length s) sock;
301 let padcatl =
302 let pad = "123" in
303 fun ss ->
304 let b = Buffer.create 16 in
305 List.iter (Buffer.add_bytes b) ss;
306 let bl = Buffer.length b in
307 let pl = bl land 3 in
308 if pl != 0
309 then (
310 Buffer.add_substring b pad 0 (4 - pl);
312 Buffer.to_bytes b;
315 let padcat s1 s2 = padcatl [s1; s2];;
317 let internreq name onlyifexists =
318 let s = makereq 16 8 8 in
319 let s = padcat s name in
320 w8 s 1 (if onlyifexists then 1 else 0);
321 w16 s 2 (Bytes.length s / 4);
322 w16 s 4 (Bytes.length name);
326 let sendintern sock s onlyifexists f =
327 let s = internreq s onlyifexists in
328 sendwithrep sock s f;
331 let createwindowreq wid parent x y w h bw eventmask vid depth mid =
332 let s = makereq 1 48 12 in
333 w8 s 1 depth;
334 w32 s 4 wid;
335 w32 s 8 parent;
336 w16 s 12 x;
337 w16 s 14 y;
338 w16 s 16 w;
339 w16 s 18 h;
340 w16 s 20 bw;
341 w16 s 22 0; (* copyfromparent *)
342 w32 s 24 vid; (* visual *)
343 w32 s 28 0x280a; (* valuemask =
344 | background pixel
345 | border pixel
346 | event mask
347 | colormap *)
348 w32 s 32 0; (* background pixel *)
349 w32 s 36 0; (* border pixel*)
350 w32 s 40 eventmask;
351 w32 s 44 mid;
355 let createcolormapreq mid wid vid =
356 let s = makereq 78 16 4 in
357 w8 s 1 0;
358 w32 s 4 mid;
359 w32 s 8 wid;
360 w32 s 12 vid;
364 let getgeometryreq wid =
365 let s = makereq 14 8 2 in
366 w32 s 4 wid;
370 let mapreq wid =
371 let s = makereq 8 8 2 in
372 w32 s 4 wid;
376 let getkeymapreq first count =
377 let s = makereq 101 8 2 in
378 w8 s 4 first;
379 w8 s 5 count;
383 let changepropreq wid prop typ format props =
384 let s = makereq 18 24 0 in
385 let s = padcat s props in
386 w8 s 1 0;
387 w16 s 2 (Bytes.length s / 4);
388 w32 s 4 wid;
389 w32 s 8 prop;
390 w32 s 12 typ;
391 w8 s 16 format;
392 let ful = Bytes.length props / (match format with
393 | 8 -> 1
394 | 16 -> 2
395 | 32 -> 4
396 | n -> error "no idea what %d means" n)
398 w32 s 20 ful;
402 let getpropreq delete wid prop typ =
403 let s = makereq 20 24 6 in
404 w8 s 1 (if delete then 1 else 0);
405 w32 s 4 wid;
406 w32 s 8 prop;
407 w32 s 12 typ;
408 w32 s 16 0;
409 w32 s 20 2;
413 let configurewindowreq wid mask values =
414 let s = makereq 12 12 0 in
415 let s = padcat s values in
416 w16 s 2 (Bytes.length s / 4);
417 w32 s 4 wid;
418 w16 s 8 mask;
422 let s32 n =
423 let s = Bytes.create 4 in
424 w32 s 0 n;
428 let clientmessage format seq wid typ data =
429 let s = makereq 33 12 0 in
430 let s = padcat s data in
431 w8 s 1 format;
432 w16 s 2 seq;
433 w32 s 4 wid;
434 w32 s 8 typ;
438 let sendeventreq propagate destwid mask data =
439 let s = makereq 25 12 11 in
440 let s = padcat s data in
441 w8 s 1 propagate;
442 w16 s 2 11;
443 w32 s 4 destwid;
444 w32 s 8 mask;
448 let getmodifiermappingreq () =
449 makereq 119 4 1;
452 let queryextensionreq name =
453 let s = makereq 98 8 0 in
454 let s = padcat s name in
455 w16 s 2 (Bytes.length s / 4);
456 w16 s 4 (Bytes.length name);
460 let getkeysym pkpk code mask =
461 if (pkpk >= 0xff80 && pkpk <= 0xffbd)
462 || (pkpk >= 0x11000000 && pkpk <= 0x1100ffff)
463 then (
464 if mask land state.numlmask != 0
465 then
466 let keysym = state.keymap.(code-state.mink).(1) in
467 if keysym = 0 then pkpk else keysym
468 else pkpk
470 else (
471 let shift =
472 if pkpk land 0xf000 = 0xf000
473 then 0
474 else (mask land 1) lxor ((mask land state.capslmask) lsr 1)
476 let index =
477 if state.xkb && mask land 0x2000 != 0
478 then
479 shift + 2
480 else
481 let l3 = (mask land state.levl3mask) != 0 in
482 let l4 = (mask land state.levl5mask) != 0 in
483 shift +
484 if l3 then (if l4 then 8 else 4) else (if l4 then 6 else 0)
486 let keysym = state.keymap.(code-state.mink).(index) in
487 if index land 1 = 1 && keysym = 0
488 then state.keymap.(code-state.mink).(index - 1)
489 else keysym
493 let getkeysym code mask =
494 let pkpk = state.keymap.(code-state.mink).(0) in
495 if state.xkb && pkpk lsr 8 = 0xfe (* XKB *)
496 then 0
497 else getkeysym pkpk code mask
500 let readresp sock =
501 let resp = readstr sock 32 in
502 let opcode = r8 resp 0 in
503 match opcode land lnot 0x80 with
504 | 0 -> (* error *)
505 let s = resp in
506 let code = r8 s 1
507 and serial = r16 s 2
508 and resid = r32 resp 4
509 and min = r16 s 8
510 and maj = r8 s 10 in
511 error "code=%d serial=%d resid=%#x min=%d maj=%d\n%S"
512 code serial resid min maj (Bytes.unsafe_to_string resp);
514 | 1 -> (* response *)
515 let rep = Queue.pop state.fifo in
516 rep resp;
518 | 2 -> (* key press *)
519 if Array.length state.keymap > 0
520 then
521 let code = r8 resp 1 in
522 let mask = r16 resp 28 in
523 let keysym = getkeysym code mask in
524 vlog "keysym = %x %c mask %#x code %d"
525 keysym (Char.unsafe_chr keysym) mask code;
526 state.t#key keysym mask;
528 | 3 -> (* key release *)
529 if Array.length state.keymap > 0
530 then
531 let code = r8 resp 1 in
532 let mask = r16 resp 28 in
533 let keysym = getkeysym code mask in
534 vlog "release keysym = %x %c mask %#x code %#d"
535 keysym (Char.unsafe_chr keysym) mask code
537 | 4 -> (* buttonpress *)
538 let n = r8 resp 1
539 and x = r16s resp 24
540 and y = r16s resp 26
541 and m = r16 resp 28 in
542 state.t#mouse n true x y m;
543 vlog "press %d" n
545 | 5 -> (* buttonrelease *)
546 let n = r8 resp 1
547 and x = r16s resp 24
548 and y = r16s resp 26
549 and m = r16 resp 28 in
550 state.t#mouse n false x y m;
551 vlog "release %d %d %d" n x y
553 | 6 -> (* motion *)
554 let x = r16s resp 24 in
555 let y = r16s resp 26 in
556 let m = r16 resp 28 in
557 if m land 0x1f00 = 0
558 then state.t#pmotion x y
559 else state.t#motion x y;
560 vlog "move %dx%d => %d" x y m
562 | 7 -> (* enter *)
563 let x = r16s resp 24
564 and y = r16s resp 26 in
565 state.t#enter x y;
566 vlog "enter %d %d" x y
568 | 8 -> (* leave *)
569 state.t#leave
571 | 18 -> (* unmap *)
572 state.t#map false;
573 vlog "unmap";
575 | 19 -> (* map *)
576 state.t#map true;
577 vlog "map";
579 | 12 -> (* exposure *)
580 vlog "exposure";
581 state.t#expose
583 | 15 -> (* visibility *)
584 let v = r8 resp 8 in
585 let vis =
586 match v with
587 | 0 -> Unobscured
588 | 1 -> PartiallyObscured
589 | 2 -> FullyObscured
590 | _ ->
591 dolog "unknown visibility %d" v;
592 Unobscured
594 state.t#visible vis;
595 vlog "visibility %d" v;
597 | 34 -> (* mapping *)
598 state.keymap <- E.a;
599 let s = getkeymapreq state.mink (state.maxk-state.mink-1) in
600 sendwithrep sock s (updkmap sock);
601 state.capslmask <- 0;
602 state.levl3mask <- 0;
603 state.levl5mask <- 0;
604 state.numlmask <- 0;
605 let s = getmodifiermappingreq () in
606 sendwithrep sock s (updmodmap sock);
608 | 33 -> (* clientmessage *)
609 let atom = r32 resp 8 in
610 if atom = state.protoatom
611 then (
612 let atom = r32 resp 12 in
613 if atom = state.deleatom
614 then state.t#quit;
616 vlog "atom %#x" atom
618 | 21 -> (* reparent *)
619 vlog "reparent"
621 | 22 -> (* configure *)
622 let x = r16s resp 16
623 and y = r16s resp 18
624 and w = r16 resp 20
625 and h = r16 resp 22 in
626 vlog "configure cur [%d %d %d %d] conf [%d %d %d %d]"
627 state.x state.y state.w state.h
628 x y w h
630 if w != state.w || h != state.h
631 then (
632 state.t#reshape w h;
634 state.w <- w;
635 state.h <- h;
636 state.x <- x;
637 state.y <- y;
638 state.t#expose
640 | 28 ->
641 let atom = r32 resp 8 in
642 if atom = state.nwmsatom
643 then
644 let s = getpropreq false state.wid atom 4 in
645 sendwithrep sock s (fun resp ->
646 let len = r32 resp 4 in
647 let nitems = r32 resp 16 in
648 let wsl =
649 if len = 0
650 then []
651 else
652 let s = readstr sock (len*4) in
653 let rec loop wsl i =
654 if i = nitems
655 then wsl
656 else
657 let atom = r32 s (i*4) in
658 let wsl =
659 if atom = state.maxhatom
660 then MaxHorz::wsl
661 else (
662 if atom = state.maxvatom
663 then MaxVert::wsl
664 else (
665 if atom = state.fulsatom
666 then (
667 state.fs <- Fs (state.x, state.y,
668 state.w, state.h);
669 Fullscreen::wsl
671 else wsl
674 in loop wsl (i+1)
676 loop [] 0
678 state.t#winstate (List.sort compare wsl)
681 | n ->
682 dolog "event %d %S" n (Bytes.unsafe_to_string resp)
685 let readresp sock =
686 let rec loop () =
687 readresp sock;
688 if hasdata sock then loop ();
690 loop ();
693 let sendstr s ?(pos=0) ?(len=Bytes.length s) sock =
694 sendstr1 s pos len sock;
695 if hasdata sock then readresp sock;
698 let reshape w h =
699 if state.fs = NoFs
700 then
701 let s = Bytes.create 8 in
702 w32 s 0 w;
703 w32 s 4 h;
704 let s = configurewindowreq state.wid 0x000c s in
705 sendstr s state.sock;
706 else state.fullscreen state.wid
709 let activatewin () =
710 state.actwin ();
713 let syncsendwithrep sock secstowait s f =
714 let completed = ref false in
715 sendwithrep sock s (fun resp -> f resp; completed := true);
716 let now = Unix.gettimeofday in
717 let deadline = now () +. secstowait in
718 let rec readtillcompletion () =
719 let sf deadline =
720 let timeout = deadline -. now () in
721 if timeout <= 0.0
722 then [], [], []
723 else Unix.select [sock] [] [] timeout
725 let r, _, _ = tempfailureretry sf deadline in
726 match r with
727 | [] -> error "didn't get X response in %f seconds, aborting" secstowait
728 | _ ->
729 readresp sock;
730 if not !completed
731 then readtillcompletion ()
733 readtillcompletion ();
736 let mapwin () =
737 let s = mapreq state.wid in
738 sendstr s state.sock;
741 let syncsendintern sock secstowait s onlyifexists f =
742 let s = internreq s onlyifexists in
743 syncsendwithrep sock secstowait s f;
746 let setup disp sock rootwid screennum w h =
747 let s = readstr sock 2 in
748 let n = Bytes.length s in
749 if n != 2
750 then error "failed to read X connection setup response n=%d" n;
751 match Bytes.get s 0 with
752 | '\000' ->
753 let reasonlen = r8 s 1 in
754 let s = readstr sock 6 in
755 let maj = r16 s 0
756 and min = r16 s 2
757 and add = r16 s 4 in
758 let len = add*4 in
759 let data = readstr sock len in
760 let reason = Bytes.sub data 0 reasonlen in
761 error "X connection failed maj=%d min=%d reason=%S"
762 maj min (Bytes.unsafe_to_string reason)
764 | '\002' -> failwith "X connection setup failed: authentication required";
766 | '\001' ->
767 let s = readstr sock 38 in
768 let maj = r16 s 0
769 and min = r16 s 2
770 and add = r16 s 4
771 and idbase = r32 s 10
772 and idmask = r32 s 14
773 and vlen = r16 s 22
774 and screens = r8 s 26
775 and formats = r8 s 27
776 and minkk = r8 s 32
777 and maxkk = r8 s 33 in
778 let data = readstr sock (4*add-32) in
779 let vendor = Bytes.sub data 0 vlen in
780 let pos = ((vlen+3) land lnot 3) + formats*8 in
782 if screennum >= screens
783 then error "invalid screen %d, max %d" screennum (screens-1);
785 let pos =
786 let rec findscreen n pos =
787 if n = screennum
788 then pos
789 else
790 let pos =
791 let ndepths = r8 data (pos+39) in
792 let rec skipdepths n pos =
793 if n = ndepths
794 then pos
795 else
796 let pos =
797 let nvisiuals = r16 data (pos+2) in
798 pos + nvisiuals*24 + 8
800 skipdepths (n+1) pos
802 skipdepths n (pos+40)
804 findscreen (n+1) pos
806 findscreen 0 pos
808 let root = if rootwid = 0 then r32 data pos else rootwid in
809 let rootw = r16 data (pos+20)
810 and rooth = r16 data (pos+22)
811 and rootdepth = r8 data (pos+38)in
813 state.mink <- minkk;
814 state.maxk <- maxkk;
815 state.idbase <- idbase;
816 vlog "vendor = %S, maj=%d min=%d" (Bytes.unsafe_to_string vendor) maj min;
817 vlog "screens = %d formats = %d" screens formats;
818 vlog "minkk = %d maxkk = %d" minkk maxkk;
819 vlog "idbase = %#x idmask = %#x" idbase idmask;
820 vlog "root=%#x %dx%d" root rootw rooth;
821 vlog "wmm = %d, hmm = %d" (r16 data (pos+24)) (r16 data (pos+26));
822 vlog "visualid = %#x" (r32 data (pos+32));
823 vlog "root depth = %d" rootdepth;
825 let wid = state.idbase in
826 let mid = wid+1 in
827 let fid = mid+1 in
829 state.wid <- wid;
830 state.fid <- fid;
832 let vid = glxinit disp wid screennum in
833 let ndepths = r8 data (pos+39) in
834 let rec finddepth n' pos =
835 if n' = ndepths
836 then error "cannot find depth for visual %#x" vid;
837 let depth = r8 data pos in
838 let nvisuals = r16 data (pos+2) in
839 let rec findvisual n pos =
840 if n = nvisuals
841 then finddepth (n'+1) pos
842 else
843 let id = r32 data pos in
844 if id = vid
845 then depth
846 else findvisual (n+1) (pos+24)
848 findvisual 0 (pos+8)
850 let depth = finddepth 0 (pos+40) in
852 let s = createcolormapreq mid root vid in
853 sendstr s sock;
855 let mask = 0
856 + 0x00000001 (* KeyPress *)
857 (* + 0x00000002 *) (* KeyRelease *)
858 + 0x00000004 (* ButtonPress *)
859 + 0x00000008 (* ButtonRelease *)
860 + 0x00000010 (* EnterWindow *)
861 + 0x00000020 (* LeaveWindow *)
862 + 0x00000040 (* PointerMotion *)
863 (* + 0x00000080 *) (* PointerMotionHint *)
864 (* + 0x00000100 *) (* Button1Motion *)
865 (* + 0x00000200 *) (* Button2Motion *)
866 (* + 0x00000400 *) (* Button3Motion *)
867 (* + 0x00000800 *) (* Button4Motion *)
868 (* + 0x00001000 *) (* Button5Motion *)
869 + 0x00002000 (* ButtonMotion *)
870 (* + 0x00004000 *) (* KeymapState *)
871 + 0x00008000 (* Exposure *)
872 + 0x00010000 (* VisibilityChange *)
873 + 0x00020000 (* StructureNotify *)
874 (* + 0x00040000 *) (* ResizeRedirect *)
875 (* + 0x00080000 *) (* SubstructureNotify *)
876 (* + 0x00100000 *) (* SubstructureRedirect *)
877 (* + 0x00200000 *) (* FocusChange *)
878 + 0x00400000 (* PropertyChange *)
879 (* + 0x00800000 *) (* ColormapChange *)
880 (* + 0x01000000 *) (* OwnerGrabButton *)
883 let s = createwindowreq wid root 0 0 w h 0 mask vid depth mid in
884 sendstr s sock;
886 sendintern
887 sock (~> "WM_PROTOCOLS") false (fun resp ->
888 state.protoatom <- r32 resp 8;
889 sendintern
890 sock (~> "WM_DELETE_WINDOW") false (fun resp ->
891 state.deleatom <- r32 resp 8;
892 let s = s32 state.deleatom in
893 let s = changepropreq wid state.protoatom 4 32 s in
894 sendstr s sock;
898 sendintern
899 sock (~> "WM_CLIENT_MACHINE") false (fun resp ->
900 let atom = r32 resp 8 in
901 let empty = E.s in
902 let hostname =
903 try Unix.gethostname ()
904 with exn ->
905 dolog "error getting host name: %s" @@ exntos exn;
906 empty
908 if hostname != empty
909 then
910 let s = changepropreq wid atom state.stringatom 8
911 (~> hostname) in
912 sendstr s sock;
913 sendintern
914 sock (~> "_NET_WM_PID") false (fun resp ->
915 let atom = r32 resp 8 in
916 let pid = Unix.getpid () in
917 let s = s32 pid in
918 let s = changepropreq wid atom 6(*cardinal*) 32 s in
919 sendstr s sock;
923 state.actwin <- (fun () ->
924 let s = Bytes.create 4 in
925 let s = configurewindowreq wid 0x40 s in
926 sendstr s state.sock;
927 let s = mapreq wid in
928 sendstr s state.sock;
931 sendintern
932 sock (~> "_NET_ACTIVE_WINDOW") true (fun resp ->
933 let atom = r32 resp 8 in
934 state.actwin <- (fun () ->
935 let data = Bytes.make 20 '\000' in
936 let cm = clientmessage 32 0 wid atom data in
937 let s = sendeventreq 0 root 0x180000 cm in
938 sendstr s state.sock;
942 syncsendintern
943 sock 2.0 (~> "WM_CLASS") false (fun resp ->
944 let atom = r32 resp 8 in
945 let llpp = ~> "llpp\000llpp\000" in
946 let s = changepropreq wid atom 31 8 llpp in
947 sendstr s sock;
950 let s = getkeymapreq state.mink (state.maxk-state.mink) in
951 sendwithrep sock s (updkmap sock);
953 let s = getmodifiermappingreq () in
954 sendwithrep sock s (updmodmap sock);
956 sendintern
957 sock (~> "UTF8_STRING") true (fun resp ->
958 let atom = r32 resp 8 in
959 if atom != 0
960 then state.stringatom <- atom;
963 let setwmname s =
964 let s = changepropreq wid 39 state.stringatom 8 s in
965 sendstr s state.sock;
967 state.setwmname <- setwmname;
968 sendintern
969 sock (~> "_NET_WM_NAME") true (fun resp ->
970 let atom = r32 resp 8 in
971 if atom != 0
972 then state.setwmname <- (fun s ->
973 setwmname s;
974 let s = changepropreq wid atom state.stringatom 8 s in
975 sendstr s state.sock;
979 state.fullscreen <- (fun wid ->
980 let s = Bytes.create 16 in
981 match state.fs with
982 | NoFs ->
983 w32 s 0 0;
984 w32 s 4 0;
985 w32 s 8 rootw;
986 w32 s 12 rooth;
987 let s = configurewindowreq wid 0x000f s in
988 sendstr s state.sock;
989 state.fs <- Fs (state.x, state.y, state.w, state.h);
991 | Fs (x, y, w, h) ->
992 w32 s 0 x;
993 w32 s 4 y;
994 w32 s 8 w;
995 w32 s 12 h;
996 let s = configurewindowreq wid 0x000f s in
997 sendstr s state.sock;
998 state.fs <- NoFs;
1001 sendintern
1002 sock (~> "_NET_WM_STATE") true (fun resp ->
1003 state.nwmsatom <- r32 resp 8;
1004 if state.nwmsatom != 0
1005 then (
1006 sendintern sock (~> "_NET_WM_STATE_MAXIMIZED_VERT") true (fun resp ->
1007 state.maxvatom <- r32 resp 8;
1009 sendintern sock (~> "_NET_WM_STATE_MAXIMIZED_HORZ") true (fun resp ->
1010 state.maxhatom <- r32 resp 8;
1012 sendintern
1013 sock (~> "_NET_WM_STATE_FULLSCREEN") true (fun resp ->
1014 state.fulsatom <- r32 resp 8;
1015 if state.fulsatom != 0
1016 then
1017 state.fullscreen <-
1018 (fun wid ->
1019 let data = Bytes.make 20 '\000' in
1020 let fs, f =
1021 match state.fs with
1022 | NoFs -> Fs (-1, -1, -1, -1), 1
1023 | Fs _ -> NoFs, 0
1025 w32 data 0 f;
1026 w32 data 4 state.fulsatom;
1028 let cm = clientmessage 32 0 wid state.nwmsatom data in
1029 let s = sendeventreq 0 root 0x180000 cm in
1030 sendstr s sock;
1031 state.fs <- fs;
1036 let s = queryextensionreq (~> "XKEYBOARD") in
1037 sendwithrep
1038 sock s (fun resp ->
1039 let present = r8 resp 8 in
1040 if present != 0
1041 then (
1042 let maj = r8 resp 9 in
1043 let s = Bytes.create 8 in
1044 w8 s 0 maj; (* XKB *)
1045 w8 s 1 0; (* XKBUseExtension *)
1046 w16 s 2 2; (* request-length *)
1047 w16 s 4 1; (* wantedMajor *)
1048 w16 s 6 0; (* watnedMinor *)
1049 sendwithrep
1050 sock s
1051 (fun resp ->
1052 let supported = r8 resp 1 in
1053 state.xkb <- supported != 0
1057 let s = getgeometryreq wid in
1058 syncsendwithrep sock 2.0 s (fun resp ->
1059 glxcompleteinit ();
1060 let w = r16 resp 16
1061 and h = r16 resp 18 in
1062 state.w <- w;
1063 state.h <- h;
1066 | c ->
1067 error "unknown connection setup response %d" (Char.code c)
1070 let getauth haddr dnum =
1071 let haddr =
1072 if emptystr haddr || haddr = "localhost"
1073 then
1074 try Unix.gethostname ()
1075 with exn ->
1076 dolog "failed to resolve `%S': %s" haddr @@ exntos exn;
1077 haddr
1078 else haddr
1080 let path =
1081 try Sys.getenv "XAUTHORITY"
1082 with Not_found ->
1083 try Filename.concat (Sys.getenv "HOME") ".Xauthority"
1084 with Not_found -> E.s
1086 let readauth ic =
1087 let r16be s =
1088 let rb pos = Char.code (Bytes.get s pos) in
1089 (rb 1) lor ((rb 0) lsl 8)
1091 let rec find () =
1092 let rs () =
1093 let s = really_input_string ic 2 in
1094 let n = r16be (~> s) in
1095 really_input_string ic n
1097 let family = really_input_string ic 2 in
1098 let addr = rs () in
1099 let nums = rs () in
1100 let optnum =
1101 try Some (int_of_string nums)
1102 with exn ->
1103 dolog
1104 "display number(%S) is not an integer (corrupt %S?): %s"
1105 nums path @@ exntos exn;
1106 None
1108 let name = rs () in
1109 let data = rs () in
1111 vlog "family %S addr %S(%S) num %S(%d) name %S data %S"
1112 family addr haddr nums dnum name data;
1113 match optnum with
1114 | Some num when addr = haddr && num = dnum ->
1115 name, data
1116 | _ -> find ()
1118 let name, data =
1119 try find ()
1120 with
1121 | End_of_file -> E.s, E.s
1122 | exn ->
1123 dolog "exception while reading X authority data (%S): %s"
1124 path @@ exntos exn;
1125 E.s, E.s
1127 close_in ic;
1128 name, data;
1130 if emptystr path
1131 then E.s, E.s
1132 else
1133 match open_in_bin path with
1134 | ic -> readauth ic
1135 | (exception exn) ->
1136 dolog "failed to open X authority file `%S' : %s" path @@ exntos exn;
1137 E.s, E.s
1140 let init t rootwid w h platform =
1141 let d =
1142 try Sys.getenv "DISPLAY"
1143 with exn ->
1144 error "cannot get DISPLAY evironment variable: %s" @@ exntos exn
1146 let getnum w b e =
1147 if b = e
1148 then error "invalid DISPLAY(%s) %S" w d
1149 else
1150 let s = String.sub d b (e - b) in
1151 try int_of_string s
1152 with exn ->
1153 error "invalid DISPLAY %S can not parse %s(%S): %s"
1154 d w s @@ exntos exn
1156 let rec phost pos =
1157 if pos = String.length d
1158 then error "invalid DISPLAY %S no display number specified" d
1159 else (
1160 if d.[pos] = ':'
1161 then
1162 let rec pdispnum pos1 =
1163 if pos1 = String.length d
1164 then getnum "display number" (pos+1) pos1, 0
1165 else
1166 match d.[pos1] with
1167 | '.' ->
1168 let dispnum = getnum "display number" (pos+1) pos1 in
1169 let rec pscreennum pos2 =
1170 if pos2 = String.length d
1171 then getnum "screen number" (pos1+1) pos2
1172 else
1173 match d.[pos2] with
1174 | '0' .. '9' -> pscreennum (pos2+1)
1175 | _ ->
1176 error "invalid DISPLAY %S, cannot parse screen number" d
1178 dispnum, pscreennum (pos1+1)
1179 | '0' .. '9' -> pdispnum (pos1+1)
1180 | _ ->
1181 error "invalid DISPLAY %S, cannot parse display number" d
1183 String.sub d 0 pos, pdispnum (pos+1)
1184 else phost (pos+1)
1187 let host, (dispnum, screennum) = phost 0 in
1188 let aname, adata = getauth host dispnum in
1189 let fd =
1190 let fd, addr =
1191 if emptystr host || host.[0] = '/' || host = "unix"
1192 then
1193 let addr =
1194 match platform with
1195 | Utils.Posx -> Unix.ADDR_UNIX d
1196 | Utils.Plinux ->
1197 Unix.ADDR_UNIX ("\000/tmp/.X11-unix/X" ^ string_of_int dispnum)
1198 | Utils.Punknown | Utils.Psun | Utils.Pbsd | Utils.Pcygwin ->
1199 Unix.ADDR_UNIX ("/tmp/.X11-unix/X" ^ string_of_int dispnum)
1201 Unix.socket Unix.PF_UNIX Unix.SOCK_STREAM 0, addr
1202 else
1203 let h =
1204 try Unix.gethostbyname host
1205 with exn -> error "cannot resolve %S: %s" host @@ exntos exn
1207 let addr = h.Unix.h_addr_list.(0) in
1208 let port = 6000 + dispnum in
1209 let fd = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
1210 fd, (Unix.ADDR_INET (addr, port))
1212 try Unix.connect fd addr; fd
1213 with exn -> error "failed to connect to X: %s" @@ exntos exn
1215 cloexec fd;
1216 let s = Bytes.create 12 in
1217 let s = padcat s (~> aname) in
1218 let s = padcat s (~> adata) in
1219 Bytes.set s 0 ordermagic;
1220 w16 s 2 11;
1221 w16 s 4 0;
1222 w16 s 6 (String.length aname);
1223 w16 s 8 (String.length adata);
1224 sendstr1 s 0 (Bytes.length s) fd;
1225 state.sock <- fd;
1226 setup d fd rootwid screennum w h;
1227 state.t <- t;
1228 fd, state.w, state.h;
1231 let settitle s =
1232 state.setwmname (~> s);
1235 let setcursor cursor =
1236 if cursor != state.curcurs
1237 then (
1238 setcursor cursor;
1239 state.curcurs <- cursor;
1243 let fullscreen () =
1244 state.fullscreen state.wid;
1247 let metamask = 0x40;;
1248 let altmask = 8;;
1249 let shiftmask = 1;;
1250 let ctrlmask = 4;;
1252 let withalt mask = mask land altmask != 0;;
1253 let withctrl mask = mask land ctrlmask != 0;;
1254 let withshift mask = mask land shiftmask != 0;;
1255 let withmeta mask = mask land metamask != 0;;
1256 let withnone mask = mask land (altmask + ctrlmask + shiftmask + metamask) = 0;;
1258 let xlatt, xlatf =
1259 let t = Hashtbl.create 20
1260 and f = Hashtbl.create 20 in
1261 let add n nl k =
1262 List.iter (fun s -> Hashtbl.add t s k) (n::nl);
1263 Hashtbl.add f k n
1265 let addc c =
1266 let s = String.make 1 c in
1267 add s [] (Char.code c)
1269 let addcr a b =
1270 let an = Char.code a and bn = Char.code b in
1271 for i = an to bn do addc (Char.chr i) done;
1273 addcr '0' '9';
1274 addcr 'a' 'z';
1275 addcr 'A' 'Z';
1276 String.iter addc "`~!@#$%^&*()-_=+\\|[{]};:,./<>?";
1277 for i = 0 to 29 do add ("f" ^ string_of_int (i+1)) [] (0xffbe + i) done;
1278 add "space" [] 0x20;
1279 add "ret" ["return"; "enter"] 0xff0d;
1280 add "tab" [] 0xff09;
1281 add "left" [] 0xff51;
1282 add "right" [] 0xff53;
1283 add "home" [] 0xff50;
1284 add "end" [] 0xff57;
1285 add "ins" ["insert"] 0xff63;
1286 add "del" ["delete"] 0xffff;
1287 add "esc" ["escape"] 0xff1b;
1288 add "pgup" ["pageup"] 0xff55;
1289 add "pgdown" ["pagedown"] 0xff56;
1290 add "backspace" [] 0xff08;
1291 add "up" [] 0xff52;
1292 add "down" [] 0xff54;
1293 add "menu" [] 0xff67;
1294 t, f;
1297 let keyname k =
1298 try Hashtbl.find xlatf k
1299 with Not_found -> Printf.sprintf "%#x" k;
1302 let namekey name =
1303 try Hashtbl.find xlatt name
1304 with Not_found ->
1305 if String.length name = 1
1306 then Char.code name.[0]
1307 else int_of_string name;
1310 let keypadtodigitkey key =
1311 if key >= 0xffb0 && key <= 0xffb9 (* keypad numbers *)
1312 then key - 0xffb0 + 48 else key
1315 let isspecialkey key =
1316 key land 0xff00 = 0xff00 (* keyboard *)
1317 || key land 0xfe00 = 0xfe00 (* xkb *)
1318 || key land 0xfd00 = 0xfd00 (* 3270 *)
1321 let kc2pv code =
1322 if code > 31 && code < 128
1323 then `Ascii (Char.chr code)
1324 else
1325 if code >= 0xffbe && code <= 0xffc6
1326 then `Fn (code - 0xffbe + 1)
1327 else
1328 match code with
1329 | 0xff08 -> `Backspace
1330 | 0xff9f -> `Delete
1331 | 0xff54 -> `Down
1332 | 0xff0d -> `Enter
1333 | 0xff1b -> `Escape
1334 | 0xff50 -> `Home
1335 | 0xff63 -> `Insert
1336 | 0xff57 -> `End
1337 | 0xffff -> `KPdelete
1338 | 0xff99 -> `KPdown
1339 | 0xff9c -> `KPend
1340 | 0xff8d -> `KPenter
1341 | 0xff95 -> `KPhome
1342 | 0xff96 -> `KPleft
1343 | 0xffad -> `KPminus
1344 | 0xff9b -> `KPnext
1345 | 0xffab -> `KPplus
1346 | 0xff9a -> `KPprior
1347 | 0xff98 -> `KPright
1348 | 0xff97 -> `KPup
1349 | 0xff51 -> `Left
1350 | 0xff56 -> `Next
1351 | 0xff55 -> `Prior
1352 | 0xff53 -> `Right
1353 | 0xff52 -> `Up
1354 | code -> `Code code