Consistency
[llpp.git] / wsi.ml
blobaa72e3dd5b49b55aa8e03d95d243bd807642b73c
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";;
32 let vlog fmt = Format.ksprintf ignore fmt;;
34 let onot = object
35 method display = ()
36 method map _ = ()
37 method expose = ()
38 method visible _ = ()
39 method reshape _ _ = ()
40 method mouse _ _ _ _ _ = ()
41 method motion _ _ = ()
42 method pmotion _ _ = ()
43 method key _ _ = ()
44 method enter _ _ = ()
45 method leave = ()
46 method winstate _ = ()
47 method quit = exit 0
48 end;;
50 class type t = object
51 method display : unit
52 method map : bool -> unit
53 method expose : unit
54 method visible : visiblestate -> unit
55 method reshape : int -> int -> unit
56 method mouse : int -> bool -> int -> int -> int -> unit
57 method motion : int -> int -> unit
58 method pmotion : int -> int -> unit
59 method key : int -> int -> unit
60 method enter : int -> int -> unit
61 method leave : unit
62 method winstate : winstate list -> unit
63 method quit : unit
64 end;;
66 type state =
67 { mutable mink : int
68 ; mutable maxk : int
69 ; mutable keymap : int array array
70 ; fifo : (bytes -> unit) Queue.t
71 ; mutable seq : int
72 ; mutable protoatom : atom
73 ; mutable deleatom : atom
74 ; mutable nwmsatom : atom
75 ; mutable maxvatom : atom
76 ; mutable maxhatom : atom
77 ; mutable fulsatom : atom
78 ; mutable idbase : int
79 ; mutable wid : int
80 ; mutable fid : int
81 ; mutable fullscreen : (int -> unit)
82 ; mutable setwmname : (bytes -> unit)
83 ; mutable actwin : (unit -> unit)
84 ; mutable stringatom : int
85 ; mutable t : t
86 ; mutable sock : Unix.file_descr
87 ; mutable x : int
88 ; mutable y : int
89 ; mutable w : int
90 ; mutable h : int
91 ; mutable fs : fs
92 ; mutable curcurs : cursor
93 ; mutable capslmask : int
94 ; mutable numlmask : int
95 ; mutable levl3mask : int
96 ; mutable levl5mask : int
97 ; mutable xkb : bool
99 and fs =
100 | NoFs
101 | Fs of (int * int * int * int)
104 let state =
105 { mink = max_int
106 ; maxk = min_int
107 ; keymap = E.a
108 ; fifo = Queue.create ()
109 ; seq = 0
110 ; protoatom = -1
111 ; deleatom = -1
112 ; nwmsatom = -1
113 ; maxvatom = -1
114 ; maxhatom = -1
115 ; fulsatom = -1
116 ; idbase = -1
117 ; wid = -1
118 ; fid = -1
119 ; fullscreen = (fun _ -> ())
120 ; setwmname = (fun _ -> ())
121 ; actwin = (fun _ -> ())
122 ; sock = Unix.stdin
123 ; t = onot
124 ; x = -1
125 ; y = -1
126 ; w = -1
127 ; h = -1
128 ; fs = NoFs
129 ; stringatom = 31
130 ; curcurs = CURSOR_INHERIT
131 ; capslmask = 0
132 ; numlmask = 0
133 ; levl3mask = 0
134 ; levl5mask = 0
135 ; xkb = false
139 include Bo;;
141 let makereq opcode len reqlen =
142 let s = Bytes.create len in
143 w8 s 0 opcode;
144 w16 s 2 reqlen;
148 let recv fd s pos len =
149 Unix.read fd s pos len;
152 let readstr sock n =
153 let s = Bytes.create n in
154 let rec loop pos n =
155 let m = tempfailureretry (recv sock s pos) n in
156 if m = 0
157 then state.t#quit;
158 if n != m
159 then (
160 ignore (tempfailureretry (Unix.select [sock] [] []) 0.01);
161 loop (pos + m) (n - m)
164 loop 0 n;
168 let sendstr1 s pos len sock =
169 let s = Bytes.unsafe_to_string s in
170 vlog "%d <= %S" state.seq s;
171 state.seq <- state.seq + 1;
172 let n = tempfailureretry (Unix.write_substring sock s pos) len in
173 if n != len
174 then error "send %d returned %d" len n;
177 let updkmap sock resp =
178 let syms = r8 resp 1 in
179 let len = r32 resp 4 in
180 let data =
181 if len > 0
182 then readstr sock (4*len)
183 else E.b
185 let m = len / syms in
186 state.keymap <- Array.make_matrix
187 (state.maxk - state.mink) syms 0xffffff;
188 let rec loop i = if i = m then () else
189 let k = i*4*syms in
190 let rec loop2 k l = if l = syms then () else
191 let v = r32 data k in
192 state.keymap.(i).(l) <- v;
193 loop2 (k+4) (l+1)
195 loop2 k 0;
196 loop (i+1);
198 loop 0;
201 let updmodmap sock resp =
202 let n = r8 resp 1 in
203 let len = r16 resp 4 in
204 let data =
205 if len > 0
206 then readstr sock (len*4)
207 else E.b
209 if len > 0 then (*???*)
210 let modmap = Array.make_matrix 8 n 0xffffff in
211 let rec loop l = if l = 8 then () else
212 let p = l*n in
213 let rec loop1 m = if m = n then () else
214 let p = p+m in
215 let code = r8 data p in
216 modmap.(l).(m) <- code;
217 if l = 1
218 then (
219 let ki = code - state.mink in
220 if ki >= 0
221 then
222 let a = state.keymap.(ki) in
223 let rec capsloop i = if i = Array.length a || i > 3 then () else
224 let s = a.(i) in
225 if s = 0xffe5
226 then state.capslmask <- 2
227 else capsloop (i+1)
229 capsloop 0;
231 else (
232 if l > 3
233 then (
234 let ki = code - state.mink in
235 if ki >= 0
236 then
237 let a = state.keymap.(ki) in
238 let rec lloop i = if i = Array.length a || i > 3 then () else
239 let s = a.(i) in
240 match s with
241 | 0xfe03 -> state.levl3mask <- 1 lsl l
242 | 0xfe11 -> state.levl5mask <- 1 lsl l
243 | 0xff7f -> state.numlmask <- 1 lsl l
244 | _ -> lloop (i+1)
246 lloop 0;
249 loop1 (m+1)
251 loop1 0;
252 loop (l+1)
254 loop 0;
257 let sendwithrep sock s f =
258 Queue.push f state.fifo;
259 sendstr1 s 0 (Bytes.length s) sock;
262 let padcatl =
263 let pad = "123" in
264 fun ss ->
265 let b = Buffer.create 16 in
266 List.iter (Buffer.add_bytes b) ss;
267 let bl = Buffer.length b in
268 let pl = bl land 3 in
269 if pl != 0
270 then (
271 Buffer.add_substring b pad 0 (4 - pl);
273 Buffer.to_bytes b;
276 let padcat s1 s2 = padcatl [s1; s2];;
278 let internreq name onlyifexists =
279 let s = makereq 16 8 8 in
280 let s = padcat s name in
281 w8 s 1 (if onlyifexists then 1 else 0);
282 w16 s 2 (Bytes.length s / 4);
283 w16 s 4 (Bytes.length name);
287 let sendintern sock s onlyifexists f =
288 let s = internreq s onlyifexists in
289 sendwithrep sock s f;
292 let createwindowreq wid parent x y w h bw eventmask vid depth mid =
293 let s = makereq 1 48 12 in
294 w8 s 1 depth;
295 w32 s 4 wid;
296 w32 s 8 parent;
297 w16 s 12 x;
298 w16 s 14 y;
299 w16 s 16 w;
300 w16 s 18 h;
301 w16 s 20 bw;
302 w16 s 22 0; (* copyfromparent *)
303 w32 s 24 vid; (* visual *)
304 w32 s 28 0x280a; (* valuemask =
305 | background pixel
306 | border pixel
307 | event mask
308 | colormap *)
309 w32 s 32 0; (* background pixel *)
310 w32 s 36 0; (* border pixel*)
311 w32 s 40 eventmask;
312 w32 s 44 mid;
316 let createcolormapreq mid wid vid =
317 let s = makereq 78 16 4 in
318 w8 s 1 0;
319 w32 s 4 mid;
320 w32 s 8 wid;
321 w32 s 12 vid;
325 let getgeometryreq wid =
326 let s = makereq 14 8 2 in
327 w32 s 4 wid;
331 let mapreq wid =
332 let s = makereq 8 8 2 in
333 w32 s 4 wid;
337 let getkeymapreq first count =
338 let s = makereq 101 8 2 in
339 w8 s 4 first;
340 w8 s 5 count;
344 let changepropreq wid prop typ format props =
345 let s = makereq 18 24 0 in
346 let s = padcat s props in
347 w8 s 1 0;
348 w16 s 2 (Bytes.length s / 4);
349 w32 s 4 wid;
350 w32 s 8 prop;
351 w32 s 12 typ;
352 w8 s 16 format;
353 let ful = Bytes.length props / (match format with
354 | 8 -> 1
355 | 16 -> 2
356 | 32 -> 4
357 | n -> error "no idea what %d means" n)
359 w32 s 20 ful;
363 let getpropreq delete wid prop typ =
364 let s = makereq 20 24 6 in
365 w8 s 1 (if delete then 1 else 0);
366 w32 s 4 wid;
367 w32 s 8 prop;
368 w32 s 12 typ;
369 w32 s 16 0;
370 w32 s 20 2;
374 let configurewindowreq wid mask values =
375 let s = makereq 12 12 0 in
376 let s = padcat s values in
377 w16 s 2 (Bytes.length s / 4);
378 w32 s 4 wid;
379 w16 s 8 mask;
383 let s32 n =
384 let s = Bytes.create 4 in
385 w32 s 0 n;
389 let clientmessage format seq wid typ data =
390 let s = makereq 33 12 0 in
391 let s = padcat s data in
392 w8 s 1 format;
393 w16 s 2 seq;
394 w32 s 4 wid;
395 w32 s 8 typ;
399 let sendeventreq propagate destwid mask data =
400 let s = makereq 25 12 11 in
401 let s = padcat s data in
402 w8 s 1 propagate;
403 w16 s 2 11;
404 w32 s 4 destwid;
405 w32 s 8 mask;
409 let getmodifiermappingreq () =
410 makereq 119 4 1;
413 let queryextensionreq name =
414 let s = makereq 98 8 0 in
415 let s = padcat s name in
416 w16 s 2 (Bytes.length s / 4);
417 w16 s 4 (Bytes.length name);
421 let getkeysym pkpk code mask =
422 if (pkpk >= 0xff80 && pkpk <= 0xffbd)
423 || (pkpk >= 0x11000000 && pkpk <= 0x1100ffff)
424 then (
425 if mask land state.numlmask != 0
426 then
427 let keysym = state.keymap.(code-state.mink).(1) in
428 if keysym = 0 then pkpk else keysym
429 else pkpk
431 else (
432 let shift =
433 if pkpk land 0xf000 = 0xf000
434 then 0
435 else (mask land 1) lxor ((mask land state.capslmask) lsr 1)
437 let index =
438 if state.xkb && mask land 0x2000 != 0
439 then
440 shift + 2
441 else
442 let l3 = (mask land state.levl3mask) != 0 in
443 let l4 = (mask land state.levl5mask) != 0 in
444 shift +
445 if l3 then (if l4 then 8 else 4) else (if l4 then 6 else 0)
447 let keysym = state.keymap.(code-state.mink).(index) in
448 if index land 1 = 1 && keysym = 0
449 then state.keymap.(code-state.mink).(index - 1)
450 else keysym
454 let getkeysym code mask =
455 let pkpk = state.keymap.(code-state.mink).(0) in
456 if state.xkb && pkpk lsr 8 = 0xfe (* XKB *)
457 then 0
458 else getkeysym pkpk code mask
461 let readresp sock =
462 let resp = readstr sock 32 in
463 let opcode = r8 resp 0 in
464 match opcode land lnot 0x80 with
465 | 0 -> (* error *)
466 let s = resp in
467 let code = r8 s 1
468 and serial = r16 s 2
469 and resid = r32 resp 4
470 and min = r16 s 8
471 and maj = r8 s 10 in
472 error "code=%d serial=%d resid=%#x min=%d maj=%d\n%S"
473 code serial resid min maj (Bytes.unsafe_to_string resp);
475 | 1 -> (* response *)
476 let rep = Queue.pop state.fifo in
477 rep resp;
479 | 2 -> (* key press *)
480 if Array.length state.keymap > 0
481 then
482 let code = r8 resp 1 in
483 let mask = r16 resp 28 in
484 let keysym = getkeysym code mask in
485 vlog "keysym = %x %c mask %#x code %d"
486 keysym (Char.unsafe_chr keysym) mask code;
487 state.t#key keysym mask;
489 | 3 -> (* key release *)
490 if Array.length state.keymap > 0
491 then
492 let code = r8 resp 1 in
493 let mask = r16 resp 28 in
494 let keysym = getkeysym code mask in
495 vlog "release keysym = %x %c mask %#x code %#d"
496 keysym (Char.unsafe_chr keysym) mask code
498 | 4 -> (* buttonpress *)
499 let n = r8 resp 1
500 and x = r16s resp 24
501 and y = r16s resp 26
502 and m = r16 resp 28 in
503 state.t#mouse n true x y m;
504 vlog "press %d" n
506 | 5 -> (* buttonrelease *)
507 let n = r8 resp 1
508 and x = r16s resp 24
509 and y = r16s resp 26
510 and m = r16 resp 28 in
511 state.t#mouse n false x y m;
512 vlog "release %d %d %d" n x y
514 | 6 -> (* motion *)
515 let x = r16s resp 24 in
516 let y = r16s resp 26 in
517 let m = r16 resp 28 in
518 if m land 0x1f00 = 0
519 then state.t#pmotion x y
520 else state.t#motion x y;
521 vlog "move %dx%d => %d" x y m
523 | 7 -> (* enter *)
524 let x = r16s resp 24
525 and y = r16s resp 26 in
526 state.t#enter x y;
527 vlog "enter %d %d" x y
529 | 8 -> (* leave *)
530 state.t#leave
532 | 18 -> (* unmap *)
533 state.t#map false;
534 vlog "unmap";
536 | 19 -> (* map *)
537 state.t#map true;
538 vlog "map";
540 | 12 -> (* exposure *)
541 vlog "exposure";
542 state.t#expose
544 | 15 -> (* visibility *)
545 let v = r8 resp 8 in
546 let vis =
547 match v with
548 | 0 -> Unobscured
549 | 1 -> PartiallyObscured
550 | 2 -> FullyObscured
551 | _ ->
552 dolog "unknown visibility %d" v;
553 Unobscured
555 state.t#visible vis;
556 vlog "visibility %d" v;
558 | 34 -> (* mapping *)
559 state.keymap <- E.a;
560 let s = getkeymapreq state.mink (state.maxk-state.mink-1) in
561 sendwithrep sock s (updkmap sock);
562 state.capslmask <- 0;
563 state.levl3mask <- 0;
564 state.levl5mask <- 0;
565 state.numlmask <- 0;
566 let s = getmodifiermappingreq () in
567 sendwithrep sock s (updmodmap sock);
569 | 33 -> (* clientmessage *)
570 let atom = r32 resp 8 in
571 if atom = state.protoatom
572 then (
573 let atom = r32 resp 12 in
574 if atom = state.deleatom
575 then state.t#quit;
577 vlog "atom %#x" atom
579 | 21 -> (* reparent *)
580 vlog "reparent"
582 | 22 -> (* configure *)
583 let x = r16s resp 16
584 and y = r16s resp 18
585 and w = r16 resp 20
586 and h = r16 resp 22 in
587 vlog "configure cur [%d %d %d %d] conf [%d %d %d %d]"
588 state.x state.y state.w state.h
589 x y w h
591 if w != state.w || h != state.h
592 then (
593 state.t#reshape w h;
595 state.w <- w;
596 state.h <- h;
597 state.x <- x;
598 state.y <- y;
599 state.t#expose
601 | 28 ->
602 let atom = r32 resp 8 in
603 if atom = state.nwmsatom
604 then
605 let s = getpropreq false state.wid atom 4 in
606 sendwithrep sock s (fun resp ->
607 let len = r32 resp 4 in
608 let nitems = r32 resp 16 in
609 let wsl =
610 if len = 0
611 then []
612 else
613 let s = readstr sock (len*4) in
614 let rec loop wsl i = if i = nitems then wsl else
615 let atom = r32 s (i*4) in
616 let wsl =
617 if atom = state.maxhatom
618 then MaxHorz::wsl
619 else (
620 if atom = state.maxvatom
621 then MaxVert::wsl
622 else (
623 if atom = state.fulsatom
624 then (
625 state.fs <- Fs (state.x, state.y, state.w, state.h);
626 Fullscreen::wsl
628 else wsl
631 in loop wsl (i+1)
633 loop [] 0
635 state.t#winstate (List.sort compare wsl)
638 | n ->
639 dolog "event %d %S" n (Bytes.unsafe_to_string resp)
642 let readresp sock =
643 let rec loop () =
644 readresp sock;
645 if hasdata sock then loop ();
647 loop ();
650 let sendstr s ?(pos=0) ?(len=Bytes.length s) sock =
651 sendstr1 s pos len sock;
652 if hasdata sock then readresp sock;
655 let reshape w h =
656 if state.fs = NoFs
657 then
658 let s = Bytes.create 8 in
659 w32 s 0 w;
660 w32 s 4 h;
661 let s = configurewindowreq state.wid 0x000c s in
662 sendstr s state.sock;
663 else state.fullscreen state.wid
666 let activatewin () =
667 state.actwin ();
670 let syncsendwithrep sock secstowait s f =
671 let completed = ref false in
672 sendwithrep sock s (fun resp -> f resp; completed := true);
673 let now = Unix.gettimeofday in
674 let deadline = now () +. secstowait in
675 let rec readtillcompletion () =
676 let sf deadline =
677 let timeout = deadline -. now () in
678 if timeout <= 0.0
679 then [], [], []
680 else Unix.select [sock] [] [] timeout
682 let r, _, _ = tempfailureretry sf deadline in
683 match r with
684 | [] -> error "didn't get X response in %f seconds, aborting" secstowait
685 | _ ->
686 readresp sock;
687 if not !completed
688 then readtillcompletion ()
690 readtillcompletion ();
693 let mapwin () =
694 let s = mapreq state.wid in
695 sendstr s state.sock;
698 let syncsendintern sock secstowait s onlyifexists f =
699 let s = internreq s onlyifexists in
700 syncsendwithrep sock secstowait s f;
703 let setup disp sock rootwid screennum w h =
704 let s = readstr sock 2 in
705 let n = Bytes.length s in
706 if n != 2
707 then error "failed to read X connection setup response n=%d" n;
708 match Bytes.get s 0 with
709 | '\000' ->
710 let reasonlen = r8 s 1 in
711 let s = readstr sock 6 in
712 let maj = r16 s 0
713 and min = r16 s 2
714 and add = r16 s 4 in
715 let len = add*4 in
716 let data = readstr sock len in
717 let reason = Bytes.sub data 0 reasonlen in
718 error "X connection failed maj=%d min=%d reason=%S"
719 maj min (Bytes.unsafe_to_string reason)
721 | '\002' -> failwith "X connection setup failed: authentication required";
723 | '\001' ->
724 let s = readstr sock 38 in
725 let maj = r16 s 0
726 and min = r16 s 2
727 and add = r16 s 4
728 and idbase = r32 s 10
729 and idmask = r32 s 14
730 and vlen = r16 s 22
731 and screens = r8 s 26
732 and formats = r8 s 27
733 and minkk = r8 s 32
734 and maxkk = r8 s 33 in
735 let data = readstr sock (4*add-32) in
736 let vendor = Bytes.sub data 0 vlen in
737 let pos = ((vlen+3) land lnot 3) + formats*8 in
739 if screennum >= screens
740 then error "invalid screen %d, max %d" screennum (screens-1);
742 let pos =
743 let rec findscreen n pos = if n = screennum then pos else
744 let pos =
745 let ndepths = r8 data (pos+39) in
746 let rec skipdepths n pos = if n = ndepths then pos else
747 let pos =
748 let nvisiuals = r16 data (pos+2) in
749 pos + nvisiuals*24 + 8
751 skipdepths (n+1) pos
753 skipdepths n (pos+40)
755 findscreen (n+1) pos
757 findscreen 0 pos
759 let root = if rootwid = 0 then r32 data pos else rootwid in
760 let rootw = r16 data (pos+20)
761 and rooth = r16 data (pos+22)
762 and rootdepth = r8 data (pos+38)in
764 state.mink <- minkk;
765 state.maxk <- maxkk;
766 state.idbase <- idbase;
767 vlog "vendor = %S, maj=%d min=%d" (Bytes.unsafe_to_string vendor) maj min;
768 vlog "screens = %d formats = %d" screens formats;
769 vlog "minkk = %d maxkk = %d" minkk maxkk;
770 vlog "idbase = %#x idmask = %#x" idbase idmask;
771 vlog "root=%#x %dx%d" root rootw rooth;
772 vlog "wmm = %d, hmm = %d" (r16 data (pos+24)) (r16 data (pos+26));
773 vlog "visualid = %#x " (r32 data (pos+32));
774 vlog "root depth = %d" rootdepth;
776 let wid = state.idbase in
777 let mid = wid+1 in
778 let fid = mid+1 in
780 state.wid <- wid;
781 state.fid <- fid;
783 let vid = glxinit disp wid screennum in
784 let ndepths = r8 data (pos+39) in
785 let rec finddepth n' pos =
786 if n' = ndepths
787 then error "couldn't find depth for visual %#x" vid;
788 let depth = r8 data pos in
789 let nvisuals = r16 data (pos+2) in
790 let rec findvisual n pos =
791 if n = nvisuals
792 then finddepth (n'+1) pos
793 else
794 let id = r32 data pos in
795 if id = vid
796 then depth
797 else findvisual (n+1) (pos+24)
799 findvisual 0 (pos+8)
801 let depth = finddepth 0 (pos+40) in
803 let s = createcolormapreq mid root vid in
804 sendstr s sock;
806 let mask = 0
807 + 0x00000001 (* KeyPress *)
808 (* + 0x00000002 *) (* KeyRelease *)
809 + 0x00000004 (* ButtonPress *)
810 + 0x00000008 (* ButtonRelease *)
811 + 0x00000010 (* EnterWindow *)
812 + 0x00000020 (* LeaveWindow *)
813 + 0x00000040 (* PointerMotion *)
814 (* + 0x00000080 *) (* PointerMotionHint *)
815 (* + 0x00000100 *) (* Button1Motion *)
816 (* + 0x00000200 *) (* Button2Motion *)
817 (* + 0x00000400 *) (* Button3Motion *)
818 (* + 0x00000800 *) (* Button4Motion *)
819 (* + 0x00001000 *) (* Button5Motion *)
820 + 0x00002000 (* ButtonMotion *)
821 (* + 0x00004000 *) (* KeymapState *)
822 + 0x00008000 (* Exposure *)
823 + 0x00010000 (* VisibilityChange *)
824 + 0x00020000 (* StructureNotify *)
825 (* + 0x00040000 *) (* ResizeRedirect *)
826 (* + 0x00080000 *) (* SubstructureNotify *)
827 (* + 0x00100000 *) (* SubstructureRedirect *)
828 (* + 0x00200000 *) (* FocusChange *)
829 + 0x00400000 (* PropertyChange *)
830 (* + 0x00800000 *) (* ColormapChange *)
831 (* + 0x01000000 *) (* OwnerGrabButton *)
834 let s = createwindowreq wid root 0 0 w h 0 mask vid depth mid in
835 sendstr s sock;
837 sendintern sock (~> "WM_PROTOCOLS") false (fun resp ->
838 state.protoatom <- r32 resp 8;
839 sendintern
840 sock (~> "WM_DELETE_WINDOW") false (fun resp ->
841 state.deleatom <- r32 resp 8;
842 let s = s32 state.deleatom in
843 let s = changepropreq wid state.protoatom 4 32 s in
844 sendstr s sock;
848 sendintern sock (~> "WM_CLIENT_MACHINE") false (fun resp ->
849 let atom = r32 resp 8 in
850 let empty = E.s in
851 let hostname =
852 try Unix.gethostname ()
853 with exn ->
854 dolog "error getting host name: %s" (exntos exn);
855 empty
857 if hostname != empty
858 then
859 let s = changepropreq wid atom state.stringatom 8
860 (~> hostname) in
861 sendstr s sock;
862 sendintern sock (~> "_NET_WM_PID") false (fun resp ->
863 let atom = r32 resp 8 in
864 let pid = Unix.getpid () in
865 let s = s32 pid in
866 let s = changepropreq wid atom 6(*cardinal*) 32 s in
867 sendstr s sock;
871 state.actwin <- (fun () ->
872 let s = Bytes.create 4 in
873 let s = configurewindowreq wid 0x40 s in
874 sendstr s state.sock;
875 let s = mapreq wid in
876 sendstr s state.sock;
879 sendintern sock (~> "_NET_ACTIVE_WINDOW") true (fun resp ->
880 let atom = r32 resp 8 in
881 state.actwin <- (fun () ->
882 let data = Bytes.make 20 '\000' in
883 let cm = clientmessage 32 0 wid atom data in
884 let s = sendeventreq 0 root 0x180000 cm in
885 sendstr s state.sock;
889 syncsendintern sock 2.0 (~> "WM_CLASS") false (fun resp ->
890 let atom = r32 resp 8 in
891 let llpp = ~> "llpp\000llpp\000" in
892 let s = changepropreq wid atom 31 8 llpp in
893 sendstr s sock;
896 let s = getkeymapreq state.mink (state.maxk-state.mink) in
897 sendwithrep sock s (updkmap sock);
899 let s = getmodifiermappingreq () in
900 sendwithrep sock s (updmodmap sock);
902 sendintern sock (~> "UTF8_STRING") true (fun resp ->
903 let atom = r32 resp 8 in
904 if atom != 0
905 then state.stringatom <- atom;
908 let setwmname s =
909 let s = changepropreq wid 39 state.stringatom 8 s in
910 sendstr s state.sock;
912 state.setwmname <- setwmname;
913 sendintern sock (~> "_NET_WM_NAME") true (fun resp ->
914 let atom = r32 resp 8 in
915 if atom != 0
916 then state.setwmname <- (fun s ->
917 setwmname s;
918 let s = changepropreq wid atom state.stringatom 8 s in
919 sendstr s state.sock;
923 state.fullscreen <- (fun wid ->
924 let s = Bytes.create 16 in
925 match state.fs with
926 | NoFs ->
927 w32 s 0 0;
928 w32 s 4 0;
929 w32 s 8 rootw;
930 w32 s 12 rooth;
931 let s = configurewindowreq wid 0x000f s in
932 sendstr s state.sock;
933 state.fs <- Fs (state.x, state.y, state.w, state.h);
935 | Fs (x, y, w, h) ->
936 w32 s 0 x;
937 w32 s 4 y;
938 w32 s 8 w;
939 w32 s 12 h;
940 let s = configurewindowreq wid 0x000f s in
941 sendstr s state.sock;
942 state.fs <- NoFs;
945 sendintern sock (~> "_NET_WM_STATE") true (fun resp ->
946 state.nwmsatom <- r32 resp 8;
947 if state.nwmsatom != 0
948 then (
949 sendintern sock (~> "_NET_WM_STATE_MAXIMIZED_VERT") true (fun resp ->
950 state.maxvatom <- r32 resp 8;
952 sendintern sock (~> "_NET_WM_STATE_MAXIMIZED_HORZ") true (fun resp ->
953 state.maxhatom <- r32 resp 8;
955 sendintern sock (~> "_NET_WM_STATE_FULLSCREEN") true (fun resp ->
956 state.fulsatom <- r32 resp 8;
957 if state.fulsatom != 0
958 then
959 state.fullscreen <-
960 (fun wid ->
961 let data = Bytes.make 20 '\000' in
962 let fs, f =
963 match state.fs with
964 | NoFs -> Fs (-1, -1, -1, -1), 1
965 | Fs _ -> NoFs, 0
967 w32 data 0 f;
968 w32 data 4 state.fulsatom;
970 let cm = clientmessage 32 0 wid state.nwmsatom data in
971 let s = sendeventreq 0 root 0x180000 cm in
972 sendstr s sock;
973 state.fs <- fs;
978 let s = queryextensionreq (~> "XKEYBOARD") in
979 sendwithrep
980 sock s (fun resp ->
981 let present = r8 resp 8 in
982 if present != 0
983 then (
984 let maj = r8 resp 9 in
985 let s = Bytes.create 8 in
986 w8 s 0 maj; (* XKB *)
987 w8 s 1 0; (* XKBUseExtension *)
988 w16 s 2 2; (* request-length *)
989 w16 s 4 1; (* wantedMajor *)
990 w16 s 6 0; (* watnedMinor *)
991 sendwithrep
992 sock s
993 (fun resp ->
994 let supported = r8 resp 1 in
995 state.xkb <- supported != 0
999 let s = getgeometryreq wid in
1000 syncsendwithrep sock 2.0 s (fun resp ->
1001 glxcompleteinit ();
1002 let w = r16 resp 16
1003 and h = r16 resp 18 in
1004 state.w <- w;
1005 state.h <- h;
1008 | c ->
1009 error "unknown conection setup response %d" (Char.code c)
1012 let getauth haddr dnum =
1013 let haddr =
1014 if haddr = "localhost" || String.length haddr = 0
1015 then
1016 try Unix.gethostname ()
1017 with exn ->
1018 dolog "failed to resolve `%S': %s" haddr (exntos exn);
1019 haddr
1020 else haddr
1022 let path =
1023 try Sys.getenv "XAUTHORITY"
1024 with Not_found ->
1025 try Filename.concat (Sys.getenv "HOME") ".Xauthority"
1026 with Not_found -> E.s
1028 let readauth ic =
1029 let r16be s =
1030 let rb pos = Char.code (Bytes.get s pos) in
1031 (rb 1) lor ((rb 0) lsl 8)
1033 let rec find () =
1034 let rs () =
1035 let s = really_input_string ic 2 in
1036 let n = r16be (~> s) in
1037 really_input_string ic n
1039 let family = really_input_string ic 2 in
1040 let addr = rs () in
1041 let nums = rs () in
1042 let optnum =
1043 try Some (int_of_string nums)
1044 with exn ->
1045 dolog
1046 "display number(%S) is not an integer (corrupt %S?): %s"
1047 nums path (exntos exn);
1048 None
1050 let name = rs () in
1051 let data = rs () in
1053 vlog "family %S addr %S(%S) num %S(%d) name %S data %S"
1054 family addr haddr nums dnum name data;
1055 match optnum with
1056 | Some num when addr = haddr && num = dnum ->
1057 name, data
1058 | _ -> find ()
1060 let name, data =
1061 try find ()
1062 with
1063 | End_of_file -> E.s, E.s
1064 | exn ->
1065 dolog "exception while reading X authority data (%S): %s"
1066 path (exntos exn);
1067 E.s, E.s
1069 close_in ic;
1070 name, data;
1072 if String.length path = 0
1073 then E.s, E.s
1074 else
1075 match open_in_bin path with
1076 | ic -> readauth ic
1077 | (exception exn) ->
1078 dolog "failed to open X authority file `%S' : %s" path (exntos exn);
1079 E.s, E.s
1082 let init t rootwid w h platform =
1083 let d =
1084 try Sys.getenv "DISPLAY"
1085 with exn ->
1086 error "could not get DISPLAY evironment variable: %s"
1087 (exntos exn)
1089 let getnum w b e =
1090 if b = e
1091 then error "invalid DISPLAY(%s) %S" w d
1092 else
1093 let s = String.sub d b (e - b) in
1094 try int_of_string s
1095 with exn ->
1096 error "invalid DISPLAY %S can not parse %s(%S): %s"
1097 d w s (exntos exn)
1099 let rec phost pos =
1100 if pos = String.length d
1101 then error "invalid DISPLAY %S no display number specified" d
1102 else (
1103 if d.[pos] = ':'
1104 then
1105 let rec pdispnum pos1 =
1106 if pos1 = String.length d
1107 then getnum "display number" (pos+1) pos1, 0
1108 else
1109 match d.[pos1] with
1110 | '.' ->
1111 let dispnum = getnum "display number" (pos+1) pos1 in
1112 let rec pscreennum pos2 =
1113 if pos2 = String.length d
1114 then getnum "screen number" (pos1+1) pos2
1115 else
1116 match d.[pos2] with
1117 | '0' .. '9' -> pscreennum (pos2+1)
1118 | _ ->
1119 error "invalid DISPLAY %S, cannot parse screen number" d
1121 dispnum, pscreennum (pos1+1)
1122 | '0' .. '9' -> pdispnum (pos1+1)
1123 | _ ->
1124 error "invalid DISPLAY %S, cannot parse display number" d
1126 String.sub d 0 pos, pdispnum (pos+1)
1127 else phost (pos+1)
1130 let host, (dispnum, screennum) = phost 0 in
1131 let aname, adata = getauth host dispnum in
1132 let fd =
1133 let fd, addr =
1134 if String.length host = 0 || host = "unix"
1135 then
1136 let addr =
1137 match platform with
1138 | Utils.Posx -> Unix.ADDR_UNIX d
1139 | Utils.Plinux ->
1140 Unix.ADDR_UNIX ("\000/tmp/.X11-unix/X" ^ string_of_int dispnum)
1141 | Utils.Punknown | Utils.Psun | Utils.Pbsd | Utils.Pcygwin ->
1142 Unix.ADDR_UNIX ("/tmp/.X11-unix/X" ^ string_of_int dispnum)
1144 Unix.socket Unix.PF_UNIX Unix.SOCK_STREAM 0, addr
1145 else
1146 let h =
1147 try Unix.gethostbyname host
1148 with exn ->
1149 error "cannot resolve %S: %s" host (exntos exn)
1151 let addr = h.Unix.h_addr_list.(0) in
1152 let port = 6000 + dispnum in
1153 let fd = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
1154 fd, (Unix.ADDR_INET (addr, port))
1156 try Unix.connect fd addr; fd
1157 with exn ->
1158 error "failed to connect to X: %s" (exntos exn)
1160 cloexec fd;
1161 let s = Bytes.create 12 in
1162 let s = padcat s (~> aname) in
1163 let s = padcat s (~> adata) in
1164 Bytes.set s 0 ordermagic;
1165 w16 s 2 11;
1166 w16 s 4 0;
1167 w16 s 6 (String.length aname);
1168 w16 s 8 (String.length adata);
1169 sendstr1 s 0 (Bytes.length s) fd;
1170 state.sock <- fd;
1171 setup d fd rootwid screennum w h;
1172 state.t <- t;
1173 fd, state.w, state.h;
1176 let settitle s =
1177 state.setwmname (~> s);
1180 let setcursor cursor =
1181 if cursor != state.curcurs
1182 then (
1183 setcursor cursor;
1184 state.curcurs <- cursor;
1188 let fullscreen () =
1189 state.fullscreen state.wid;
1192 let metamask = 0x40;;
1193 let altmask = 8;;
1194 let shiftmask = 1;;
1195 let ctrlmask = 4;;
1197 let withalt mask = mask land altmask != 0;;
1198 let withctrl mask = mask land ctrlmask != 0;;
1199 let withshift mask = mask land shiftmask != 0;;
1200 let withmeta mask = mask land metamask != 0;;
1201 let withnone mask = mask land (altmask + ctrlmask + shiftmask + metamask) = 0;;
1203 let xlatt, xlatf =
1204 let t = Hashtbl.create 20
1205 and f = Hashtbl.create 20 in
1206 let add n nl k =
1207 List.iter (fun s -> Hashtbl.add t s k) (n::nl);
1208 Hashtbl.add f k n
1210 let addc c =
1211 let s = String.make 1 c in
1212 add s [] (Char.code c)
1214 let addcr a b =
1215 let an = Char.code a and bn = Char.code b in
1216 for i = an to bn do addc (Char.chr i) done;
1218 addcr '0' '9';
1219 addcr 'a' 'z';
1220 addcr 'A' 'Z';
1221 String.iter addc "`~!@#$%^&*()-_=+\\|[{]};:,./<>?";
1222 for i = 0 to 29 do add ("f" ^ string_of_int (i+1)) [] (0xffbe + i) done;
1223 add "space" [] 0x20;
1224 add "ret" ["return"; "enter"] 0xff0d;
1225 add "tab" [] 0xff09;
1226 add "left" [] 0xff51;
1227 add "right" [] 0xff53;
1228 add "home" [] 0xff50;
1229 add "end" [] 0xff57;
1230 add "ins" ["insert"] 0xff63;
1231 add "del" ["delete"] 0xffff;
1232 add "esc" ["escape"] 0xff1b;
1233 add "pgup" ["pageup"] 0xff55;
1234 add "pgdown" ["pagedown"] 0xff56;
1235 add "backspace" [] 0xff08;
1236 add "up" [] 0xff52;
1237 add "down" [] 0xff54;
1238 add "menu" [] 0xff67;
1239 t, f;
1242 let keyname k =
1243 try Hashtbl.find xlatf k
1244 with Not_found -> Printf.sprintf "%#x" k;
1247 let namekey name =
1248 try Hashtbl.find xlatt name
1249 with Not_found ->
1250 if String.length name = 1
1251 then Char.code name.[0]
1252 else int_of_string name;