Cosmetics
[llpp.git] / wsi.ml
blob4ce9a174d0d20670ccdb5916db0df06a02346cea
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 glx : int -> unit = "ml_glx";;
18 external glxsync : unit -> unit = "ml_glxsync";;
19 external swapb : unit -> unit = "ml_swapb";;
21 let vlog fmt = Format.kprintf ignore fmt;;
23 let onot = object
24 method display = ()
25 method expose = ()
26 method visible = ()
27 method reshape _ _ = ()
28 method mouse _ _ _ _ _ = ()
29 method motion _ _ = ()
30 method pmotion _ _ = ()
31 method key _ _ = ()
32 method enter _ _ = ()
33 method leave = ()
34 method winstate _ = ()
35 method quit = exit 0
36 end;;
38 class type t = object
39 method display : unit
40 method expose : unit
41 method visible : unit
42 method reshape : int -> int -> unit
43 method mouse : int -> bool -> int -> int -> int -> unit
44 method motion : int -> int -> unit
45 method pmotion : int -> int -> unit
46 method key : int -> int -> unit
47 method enter : int -> int -> unit
48 method leave : unit
49 method winstate : winstate list -> unit
50 method quit : unit
51 end;;
53 type state =
54 { mutable mink : int
55 ; mutable maxk : int
56 ; mutable keymap : int array array
57 ; fifo : (string -> unit) Queue.t
58 ; mutable seq : int
59 ; mutable protoatom : int
60 ; mutable deleatom : int
61 ; mutable nwmsatom : int
62 ; mutable maxvatom : int
63 ; mutable maxhatom : int
64 ; mutable fulsatom : int
65 ; mutable idbase : int
66 ; mutable fullscreen : (int -> unit)
67 ; mutable setwmname : (string -> unit)
68 ; mutable actwin : (unit -> unit)
69 ; mutable stringatom : int
70 ; mutable t : t
71 ; mutable sock : Unix.file_descr
72 ; mutable x : int
73 ; mutable y : int
74 ; mutable w : int
75 ; mutable h : int
76 ; mutable fs : fs
77 ; mutable curcurs : cursor
78 ; mutable capslmask : int
79 ; mutable numlmask : int
80 ; mutable levl3mask : int
81 ; mutable levl5mask : int
83 and fs =
84 | NoFs
85 | Fs of (int * int * int * int)
88 let state =
89 { mink = max_int
90 ; maxk = min_int
91 ; keymap = E.a
92 ; fifo = Queue.create ()
93 ; seq = 0
94 ; protoatom = -1
95 ; deleatom = -1
96 ; nwmsatom = -1
97 ; maxvatom = -1
98 ; maxhatom = -1
99 ; fulsatom = -1
100 ; idbase = -1
101 ; fullscreen = (fun _ -> ())
102 ; setwmname = (fun _ -> ())
103 ; actwin = (fun _ -> ())
104 ; sock = Unix.stdin
105 ; t = onot
106 ; x = -1
107 ; y = -1
108 ; w = -1
109 ; h = -1
110 ; fs = NoFs
111 ; stringatom = 31
112 ; curcurs = CURSOR_INHERIT
113 ; capslmask = 0
114 ; numlmask = 0
115 ; levl3mask = 0
116 ; levl5mask = 0
120 include Bo;;
122 let makereq opcode len reqlen =
123 let s = String.make len '\000' in
124 w8 s 0 opcode;
125 w16 s 2 reqlen;
129 let recv fd s pos len =
130 Unix.recv fd s pos len [];
133 let readstr sock n =
134 let s = String.create n in
135 let rec loop pos n =
136 let m = tempfailureretry (recv sock s pos) n in
137 if m = 0
138 then state.t#quit;
139 if n != m
140 then (
141 ignore (tempfailureretry (Unix.select [sock] [] []) 0.01);
142 loop (pos + m) (n - m)
145 loop 0 n;
149 let sendstr1 s pos len sock =
150 vlog "%d <= %S" state.seq s;
151 state.seq <- state.seq + 1;
152 let n = tempfailureretry (Unix.send sock s pos len) [] in
153 if n != len
154 then error "send %d returned %d" len n;
157 let updkmap sock resp =
158 let syms = r8 resp 1 in
159 let len = r32 resp 4 in
160 let data =
161 if len > 0
162 then readstr sock (4*len)
163 else E.s
165 let m = len / syms in
166 state.keymap <- Array.make_matrix
167 (state.maxk - state.mink) syms 0xffffff;
168 let rec loop i = if i = m then () else
169 let k = i*4*syms in
170 let rec loop2 k l = if l = syms then () else
171 let v = r32 data k in
172 state.keymap.(i).(l) <- v;
173 loop2 (k+4) (l+1)
175 loop2 k 0;
176 loop (i+1);
178 loop 0;
181 let updmodmap sock resp =
182 let n = r8 resp 1 in
183 let len = r16 resp 4 in
184 let data =
185 if len > 0
186 then readstr sock (len*4)
187 else E.s
189 if len > 0 then (*???*)
190 let modmap = Array.make_matrix 8 n 0xffffff in
191 let rec loop l = if l = 8 then () else
192 let p = l*n in
193 let rec loop1 m = if m = n then () else
194 let p = p+m in
195 let code = r8 data p in
196 modmap.(l).(m) <- code;
197 if l = 1
198 then (
199 let ki = code - state.mink in
200 if ki >= 0
201 then
202 let a = state.keymap.(ki) in
203 let rec capsloop i = if i = Array.length a || i > 3 then () else
204 let s = a.(i) in
205 if s = 0xffe5
206 then state.capslmask <- 2
207 else capsloop (i+1)
209 capsloop 0;
211 else (
212 if l > 3
213 then (
214 let ki = code - state.mink in
215 if ki >= 0
216 then
217 let a = state.keymap.(ki) in
218 let rec lloop i = if i = Array.length a || i > 3 then () else
219 let s = a.(i) in
220 match s with
221 | 0xfe03 -> state.levl3mask <- 1 lsl l
222 | 0xfe11 -> state.levl5mask <- 1 lsl l
223 | 0xff7f -> state.numlmask <- 1 lsl l
224 | _ -> lloop (i+1)
226 lloop 0;
229 loop1 (m+1)
231 loop1 0;
232 loop (l+1)
234 loop 0;
237 let sendwithrep sock s f =
238 Queue.push f state.fifo;
239 sendstr1 s 0 (String.length s) sock;
242 let padcatl ss =
243 let b = Buffer.create 16 in
244 List.iter (Buffer.add_string b) ss;
245 let bl = Buffer.length b in
246 let pl = bl land 3 in
247 if pl != 0
248 then (
249 let pad = "123" in
250 Buffer.add_substring b pad 0 (4 - pl);
252 Buffer.contents b;
255 let padcat s1 s2 = padcatl [s1; s2];;
257 let internreq name onlyifexists =
258 let s = makereq 16 8 8 in
259 let s = padcat s name in
260 if onlyifexists then w8 s 1 1;
261 w16 s 2 (String.length s / 4);
262 w16 s 4 (String.length name);
266 let sendintern sock s onlyifexists f =
267 let s = internreq s onlyifexists in
268 sendwithrep sock s f;
271 let createwindowreq wid parent x y w h bw mask =
272 let s = makereq 1 36 9 in
273 w32 s 4 wid;
274 w32 s 8 parent;
275 w16 s 12 x;
276 w16 s 14 y;
277 w16 s 16 w;
278 w16 s 18 h;
279 w16 s 20 bw;
280 w16 s 22 1;
281 w32 s 24 0;
282 w32 s 28 0x800; (* eventmask *)
283 w32 s 32 mask;
287 let getgeometryreq wid =
288 let s = makereq 14 8 2 in
289 w32 s 4 wid;
293 let mapreq wid =
294 let s = makereq 8 8 2 in
295 w32 s 4 wid;
299 let getkeymapreq first count =
300 let s = makereq 101 8 2 in
301 w8 s 4 first;
302 w8 s 5 count;
306 let changepropreq wid prop typ format props =
307 let s = makereq 18 24 0 in
308 let s = padcat s props in
309 w16 s 2 (String.length s / 4);
310 w32 s 4 wid;
311 w32 s 8 prop;
312 w32 s 12 typ;
313 w8 s 16 format;
314 let ful = String.length props / (match format with
315 | 8 -> 1
316 | 16 -> 2
317 | 32 -> 4
318 | n -> error "no idea what %d means" n)
320 w32 s 20 ful;
324 let getpropreq delete wid prop typ =
325 let s = makereq 20 24 6 in
326 if delete then w8 s 1 1;
327 w32 s 4 wid;
328 w32 s 8 prop;
329 w32 s 12 typ;
330 w32 s 16 0;
331 w32 s 20 2;
335 let openfontreq fid name =
336 let s = makereq 45 12 0 in
337 let s = padcat s name in
338 w16 s 2 (String.length s / 4);
339 w32 s 4 fid;
340 w16 s 8 (String.length name);
344 let createglyphcursorreq fid cid cindex =
345 let s = makereq 94 32 8 in
346 w32 s 4 cid;
347 w32 s 8 fid;
348 w32 s 12 fid;
349 w16 s 16 cindex;
350 w16 s 18 (cindex+1);
351 w16 s 20 0;
352 w16 s 22 0;
353 w16 s 24 0;
354 w16 s 26 0xffff;
355 w16 s 28 0xffff;
356 w16 s 30 0xffff;
360 let changewindowattributesreq wid mask attrs =
361 let s = makereq 2 12 0 in
362 let s = padcat s attrs in
363 w16 s 2 (String.length s / 4);
364 w32 s 4 wid;
365 w32 s 8 mask;
369 let configurewindowreq wid mask values =
370 let s = makereq 2 12 0 in
371 let s = padcat s values in
372 w16 s 2 (String.length s / 4);
373 w32 s 4 wid;
374 w16 s 8 mask;
378 let s32 n =
379 let s = "1234" in
380 w32 s 0 n;
384 let clientmessage format seq wid typ data =
385 let s = makereq 33 12 0 in
386 let s = padcat s data in
387 w8 s 1 format;
388 w16 s 2 seq;
389 w32 s 4 wid;
390 w32 s 8 typ;
394 let sendeventreq propagate destwid mask data =
395 let s = makereq 25 12 11 in
396 let s = padcat s data in
397 w8 s 1 propagate;
398 w16 s 2 11;
399 w32 s 4 destwid;
400 w32 s 8 mask;
404 let getmodifiermappingreq () =
405 makereq 119 4 1;
408 let getkeysym code mask =
409 let pkpk = state.keymap.(code-state.mink).(0) in
410 if (pkpk >= 0xff80 && pkpk <= 0xffbd)
411 || (pkpk >= 0x11000000 && pkpk <= 0x1100ffff)
412 then (
413 if mask land state.numlmask != 0
414 then
415 let keysym = state.keymap.(code-state.mink).(1) in
416 if keysym = 0 then pkpk else keysym
417 else pkpk
419 else (
420 let shift =
421 if pkpk land 0xf000 = 0xf000
422 then 0
423 else (mask land 1) lxor ((mask land state.capslmask) lsr 1)
425 let index =
426 let l3 = (mask land state.levl3mask) != 0 in
427 let l4 = (mask land state.levl5mask) != 0 in
428 shift +
429 if l3 then (if l4 then 8 else 4) else (if l4 then 6 else 0)
431 let keysym = state.keymap.(code-state.mink).(index) in
432 if index land 1 = 1 && keysym = 0
433 then state.keymap.(code-state.mink).(index - 1)
434 else keysym
438 let readresp sock =
439 let resp = readstr sock 32 in
440 let opcode = r8 resp 0 in
441 match opcode land lnot 0x80 with
442 | 0 -> (* error *)
443 let s = resp in
444 let code = r8 s 1
445 and serial = r16 s 2
446 and resid = r32 resp 4
447 and min = r16 s 8
448 and maj = r8 s 10 in
449 error "code=%d serial=%d resid=%#x min=%d maj=%d\n%S"
450 code serial resid min maj resp;
452 | 1 -> (* response *)
453 let rep = Queue.pop state.fifo in
454 rep resp;
456 | 2 -> (* key press *)
457 if Array.length state.keymap > 0
458 then
459 let code = r8 resp 1 in
460 let mask = r16 resp 28 in
461 let keysym = getkeysym code mask in
462 vlog "keysym = %x %c mask %#x code %d"
463 keysym (Char.unsafe_chr keysym) mask code;
464 state.t#key keysym mask;
466 | 3 -> (* key release *)
467 if Array.length state.keymap > 0
468 then
469 let code = r8 resp 1 in
470 let mask = r16 resp 28 in
471 let keysym = getkeysym code mask in
472 vlog "release keysym = %x %c mask %#x code %#d"
473 keysym (Char.unsafe_chr keysym) mask code
475 | 4 -> (* buttonpress *)
476 let n = r8 resp 1
477 and x = r16s resp 24
478 and y = r16s resp 26
479 and m = r16 resp 28 in
480 state.t#mouse n true x y m;
481 vlog "press %d" n
483 | 5 -> (* buttonrelease *)
484 let n = r8 resp 1
485 and x = r16s resp 24
486 and y = r16s resp 26
487 and m = r16 resp 28 in
488 state.t#mouse n false x y m;
489 vlog "release %d %d %d" n x y
491 | 6 -> (* motion *)
492 let x = r16s resp 24 in
493 let y = r16s resp 26 in
494 let m = r16 resp 28 in
495 if m land 0x1f00 = 0
496 then state.t#pmotion x y
497 else state.t#motion x y;
498 vlog "move %dx%d => %d" x y m
500 | 7 -> (* enter *)
501 let x = r16s resp 24
502 and y = r16s resp 26 in
503 state.t#enter x y;
504 vlog "enter %d %d" x y
506 | 8 -> (* leave *)
507 state.t#leave
509 | 18 -> vlog "unmap";
511 | 19 -> (* map *)
512 vlog "map";
514 | 12 -> (* exposure *)
515 vlog "exposure";
516 state.t#expose
518 | 15 -> (* visibility *)
519 let vis = r8 resp 8 in
520 if vis != 2 then state.t#visible;
521 vlog "visibility %d" vis;
523 | 34 -> (* mapping *)
524 state.keymap <- E.a;
525 let s = getkeymapreq state.mink (state.maxk-state.mink-1) in
526 sendwithrep sock s (updkmap sock);
527 state.capslmask <- 0;
528 state.levl3mask <- 0;
529 state.levl5mask <- 0;
530 state.numlmask <- 0;
531 let s = getmodifiermappingreq () in
532 sendwithrep sock s (updmodmap sock);
534 | 33 -> (* clientmessage *)
535 let atom = r32 resp 8 in
536 if atom = state.protoatom
537 then (
538 let atom = r32 resp 12 in
539 if atom = state.deleatom
540 then state.t#quit;
542 vlog "atom %#x" atom
544 | 21 -> (* reparent *)
545 vlog "reparent"
547 | 22 -> (* configure *)
548 let x = r16s resp 16
549 and y = r16s resp 18
550 and w = r16 resp 20
551 and h = r16 resp 22 in
552 vlog "configure cur [%d %d %d %d] conf [%d %d %d %d]"
553 state.x state.y state.w state.h
554 x y w h
556 glxsync ();
557 if w != state.w || h != state.h
558 then (
559 state.t#reshape w h;
561 state.w <- w;
562 state.h <- h;
563 state.x <- x;
564 state.y <- y;
565 state.t#expose
567 | 28 ->
568 let atom = r32 resp 8 in
569 if atom = state.nwmsatom
570 then
571 let s = getpropreq false state.idbase atom 4 in
572 sendwithrep sock s (fun resp ->
573 let len = r32 resp 4 in
574 let nitems = r32 resp 16 in
575 let wsl =
576 if len = 0
577 then []
578 else
579 let s = readstr sock (len*4) in
580 let rec loop wsl i = if i = nitems then wsl else
581 let atom = r32 s (i*4) in
582 let wsl =
583 if atom = state.maxhatom
584 then MaxHorz::wsl
585 else (
586 if atom = state.maxvatom
587 then MaxVert::wsl
588 else (
589 if atom = state.fulsatom
590 then (
591 state.fs <- Fs (state.x, state.y, state.w, state.h);
592 Fullscreen::wsl
594 else wsl
597 in loop wsl (i+1)
599 loop [] 0
601 state.t#winstate (List.sort compare wsl)
604 | n ->
605 dolog "event %d %S" n resp
608 let readresp sock =
609 let rec loop () =
610 readresp sock;
611 if hasdata sock then loop ();
613 loop ();
616 let sendstr s ?(pos=0) ?(len=String.length s) sock =
617 sendstr1 s pos len sock;
618 if hasdata sock then readresp sock;
621 let reshape w h =
622 if state.fs = NoFs
623 then
624 let s = "wwuuhhuu" in
625 w32 s 0 w;
626 w32 s 4 h;
627 let s = configurewindowreq state.idbase 0x000c s in
628 sendstr s state.sock;
629 else state.fullscreen state.idbase
632 let activatewin () =
633 state.actwin ();
636 let syncsendwithrep sock secstowait s f =
637 let completed = ref false in
638 sendwithrep sock s (fun resp -> f resp; completed := true);
639 let now = Unix.gettimeofday in
640 let deadline = now () +. secstowait in
641 let rec readtillcompletion () =
642 let sf deadline =
643 let timeout = deadline -. now () in
644 if timeout <= 0.0
645 then [], [], []
646 else Unix.select [sock] [] [] timeout
648 let r, _, _ = tempfailureretry sf deadline in
649 match r with
650 | [] -> error "didn't get X response in %f seconds, aborting" secstowait
651 | _ ->
652 readresp sock;
653 if not !completed
654 then readtillcompletion ()
656 readtillcompletion ();
659 let mapwin () =
660 let s = mapreq state.idbase in
661 sendstr s state.sock;
664 let syncsendintern sock secstowait s onlyifexists f =
665 let s = internreq s onlyifexists in
666 syncsendwithrep sock secstowait s f;
669 let setup sock screennum w h =
670 let s = readstr sock 2 in
671 let n = String.length s in
672 if n != 2
673 then error "failed to read X connection setup response n=%d" n;
674 match s.[0] with
675 | '\000' ->
676 let reasonlen = r8 s 1 in
677 let s = readstr sock 6 in
678 let maj = r16 s 0
679 and min = r16 s 2
680 and add = r16 s 4 in
681 let len = add*4 in
682 let data = readstr sock len in
683 let reason = String.sub data 0 reasonlen in
684 error "X connection failed maj=%d min=%d reason=%S"
685 maj min reason
687 | '\002' -> failwith "X connection setup failed: authentication required";
689 | '\001' ->
690 let s = readstr sock 38 in
691 let maj = r16 s 0
692 and min = r16 s 2
693 and add = r16 s 4
694 and idbase = r32 s 10
695 and idmask = r32 s 14
696 and vlen = r16 s 22
697 and screens = r8 s 26
698 and formats = r8 s 27
699 and minkk = r8 s 32
700 and maxkk = r8 s 33 in
701 let data = readstr sock (4*add-32) in
702 let vendor = String.sub data 0 vlen in
703 let pos = ((vlen+3) land lnot 3) + formats*8 in
705 if screennum >= screens
706 then error "invalid screen %d, max %d" screennum (screens-1);
708 let pos =
709 let s = data in
710 let rec findscreen n pos = if n = screennum then pos else
711 let pos =
712 let ndepths = r8 s (pos+39) in
713 let rec skipdepths n pos = if n = ndepths then pos else
714 let pos =
715 let nvisiuals = r16 s (pos+2) in
716 pos + nvisiuals*24 + 8
718 skipdepths (n+1) pos
720 skipdepths n (pos+40)
722 findscreen (n+1) pos
724 findscreen 0 pos
726 let root = r32 data pos in
727 let rootw = r16 data (pos+20)
728 and rooth = r16 data (pos+22) in
729 state.mink <- minkk;
730 state.maxk <- maxkk;
731 state.idbase <- idbase;
732 vlog "vendor = %S, maj=%d min=%d" vendor maj min;
733 vlog "screens = %d formats = %d" screens formats;
734 vlog "minkk = %d maxkk = %d" minkk maxkk;
735 vlog "idbase = %#x idmask = %#x" idbase idmask;
736 vlog "root=%#x %dx%d" root rootw rooth;
738 let mask = 0
739 + 0x00000001 (* KeyPress *)
740 (* + 0x00000002 *) (* KeyRelease *)
741 + 0x00000004 (* ButtonPress *)
742 + 0x00000008 (* ButtonRelease *)
743 + 0x00000010 (* EnterWindow *)
744 + 0x00000020 (* LeaveWindow *)
745 + 0x00000040 (* PointerMotion *)
746 (* + 0x00000080 *) (* PointerMotionHint *)
747 (* + 0x00000100 *) (* Button1Motion *)
748 (* + 0x00000200 *) (* Button2Motion *)
749 (* + 0x00000400 *) (* Button3Motion *)
750 (* + 0x00000800 *) (* Button4Motion *)
751 (* + 0x00001000 *) (* Button5Motion *)
752 + 0x00002000 (* ButtonMotion *)
753 (* + 0x00004000 *) (* KeymapState *)
754 + 0x00008000 (* Exposure *)
755 + 0x00010000 (* VisibilityChange *)
756 + 0x00020000 (* StructureNotify *)
757 (* + 0x00040000 *) (* ResizeRedirect *)
758 (* + 0x00080000 *) (* SubstructureNotify *)
759 (* + 0x00100000 *) (* SubstructureRedirect *)
760 (* + 0x00200000 *) (* FocusChange *)
761 + 0x00400000 (* PropertyChange *)
762 (* + 0x00800000 *) (* ColormapChange *)
763 (* + 0x01000000 *) (* OwnerGrabButton *)
765 let wid = state.idbase in
766 let s = createwindowreq wid root 0 0 w h 0 mask in
767 sendstr s sock;
769 sendintern sock "WM_PROTOCOLS" false (fun resp ->
770 state.protoatom <- r32 resp 8;
771 sendintern sock "WM_DELETE_WINDOW" false (fun resp ->
772 state.deleatom <- r32 resp 8;
773 let s = s32 state.deleatom in
774 let s = changepropreq wid state.protoatom 4 32 s in
775 sendstr s sock;
779 sendintern sock "WM_CLIENT_MACHINE" false (fun resp ->
780 let atom = r32 resp 8 in
781 let empty = E.s in
782 let hostname =
783 try Unix.gethostname ()
784 with exn ->
785 dolog "error getting host name: %s" (exntos exn);
786 empty
788 if hostname != empty
789 then
790 let s = changepropreq wid atom state.stringatom 8 hostname in
791 sendstr s sock;
792 sendintern sock "_NET_WM_PID" false (fun resp ->
793 let atom = r32 resp 8 in
794 let pid = Unix.getpid () in
795 let s = s32 pid in
796 let s = changepropreq wid atom (* cardinal *)6 32 s in
797 sendstr s sock;
801 state.actwin <- (fun () ->
802 let s = String.create 4 in
803 let s = configurewindowreq state.idbase 0x40 s in
804 sendstr s state.sock;
805 let s = mapreq state.idbase in
806 sendstr s state.sock;
809 sendintern sock "_NET_ACTIVE_WINDOW" true (fun resp ->
810 let atom = r32 resp 8 in
811 state.actwin <- (fun () ->
812 let data = String.make 20 '\000' in
813 let cm = clientmessage 32 0 wid atom data in
814 let s = sendeventreq 0 root 0x180000 cm in
815 sendstr s state.sock;
819 syncsendintern sock 2.0 "WM_CLASS" false (fun resp ->
820 let atom = r32 resp 8 in
821 let llpp = "llpp\000llpp\000" in
822 let s = changepropreq wid atom 31 8 llpp in
823 sendstr s sock;
826 let s = getkeymapreq state.mink (state.maxk-state.mink) in
827 sendwithrep sock s (updkmap sock);
829 let s = getmodifiermappingreq () in
830 sendwithrep sock s (updmodmap sock);
832 let s = openfontreq (wid+1) "cursor" in
833 sendstr s sock;
835 Array.iteri (fun i glyphindex ->
836 let s = createglyphcursorreq (wid+1) (wid+2+i) glyphindex in
837 sendstr s sock;
838 ) [|34;48;50;58;128;152|];
840 sendintern sock "UTF8_STRING" true (fun resp ->
841 let atom = r32 resp 8 in
842 if atom != 0
843 then state.stringatom <- atom;
846 let setwmname s =
847 let s = changepropreq state.idbase 39 state.stringatom 8 s in
848 sendstr s state.sock;
850 state.setwmname <- setwmname;
851 sendintern sock "_NET_WM_NAME" true (fun resp ->
852 let atom = r32 resp 8 in
853 if atom != 0
854 then state.setwmname <- (fun s ->
855 setwmname s;
856 let s = changepropreq state.idbase atom state.stringatom 8 s in
857 sendstr s state.sock;
861 state.fullscreen <- (fun wid ->
862 let s = "xxuuyyuuwwuuhhuu" in
863 match state.fs with
864 | NoFs ->
865 w32 s 0 0;
866 w32 s 4 0;
867 w32 s 8 rootw;
868 w32 s 12 rooth;
869 let s = configurewindowreq wid 0x000f s in
870 sendstr s state.sock;
871 state.fs <- Fs (state.x, state.y, state.w, state.h);
873 | Fs (x, y, w, h) ->
874 w32 s 0 x;
875 w32 s 4 y;
876 w32 s 8 w;
877 w32 s 12 h;
878 let s = configurewindowreq wid 0x000f s in
879 sendstr s state.sock;
880 state.fs <- NoFs;
883 sendintern sock "_NET_WM_STATE" true (fun resp ->
884 state.nwmsatom <- r32 resp 8;
885 if state.nwmsatom != 0
886 then (
887 sendintern sock "_NET_WM_STATE_MAXIMIZED_VERT" true (fun resp ->
888 state.maxvatom <- r32 resp 8;
890 sendintern sock "_NET_WM_STATE_MAXIMIZED_HORZ" true (fun resp ->
891 state.maxhatom <- r32 resp 8;
893 sendintern sock "_NET_WM_STATE_FULLSCREEN" true (fun resp ->
894 state.fulsatom <- r32 resp 8;
895 if state.fulsatom != 0
896 then
897 state.fullscreen <-
898 (fun wid ->
899 let data = String.make 20 '\000' in
900 let fs, f =
901 match state.fs with
902 | NoFs -> Fs (-1, -1, -1, -1), 1
903 | Fs _ -> NoFs, 0
905 w32 data 0 f;
906 w32 data 4 state.fulsatom;
908 let cm = clientmessage 32 0 wid state.nwmsatom data in
909 let s = sendeventreq 0 root 0x180000 cm in
910 sendstr s sock;
911 state.fs <- fs;
916 let s = getgeometryreq wid in
917 syncsendwithrep sock 2.0 s (fun resp ->
918 glx wid;
919 let w = r16 resp 16
920 and h = r16 resp 18 in
921 state.w <- w;
922 state.h <- h;
925 | c ->
926 error "unknown conection setup response %d" (Char.code c)
929 let getauth haddr dnum =
930 let haddr =
931 if haddr = "localhost" || String.length haddr = 0
932 then
933 try Unix.gethostname ()
934 with exn ->
935 dolog "failed to resolve `%S': %s" haddr (exntos exn);
936 haddr
937 else haddr
939 let path =
940 try Sys.getenv "XAUTHORITY"
941 with Not_found ->
942 try Filename.concat (Sys.getenv "HOME") ".Xauthority"
943 with Not_found -> E.s
945 let readauth ic =
946 let input_string ic len =
947 let s = String.create len in
948 really_input ic s 0 len;
951 let r16be s =
952 let rb pos = Char.code (String.get s pos) in
953 (rb 1) lor ((rb 0) lsl 8)
955 let rec find () =
956 let rs () =
957 let s = input_string ic 2 in
958 let n = r16be s in
959 input_string ic n
961 let family = input_string ic 2 in
962 let addr = rs () in
963 let nums = rs () in
964 let optnum =
965 try Some (int_of_string nums)
966 with exn ->
967 dolog
968 "display number(%S) is not an integer (corrupt %S?): %s"
969 nums path (exntos exn);
970 None
972 let name = rs () in
973 let data = rs () in
975 vlog "family %S addr %S(%S) num %S(%d) name %S data %S"
976 family addr haddr nums dnum name data;
977 match optnum with
978 | Some num when addr = haddr && num = dnum ->
979 name, data
980 | _ -> find ()
982 let name, data =
983 try find ()
984 with
985 | End_of_file -> E.s, E.s
986 | exn ->
987 dolog "exception while reading X authority data (%S): %s"
988 path (exntos exn);
989 E.s, E.s
991 close_in ic;
992 name, data;
994 let opt =
996 if String.length path = 0
997 then None
998 else Some (open_in_bin path)
999 with exn ->
1000 if Sys.file_exists path
1001 then
1002 dolog "failed to open X authority file `%S' : %s"
1003 path (exntos exn);
1004 None
1006 match opt with
1007 | None -> E.s, E.s
1008 | Some ic -> readauth ic
1011 let init t w h osx =
1012 let d =
1013 try Sys.getenv "DISPLAY"
1014 with exn ->
1015 error "could not get DISPLAY evironment variable: %s"
1016 (exntos exn)
1018 let getnum w b e =
1019 if b = e
1020 then error "invalid DISPLAY(%s) %S" w d
1021 else
1022 let s = String.sub d b (e - b) in
1023 try int_of_string s
1024 with exn ->
1025 error "invalid DISPLAY %S can not parse %s(%S): %s"
1026 d w s (exntos exn)
1028 let rec phost pos =
1029 if pos = String.length d
1030 then error "invalid DISPLAY %S no display number specified" d
1031 else (
1032 if d.[pos] = ':'
1033 then
1034 let rec pdispnum pos1 =
1035 if pos1 = String.length d
1036 then getnum "display number" (pos+1) pos1, 0
1037 else
1038 match d.[pos1] with
1039 | '.' ->
1040 let dispnum = getnum "display number" (pos+1) pos1 in
1041 let rec pscreennum pos2 =
1042 if pos2 = String.length d
1043 then getnum "screen number" (pos1+1) pos2
1044 else
1045 match d.[pos2] with
1046 | '0' .. '9' -> pscreennum (pos2+1)
1047 | _ ->
1048 error "invalid DISPLAY %S, cannot parse screen number" d
1050 dispnum, pscreennum (pos1+1)
1051 | '0' .. '9' -> pdispnum (pos1+1)
1052 | _ ->
1053 error "invalid DISPLAY %S, cannot parse display number" d
1055 String.sub d 0 pos, pdispnum (pos+1)
1056 else phost (pos+1)
1059 let host, (dispnum, screennum) = phost 0 in
1060 let aname, adata = getauth host dispnum in
1061 let fd =
1062 let fd, addr =
1063 if osx || String.length host = 0 || host = "unix"
1064 then
1065 let addr =
1066 if osx
1067 then Unix.ADDR_UNIX d
1068 else Unix.ADDR_UNIX ("/tmp/.X11-unix/X" ^ string_of_int dispnum)
1070 let fd = Unix.socket Unix.PF_UNIX Unix.SOCK_STREAM 0 in
1071 fd, addr
1072 else
1073 let h =
1074 try Unix.gethostbyname host
1075 with exn ->
1076 error "cannot resolve %S: %s" host (exntos exn)
1078 let addr = h.Unix.h_addr_list.(0) in
1079 let port = 6000 + dispnum in
1080 let fd = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
1081 fd, (Unix.ADDR_INET (addr, port))
1083 try Unix.connect fd addr; fd
1084 with exn ->
1085 error "failed to connect to X: %s" (exntos exn)
1087 cloexec fd;
1088 let s = String.create 12 in
1089 let s = padcat s aname in
1090 let s = padcat s adata in
1091 s.[0] <- ordermagic;
1092 w16 s 2 11;
1093 w16 s 4 0;
1094 w16 s 6 (String.length aname);
1095 w16 s 8 (String.length adata);
1096 sendstr1 s 0 (String.length s) fd;
1097 state.sock <- fd;
1098 setup fd screennum w h;
1099 state.t <- t;
1100 fd, state.w, state.h;
1103 let settitle s =
1104 state.setwmname s;
1107 let setcursor cursor =
1108 if cursor != state.curcurs
1109 then
1110 let n =
1111 match cursor with
1112 | CURSOR_INHERIT -> -1
1113 | CURSOR_INFO -> 3
1114 | CURSOR_CYCLE -> 2
1115 | CURSOR_CROSSHAIR -> 0
1116 | CURSOR_TEXT -> 5
1118 let s = s32 (if n = -1 then 0 else state.idbase+2+n) in
1119 let s = changewindowattributesreq state.idbase (*cursor*)0x4000 s in
1120 sendstr s state.sock;
1121 state.curcurs <- cursor;
1124 let fullscreen () =
1125 state.fullscreen state.idbase;
1128 let metamask = 0x40;;
1129 let altmask = 8;;
1130 let shiftmask = 1;;
1131 let ctrlmask = 4;;
1133 let withalt mask = mask land altmask != 0;;
1134 let withctrl mask = mask land ctrlmask != 0;;
1135 let withshift mask = mask land shiftmask != 0;;
1136 let withmeta mask = mask land metamask != 0;;
1137 let withnone mask = mask land (altmask + ctrlmask + shiftmask + metamask) = 0;;
1139 let xlatt, xlatf =
1140 let t = Hashtbl.create 20
1141 and f = Hashtbl.create 20 in
1142 let add n nl k =
1143 List.iter (fun s -> Hashtbl.add t s k) (n::nl);
1144 Hashtbl.add f k n
1146 let addc c =
1147 let s = String.create 1 in
1148 s.[0] <- c;
1149 add s [] (Char.code c)
1151 let addcr a b =
1152 let an = Char.code a and bn = Char.code b in
1153 for i = an to bn do addc (Char.chr i) done;
1155 addcr '0' '9';
1156 addcr 'a' 'z';
1157 addcr 'A' 'Z';
1158 String.iter addc "`~!@#$%^&*()-_=+\\|[{]};:,./<>?";
1159 for i = 0 to 29 do add ("f" ^ string_of_int (i+1)) [] (0xffbe + i) done;
1160 add "space" [] 0x20;
1161 add "ret" ["return"; "enter"] 0xff0d;
1162 add "tab" [] 0xff09;
1163 add "left" [] 0xff51;
1164 add "right" [] 0xff53;
1165 add "home" [] 0xff50;
1166 add "end" [] 0xff57;
1167 add "ins" ["insert"] 0xff63;
1168 add "del" ["delete"] 0xffff;
1169 add "esc" ["escape"] 0xff1b;
1170 add "pgup" ["pageup"] 0xff55;
1171 add "pgdown" ["pagedown"] 0xff56;
1172 add "backspace" [] 0xff08;
1173 add "up" [] 0xff52;
1174 add "down" [] 0xff54;
1175 t, f;
1178 let keyname k =
1179 try Hashtbl.find xlatf k
1180 with Not_found -> Printf.sprintf "%#x" k;
1183 let namekey name =
1184 try Hashtbl.find xlatt name
1185 with Not_found ->
1186 if String.length name = 1
1187 then Char.code name.[0]
1188 else int_of_string name;