Fugly
[llpp.git] / wsi.ml
blob483ac5535d50d8e04a348dc0dba1fb97a3d2f8be
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 reshape _ _ = ()
27 method mouse _ _ _ _ _ = ()
28 method motion _ _ = ()
29 method pmotion _ _ = ()
30 method key _ _ = ()
31 method enter _ _ = ()
32 method leave = ()
33 method winstate _ = ()
34 method quit = exit 0
35 end;;
37 class type t = object
38 method display : unit
39 method expose : unit
40 method reshape : int -> int -> unit
41 method mouse : int -> bool -> int -> int -> int -> unit
42 method motion : int -> int -> unit
43 method pmotion : int -> int -> unit
44 method key : int -> int -> unit
45 method enter : int -> int -> unit
46 method leave : unit
47 method winstate : winstate list -> unit
48 method quit : unit
49 end;;
51 type state =
52 { mutable mink : int
53 ; mutable maxk : int
54 ; mutable keymap : int array array
55 ; fifo : (string -> unit) Queue.t
56 ; mutable seq : int
57 ; mutable protoatom : int
58 ; mutable deleatom : int
59 ; mutable nwmsatom : int
60 ; mutable maxvatom : int
61 ; mutable maxhatom : int
62 ; mutable fulsatom : int
63 ; mutable idbase : int
64 ; mutable fullscreen : (int -> unit)
65 ; mutable setwmname : (string -> unit)
66 ; mutable actwin : (unit -> unit)
67 ; mutable stringatom : int
68 ; mutable t : t
69 ; mutable sock : Unix.file_descr
70 ; mutable x : int
71 ; mutable y : int
72 ; mutable w : int
73 ; mutable h : int
74 ; mutable fs : fs
75 ; mutable curcurs : cursor
76 ; mutable capslmask : int
77 ; mutable numlmask : int
78 ; mutable levl3mask : int
79 ; mutable levl5mask : int
81 and fs =
82 | NoFs
83 | Fs of (int * int * int * int)
86 let state =
87 { mink = max_int
88 ; maxk = min_int
89 ; keymap = [||]
90 ; fifo = Queue.create ()
91 ; seq = 0
92 ; protoatom = -1
93 ; deleatom = -1
94 ; nwmsatom = -1
95 ; maxvatom = -1
96 ; maxhatom = -1
97 ; fulsatom = -1
98 ; idbase = -1
99 ; fullscreen = (fun _ -> ())
100 ; setwmname = (fun _ -> ())
101 ; actwin = (fun _ -> ())
102 ; sock = Unix.stdin
103 ; t = onot
104 ; x = -1
105 ; y = -1
106 ; w = -1
107 ; h = -1
108 ; fs = NoFs
109 ; stringatom = 31
110 ; curcurs = CURSOR_INHERIT
111 ; capslmask = 0
112 ; numlmask = 0
113 ; levl3mask = 0
114 ; levl5mask = 0
118 let w8 s pos i = String.set s pos (Char.chr (i land 0xff));;
120 let w16 s pos i =
121 w8 s pos i;
122 w8 s (pos+1) (i lsr 8);
125 let w32 s pos i =
126 w16 s pos i;
127 w16 s (pos+2) (i lsr 16);
130 let r16 s pos =
131 let rb pos1 = Char.code (String.get s (pos + pos1)) in
132 (rb 0) lor ((rb 1) lsl 8)
135 let r16s s pos =
136 let i = r16 s pos in
137 i - ((i land 0x8000) lsl 1);
140 let r8 s pos = Char.code (String.get s pos);;
142 let r32 s pos =
143 let rb pos1 = Char.code (String.get s (pos + pos1)) in
144 let l = (rb 0) lor ((rb 1) lsl 8)
145 and u = (rb 2) lor ((rb 3) lsl 8) in
146 (u lsl 16) lor l
149 let readstr sock n =
150 let s = String.create n in
151 let rec loop pos n =
152 let m = tempfailureretry (Unix.read sock s pos) n in
153 if m = 0
154 then state.t#quit;
155 if n != m
156 then (
157 ignore (tempfailureretry (Unix.select [sock] [] []) 0.01);
158 loop (pos + m) (n - m)
161 loop 0 n;
165 let sendstr1 s pos len sock =
166 vlog "%d => %S" state.seq s;
167 state.seq <- state.seq + 1;
168 let n = tempfailureretry (Unix.send sock s pos len) [] in
169 if n != len
170 then error "send %d returned %d" len n;
173 let updkmap sock resp =
174 let syms = r8 resp 1 in
175 let len = r32 resp 4 in
176 let data =
177 if len > 0
178 then readstr sock (4*len)
179 else ""
181 let m = len / syms in
182 state.keymap <- Array.make_matrix
183 (state.maxk - state.mink) syms 0xffffff;
184 let rec loop i = if i = m then () else
185 let k = i*4*syms in
186 let rec loop2 k l = if l = syms then () else
187 let v = r32 data k in
188 state.keymap.(i).(l) <- v;
189 loop2 (k+4) (l+1)
191 loop2 k 0;
192 loop (i+1);
194 loop 0;
197 let updmodmap sock resp =
198 let n = r8 resp 1 in
199 let len = r16 resp 4 in
200 let data =
201 if len > 0
202 then readstr sock (len*4)
203 else ""
205 let modmap = Array.make_matrix 8 n 0xffffff in
206 let rec loop l = if l = 8 then () else
207 let p = l*n in
208 let rec loop1 m = if m = n then () else
209 let p = p+m in
210 let code = r8 data p in
211 modmap.(l).(m) <- code;
212 if l = 1
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 capsloop i = if i = Array.length a || i > 3 then () else
219 let s = a.(i) in
220 if s = 0xffe5
221 then state.capslmask <- 2
222 else capsloop (i+1)
224 capsloop 0;
226 else (
227 if l > 3
228 then (
229 let ki = code - state.mink in
230 if ki >= 0
231 then
232 let a = state.keymap.(ki) in
233 let rec lloop i = if i = Array.length a || i > 3 then () else
234 let s = a.(i) in
235 match s with
236 | 0xfe03 -> state.levl3mask <- 1 lsl l
237 | 0xfe11 -> state.levl5mask <- 1 lsl l
238 | 0xff7f -> state.numlmask <- 1 lsl l
239 | _ -> lloop (i+1)
241 lloop 0;
244 loop1 (m+1)
246 loop1 0;
247 loop (l+1)
249 loop 0;
252 let sendwithrep sock s f =
253 Queue.push f state.fifo;
254 sendstr1 s 0 (String.length s) sock;
257 let padcatl ss =
258 let b = Buffer.create 16 in
259 List.iter (Buffer.add_string b) ss;
260 let bl = Buffer.length b in
261 let pl = bl land 3 in
262 if pl != 0
263 then (
264 let pad = "123" in
265 Buffer.add_substring b pad 0 (4 - pl);
267 Buffer.contents b;
270 let padcat s1 s2 = padcatl [s1; s2];;
272 let internreq name onlyifexists =
273 let s = "\016\000\000\000\000\000\000\000" in
274 let s = padcat s name in
275 if onlyifexists then w8 s 1 1;
276 w16 s 2 (String.length s / 4);
277 w16 s 4 (String.length name);
281 let sendintern sock s onlyifexists f =
282 let s = internreq s onlyifexists in
283 sendwithrep sock s f;
286 let createwindowreq wid parent x y w h bw mask =
287 let s = "\001\000\009\000wwwwppppxxyywwhhbwccvvvvmmmmeeee" in
288 w32 s 4 wid;
289 w32 s 8 parent;
290 w16 s 12 x;
291 w16 s 14 y;
292 w16 s 16 w;
293 w16 s 18 h;
294 w16 s 20 bw;
295 w16 s 22 1;
296 w32 s 24 0;
297 w32 s 28 0x800; (* eventmask *)
298 w32 s 32 mask;
302 let getgeometryreq wid =
303 let s = "\014u\002\000dddd" in
304 w32 s 4 wid;
308 let mapreq wid =
309 let s = "\008u\002\000wwww" in
310 w32 s 4 wid;
314 let getkeymapreq first count =
315 let s = "\101u\002\000fcuu" in
316 w8 s 4 first;
317 w8 s 5 count;
321 let changepropreq wid prop typ format props =
322 let s = "\018\000llwwwwppppttttfuuuLLLL" in
323 let s = padcat s props in
324 w16 s 2 (String.length s / 4);
325 w32 s 4 wid;
326 w32 s 8 prop;
327 w32 s 12 typ;
328 w8 s 16 format;
329 let ful = String.length props / (match format with
330 | 8 -> 1
331 | 16 -> 2
332 | 32 -> 4
333 | n -> error "no idea what %d means" n)
335 w32 s 20 ful;
339 let getpropreq delete wid prop typ =
340 let s = "\020\000\006\000wwwwppppttttooooLLLL" in
341 if delete then w8 s 1 1;
342 w32 s 4 wid;
343 w32 s 8 prop;
344 w32 s 12 typ;
345 w32 s 16 0;
346 w32 s 20 2;
350 let openfontreq fid name =
351 let s = "\045ullffffnnuu" in
352 let s = padcat s name in
353 w16 s 2 (String.length s / 4);
354 w32 s 4 fid;
355 w16 s 8 (String.length name);
359 let createglyphcursorreq fid cid cindex =
360 let s = "\094u\008\000ccccffffffffssmmrrggbbRRGGBB" in
361 w32 s 4 cid;
362 w32 s 8 fid;
363 w32 s 12 fid;
364 w16 s 16 cindex;
365 w16 s 18 (cindex+1);
366 w16 s 20 0;
367 w16 s 22 0;
368 w16 s 24 0;
369 w16 s 26 0xffff;
370 w16 s 28 0xffff;
371 w16 s 30 0xffff;
375 let changewindowattributesreq wid mask attrs =
376 let s = "\002ullwwwwmmmm" in
377 let s = padcat s attrs in
378 w16 s 2 (String.length s / 4);
379 w32 s 4 wid;
380 w32 s 8 mask;
384 let configurewindowreq wid mask values =
385 let s = "\012ullwwwwmmuu" in
386 let s = padcat s values in
387 w16 s 2 (String.length s / 4);
388 w32 s 4 wid;
389 w16 s 8 mask;
393 let s32 n =
394 let s = "1234" in
395 w32 s 0 n;
399 let clientmessage format seq wid typ data =
400 let s = "\033fsswwwwtttt" in
401 let s = padcat s data in
402 w8 s 1 format;
403 w16 s 2 seq;
404 w32 s 4 wid;
405 w32 s 8 typ;
409 let sendeventreq propagate destwid mask data =
410 let s = "\025p\011\000wwwwmmmm" in
411 let s = padcat s data in
412 w8 s 1 propagate;
413 w32 s 4 destwid;
414 w32 s 8 mask;
418 let getmodifiermappingreq () =
419 let s = "\119u\001\000" in
423 let getkeysym code mask =
424 let pkpk = state.keymap.(code-state.mink).(0) in
425 if (pkpk >= 0xff80 && pkpk <= 0xffbd)
426 || (pkpk >= 0x11000000 && pkpk <= 0x1100ffff)
427 then (
428 if mask land state.numlmask != 0
429 then
430 let keysym = state.keymap.(code-state.mink).(1) in
431 if keysym = 0 then pkpk else keysym
432 else pkpk
434 else (
435 let shift = (mask land 1) lxor ((mask land state.capslmask) lsr 1) in
436 let index =
437 let l3 = (mask land state.levl3mask) != 0 in
438 let l4 = (mask land state.levl5mask) != 0 in
439 shift +
440 if l3 then (if l4 then 8 else 4) else (if l4 then 6 else 0)
442 let keysym = state.keymap.(code-state.mink).(index) in
443 if index land 1 = 1 && keysym = 0
444 then state.keymap.(code-state.mink).(index - 1)
445 else keysym
449 let readresp sock =
450 let resp = readstr sock 32 in
451 let opcode = r8 resp 0 in
452 match opcode land lnot 0x80 with
453 | 0 -> (* error *)
454 let s = resp in
455 let code = r8 s 1
456 and serial = r16 s 2
457 and resid = r32 resp 4
458 and min = r16 s 8
459 and maj = r8 s 10 in
460 error "code=%d serial=%d resid=%#x min=%d maj=%d\n%S"
461 code serial resid min maj resp;
463 | 1 -> (* response *)
464 let rep = Queue.pop state.fifo in
465 rep resp;
467 | 2 -> (* key press *)
468 if Array.length state.keymap > 0
469 then
470 let code = r8 resp 1 in
471 let mask = r16 resp 28 in
472 let keysym = getkeysym code mask in
473 vlog "keysym = %x %c mask %#x code %d"
474 keysym (Char.unsafe_chr keysym) mask code;
475 state.t#key keysym mask;
477 | 3 -> (* key release *)
478 if Array.length state.keymap > 0
479 then
480 let code = r8 resp 1 in
481 let mask = r16 resp 28 in
482 let keysym = getkeysym code mask in
483 vlog "release keysym = %x %c mask %#x code %#d"
484 keysym (Char.unsafe_chr keysym) mask code
486 | 4 -> (* buttonpress *)
487 let n = r8 resp 1
488 and x = r16s resp 24
489 and y = r16s resp 26
490 and m = r16 resp 28 in
491 state.t#mouse n true x y m;
492 vlog "press %d" n
494 | 5 -> (* buttonrelease *)
495 let n = r8 resp 1
496 and x = r16s resp 24
497 and y = r16s resp 26
498 and m = r16 resp 28 in
499 state.t#mouse n false x y m;
500 vlog "release %d %d %d" n x y
502 | 6 -> (* motion *)
503 let x = r16s resp 24 in
504 let y = r16s resp 26 in
505 let m = r16 resp 28 in
506 if m land 0x1f00 = 0
507 then state.t#pmotion x y
508 else state.t#motion x y;
509 vlog "move %dx%d => %d" x y m
511 | 7 -> (* enter *)
512 let x = r16s resp 24
513 and y = r16s resp 26 in
514 state.t#enter x y;
515 vlog "enter %d %d" x y
517 | 8 -> (* leave *)
518 state.t#leave
520 | 18 -> vlog "unmap";
522 | 19 -> (* map *)
523 vlog "map";
525 | 12 -> (* exposure *)
526 vlog "exposure";
527 state.t#expose
529 | 15 -> (* visibility *)
530 let vis = r8 resp 8 in
531 if vis != 2 then state.t#expose;
532 vlog "visibility %d" vis;
534 | 34 -> (* mapping *)
535 state.keymap <- [||];
536 let s = getkeymapreq state.mink (state.maxk-state.mink-1) in
537 sendwithrep sock s (updkmap sock);
538 state.capslmask <- 0;
539 state.levl3mask <- 0;
540 state.levl5mask <- 0;
541 state.numlmask <- 0;
542 let s = getmodifiermappingreq () in
543 sendwithrep sock s (updmodmap sock);
546 | 33 -> (* clientmessage *)
547 let atom = r32 resp 8 in
548 if atom = state.protoatom
549 then (
550 let atom = r32 resp 12 in
551 if atom = state.deleatom
552 then state.t#quit;
554 vlog "atom %#x" atom
556 | 21 -> (* reparent *)
557 vlog "reparent"
559 | 22 -> (* configure *)
560 let x = r16s resp 16
561 and y = r16s resp 18
562 and w = r16 resp 20
563 and h = r16 resp 22 in
564 vlog "configure cur [%d %d %d %d] conf [%d %d %d %d]"
565 state.x state.y state.w state.h
566 x y w h
568 glxsync ();
569 if w != state.w || h != state.h
570 then (
571 state.t#reshape w h;
573 state.w <- w;
574 state.h <- h;
575 state.x <- x;
576 state.y <- y;
577 state.t#expose
579 | 28 ->
580 let atom = r32 resp 8 in
581 if atom = state.nwmsatom
582 then
583 let s = getpropreq false state.idbase atom 4 in
584 sendwithrep sock s (fun resp ->
585 let len = r32 resp 4 in
586 let nitems = r32 resp 16 in
587 let wsl =
588 if len = 0
589 then []
590 else
591 let s = readstr sock (len*4) in
592 let rec loop wsl i = if i = nitems then wsl else
593 let atom = r32 s (i*4) in
594 let wsl =
595 if atom = state.maxhatom
596 then MaxHorz::wsl
597 else (
598 if atom = state.maxvatom
599 then MaxVert::wsl
600 else (
601 if atom = state.fulsatom
602 then (
603 state.fs <- Fs (state.x, state.y, state.w, state.h);
604 Fullscreen::wsl
606 else wsl
609 in loop wsl (i+1)
611 loop [] 0
613 state.t#winstate (List.sort compare wsl)
616 | n ->
617 dolog "event %d %S" n resp
620 let readresp sock =
621 let rec loop () =
622 readresp sock;
623 if hasdata sock then loop ();
625 loop ();
628 let sendstr s ?(pos=0) ?(len=String.length s) sock =
629 sendstr1 s pos len sock;
630 if hasdata sock then readresp sock;
633 let reshape w h =
634 if state.fs = NoFs
635 then
636 let s = "wwuuhhuu" in
637 w32 s 0 w;
638 w32 s 4 h;
639 let s = configurewindowreq state.idbase 0x000c s in
640 sendstr s state.sock;
641 else state.fullscreen state.idbase
644 let activatewin () =
645 state.actwin ();
648 let syncsendwithrep sock secstowait s f =
649 let completed = ref false in
650 sendwithrep sock s (fun resp -> f resp; completed := true);
651 let now = Unix.gettimeofday in
652 let deadline = now () +. secstowait in
653 let rec readtillcompletion () =
654 let sf deadline =
655 let timeout = deadline -. now () in
656 if timeout <= 0.0
657 then [], [], []
658 else Unix.select [sock] [] [] timeout
660 let r, _, _ = tempfailureretry sf deadline in
661 match r with
662 | [] -> error "didn't get X response in %f seconds, aborting" secstowait
663 | _ ->
664 readresp sock;
665 if not !completed
666 then readtillcompletion ()
668 readtillcompletion ();
671 let syncmapwin () =
672 let s = mapreq state.idbase in
673 syncsendwithrep state.sock 1.0 s (fun _ -> ());
676 let syncsendintern sock secstowait s onlyifexists f =
677 let s = internreq s onlyifexists in
678 syncsendwithrep sock secstowait s f;
681 let setup sock screennum w h =
682 let s = readstr sock 2 in
683 let n = String.length s in
684 if n != 2
685 then error "failed to read X connection setup response n=%d" n;
686 match s.[0] with
687 | '\000' ->
688 let reasonlen = r8 s 1 in
689 let s = readstr sock 6 in
690 let maj = r16 s 0
691 and min = r16 s 2
692 and add = r16 s 4 in
693 let len = add*4 in
694 let data = readstr sock len in
695 let reason = String.sub data 0 reasonlen in
696 error "X connection failed maj=%d min=%d reason=%S"
697 maj min reason
699 | '\002' -> failwith "X connection setup failed: authentication required";
701 | '\001' ->
702 let s = readstr sock 38 in
703 let maj = r16 s 0
704 and min = r16 s 2
705 and add = r16 s 4
706 and idbase = r32 s 10
707 and idmask = r32 s 14
708 and vlen = r16 s 22
709 and screens = r8 s 26
710 and formats = r8 s 27
711 and minkk = r8 s 32
712 and maxkk = r8 s 33 in
713 let data = readstr sock (4*add-32) in
714 let vendor = String.sub data 0 vlen in
715 let pos = ((vlen+3) land lnot 3) + formats*8 in
717 if screennum >= screens
718 then error "invalid screen %d, max %d" screennum (screens-1);
720 let pos =
721 let s = data in
722 let rec findscreen n pos = if n = screennum then pos else
723 let pos =
724 let ndepths = r8 s (pos+39) in
725 let rec skipdepths n pos = if n = ndepths then pos else
726 let pos =
727 let nvisiuals = r16 s (pos+2) in
728 pos + nvisiuals*24 + 8
730 skipdepths (n+1) pos
732 skipdepths n (pos+40)
734 findscreen (n+1) pos
736 findscreen 0 pos
738 let root = r32 data pos in
739 let rootw = r16 data (pos+20)
740 and rooth = r16 data (pos+22) in
741 state.mink <- minkk;
742 state.maxk <- maxkk;
743 state.idbase <- idbase;
744 vlog "vendor = %S, maj=%d min=%d" vendor maj min;
745 vlog "screens = %d formats = %d" screens formats;
746 vlog "minkk = %d maxkk = %d" minkk maxkk;
747 vlog "idbase = %#x idmask = %#x" idbase idmask;
748 vlog "root=%#x %dx%d" root rootw rooth;
750 let mask = 0
751 + 0x00000001 (* KeyPress *)
752 (* + 0x00000002 *) (* KeyRelease *)
753 + 0x00000004 (* ButtonPress *)
754 + 0x00000008 (* ButtonRelease *)
755 + 0x00000010 (* EnterWindow *)
756 + 0x00000020 (* LeaveWindow *)
757 + 0x00000040 (* PointerMotion *)
758 (* + 0x00000080 *) (* PointerMotionHint *)
759 (* + 0x00000100 *) (* Button1Motion *)
760 (* + 0x00000200 *) (* Button2Motion *)
761 (* + 0x00000400 *) (* Button3Motion *)
762 (* + 0x00000800 *) (* Button4Motion *)
763 (* + 0x00001000 *) (* Button5Motion *)
764 + 0x00002000 (* ButtonMotion *)
765 (* + 0x00004000 *) (* KeymapState *)
766 + 0x00008000 (* Exposure *)
767 + 0x00010000 (* VisibilityChange *)
768 + 0x00020000 (* StructureNotify *)
769 (* + 0x00040000 *) (* ResizeRedirect *)
770 (* + 0x00080000 *) (* SubstructureNotify *)
771 (* + 0x00100000 *) (* SubstructureRedirect *)
772 (* + 0x00200000 *) (* FocusChange *)
773 + 0x00400000 (* PropertyChange *)
774 (* + 0x00800000 *) (* ColormapChange *)
775 (* + 0x01000000 *) (* OwnerGrabButton *)
777 let wid = state.idbase in
778 let s = createwindowreq wid root 0 0 w h 0 mask in
779 sendstr s sock;
781 sendintern sock "WM_PROTOCOLS" false (fun resp ->
782 state.protoatom <- r32 resp 8;
783 sendintern sock "WM_DELETE_WINDOW" false (fun resp ->
784 state.deleatom <- r32 resp 8;
785 let s = s32 state.deleatom in
786 let s = changepropreq wid state.protoatom 4 32 s in
787 sendstr s sock;
791 sendintern sock "WM_CLIENT_MACHINE" false (fun resp ->
792 let atom = r32 resp 8 in
793 let empty = "" in
794 let hostname =
795 try Unix.gethostname ()
796 with exn ->
797 dolog "error getting host name: %s" (exntos exn);
798 empty
800 if hostname != empty
801 then
802 let s = changepropreq wid atom state.stringatom 8 hostname in
803 sendstr s sock;
804 sendintern sock "_NET_WM_PID" false (fun resp ->
805 let atom = r32 resp 8 in
806 let pid = Unix.getpid () in
807 let s = s32 pid in
808 let s = changepropreq wid atom (* cardinal *)6 32 s in
809 sendstr s sock;
813 state.actwin <- (fun () ->
814 let s = "\000uuu" in
815 let s = configurewindowreq state.idbase 0x40 s in
816 sendstr s state.sock;
817 let s = mapreq state.idbase in
818 sendstr s state.sock;
821 sendintern sock "_NET_ACTIVE_WINDOW" true (fun resp ->
822 let atom = r32 resp 8 in
823 state.actwin <- (fun () ->
824 let data = String.make 20 '\000' in
825 let cm = clientmessage 32 0 wid atom data in
826 let s = sendeventreq 0 root 0x180000 cm in
827 sendstr s state.sock;
831 syncsendintern sock 2.0 "WM_CLASS" false (fun resp ->
832 let atom = r32 resp 8 in
833 let llpp = "llpp\000llpp\000" in
834 let s = changepropreq wid atom 31 8 llpp in
835 sendstr s sock;
838 let s = getkeymapreq state.mink (state.maxk-state.mink) in
839 sendwithrep sock s (updkmap sock);
841 let s = getmodifiermappingreq () in
842 sendwithrep sock s (updmodmap sock);
844 let s = openfontreq (wid+1) "cursor" in
845 sendstr s sock;
847 Array.iteri (fun i glyphindex ->
848 let s = createglyphcursorreq (wid+1) (wid+2+i) glyphindex in
849 sendstr s sock;
850 ) [|34;48;50;58;128;152|];
852 sendintern sock "UTF8_STRING" true (fun resp ->
853 let atom = r32 resp 8 in
854 if atom != 0
855 then state.stringatom <- atom;
858 let setwmname s =
859 let s = changepropreq state.idbase 39 state.stringatom 8 s in
860 sendstr s state.sock;
862 state.setwmname <- setwmname;
863 sendintern sock "_NET_WM_NAME" true (fun resp ->
864 let atom = r32 resp 8 in
865 if atom != 0
866 then state.setwmname <- (fun s ->
867 setwmname s;
868 let s = changepropreq state.idbase atom state.stringatom 8 s in
869 sendstr s state.sock;
873 state.fullscreen <- (fun wid ->
874 let s = "xxuuyyuuwwuuhhuu" in
875 match state.fs with
876 | NoFs ->
877 w32 s 0 0;
878 w32 s 4 0;
879 w32 s 8 rootw;
880 w32 s 12 rooth;
881 let s = configurewindowreq wid 0x000f s in
882 sendstr s state.sock;
883 state.fs <- Fs (state.x, state.y, state.w, state.h);
885 | Fs (x, y, w, h) ->
886 w32 s 0 x;
887 w32 s 4 y;
888 w32 s 8 w;
889 w32 s 12 h;
890 let s = configurewindowreq wid 0x000f s in
891 sendstr s state.sock;
892 state.fs <- NoFs;
895 sendintern sock "_NET_WM_STATE" true (fun resp ->
896 state.nwmsatom <- r32 resp 8;
897 if state.nwmsatom != 0
898 then (
899 sendintern sock "_NET_WM_STATE_MAXIMIZED_VERT" true (fun resp ->
900 state.maxvatom <- r32 resp 8;
902 sendintern sock "_NET_WM_STATE_MAXIMIZED_HORZ" true (fun resp ->
903 state.maxhatom <- r32 resp 8;
905 sendintern sock "_NET_WM_STATE_FULLSCREEN" true (fun resp ->
906 state.fulsatom <- r32 resp 8;
907 if state.fulsatom != 0
908 then
909 state.fullscreen <-
910 (fun wid ->
911 let data = String.make 20 '\000' in
912 let fs, f =
913 match state.fs with
914 | NoFs -> Fs (-1, -1, -1, -1), 1
915 | Fs _ -> NoFs, 0
917 w32 data 0 f;
918 w32 data 4 state.fulsatom;
920 let cm = clientmessage 32 0 wid state.nwmsatom data in
921 let s = sendeventreq 0 root 0x180000 cm in
922 sendstr s sock;
923 state.fs <- fs;
928 let s = getgeometryreq wid in
929 syncsendwithrep sock 2.0 s (fun resp ->
930 glx wid;
931 let w = r16 resp 16
932 and h = r16 resp 18 in
933 state.w <- w;
934 state.h <- h;
937 | c ->
938 error "unknown conection setup response %d" (Char.code c)
941 let getauth haddr dnum =
942 let haddr =
943 if haddr = "localhost" || String.length haddr = 0
944 then
945 try Unix.gethostname ()
946 with exn ->
947 dolog "failed to resolve `%S': %s" haddr (exntos exn);
948 haddr
949 else haddr
951 let path =
952 try Sys.getenv "XAUTHORITY"
953 with Not_found ->
954 try Filename.concat (Sys.getenv "HOME") ".Xauthority"
955 with Not_found -> ""
957 let readauth ic =
958 let input_string ic len =
959 let s = String.create len in
960 really_input ic s 0 len;
963 let r16 s =
964 let rb pos = Char.code (String.get s pos) in
965 (rb 1) lor ((rb 0) lsl 8)
967 let rec find () =
968 let rs () =
969 let s = input_string ic 2 in
970 let n = r16 s in
971 input_string ic n
973 let family = input_string ic 2 in
974 let addr = rs () in
975 let nums = rs () in
976 let optnum =
977 try Some (int_of_string nums)
978 with exn ->
979 dolog
980 "display number(%S) is not an integer (corrupt %S?): %s"
981 nums path (exntos exn);
982 None
984 let name = rs () in
985 let data = rs () in
987 vlog "family %S addr %S(%S) num %S(%d) name %S data %S"
988 family addr haddr nums dnum name data;
989 match optnum with
990 | Some num when addr = haddr && num = dnum ->
991 name, data
992 | _ -> find ()
994 let name, data =
995 try find ()
996 with
997 | End_of_file -> "", ""
998 | exn ->
999 dolog "exception while reading X authority data (%S): %s"
1000 path (exntos exn);
1001 "", ""
1003 close_in ic;
1004 name, data;
1006 let opt =
1008 if String.length path = 0
1009 then None
1010 else Some (open_in_bin path)
1011 with exn ->
1012 if Sys.file_exists path
1013 then
1014 dolog "failed to open X authority file `%S' : %s"
1015 path (exntos exn);
1016 None
1018 match opt with
1019 | None -> "", ""
1020 | Some ic -> readauth ic
1023 let init t w h osx =
1024 let d =
1025 try Sys.getenv "DISPLAY"
1026 with exn ->
1027 error "could not get DISPLAY evironment variable: %s"
1028 (exntos exn)
1030 let getnum w b e =
1031 if b = e
1032 then error "invalid DISPLAY(%s) %S" w d
1033 else
1034 let s = String.sub d b (e - b) in
1035 try int_of_string s
1036 with exn ->
1037 error "invalid DISPLAY %S can not parse %s(%S): %s"
1038 d w s (exntos exn)
1040 let rec phost pos =
1041 if pos = String.length d
1042 then error "invalid DISPLAY %S no display number specified" d
1043 else (
1044 if d.[pos] = ':'
1045 then
1046 let rec pdispnum pos1 =
1047 if pos1 = String.length d
1048 then getnum "display number" (pos+1) pos1, 0
1049 else
1050 match d.[pos1] with
1051 | '.' ->
1052 let dispnum = getnum "display number" (pos+1) pos1 in
1053 let rec pscreennum pos2 =
1054 if pos2 = String.length d
1055 then getnum "screen number" (pos1+1) pos2
1056 else
1057 match d.[pos2] with
1058 | '0' .. '9' -> pscreennum (pos2+1)
1059 | _ ->
1060 error "invalid DISPLAY %S, cannot parse screen number" d
1062 dispnum, pscreennum (pos1+1)
1063 | '0' .. '9' -> pdispnum (pos1+1)
1064 | _ ->
1065 error "invalid DISPLAY %S, cannot parse display number" d
1067 String.sub d 0 pos, pdispnum (pos+1)
1068 else phost (pos+1)
1071 let host, (dispnum, screennum) = phost 0 in
1072 let aname, adata = getauth host dispnum in
1073 let fd =
1074 let fd, addr =
1075 if osx || String.length host = 0 || host = "unix"
1076 then
1077 let addr =
1078 if osx
1079 then Unix.ADDR_UNIX d
1080 else Unix.ADDR_UNIX ("/tmp/.X11-unix/X" ^ string_of_int dispnum)
1082 let fd = Unix.socket Unix.PF_UNIX Unix.SOCK_STREAM 0 in
1083 fd, addr
1084 else
1085 let h =
1086 try Unix.gethostbyname host
1087 with exn ->
1088 error "cannot resolve %S: %s" host (exntos exn)
1090 let addr = h.Unix.h_addr_list.(0) in
1091 let port = 6000 + dispnum in
1092 let fd = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
1093 fd, (Unix.ADDR_INET (addr, port))
1095 try Unix.connect fd addr; fd
1096 with exn ->
1097 error "failed to connect to X: %s" (exntos exn)
1099 cloexec fd;
1100 let s = "luMMmmnndduu" in
1101 let s = padcat s aname in
1102 let s = padcat s adata in
1103 w16 s 2 11;
1104 w16 s 4 0;
1105 w16 s 6 (String.length aname);
1106 w16 s 8 (String.length adata);
1107 sendstr1 s 0 (String.length s) fd;
1108 state.sock <- fd;
1109 setup fd screennum w h;
1110 state.t <- t;
1111 fd, state.w, state.h;
1114 let settitle s =
1115 state.setwmname s;
1118 let setcursor cursor =
1119 if cursor != state.curcurs
1120 then
1121 let n =
1122 match cursor with
1123 | CURSOR_INHERIT -> -1
1124 | CURSOR_INFO -> 3
1125 | CURSOR_CYCLE -> 2
1126 | CURSOR_CROSSHAIR -> 0
1127 | CURSOR_TEXT -> 5
1129 let s = s32 (if n = -1 then 0 else state.idbase+2+n) in
1130 let s = changewindowattributesreq state.idbase (*cursor*)0x4000 s in
1131 sendstr s state.sock;
1132 state.curcurs <- cursor;
1135 let fullscreen () =
1136 state.fullscreen state.idbase;
1139 let metamask = 0x40;;
1140 let altmask = 8;;
1141 let shiftmask = 1;;
1142 let ctrlmask = 4;;
1144 let withalt mask = mask land altmask != 0;;
1145 let withctrl mask = mask land ctrlmask != 0;;
1146 let withshift mask = mask land shiftmask != 0;;
1147 let withmeta mask = mask land metamask != 0;;
1148 let withnone mask = mask land (altmask + ctrlmask + shiftmask + metamask) = 0;;
1150 let xlatt, xlatf =
1151 let t = Hashtbl.create 20
1152 and f = Hashtbl.create 20 in
1153 let add n nl k =
1154 List.iter (fun s -> Hashtbl.add t s k) (n::nl);
1155 Hashtbl.add f k n
1157 let addc c =
1158 let s = String.create 1 in
1159 s.[0] <- c;
1160 add s [] (Char.code c)
1162 let addcr a b =
1163 let an = Char.code a and bn = Char.code b in
1164 for i = an to bn do addc (Char.chr i) done;
1166 addcr '0' '9';
1167 addcr 'a' 'z';
1168 addcr 'A' 'Z';
1169 String.iter addc "`~!@#$%^&*()-_=+\\|[{]};:,./<>?";
1170 for i = 0 to 29 do add ("f" ^ string_of_int (i+1)) [] (0xffbe + i) done;
1171 add "space" [] 0x20;
1172 add "ret" ["return"; "enter"] 0xff0d;
1173 add "tab" [] 0xff09;
1174 add "left" [] 0xff51;
1175 add "right" [] 0xff53;
1176 add "home" [] 0xff50;
1177 add "end" [] 0xff57;
1178 add "ins" ["insert"] 0xff63;
1179 add "del" ["delete"] 0xffff;
1180 add "esc" ["escape"] 0xff1b;
1181 add "pgup" ["pageup"] 0xff55;
1182 add "pgdown" ["pagedown"] 0xff56;
1183 add "backspace" [] 0xff08;
1184 add "up" [] 0xff52;
1185 add "down" [] 0xff54;
1186 t, f;
1189 let keyname k =
1190 try Hashtbl.find xlatf k
1191 with Not_found -> Printf.sprintf "%#x" k;
1194 let namekey name =
1195 try Hashtbl.find xlatt name
1196 with Not_found ->
1197 if String.length name = 1
1198 then Char.code name.[0]
1199 else int_of_string name;