Fix menu/meta mixup
[llpp.git] / wsi.ml
blobcfc17449f64922170d27424f699f5e8b8dbe165e
1 open Utils;;
3 type cursor =
4 | CURSOR_INHERIT
5 | CURSOR_INFO
6 | CURSOR_CYCLE
7 | CURSOR_CROSSHAIR
8 | CURSOR_TEXT
9 ;;
11 type winstate =
12 | MaxVert
13 | MaxHorz
14 | Fullscreen
17 external glx1 : unit -> int = "ml_glx1";;
18 external glx2 : int -> unit = "ml_glx2";;
19 external glxsync : unit -> unit = "ml_glxsync";;
20 external swapb : unit -> unit = "ml_swapb";;
22 let vlog fmt = Format.kprintf ignore fmt;;
24 let onot = object
25 method display = ()
26 method expose = ()
27 method visible = ()
28 method reshape _ _ = ()
29 method mouse _ _ _ _ _ = ()
30 method motion _ _ = ()
31 method pmotion _ _ = ()
32 method key _ _ = ()
33 method enter _ _ = ()
34 method leave = ()
35 method winstate _ = ()
36 method quit = exit 0
37 end;;
39 class type t = object
40 method display : unit
41 method expose : unit
42 method visible : unit
43 method reshape : int -> int -> unit
44 method mouse : int -> bool -> int -> int -> int -> unit
45 method motion : int -> int -> unit
46 method pmotion : int -> int -> unit
47 method key : int -> int -> unit
48 method enter : int -> int -> unit
49 method leave : unit
50 method winstate : winstate list -> unit
51 method quit : unit
52 end;;
54 type state =
55 { mutable mink : int
56 ; mutable maxk : int
57 ; mutable keymap : int array array
58 ; fifo : (string -> unit) Queue.t
59 ; mutable seq : int
60 ; mutable protoatom : int
61 ; mutable deleatom : int
62 ; mutable nwmsatom : int
63 ; mutable maxvatom : int
64 ; mutable maxhatom : int
65 ; mutable fulsatom : int
66 ; mutable idbase : int
67 ; mutable fid : int
68 ; mutable fullscreen : (int -> unit)
69 ; mutable setwmname : (string -> unit)
70 ; mutable actwin : (unit -> unit)
71 ; mutable stringatom : int
72 ; mutable t : t
73 ; mutable sock : Unix.file_descr
74 ; mutable x : int
75 ; mutable y : int
76 ; mutable w : int
77 ; mutable h : int
78 ; mutable fs : fs
79 ; mutable curcurs : cursor
80 ; mutable capslmask : int
81 ; mutable numlmask : int
82 ; mutable levl3mask : int
83 ; mutable levl5mask : int
85 and fs =
86 | NoFs
87 | Fs of (int * int * int * int)
90 let state =
91 { mink = max_int
92 ; maxk = min_int
93 ; keymap = E.a
94 ; fifo = Queue.create ()
95 ; seq = 0
96 ; protoatom = -1
97 ; deleatom = -1
98 ; nwmsatom = -1
99 ; maxvatom = -1
100 ; maxhatom = -1
101 ; fulsatom = -1
102 ; idbase = -1
103 ; fid = -1
104 ; fullscreen = (fun _ -> ())
105 ; setwmname = (fun _ -> ())
106 ; actwin = (fun _ -> ())
107 ; sock = Unix.stdin
108 ; t = onot
109 ; x = -1
110 ; y = -1
111 ; w = -1
112 ; h = -1
113 ; fs = NoFs
114 ; stringatom = 31
115 ; curcurs = CURSOR_INHERIT
116 ; capslmask = 0
117 ; numlmask = 0
118 ; levl3mask = 0
119 ; levl5mask = 0
123 include Bo;;
125 let makereq opcode len reqlen =
126 let s = String.create len in
127 w8 s 0 opcode;
128 w16 s 2 reqlen;
132 let recv fd s pos len =
133 Unix.recv fd s pos len [];
136 let readstr sock n =
137 let s = String.create n in
138 let rec loop pos n =
139 let m = tempfailureretry (recv sock s pos) n in
140 if m = 0
141 then state.t#quit;
142 if n != m
143 then (
144 ignore (tempfailureretry (Unix.select [sock] [] []) 0.01);
145 loop (pos + m) (n - m)
148 loop 0 n;
152 let sendstr1 s pos len sock =
153 vlog "%d <= %S" state.seq s;
154 state.seq <- state.seq + 1;
155 let n = tempfailureretry (Unix.send sock s pos len) [] in
156 if n != len
157 then error "send %d returned %d" len n;
160 let updkmap sock resp =
161 let syms = r8 resp 1 in
162 let len = r32 resp 4 in
163 let data =
164 if len > 0
165 then readstr sock (4*len)
166 else E.s
168 let m = len / syms in
169 state.keymap <- Array.make_matrix
170 (state.maxk - state.mink) syms 0xffffff;
171 let rec loop i = if i = m then () else
172 let k = i*4*syms in
173 let rec loop2 k l = if l = syms then () else
174 let v = r32 data k in
175 state.keymap.(i).(l) <- v;
176 loop2 (k+4) (l+1)
178 loop2 k 0;
179 loop (i+1);
181 loop 0;
184 let updmodmap sock resp =
185 let n = r8 resp 1 in
186 let len = r16 resp 4 in
187 let data =
188 if len > 0
189 then readstr sock (len*4)
190 else E.s
192 if len > 0 then (*???*)
193 let modmap = Array.make_matrix 8 n 0xffffff in
194 let rec loop l = if l = 8 then () else
195 let p = l*n in
196 let rec loop1 m = if m = n then () else
197 let p = p+m in
198 let code = r8 data p in
199 modmap.(l).(m) <- code;
200 if l = 1
201 then (
202 let ki = code - state.mink in
203 if ki >= 0
204 then
205 let a = state.keymap.(ki) in
206 let rec capsloop i = if i = Array.length a || i > 3 then () else
207 let s = a.(i) in
208 if s = 0xffe5
209 then state.capslmask <- 2
210 else capsloop (i+1)
212 capsloop 0;
214 else (
215 if l > 3
216 then (
217 let ki = code - state.mink in
218 if ki >= 0
219 then
220 let a = state.keymap.(ki) in
221 let rec lloop i = if i = Array.length a || i > 3 then () else
222 let s = a.(i) in
223 match s with
224 | 0xfe03 -> state.levl3mask <- 1 lsl l
225 | 0xfe11 -> state.levl5mask <- 1 lsl l
226 | 0xff7f -> state.numlmask <- 1 lsl l
227 | _ -> lloop (i+1)
229 lloop 0;
232 loop1 (m+1)
234 loop1 0;
235 loop (l+1)
237 loop 0;
240 let sendwithrep sock s f =
241 Queue.push f state.fifo;
242 sendstr1 s 0 (String.length s) sock;
245 let padcatl ss =
246 let b = Buffer.create 16 in
247 List.iter (Buffer.add_string b) ss;
248 let bl = Buffer.length b in
249 let pl = bl land 3 in
250 if pl != 0
251 then (
252 let pad = "123" in
253 Buffer.add_substring b pad 0 (4 - pl);
255 Buffer.contents b;
258 let padcat s1 s2 = padcatl [s1; s2];;
260 let internreq name onlyifexists =
261 let s = makereq 16 8 8 in
262 let s = padcat s name in
263 w8 s 1 (if onlyifexists then 1 else 0);
264 w16 s 2 (String.length s / 4);
265 w16 s 4 (String.length name);
269 let sendintern sock s onlyifexists f =
270 let s = internreq s onlyifexists in
271 sendwithrep sock s f;
274 let createwindowreq wid parent x y w h bw eventmask vid depth mid =
275 let s = makereq 1 48 12 in
276 w8 s 1 depth;
277 w32 s 4 wid;
278 w32 s 8 parent;
279 w16 s 12 x;
280 w16 s 14 y;
281 w16 s 16 w;
282 w16 s 18 h;
283 w16 s 20 bw;
284 w16 s 22 0; (* inputoutput *)
285 w32 s 24 vid; (* visual *)
286 w32 s 28 0x280a; (* eventmask*)
287 w32 s 32 0;
288 w32 s 36 0;
289 w32 s 40 eventmask;
290 w32 s 44 mid;
294 let createcolormapreq mid wid vid =
295 let s = makereq 78 16 4 in
296 w8 s 1 0;
297 w32 s 4 mid;
298 w32 s 8 wid;
299 w32 s 12 vid;
303 let getgeometryreq wid =
304 let s = makereq 14 8 2 in
305 w32 s 4 wid;
309 let mapreq wid =
310 let s = makereq 8 8 2 in
311 w32 s 4 wid;
315 let getkeymapreq first count =
316 let s = makereq 101 8 2 in
317 w8 s 4 first;
318 w8 s 5 count;
322 let changepropreq wid prop typ format props =
323 let s = makereq 18 24 0 in
324 let s = padcat s props in
325 w8 s 1 0;
326 w16 s 2 (String.length s / 4);
327 w32 s 4 wid;
328 w32 s 8 prop;
329 w32 s 12 typ;
330 w8 s 16 format;
331 let ful = String.length props / (match format with
332 | 8 -> 1
333 | 16 -> 2
334 | 32 -> 4
335 | n -> error "no idea what %d means" n)
337 w32 s 20 ful;
341 let getpropreq delete wid prop typ =
342 let s = makereq 20 24 6 in
343 w8 s 1 (if delete then 1 else 0);
344 w32 s 4 wid;
345 w32 s 8 prop;
346 w32 s 12 typ;
347 w32 s 16 0;
348 w32 s 20 2;
352 let openfontreq fid name =
353 let s = makereq 45 12 0 in
354 let s = padcat s name in
355 w16 s 2 (String.length s / 4);
356 w32 s 4 fid;
357 w16 s 8 (String.length name);
361 let createglyphcursorreq fid cid cindex =
362 let s = makereq 94 32 8 in
363 w32 s 4 cid;
364 w32 s 8 fid;
365 w32 s 12 fid;
366 w16 s 16 cindex;
367 w16 s 18 (cindex+1);
368 w16 s 20 0;
369 w16 s 22 0;
370 w16 s 24 0;
371 w16 s 26 0xffff;
372 w16 s 28 0xffff;
373 w16 s 30 0xffff;
377 let changewindowattributesreq wid mask attrs =
378 let s = makereq 2 12 0 in
379 let s = padcat s attrs in
380 w16 s 2 (String.length s / 4);
381 w32 s 4 wid;
382 w32 s 8 mask;
386 let configurewindowreq wid mask values =
387 let s = makereq 2 12 0 in
388 let s = padcat s values in
389 w16 s 2 (String.length s / 4);
390 w32 s 4 wid;
391 w16 s 8 mask;
395 let s32 n =
396 let s = "1234" in
397 w32 s 0 n;
401 let clientmessage format seq wid typ data =
402 let s = makereq 33 12 0 in
403 let s = padcat s data in
404 w8 s 1 format;
405 w16 s 2 seq;
406 w32 s 4 wid;
407 w32 s 8 typ;
411 let sendeventreq propagate destwid mask data =
412 let s = makereq 25 12 11 in
413 let s = padcat s data in
414 w8 s 1 propagate;
415 w16 s 2 11;
416 w32 s 4 destwid;
417 w32 s 8 mask;
421 let getmodifiermappingreq () =
422 makereq 119 4 1;
425 let getkeysym code mask =
426 let pkpk = state.keymap.(code-state.mink).(0) in
427 if (pkpk >= 0xff80 && pkpk <= 0xffbd)
428 || (pkpk >= 0x11000000 && pkpk <= 0x1100ffff)
429 then (
430 if mask land state.numlmask != 0
431 then
432 let keysym = state.keymap.(code-state.mink).(1) in
433 if keysym = 0 then pkpk else keysym
434 else pkpk
436 else (
437 let shift =
438 if pkpk land 0xf000 = 0xf000
439 then 0
440 else (mask land 1) lxor ((mask land state.capslmask) lsr 1)
442 let index =
443 let l3 = (mask land state.levl3mask) != 0 in
444 let l4 = (mask land state.levl5mask) != 0 in
445 shift +
446 if l3 then (if l4 then 8 else 4) else (if l4 then 6 else 0)
448 let keysym = state.keymap.(code-state.mink).(index) in
449 if index land 1 = 1 && keysym = 0
450 then state.keymap.(code-state.mink).(index - 1)
451 else keysym
455 let readresp sock =
456 let resp = readstr sock 32 in
457 let opcode = r8 resp 0 in
458 match opcode land lnot 0x80 with
459 | 0 -> (* error *)
460 let s = resp in
461 let code = r8 s 1
462 and serial = r16 s 2
463 and resid = r32 resp 4
464 and min = r16 s 8
465 and maj = r8 s 10 in
466 error "code=%d serial=%d resid=%#x min=%d maj=%d\n%S"
467 code serial resid min maj resp;
469 | 1 -> (* response *)
470 let rep = Queue.pop state.fifo in
471 rep resp;
473 | 2 -> (* key press *)
474 if Array.length state.keymap > 0
475 then
476 let code = r8 resp 1 in
477 let mask = r16 resp 28 in
478 let keysym = getkeysym code mask in
479 vlog "keysym = %x %c mask %#x code %d"
480 keysym (Char.unsafe_chr keysym) mask code;
481 state.t#key keysym mask;
483 | 3 -> (* key release *)
484 if Array.length state.keymap > 0
485 then
486 let code = r8 resp 1 in
487 let mask = r16 resp 28 in
488 let keysym = getkeysym code mask in
489 vlog "release keysym = %x %c mask %#x code %#d"
490 keysym (Char.unsafe_chr keysym) mask code
492 | 4 -> (* buttonpress *)
493 let n = r8 resp 1
494 and x = r16s resp 24
495 and y = r16s resp 26
496 and m = r16 resp 28 in
497 state.t#mouse n true x y m;
498 vlog "press %d" n
500 | 5 -> (* buttonrelease *)
501 let n = r8 resp 1
502 and x = r16s resp 24
503 and y = r16s resp 26
504 and m = r16 resp 28 in
505 state.t#mouse n false x y m;
506 vlog "release %d %d %d" n x y
508 | 6 -> (* motion *)
509 let x = r16s resp 24 in
510 let y = r16s resp 26 in
511 let m = r16 resp 28 in
512 if m land 0x1f00 = 0
513 then state.t#pmotion x y
514 else state.t#motion x y;
515 vlog "move %dx%d => %d" x y m
517 | 7 -> (* enter *)
518 let x = r16s resp 24
519 and y = r16s resp 26 in
520 state.t#enter x y;
521 vlog "enter %d %d" x y
523 | 8 -> (* leave *)
524 state.t#leave
526 | 18 -> vlog "unmap";
528 | 19 -> (* map *)
529 vlog "map";
531 | 12 -> (* exposure *)
532 vlog "exposure";
533 state.t#expose
535 | 15 -> (* visibility *)
536 let vis = r8 resp 8 in
537 if vis != 2 then state.t#visible;
538 vlog "visibility %d" vis;
540 | 34 -> (* mapping *)
541 state.keymap <- E.a;
542 let s = getkeymapreq state.mink (state.maxk-state.mink-1) in
543 sendwithrep sock s (updkmap sock);
544 state.capslmask <- 0;
545 state.levl3mask <- 0;
546 state.levl5mask <- 0;
547 state.numlmask <- 0;
548 let s = getmodifiermappingreq () in
549 sendwithrep sock s (updmodmap sock);
551 | 33 -> (* clientmessage *)
552 let atom = r32 resp 8 in
553 if atom = state.protoatom
554 then (
555 let atom = r32 resp 12 in
556 if atom = state.deleatom
557 then state.t#quit;
559 vlog "atom %#x" atom
561 | 21 -> (* reparent *)
562 vlog "reparent"
564 | 22 -> (* configure *)
565 let x = r16s resp 16
566 and y = r16s resp 18
567 and w = r16 resp 20
568 and h = r16 resp 22 in
569 vlog "configure cur [%d %d %d %d] conf [%d %d %d %d]"
570 state.x state.y state.w state.h
571 x y w h
573 glxsync ();
574 if w != state.w || h != state.h
575 then (
576 state.t#reshape w h;
578 state.w <- w;
579 state.h <- h;
580 state.x <- x;
581 state.y <- y;
582 state.t#expose
584 | 28 ->
585 let atom = r32 resp 8 in
586 if atom = state.nwmsatom
587 then
588 let s = getpropreq false state.idbase atom 4 in
589 sendwithrep sock s (fun resp ->
590 let len = r32 resp 4 in
591 let nitems = r32 resp 16 in
592 let wsl =
593 if len = 0
594 then []
595 else
596 let s = readstr sock (len*4) in
597 let rec loop wsl i = if i = nitems then wsl else
598 let atom = r32 s (i*4) in
599 let wsl =
600 if atom = state.maxhatom
601 then MaxHorz::wsl
602 else (
603 if atom = state.maxvatom
604 then MaxVert::wsl
605 else (
606 if atom = state.fulsatom
607 then (
608 state.fs <- Fs (state.x, state.y, state.w, state.h);
609 Fullscreen::wsl
611 else wsl
614 in loop wsl (i+1)
616 loop [] 0
618 state.t#winstate (List.sort compare wsl)
621 | n ->
622 dolog "event %d %S" n resp
625 let readresp sock =
626 let rec loop () =
627 readresp sock;
628 if hasdata sock then loop ();
630 loop ();
633 let sendstr s ?(pos=0) ?(len=String.length s) sock =
634 sendstr1 s pos len sock;
635 if hasdata sock then readresp sock;
638 let reshape w h =
639 if state.fs = NoFs
640 then
641 let s = "wwuuhhuu" in
642 w32 s 0 w;
643 w32 s 4 h;
644 let s = configurewindowreq state.idbase 0x000c s in
645 sendstr s state.sock;
646 else state.fullscreen state.idbase
649 let activatewin () =
650 state.actwin ();
653 let syncsendwithrep sock secstowait s f =
654 let completed = ref false in
655 sendwithrep sock s (fun resp -> f resp; completed := true);
656 let now = Unix.gettimeofday in
657 let deadline = now () +. secstowait in
658 let rec readtillcompletion () =
659 let sf deadline =
660 let timeout = deadline -. now () in
661 if timeout <= 0.0
662 then [], [], []
663 else Unix.select [sock] [] [] timeout
665 let r, _, _ = tempfailureretry sf deadline in
666 match r with
667 | [] -> error "didn't get X response in %f seconds, aborting" secstowait
668 | _ ->
669 readresp sock;
670 if not !completed
671 then readtillcompletion ()
673 readtillcompletion ();
676 let mapwin () =
677 let s = mapreq state.idbase in
678 sendstr s state.sock;
681 let syncsendintern sock secstowait s onlyifexists f =
682 let s = internreq s onlyifexists in
683 syncsendwithrep sock secstowait s f;
686 let setup sock screennum w h =
687 let s = readstr sock 2 in
688 let n = String.length s in
689 if n != 2
690 then error "failed to read X connection setup response n=%d" n;
691 match s.[0] with
692 | '\000' ->
693 let reasonlen = r8 s 1 in
694 let s = readstr sock 6 in
695 let maj = r16 s 0
696 and min = r16 s 2
697 and add = r16 s 4 in
698 let len = add*4 in
699 let data = readstr sock len in
700 let reason = String.sub data 0 reasonlen in
701 error "X connection failed maj=%d min=%d reason=%S"
702 maj min reason
704 | '\002' -> failwith "X connection setup failed: authentication required";
706 | '\001' ->
707 let s = readstr sock 38 in
708 let maj = r16 s 0
709 and min = r16 s 2
710 and add = r16 s 4
711 and idbase = r32 s 10
712 and idmask = r32 s 14
713 and vlen = r16 s 22
714 and screens = r8 s 26
715 and formats = r8 s 27
716 and minkk = r8 s 32
717 and maxkk = r8 s 33 in
718 let data = readstr sock (4*add-32) in
719 let vendor = String.sub data 0 vlen in
720 let pos = ((vlen+3) land lnot 3) + formats*8 in
722 if screennum >= screens
723 then error "invalid screen %d, max %d" screennum (screens-1);
725 let pos =
726 let s = data in
727 let rec findscreen n pos = if n = screennum then pos else
728 let pos =
729 let ndepths = r8 s (pos+39) in
730 let rec skipdepths n pos = if n = ndepths then pos else
731 let pos =
732 let nvisiuals = r16 s (pos+2) in
733 pos + nvisiuals*24 + 8
735 skipdepths (n+1) pos
737 skipdepths n (pos+40)
739 findscreen (n+1) pos
741 findscreen 0 pos
743 let root = r32 data pos in
744 let rootw = r16 data (pos+20)
745 and rooth = r16 data (pos+22) in
746 state.mink <- minkk;
747 state.maxk <- maxkk;
748 state.idbase <- idbase;
749 vlog "vendor = %S, maj=%d min=%d" vendor maj min;
750 vlog "screens = %d formats = %d" screens formats;
751 vlog "minkk = %d maxkk = %d" minkk maxkk;
752 vlog "idbase = %#x idmask = %#x" idbase idmask;
753 vlog "root=%#x %dx%d" root rootw rooth;
754 vlog "wmm = %d, hmm = %d" (r16 data (pos+24)) (r16 data (pos+26));
755 vlog "visualid = %#x " (r32 data (pos+32));
757 let vid = glx1 () in
758 let ndepths = r8 data (pos+39) in
759 let rec finddepth n' pos =
760 if n' = ndepths
761 then error "couldn't find depth for visual %#x" vid;
762 let depth = r8 data pos in
763 let nvisuals = r16 data (pos+2) in
764 let rec findvisual n pos =
765 if n = nvisuals
766 then finddepth (n'+1) pos
767 else
768 let id = r32 data pos in
769 if id = vid
770 then depth
771 else findvisual (n+1) (pos+24)
773 findvisual 0 (pos+8)
775 let depth = finddepth 0 (pos+40) in
777 let wid = state.idbase in
778 let mid = wid+1 in
779 let fid = mid+1 in
781 state.fid <- fid;
783 let s = createcolormapreq mid root vid in
784 sendstr s sock;
786 let mask = 0
787 + 0x00000001 (* KeyPress *)
788 (* + 0x00000002 *) (* KeyRelease *)
789 + 0x00000004 (* ButtonPress *)
790 + 0x00000008 (* ButtonRelease *)
791 + 0x00000010 (* EnterWindow *)
792 + 0x00000020 (* LeaveWindow *)
793 + 0x00000040 (* PointerMotion *)
794 (* + 0x00000080 *) (* PointerMotionHint *)
795 (* + 0x00000100 *) (* Button1Motion *)
796 (* + 0x00000200 *) (* Button2Motion *)
797 (* + 0x00000400 *) (* Button3Motion *)
798 (* + 0x00000800 *) (* Button4Motion *)
799 (* + 0x00001000 *) (* Button5Motion *)
800 + 0x00002000 (* ButtonMotion *)
801 (* + 0x00004000 *) (* KeymapState *)
802 + 0x00008000 (* Exposure *)
803 + 0x00010000 (* VisibilityChange *)
804 + 0x00020000 (* StructureNotify *)
805 (* + 0x00040000 *) (* ResizeRedirect *)
806 (* + 0x00080000 *) (* SubstructureNotify *)
807 (* + 0x00100000 *) (* SubstructureRedirect *)
808 (* + 0x00200000 *) (* FocusChange *)
809 + 0x00400000 (* PropertyChange *)
810 (* + 0x00800000 *) (* ColormapChange *)
811 (* + 0x01000000 *) (* OwnerGrabButton *)
813 let s = createwindowreq wid root 0 0 w h 0 mask vid depth mid in
814 sendstr s sock;
816 sendintern sock "WM_PROTOCOLS" false (fun resp ->
817 state.protoatom <- r32 resp 8;
818 sendintern sock "WM_DELETE_WINDOW" false (fun resp ->
819 state.deleatom <- r32 resp 8;
820 let s = s32 state.deleatom in
821 let s = changepropreq wid state.protoatom 4 32 s in
822 sendstr s sock;
826 sendintern sock "WM_CLIENT_MACHINE" false (fun resp ->
827 let atom = r32 resp 8 in
828 let empty = E.s in
829 let hostname =
830 try Unix.gethostname ()
831 with exn ->
832 dolog "error getting host name: %s" (exntos exn);
833 empty
835 if hostname != empty
836 then
837 let s = changepropreq wid atom state.stringatom 8 hostname in
838 sendstr s sock;
839 sendintern sock "_NET_WM_PID" false (fun resp ->
840 let atom = r32 resp 8 in
841 let pid = Unix.getpid () in
842 let s = s32 pid in
843 let s = changepropreq wid atom (* cardinal *)6 32 s in
844 sendstr s sock;
848 state.actwin <- (fun () ->
849 let s = String.create 4 in
850 let s = configurewindowreq wid 0x40 s in
851 sendstr s state.sock;
852 let s = mapreq wid in
853 sendstr s state.sock;
856 sendintern sock "_NET_ACTIVE_WINDOW" true (fun resp ->
857 let atom = r32 resp 8 in
858 state.actwin <- (fun () ->
859 let data = String.make 20 '\000' in
860 let cm = clientmessage 32 0 wid atom data in
861 let s = sendeventreq 0 root 0x180000 cm in
862 sendstr s state.sock;
866 syncsendintern sock 2.0 "WM_CLASS" false (fun resp ->
867 let atom = r32 resp 8 in
868 let llpp = "llpp\000llpp\000" in
869 let s = changepropreq wid atom 31 8 llpp in
870 sendstr s sock;
873 let s = getkeymapreq state.mink (state.maxk-state.mink) in
874 sendwithrep sock s (updkmap sock);
876 let s = getmodifiermappingreq () in
877 sendwithrep sock s (updmodmap sock);
879 let s = openfontreq fid "cursor" in
880 sendstr s sock;
882 Array.iteri (fun i glyphindex ->
883 let s = createglyphcursorreq fid (fid+1+i) glyphindex in
884 sendstr s sock;
885 ) [|34;48;50;58;128;152|];
887 sendintern sock "UTF8_STRING" true (fun resp ->
888 let atom = r32 resp 8 in
889 if atom != 0
890 then state.stringatom <- atom;
893 let setwmname s =
894 let s = changepropreq wid 39 state.stringatom 8 s in
895 sendstr s state.sock;
897 state.setwmname <- setwmname;
898 sendintern sock "_NET_WM_NAME" true (fun resp ->
899 let atom = r32 resp 8 in
900 if atom != 0
901 then state.setwmname <- (fun s ->
902 setwmname s;
903 let s = changepropreq wid atom state.stringatom 8 s in
904 sendstr s state.sock;
908 state.fullscreen <- (fun wid ->
909 let s = "xxuuyyuuwwuuhhuu" in
910 match state.fs with
911 | NoFs ->
912 w32 s 0 0;
913 w32 s 4 0;
914 w32 s 8 rootw;
915 w32 s 12 rooth;
916 let s = configurewindowreq wid 0x000f s in
917 sendstr s state.sock;
918 state.fs <- Fs (state.x, state.y, state.w, state.h);
920 | Fs (x, y, w, h) ->
921 w32 s 0 x;
922 w32 s 4 y;
923 w32 s 8 w;
924 w32 s 12 h;
925 let s = configurewindowreq wid 0x000f s in
926 sendstr s state.sock;
927 state.fs <- NoFs;
930 sendintern sock "_NET_WM_STATE" true (fun resp ->
931 state.nwmsatom <- r32 resp 8;
932 if state.nwmsatom != 0
933 then (
934 sendintern sock "_NET_WM_STATE_MAXIMIZED_VERT" true (fun resp ->
935 state.maxvatom <- r32 resp 8;
937 sendintern sock "_NET_WM_STATE_MAXIMIZED_HORZ" true (fun resp ->
938 state.maxhatom <- r32 resp 8;
940 sendintern sock "_NET_WM_STATE_FULLSCREEN" true (fun resp ->
941 state.fulsatom <- r32 resp 8;
942 if state.fulsatom != 0
943 then
944 state.fullscreen <-
945 (fun wid ->
946 let data = String.make 20 '\000' in
947 let fs, f =
948 match state.fs with
949 | NoFs -> Fs (-1, -1, -1, -1), 1
950 | Fs _ -> NoFs, 0
952 w32 data 0 f;
953 w32 data 4 state.fulsatom;
955 let cm = clientmessage 32 0 wid state.nwmsatom data in
956 let s = sendeventreq 0 root 0x180000 cm in
957 sendstr s sock;
958 state.fs <- fs;
963 let s = getgeometryreq wid in
964 syncsendwithrep sock 2.0 s (fun resp ->
965 glx2 wid;
966 let w = r16 resp 16
967 and h = r16 resp 18 in
968 state.w <- w;
969 state.h <- h;
972 | c ->
973 error "unknown conection setup response %d" (Char.code c)
976 let getauth haddr dnum =
977 let haddr =
978 if haddr = "localhost" || String.length haddr = 0
979 then
980 try Unix.gethostname ()
981 with exn ->
982 dolog "failed to resolve `%S': %s" haddr (exntos exn);
983 haddr
984 else haddr
986 let path =
987 try Sys.getenv "XAUTHORITY"
988 with Not_found ->
989 try Filename.concat (Sys.getenv "HOME") ".Xauthority"
990 with Not_found -> E.s
992 let readauth ic =
993 let input_string ic len =
994 let s = String.create len in
995 really_input ic s 0 len;
998 let r16be s =
999 let rb pos = Char.code (String.get s pos) in
1000 (rb 1) lor ((rb 0) lsl 8)
1002 let rec find () =
1003 let rs () =
1004 let s = input_string ic 2 in
1005 let n = r16be s in
1006 input_string ic n
1008 let family = input_string ic 2 in
1009 let addr = rs () in
1010 let nums = rs () in
1011 let optnum =
1012 try Some (int_of_string nums)
1013 with exn ->
1014 dolog
1015 "display number(%S) is not an integer (corrupt %S?): %s"
1016 nums path (exntos exn);
1017 None
1019 let name = rs () in
1020 let data = rs () in
1022 vlog "family %S addr %S(%S) num %S(%d) name %S data %S"
1023 family addr haddr nums dnum name data;
1024 match optnum with
1025 | Some num when addr = haddr && num = dnum ->
1026 name, data
1027 | _ -> find ()
1029 let name, data =
1030 try find ()
1031 with
1032 | End_of_file -> E.s, E.s
1033 | exn ->
1034 dolog "exception while reading X authority data (%S): %s"
1035 path (exntos exn);
1036 E.s, E.s
1038 close_in ic;
1039 name, data;
1041 let opt =
1043 if String.length path = 0
1044 then None
1045 else Some (open_in_bin path)
1046 with exn ->
1047 if Sys.file_exists path
1048 then
1049 dolog "failed to open X authority file `%S' : %s"
1050 path (exntos exn);
1051 None
1053 match opt with
1054 | None -> E.s, E.s
1055 | Some ic -> readauth ic
1058 let init t w h osx =
1059 let d =
1060 try Sys.getenv "DISPLAY"
1061 with exn ->
1062 error "could not get DISPLAY evironment variable: %s"
1063 (exntos exn)
1065 let getnum w b e =
1066 if b = e
1067 then error "invalid DISPLAY(%s) %S" w d
1068 else
1069 let s = String.sub d b (e - b) in
1070 try int_of_string s
1071 with exn ->
1072 error "invalid DISPLAY %S can not parse %s(%S): %s"
1073 d w s (exntos exn)
1075 let rec phost pos =
1076 if pos = String.length d
1077 then error "invalid DISPLAY %S no display number specified" d
1078 else (
1079 if d.[pos] = ':'
1080 then
1081 let rec pdispnum pos1 =
1082 if pos1 = String.length d
1083 then getnum "display number" (pos+1) pos1, 0
1084 else
1085 match d.[pos1] with
1086 | '.' ->
1087 let dispnum = getnum "display number" (pos+1) pos1 in
1088 let rec pscreennum pos2 =
1089 if pos2 = String.length d
1090 then getnum "screen number" (pos1+1) pos2
1091 else
1092 match d.[pos2] with
1093 | '0' .. '9' -> pscreennum (pos2+1)
1094 | _ ->
1095 error "invalid DISPLAY %S, cannot parse screen number" d
1097 dispnum, pscreennum (pos1+1)
1098 | '0' .. '9' -> pdispnum (pos1+1)
1099 | _ ->
1100 error "invalid DISPLAY %S, cannot parse display number" d
1102 String.sub d 0 pos, pdispnum (pos+1)
1103 else phost (pos+1)
1106 let host, (dispnum, screennum) = phost 0 in
1107 let aname, adata = getauth host dispnum in
1108 let fd =
1109 let fd, addr =
1110 if osx || String.length host = 0 || host = "unix"
1111 then
1112 let addr =
1113 if osx
1114 then Unix.ADDR_UNIX d
1115 else Unix.ADDR_UNIX ("/tmp/.X11-unix/X" ^ string_of_int dispnum)
1117 let fd = Unix.socket Unix.PF_UNIX Unix.SOCK_STREAM 0 in
1118 fd, addr
1119 else
1120 let h =
1121 try Unix.gethostbyname host
1122 with exn ->
1123 error "cannot resolve %S: %s" host (exntos exn)
1125 let addr = h.Unix.h_addr_list.(0) in
1126 let port = 6000 + dispnum in
1127 let fd = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
1128 fd, (Unix.ADDR_INET (addr, port))
1130 try Unix.connect fd addr; fd
1131 with exn ->
1132 error "failed to connect to X: %s" (exntos exn)
1134 cloexec fd;
1135 let s = String.create 12 in
1136 let s = padcat s aname in
1137 let s = padcat s adata in
1138 s.[0] <- ordermagic;
1139 w16 s 2 11;
1140 w16 s 4 0;
1141 w16 s 6 (String.length aname);
1142 w16 s 8 (String.length adata);
1143 sendstr1 s 0 (String.length s) fd;
1144 state.sock <- fd;
1145 setup fd screennum w h;
1146 state.t <- t;
1147 fd, state.w, state.h;
1150 let settitle s =
1151 state.setwmname s;
1154 let setcursor cursor =
1155 if cursor != state.curcurs
1156 then
1157 let n =
1158 match cursor with
1159 | CURSOR_INHERIT -> -1
1160 | CURSOR_INFO -> 3
1161 | CURSOR_CYCLE -> 2
1162 | CURSOR_CROSSHAIR -> 0
1163 | CURSOR_TEXT -> 5
1165 let s = s32 (if n = -1 then 0 else state.fid+1+n) in
1166 let s = changewindowattributesreq state.idbase (*cursor*)0x4000 s in
1167 sendstr s state.sock;
1168 state.curcurs <- cursor;
1171 let fullscreen () =
1172 state.fullscreen state.idbase;
1175 let metamask = 0x40;;
1176 let altmask = 8;;
1177 let shiftmask = 1;;
1178 let ctrlmask = 4;;
1180 let withalt mask = mask land altmask != 0;;
1181 let withctrl mask = mask land ctrlmask != 0;;
1182 let withshift mask = mask land shiftmask != 0;;
1183 let withmeta mask = mask land metamask != 0;;
1184 let withnone mask = mask land (altmask + ctrlmask + shiftmask + metamask) = 0;;
1186 let xlatt, xlatf =
1187 let t = Hashtbl.create 20
1188 and f = Hashtbl.create 20 in
1189 let add n nl k =
1190 List.iter (fun s -> Hashtbl.add t s k) (n::nl);
1191 Hashtbl.add f k n
1193 let addc c =
1194 let s = String.create 1 in
1195 s.[0] <- c;
1196 add s [] (Char.code c)
1198 let addcr a b =
1199 let an = Char.code a and bn = Char.code b in
1200 for i = an to bn do addc (Char.chr i) done;
1202 addcr '0' '9';
1203 addcr 'a' 'z';
1204 addcr 'A' 'Z';
1205 String.iter addc "`~!@#$%^&*()-_=+\\|[{]};:,./<>?";
1206 for i = 0 to 29 do add ("f" ^ string_of_int (i+1)) [] (0xffbe + i) done;
1207 add "space" [] 0x20;
1208 add "ret" ["return"; "enter"] 0xff0d;
1209 add "tab" [] 0xff09;
1210 add "left" [] 0xff51;
1211 add "right" [] 0xff53;
1212 add "home" [] 0xff50;
1213 add "end" [] 0xff57;
1214 add "ins" ["insert"] 0xff63;
1215 add "del" ["delete"] 0xffff;
1216 add "esc" ["escape"] 0xff1b;
1217 add "pgup" ["pageup"] 0xff55;
1218 add "pgdown" ["pagedown"] 0xff56;
1219 add "backspace" [] 0xff08;
1220 add "up" [] 0xff52;
1221 add "down" [] 0xff54;
1222 add "menu" [] 0xff67;
1223 t, f;
1226 let keyname k =
1227 try Hashtbl.find xlatf k
1228 with Not_found -> Printf.sprintf "%#x" k;
1231 let namekey name =
1232 try Hashtbl.find xlatt name
1233 with Not_found ->
1234 if String.length name = 1
1235 then Char.code name.[0]
1236 else int_of_string name;