Ignore gravity
[llpp.git] / wsi / x11 / wsi.ml
blobd6bb18e9f6c5d575244ad7aa29eddcb447b722c0
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 onot = object
34 method display = ()
35 method map _ = ()
36 method expose = ()
37 method visible _ = ()
38 method reshape _ _ = ()
39 method mouse _ _ _ _ _ = ()
40 method motion _ _ = ()
41 method pmotion _ _ = ()
42 method key _ _ = ()
43 method enter _ _ = ()
44 method leave = ()
45 method winstate _ = ()
46 method quit : 'a. 'a = exit 0
47 method scroll _ _ = ()
48 method zoom _ _ _ = ()
49 method opendoc _ = ()
50 end;;
52 class type t =
53 object
54 method display : unit
55 method map : bool -> unit
56 method expose : unit
57 method visible : visiblestate -> unit
58 method reshape : int -> int -> unit
59 method mouse : int -> bool -> int -> int -> int -> unit
60 method motion : int -> int -> unit
61 method pmotion : int -> int -> unit
62 method key : int -> int -> unit
63 method enter : int -> int -> unit
64 method leave : unit
65 method winstate : winstate list -> unit
66 method quit : 'a. 'a
67 method scroll : int -> int -> unit
68 method zoom : float -> int -> int -> unit
69 method opendoc : string -> unit
70 end;;
72 type state =
73 { mutable mink : int
74 ; mutable maxk : int
75 ; mutable keymap : int array array
76 ; fifo : (bytes -> unit) Queue.t
77 ; mutable seq : int
78 ; mutable protoatom : atom
79 ; mutable deleatom : atom
80 ; mutable nwmsatom : atom
81 ; mutable maxvatom : atom
82 ; mutable maxhatom : atom
83 ; mutable fulsatom : atom
84 ; mutable idbase : int
85 ; mutable wid : int
86 ; mutable fid : int
87 ; mutable fullscreen : (int -> unit)
88 ; mutable setwmname : (bytes -> unit)
89 ; mutable actwin : (unit -> unit)
90 ; mutable stringatom : int
91 ; mutable t : t
92 ; mutable sock : Unix.file_descr
93 ; mutable x : int
94 ; mutable y : int
95 ; mutable w : int
96 ; mutable h : int
97 ; mutable fs : fs
98 ; mutable curcurs : cursor
99 ; mutable capslmask : int
100 ; mutable numlmask : int
101 ; mutable levl3mask : int
102 ; mutable levl5mask : int
103 ; mutable xkb : bool
105 and fs =
106 | NoFs
107 | Fs of (int * int * int * int)
110 let state =
111 { mink = max_int
112 ; maxk = min_int
113 ; keymap = E.a
114 ; fifo = Queue.create ()
115 ; seq = 0
116 ; protoatom = -1
117 ; deleatom = -1
118 ; nwmsatom = -1
119 ; maxvatom = -1
120 ; maxhatom = -1
121 ; fulsatom = -1
122 ; idbase = -1
123 ; wid = -1
124 ; fid = -1
125 ; fullscreen = (fun _ -> ())
126 ; setwmname = (fun _ -> ())
127 ; actwin = (fun _ -> ())
128 ; sock = Unix.stdin
129 ; t = onot
130 ; x = -1
131 ; y = -1
132 ; w = -1
133 ; h = -1
134 ; fs = NoFs
135 ; stringatom = 31
136 ; curcurs = CURSOR_TEXT
137 ; capslmask = 0
138 ; numlmask = 0
139 ; levl3mask = 0
140 ; levl5mask = 0
141 ; xkb = false
145 let ordermagic = 'l';;
147 let makereq opcode len reqlen =
148 let s = Bytes.create len in
149 w8 s 0 opcode;
150 w16 s 2 reqlen;
154 let readstr sock n =
155 let s = Bytes.create n in
156 let rec loop pos n =
157 let m = tempfailureretry (Unix.read sock s pos) n in
158 if m = 0
159 then state.t#quit;
160 if n != m
161 then (
162 ignore (tempfailureretry (Unix.select [sock] [] []) 0.01);
163 loop (pos + m) (n - m)
166 loop 0 n;
170 let sendstr1 s pos len sock =
171 let s = Bytes.unsafe_to_string s in
172 vlog "%d <= %S" state.seq s;
173 state.seq <- state.seq + 1;
174 let n = tempfailureretry (Unix.write_substring sock s pos) len in
175 if n != len
176 then error "send %d returned %d" len n;
179 let updkmap sock resp =
180 let syms = r8 resp 1 in
181 let len = r32 resp 4 in
182 let data =
183 if len > 0
184 then readstr sock (4*len)
185 else E.b
187 let m = len / syms in
188 state.keymap <- Array.make_matrix
189 (state.maxk - state.mink) syms 0xffffff;
190 let rec loop i = if i = m then () else
191 let k = i*4*syms in
192 let rec loop2 k l = if l = syms then () else
193 let v = r32 data k in
194 state.keymap.(i).(l) <- v;
195 loop2 (k+4) (l+1)
197 loop2 k 0;
198 loop (i+1);
200 loop 0;
203 let updmodmap sock resp =
204 let n = r8 resp 1 in
205 let len = r16 resp 4 in
206 let data =
207 if len > 0
208 then readstr sock (len*4)
209 else E.b
211 if len > 0 then (*???*)
212 let modmap = Array.make_matrix 8 n 0xffffff in
213 let rec loop l =
214 if l = 8
215 then ()
216 else
217 let p = l*n in
218 let rec loop1 m =
219 if m = n then ()
220 else
221 let p = p+m in
222 let code = r8 data p in
223 modmap.(l).(m) <- code;
224 if l = 1
225 then (
226 let ki = code - state.mink in
227 if ki >= 0
228 then
229 let a = state.keymap.(ki) in
230 let rec capsloop i =
231 if i = Array.length a || i > 3
232 then ()
233 else
234 let s = a.(i) in
235 if s = 0xffe5
236 then state.capslmask <- 2
237 else capsloop (i+1)
239 capsloop 0;
241 else (
242 if l > 3
243 then (
244 let ki = code - state.mink in
245 if ki >= 0
246 then
247 let a = state.keymap.(ki) in
248 let rec lloop i =
249 if i = Array.length a || i > 3
250 then ()
251 else
252 let s = a.(i) in
253 match s with
254 | 0xfe03 -> state.levl3mask <- 1 lsl l
255 | 0xfe11 -> state.levl5mask <- 1 lsl l
256 | 0xff7f -> state.numlmask <- 1 lsl l
257 | _ -> lloop (i+1)
259 lloop 0;
262 loop1 (m+1)
264 loop1 0;
265 loop (l+1)
267 loop 0;
270 let sendwithrep sock s f =
271 Queue.push f state.fifo;
272 sendstr1 s 0 (Bytes.length s) sock;
275 let padcatl =
276 let pad = "123" in
277 fun ss ->
278 let b = Buffer.create 16 in
279 List.iter (Buffer.add_bytes b) ss;
280 let bl = Buffer.length b in
281 let pl = bl land 3 in
282 if pl != 0
283 then (
284 Buffer.add_substring b pad 0 (4 - pl);
286 Buffer.to_bytes b;
289 let padcat s1 s2 = padcatl [s1; s2];;
291 let internreq name onlyifexists =
292 let s = makereq 16 8 8 in
293 let s = padcat s name in
294 w8 s 1 (if onlyifexists then 1 else 0);
295 w16 s 2 (Bytes.length s / 4);
296 w16 s 4 (Bytes.length name);
300 let sendintern sock s onlyifexists f =
301 let s = internreq s onlyifexists in
302 sendwithrep sock s f;
305 let createwindowreq wid parent x y w h bw eventmask vid depth mid =
306 let s = makereq 1 48 12 in
307 w8 s 1 depth;
308 w32 s 4 wid;
309 w32 s 8 parent;
310 w16 s 12 x;
311 w16 s 14 y;
312 w16 s 16 w;
313 w16 s 18 h;
314 w16 s 20 bw;
315 w16 s 22 0; (* copyfromparent *)
316 w32 s 24 vid; (* visual *)
317 w32 s 28 0x280a; (* valuemask =
318 | background pixel
319 | border pixel
320 | event mask
321 | colormap *)
322 w32 s 32 0; (* background pixel *)
323 w32 s 36 0; (* border pixel*)
324 w32 s 40 eventmask;
325 w32 s 44 mid;
329 let createcolormapreq mid wid vid =
330 let s = makereq 78 16 4 in
331 w8 s 1 0;
332 w32 s 4 mid;
333 w32 s 8 wid;
334 w32 s 12 vid;
338 let getgeometryreq wid =
339 let s = makereq 14 8 2 in
340 w32 s 4 wid;
344 let mapreq wid =
345 let s = makereq 8 8 2 in
346 w32 s 4 wid;
350 let getkeymapreq first count =
351 let s = makereq 101 8 2 in
352 w8 s 4 first;
353 w8 s 5 count;
357 let changepropreq wid prop typ format props =
358 let s = makereq 18 24 0 in
359 let s = padcat s props in
360 w8 s 1 0;
361 w16 s 2 (Bytes.length s / 4);
362 w32 s 4 wid;
363 w32 s 8 prop;
364 w32 s 12 typ;
365 w8 s 16 format;
366 let ful = Bytes.length props / (match format with
367 | 8 -> 1
368 | 16 -> 2
369 | 32 -> 4
370 | n -> error "no idea what %d means" n)
372 w32 s 20 ful;
376 let getpropreq delete wid prop typ =
377 let s = makereq 20 24 6 in
378 w8 s 1 (if delete then 1 else 0);
379 w32 s 4 wid;
380 w32 s 8 prop;
381 w32 s 12 typ;
382 w32 s 16 0;
383 w32 s 20 2;
387 let configurewindowreq wid mask values =
388 let s = makereq 12 12 0 in
389 let s = padcat s values in
390 w16 s 2 (Bytes.length s / 4);
391 w32 s 4 wid;
392 w16 s 8 mask;
396 let s32 n =
397 let s = Bytes.create 4 in
398 w32 s 0 n;
402 let clientmessage format seq wid typ data =
403 let s = makereq 33 12 0 in
404 let s = padcat s data in
405 w8 s 1 format;
406 w16 s 2 seq;
407 w32 s 4 wid;
408 w32 s 8 typ;
412 let sendeventreq propagate destwid mask data =
413 let s = makereq 25 12 11 in
414 let s = padcat s data in
415 w8 s 1 propagate;
416 w16 s 2 11;
417 w32 s 4 destwid;
418 w32 s 8 mask;
422 let getmodifiermappingreq () =
423 makereq 119 4 1;
426 let queryextensionreq name =
427 let s = makereq 98 8 0 in
428 let s = padcat s name in
429 w16 s 2 (Bytes.length s / 4);
430 w16 s 4 (Bytes.length name);
434 let getkeysym pkpk code mask =
435 if (pkpk >= 0xff80 && pkpk <= 0xffbd)
436 || (pkpk >= 0x11000000 && pkpk <= 0x1100ffff)
437 then (
438 if mask land state.numlmask != 0
439 then
440 let keysym = state.keymap.(code-state.mink).(1) in
441 if keysym = 0 then pkpk else keysym
442 else pkpk
444 else (
445 let shift =
446 if pkpk land 0xf000 = 0xf000
447 then 0
448 else (mask land 1) lxor ((mask land state.capslmask) lsr 1)
450 let index =
451 if state.xkb && mask land 0x2000 != 0
452 then
453 shift + 2
454 else
455 let l3 = (mask land state.levl3mask) != 0 in
456 let l4 = (mask land state.levl5mask) != 0 in
457 shift +
458 if l3 then (if l4 then 8 else 4) else (if l4 then 6 else 0)
460 let keysym = state.keymap.(code-state.mink).(index) in
461 if index land 1 = 1 && keysym = 0
462 then state.keymap.(code-state.mink).(index - 1)
463 else keysym
467 let getkeysym code mask =
468 let pkpk = state.keymap.(code-state.mink).(0) in
469 if state.xkb && pkpk lsr 8 = 0xfe (* XKB *)
470 then 0
471 else getkeysym pkpk code mask
474 let readresp sock =
475 let resp = readstr sock 32 in
476 let opcode = r8 resp 0 in
477 match opcode land lnot 0x80 with
478 | 0 -> (* error *)
479 let s = resp in
480 let code = r8 s 1
481 and serial = r16 s 2
482 and resid = r32 resp 4
483 and min = r16 s 8
484 and maj = r8 s 10 in
485 error "code=%d serial=%d resid=%#x min=%d maj=%d\n%S"
486 code serial resid min maj (Bytes.unsafe_to_string resp);
488 | 1 -> (* response *)
489 let rep = Queue.pop state.fifo in
490 rep resp;
492 | 2 -> (* key press *)
493 if Array.length state.keymap > 0
494 then
495 let code = r8 resp 1 in
496 let mask = r16 resp 28 in
497 let keysym = getkeysym code mask in
498 vlog "keysym = %x %c mask %#x code %d"
499 keysym (Char.unsafe_chr keysym) mask code;
500 if keysym != 0
501 then state.t#key keysym mask
503 | 3 -> (* key release *)
504 if Array.length state.keymap > 0
505 then
506 let code = r8 resp 1 in
507 let mask = r16 resp 28 in
508 let keysym = getkeysym code mask in
509 vlog "release keysym = %x %c mask %#x code %#d"
510 keysym (Char.unsafe_chr keysym) mask code
512 | 4 -> (* buttonpress *)
513 let n = r8 resp 1
514 and x = r16s resp 24
515 and y = r16s resp 26
516 and m = r16 resp 28 in
517 state.t#mouse n true x y m;
518 vlog "press %d" n
520 | 5 -> (* buttonrelease *)
521 let n = r8 resp 1
522 and x = r16s resp 24
523 and y = r16s resp 26
524 and m = r16 resp 28 in
525 state.t#mouse n false x y m;
526 vlog "release %d %d %d" n x y
528 | 6 -> (* motion *)
529 let x = r16s resp 24 in
530 let y = r16s resp 26 in
531 let m = r16 resp 28 in
532 if m land 0x1f00 = 0
533 then state.t#pmotion x y
534 else state.t#motion x y;
535 vlog "move %dx%d => %d" x y m
537 | 7 -> (* enter *)
538 let x = r16s resp 24
539 and y = r16s resp 26 in
540 state.t#enter x y;
541 vlog "enter %d %d" x y
543 | 8 -> (* leave *)
544 state.t#leave
546 | 18 -> (* unmap *)
547 state.t#map false;
548 vlog "unmap";
550 | 19 -> (* map *)
551 state.t#map true;
552 vlog "map";
554 | 12 -> (* exposure *)
555 vlog "exposure";
556 state.t#expose
558 | 15 -> (* visibility *)
559 let v = r8 resp 8 in
560 let vis =
561 match v with
562 | 0 -> Unobscured
563 | 1 -> PartiallyObscured
564 | 2 -> FullyObscured
565 | _ ->
566 dolog "unknown visibility %d" v;
567 Unobscured
569 state.t#visible vis;
570 vlog "visibility %d" v;
572 | 34 -> (* mapping *)
573 state.keymap <- E.a;
574 let s = getkeymapreq state.mink (state.maxk-state.mink-1) in
575 sendwithrep sock s (updkmap sock);
576 state.capslmask <- 0;
577 state.levl3mask <- 0;
578 state.levl5mask <- 0;
579 state.numlmask <- 0;
580 let s = getmodifiermappingreq () in
581 sendwithrep sock s (updmodmap sock);
583 | 33 -> (* clientmessage *)
584 let atom = r32 resp 8 in
585 if atom = state.protoatom
586 then (
587 let atom = r32 resp 12 in
588 if atom = state.deleatom
589 then state.t#quit;
591 vlog "atom %#x" atom
593 | 21 -> (* reparent *)
594 vlog "reparent"
596 | 22 -> (* configure *)
597 let x = r16s resp 16
598 and y = r16s resp 18
599 and w = r16 resp 20
600 and h = r16 resp 22 in
601 vlog "configure cur [%d %d %d %d] conf [%d %d %d %d]"
602 state.x state.y state.w state.h
603 x y w h;
604 if w != state.w || h != state.h
605 then state.t#reshape w h;
606 state.w <- w;
607 state.h <- h;
608 state.x <- x;
609 state.y <- y;
610 state.t#expose
612 | 28 ->
613 let atom = r32 resp 8 in
614 if atom = state.nwmsatom
615 then
616 let s = getpropreq false state.wid atom 4 in
617 sendwithrep sock s (fun resp ->
618 state.fs <- NoFs;
619 let len = r32 resp 4 in
620 let nitems = r32 resp 16 in
621 let wsl =
622 if len = 0
623 then []
624 else
625 let s = readstr sock (len*4) in
626 let rec loop wsl i =
627 if i = nitems
628 then wsl
629 else
630 let atom = r32 s (i*4) in
631 let wsl =
632 if atom = state.maxhatom
633 then MaxHorz::wsl
634 else (
635 if atom = state.maxvatom
636 then MaxVert::wsl
637 else (
638 if atom = state.fulsatom
639 then (
640 state.fs <- Fs (state.x, state.y,
641 state.w, state.h);
642 Fullscreen::wsl
644 else wsl
647 in loop wsl (i+1)
649 loop [] 0
651 state.t#winstate (List.sort compare wsl)
654 | 24 ->
655 (* ignore gravity events *)
658 | n ->
659 dolog "event %d %S" n (Bytes.unsafe_to_string resp)
662 let readresp sock =
663 let rec loop () =
664 readresp sock;
665 if hasdata sock then loop ();
667 loop ();
670 let sendstr s ?(pos=0) ?(len=Bytes.length s) sock =
671 sendstr1 s pos len sock;
672 if hasdata sock then readresp sock;
675 let reshape w h =
676 if state.fs = NoFs
677 then
678 let s = Bytes.create 8 in
679 w32 s 0 w;
680 w32 s 4 h;
681 let s = configurewindowreq state.wid 0x000c s in
682 sendstr s state.sock;
683 else state.fullscreen state.wid
686 let activatewin () =
687 state.actwin ();
690 let syncsendwithrep sock secstowait s f =
691 let completed = ref false in
692 sendwithrep sock s (fun resp -> f resp; completed := true);
693 let now = Unix.gettimeofday in
694 let deadline = now () +. secstowait in
695 let rec readtillcompletion () =
696 let sf deadline =
697 let timeout = deadline -. now () in
698 if timeout <= 0.0
699 then [], [], []
700 else Unix.select [sock] [] [] timeout
702 let r, _, _ = tempfailureretry sf deadline in
703 match r with
704 | [] -> error "didn't get X response in %f seconds, aborting" secstowait
705 | _ ->
706 readresp sock;
707 if not !completed
708 then readtillcompletion ()
710 readtillcompletion ();
713 let mapwin () =
714 let s = mapreq state.wid in
715 sendstr s state.sock;
718 let syncsendintern sock secstowait s onlyifexists f =
719 let s = internreq s onlyifexists in
720 syncsendwithrep sock secstowait s f;
723 let setup disp sock rootwid screennum w h =
724 let s = readstr sock 2 in
725 let n = Bytes.length s in
726 if n != 2
727 then error "failed to read X connection setup response n=%d" n;
728 match Bytes.get s 0 with
729 | '\000' ->
730 let reasonlen = r8 s 1 in
731 let s = readstr sock 6 in
732 let maj = r16 s 0
733 and min = r16 s 2
734 and add = r16 s 4 in
735 let len = add*4 in
736 let data = readstr sock len in
737 let reason = Bytes.sub data 0 reasonlen in
738 error "X connection failed maj=%d min=%d reason=%S"
739 maj min (Bytes.unsafe_to_string reason)
741 | '\002' -> failwith "X connection setup failed: authentication required";
743 | '\001' ->
744 let s = readstr sock 38 in
745 let maj = r16 s 0
746 and min = r16 s 2
747 and add = r16 s 4
748 and idbase = r32 s 10
749 and idmask = r32 s 14
750 and vlen = r16 s 22
751 and screens = r8 s 26
752 and formats = r8 s 27
753 and minkk = r8 s 32
754 and maxkk = r8 s 33 in
755 let data = readstr sock (4*add-32) in
756 let vendor = Bytes.sub data 0 vlen in
757 let pos = ((vlen+3) land lnot 3) + formats*8 in
759 if screennum >= screens
760 then error "invalid screen %d, max %d" screennum (screens-1);
762 let pos =
763 let rec findscreen n pos =
764 if n = screennum
765 then pos
766 else
767 let pos =
768 let ndepths = r8 data (pos+39) in
769 let rec skipdepths n pos =
770 if n = ndepths
771 then pos
772 else
773 let pos =
774 let nvisiuals = r16 data (pos+2) in
775 pos + nvisiuals*24 + 8
777 skipdepths (n+1) pos
779 skipdepths n (pos+40)
781 findscreen (n+1) pos
783 findscreen 0 pos
785 let root = if rootwid = 0 then r32 data pos else rootwid in
786 let rootw = r16 data (pos+20)
787 and rooth = r16 data (pos+22)
788 and rootdepth = r8 data (pos+38)in
790 state.mink <- minkk;
791 state.maxk <- maxkk;
792 state.idbase <- idbase;
793 vlog "vendor = %S, maj=%d min=%d" (Bytes.unsafe_to_string vendor) maj min;
794 vlog "screens = %d formats = %d" screens formats;
795 vlog "minkk = %d maxkk = %d" minkk maxkk;
796 vlog "idbase = %#x idmask = %#x" idbase idmask;
797 vlog "root=%#x %dx%d" root rootw rooth;
798 vlog "wmm = %d, hmm = %d" (r16 data (pos+24)) (r16 data (pos+26));
799 vlog "visualid = %#x" (r32 data (pos+32));
800 vlog "root depth = %d" rootdepth;
802 let wid = state.idbase in
803 let mid = wid+1 in
804 let fid = mid+1 in
806 state.wid <- wid;
807 state.fid <- fid;
809 let vid = glxinit disp wid screennum in
810 let ndepths = r8 data (pos+39) in
811 let rec finddepth n' pos =
812 if n' = ndepths
813 then error "cannot find depth for visual %#x" vid;
814 let depth = r8 data pos in
815 let nvisuals = r16 data (pos+2) in
816 let rec findvisual n pos =
817 if n = nvisuals
818 then finddepth (n'+1) pos
819 else
820 let id = r32 data pos in
821 if id = vid
822 then depth
823 else findvisual (n+1) (pos+24)
825 findvisual 0 (pos+8)
827 let depth = finddepth 0 (pos+40) in
829 let s = createcolormapreq mid root vid in
830 sendstr s sock;
832 let mask = 0
833 + 0x00000001 (* KeyPress *)
834 (* + 0x00000002 *) (* KeyRelease *)
835 + 0x00000004 (* ButtonPress *)
836 + 0x00000008 (* ButtonRelease *)
837 + 0x00000010 (* EnterWindow *)
838 + 0x00000020 (* LeaveWindow *)
839 + 0x00000040 (* PointerMotion *)
840 (* + 0x00000080 *) (* PointerMotionHint *)
841 (* + 0x00000100 *) (* Button1Motion *)
842 (* + 0x00000200 *) (* Button2Motion *)
843 (* + 0x00000400 *) (* Button3Motion *)
844 (* + 0x00000800 *) (* Button4Motion *)
845 (* + 0x00001000 *) (* Button5Motion *)
846 + 0x00002000 (* ButtonMotion *)
847 (* + 0x00004000 *) (* KeymapState *)
848 + 0x00008000 (* Exposure *)
849 + 0x00010000 (* VisibilityChange *)
850 + 0x00020000 (* StructureNotify *)
851 (* + 0x00040000 *) (* ResizeRedirect *)
852 (* + 0x00080000 *) (* SubstructureNotify *)
853 (* + 0x00100000 *) (* SubstructureRedirect *)
854 (* + 0x00200000 *) (* FocusChange *)
855 + 0x00400000 (* PropertyChange *)
856 (* + 0x00800000 *) (* ColormapChange *)
857 (* + 0x01000000 *) (* OwnerGrabButton *)
860 let s = createwindowreq wid root 0 0 w h 0 mask vid depth mid in
861 sendstr s sock;
863 sendintern
864 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
876 sock (~> "WM_CLIENT_MACHINE") false (fun resp ->
877 let atom = r32 resp 8 in
878 let empty = E.s in
879 let hostname =
880 try Unix.gethostname ()
881 with exn ->
882 dolog "error getting host name: %s" @@ exntos exn;
883 empty
885 if hostname != empty
886 then
887 let s = changepropreq wid atom state.stringatom 8
888 (~> hostname) in
889 sendstr s sock;
890 sendintern
891 sock (~> "_NET_WM_PID") false (fun resp ->
892 let atom = r32 resp 8 in
893 let pid = Unix.getpid () in
894 let s = s32 pid in
895 let s = changepropreq wid atom 6(*cardinal*) 32 s in
896 sendstr s sock;
900 state.actwin <- (fun () ->
901 let s = Bytes.create 4 in
902 let s = configurewindowreq wid 0x40 s in
903 sendstr s state.sock;
904 let s = mapreq wid in
905 sendstr s state.sock;
908 sendintern
909 sock (~> "_NET_ACTIVE_WINDOW") true (fun resp ->
910 let atom = r32 resp 8 in
911 state.actwin <- (fun () ->
912 let data = Bytes.make 20 '\000' in
913 let cm = clientmessage 32 0 wid atom data in
914 let s = sendeventreq 0 root 0x180000 cm in
915 sendstr s state.sock;
919 syncsendintern
920 sock 2.0 (~> "WM_CLASS") false (fun resp ->
921 let atom = r32 resp 8 in
922 let llpp = ~> "llpp\000llpp\000" in
923 let s = changepropreq wid atom 31 8 llpp in
924 sendstr s sock;
927 let s = getkeymapreq state.mink (state.maxk-state.mink) in
928 sendwithrep sock s (updkmap sock);
930 let s = getmodifiermappingreq () in
931 sendwithrep sock s (updmodmap sock);
933 sendintern
934 sock (~> "UTF8_STRING") true (fun resp ->
935 let atom = r32 resp 8 in
936 if atom != 0
937 then state.stringatom <- atom;
940 let setwmname s =
941 let s = changepropreq wid 39 state.stringatom 8 s in
942 sendstr s state.sock;
944 state.setwmname <- setwmname;
945 sendintern
946 sock (~> "_NET_WM_NAME") true (fun resp ->
947 let atom = r32 resp 8 in
948 if atom != 0
949 then state.setwmname <- (fun s ->
950 setwmname s;
951 let s = changepropreq wid atom state.stringatom 8 s in
952 sendstr s state.sock;
956 state.fullscreen <- (fun wid ->
957 let s = Bytes.create 16 in
958 match state.fs with
959 | NoFs ->
960 w32 s 0 0;
961 w32 s 4 0;
962 w32 s 8 rootw;
963 w32 s 12 rooth;
964 let s = configurewindowreq wid 0x000f s in
965 sendstr s state.sock;
966 state.fs <- Fs (state.x, state.y, state.w, state.h);
968 | Fs (x, y, w, h) ->
969 w32 s 0 x;
970 w32 s 4 y;
971 w32 s 8 w;
972 w32 s 12 h;
973 let s = configurewindowreq wid 0x000f s in
974 sendstr s state.sock;
975 state.fs <- NoFs;
978 sendintern
979 sock (~> "_NET_WM_STATE") true (fun resp ->
980 state.nwmsatom <- r32 resp 8;
981 if state.nwmsatom != 0
982 then (
983 sendintern sock (~> "_NET_WM_STATE_MAXIMIZED_VERT") true (fun resp ->
984 state.maxvatom <- r32 resp 8;
986 sendintern sock (~> "_NET_WM_STATE_MAXIMIZED_HORZ") true (fun resp ->
987 state.maxhatom <- r32 resp 8;
989 sendintern
990 sock (~> "_NET_WM_STATE_FULLSCREEN") true (fun resp ->
991 state.fulsatom <- r32 resp 8;
992 if state.fulsatom != 0
993 then
994 state.fullscreen <-
995 (fun wid ->
996 let data = Bytes.make 20 '\000' in
997 let fs, f =
998 match state.fs with
999 | NoFs -> Fs (-1, -1, -1, -1), 1
1000 | Fs _ -> NoFs, 0
1002 w32 data 0 f;
1003 w32 data 4 state.fulsatom;
1005 let cm = clientmessage 32 0 wid state.nwmsatom data in
1006 let s = sendeventreq 0 root 0x180000 cm in
1007 sendstr s sock;
1008 state.fs <- fs;
1013 let s = queryextensionreq (~> "XKEYBOARD") in
1014 sendwithrep
1015 sock s (fun resp ->
1016 let present = r8 resp 8 in
1017 if present != 0
1018 then (
1019 let maj = r8 resp 9 in
1020 let s = Bytes.create 8 in
1021 w8 s 0 maj; (* XKB *)
1022 w8 s 1 0; (* XKBUseExtension *)
1023 w16 s 2 2; (* request-length *)
1024 w16 s 4 1; (* wantedMajor *)
1025 w16 s 6 0; (* watnedMinor *)
1026 sendwithrep
1027 sock s
1028 (fun resp ->
1029 let supported = r8 resp 1 in
1030 state.xkb <- supported != 0
1034 let s = getgeometryreq wid in
1035 syncsendwithrep sock 2.0 s (fun resp ->
1036 glxcompleteinit ();
1037 let w = r16 resp 16
1038 and h = r16 resp 18 in
1039 state.w <- w;
1040 state.h <- h;
1043 | c ->
1044 error "unknown connection setup response %d" (Char.code c)
1047 let getauth haddr dnum =
1048 let haddr =
1049 if emptystr haddr || haddr = "localhost"
1050 then
1051 try Unix.gethostname ()
1052 with exn ->
1053 dolog "failed to resolve `%S': %s" haddr @@ exntos exn;
1054 haddr
1055 else haddr
1057 let path =
1058 try Sys.getenv "XAUTHORITY"
1059 with Not_found ->
1060 try Filename.concat (Sys.getenv "HOME") ".Xauthority"
1061 with Not_found -> E.s
1063 let readauth ic =
1064 let r16be s =
1065 let rb pos = Char.code (Bytes.get s pos) in
1066 (rb 1) lor ((rb 0) lsl 8)
1068 let rec find () =
1069 let rs () =
1070 let s = really_input_string ic 2 in
1071 let n = r16be (~> s) in
1072 really_input_string ic n
1074 let family = really_input_string ic 2 in
1075 let addr = rs () in
1076 let nums = rs () in
1077 let optnum =
1078 try Some (int_of_string nums)
1079 with exn ->
1080 dolog
1081 "display number(%S) is not an integer (corrupt %S?): %s"
1082 nums path @@ exntos exn;
1083 None
1085 let name = rs () in
1086 let data = rs () in
1088 vlog "family %S addr %S(%S) num %S(%d) name %S data %S"
1089 family addr haddr nums dnum name data;
1090 match optnum with
1091 | Some num when addr = haddr && num = dnum ->
1092 name, data
1093 | _ -> find ()
1095 let name, data =
1096 try find ()
1097 with
1098 | End_of_file -> E.s, E.s
1099 | exn ->
1100 dolog "exception while reading X authority data (%S): %s"
1101 path @@ exntos exn;
1102 E.s, E.s
1104 close_in ic;
1105 name, data;
1107 if emptystr path
1108 then E.s, E.s
1109 else
1110 match open_in_bin path with
1111 | ic -> readauth ic
1112 | exception exn ->
1113 dolog "failed to open X authority file `%S' : %s" path @@ exntos exn;
1114 E.s, E.s
1117 let init t rootwid w h platform =
1118 let d =
1119 try Sys.getenv "DISPLAY"
1120 with exn ->
1121 error "cannot get DISPLAY evironment variable: %s" @@ exntos exn
1123 let getnum w b e =
1124 if b = e
1125 then error "invalid DISPLAY(%s) %S" w d
1126 else
1127 let s = String.sub d b (e - b) in
1128 try int_of_string s
1129 with exn ->
1130 error "invalid DISPLAY %S can not parse %s(%S): %s"
1131 d w s @@ exntos exn
1133 let rec phost pos =
1134 if pos = String.length d
1135 then error "invalid DISPLAY %S no display number specified" d
1136 else (
1137 if d.[pos] = ':'
1138 then
1139 let rec pdispnum pos1 =
1140 if pos1 = String.length d
1141 then getnum "display number" (pos+1) pos1, 0
1142 else
1143 match d.[pos1] with
1144 | '.' ->
1145 let dispnum = getnum "display number" (pos+1) pos1 in
1146 let rec pscreennum pos2 =
1147 if pos2 = String.length d
1148 then getnum "screen number" (pos1+1) pos2
1149 else
1150 match d.[pos2] with
1151 | '0' .. '9' -> pscreennum (pos2+1)
1152 | _ ->
1153 error "invalid DISPLAY %S, cannot parse screen number" d
1155 dispnum, pscreennum (pos1+1)
1156 | '0' .. '9' -> pdispnum (pos1+1)
1157 | _ ->
1158 error "invalid DISPLAY %S, cannot parse display number" d
1160 String.sub d 0 pos, pdispnum (pos+1)
1161 else phost (pos+1)
1164 let host, (dispnum, screennum) = phost 0 in
1165 let aname, adata = getauth host dispnum in
1166 let fd =
1167 let fd, addr =
1168 if emptystr host || host.[0] = '/' || host = "unix"
1169 then
1170 let addr =
1171 match platform with
1172 | Utils.Posx -> Unix.ADDR_UNIX d
1173 | Utils.Plinux ->
1174 Unix.ADDR_UNIX ("\000/tmp/.X11-unix/X" ^ string_of_int dispnum)
1175 | Utils.Punknown | Utils.Psun | Utils.Pbsd | Utils.Pcygwin ->
1176 Unix.ADDR_UNIX ("/tmp/.X11-unix/X" ^ string_of_int dispnum)
1178 Unix.socket Unix.PF_UNIX Unix.SOCK_STREAM 0, addr
1179 else
1180 let h =
1181 try Unix.gethostbyname host
1182 with exn -> error "cannot resolve %S: %s" host @@ exntos exn
1184 let addr = h.Unix.h_addr_list.(0) in
1185 let port = 6000 + dispnum in
1186 let fd = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
1187 fd, (Unix.ADDR_INET (addr, port))
1189 try Unix.connect fd addr; fd
1190 with exn -> error "failed to connect to X: %s" @@ exntos exn
1192 cloexec fd;
1193 let s = Bytes.create 12 in
1194 let s = padcat s (~> aname) in
1195 let s = padcat s (~> adata) in
1196 Bytes.set s 0 ordermagic;
1197 w16 s 2 11;
1198 w16 s 4 0;
1199 w16 s 6 (String.length aname);
1200 w16 s 8 (String.length adata);
1201 sendstr1 s 0 (Bytes.length s) fd;
1202 state.sock <- fd;
1203 setup d fd rootwid screennum w h;
1204 state.t <- t;
1205 fd, state.w, state.h;
1208 let settitle s =
1209 state.setwmname (~> s);
1212 let setcursor cursor =
1213 if cursor != state.curcurs
1214 then (
1215 setcursor cursor;
1216 state.curcurs <- cursor;
1220 let fullscreen () =
1221 state.fullscreen state.wid;
1224 let metamask = 0x40;;
1225 let altmask = 8;;
1226 let shiftmask = 1;;
1227 let ctrlmask = 4;;
1229 let withalt mask = mask land altmask != 0;;
1230 let withctrl mask = mask land ctrlmask != 0;;
1231 let withshift mask = mask land shiftmask != 0;;
1232 let withmeta mask = mask land metamask != 0;;
1233 let withnone mask = mask land (altmask + ctrlmask + shiftmask + metamask) = 0;;
1235 let xlatt, xlatf =
1236 let t = Hashtbl.create 20
1237 and f = Hashtbl.create 20 in
1238 let add n nl k =
1239 List.iter (fun s -> Hashtbl.add t s k) (n::nl);
1240 Hashtbl.add f k n
1242 let addc c =
1243 let s = String.make 1 c in
1244 add s [] (Char.code c)
1246 let addcr a b =
1247 let an = Char.code a and bn = Char.code b in
1248 for i = an to bn do addc (Char.chr i) done;
1250 addcr '0' '9';
1251 addcr 'a' 'z';
1252 addcr 'A' 'Z';
1253 String.iter addc "`~!@#$%^&*()-_=+\\|[{]};:,./<>?";
1254 for i = 0 to 29 do add ("f" ^ string_of_int (i+1)) [] (0xffbe + i) done;
1255 add "space" [] 0x20;
1256 add "ret" ["return"; "enter"] 0xff0d;
1257 add "tab" [] 0xff09;
1258 add "left" [] 0xff51;
1259 add "right" [] 0xff53;
1260 add "home" [] 0xff50;
1261 add "end" [] 0xff57;
1262 add "ins" ["insert"] 0xff63;
1263 add "del" ["delete"] 0xffff;
1264 add "esc" ["escape"] 0xff1b;
1265 add "pgup" ["pageup"] 0xff55;
1266 add "pgdown" ["pagedown"] 0xff56;
1267 add "backspace" [] 0xff08;
1268 add "up" [] 0xff52;
1269 add "down" [] 0xff54;
1270 add "menu" [] 0xff67;
1271 t, f;
1274 let keyname k =
1275 try Hashtbl.find xlatf k
1276 with Not_found -> Printf.sprintf "%#x" k;
1279 let namekey name =
1280 try Hashtbl.find xlatt name
1281 with Not_found ->
1282 if String.length name = 1
1283 then Char.code name.[0]
1284 else int_of_string name;
1287 let kc2kt =
1288 let open Keys in
1289 function
1290 | 0xff08 -> Backspace
1291 | 0xff0d -> Enter
1292 | 0xff1b -> Escape
1293 | 0xff50 -> Home
1294 | 0xff51 -> Left
1295 | 0xff52 -> Up
1296 | 0xff53 -> Right
1297 | 0xff54 -> Down
1298 | 0xff55 -> Prior
1299 | 0xff56 -> Next
1300 | 0xff57 -> End
1301 | 0xff63 -> Insert
1302 | 0xff8d -> Enter
1303 | 0xff95 -> Home
1304 | 0xff96 -> Left
1305 | 0xff97 -> Up
1306 | 0xff98 -> Right
1307 | 0xff99 -> Down
1308 | 0xff9a -> Prior
1309 | 0xff9b -> Next
1310 | 0xff9c -> End
1311 | 0xff9f -> Delete
1312 | 0xffab -> Ascii '+'
1313 | 0xffad -> Ascii '-'
1314 | 0xffff -> Delete
1315 | code when code > 31 && code < 128 -> Ascii (Char.unsafe_chr code)
1316 | code when code >= 0xffb0 && code <= 0xffb9 ->
1317 Ascii (Char.unsafe_chr (code - 0xffb0 + 0x30))
1318 | code when code >= 0xffbe && code <= 0xffc8 -> Fn (code - 0xffbe + 1)
1319 | code when code land 0xff00 = 0xff00 -> Ctrl code
1320 | code -> Code code
1323 let fontsizefactor () = 1