Fix typo
[llpp.git] / wsi.ml
blobc5be67456eb59d0691832664a17e4ee123efdd85
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 = [||]
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 let w8 s pos i = String.set s pos (Char.chr (i land 0xff));;
122 let w16 s pos i =
123 w8 s pos i;
124 w8 s (pos+1) (i lsr 8);
127 let w32 s pos i =
128 w16 s pos i;
129 w16 s (pos+2) (i lsr 16);
132 let r16 s pos =
133 let rb pos1 = Char.code (String.get s (pos + pos1)) in
134 (rb 0) lor ((rb 1) lsl 8)
137 let r16s s pos =
138 let i = r16 s pos in
139 i - ((i land 0x8000) lsl 1);
142 let r8 s pos = Char.code (String.get s pos);;
144 let r32 s pos =
145 let rb pos1 = Char.code (String.get s (pos + pos1)) in
146 let l = (rb 0) lor ((rb 1) lsl 8)
147 and u = (rb 2) lor ((rb 3) lsl 8) in
148 (u lsl 16) lor l
151 let readstr sock n =
152 let s = String.create n in
153 let rec loop pos n =
154 let m = tempfailureretry (Unix.read sock s pos) n in
155 if m = 0
156 then state.t#quit;
157 if n != m
158 then (
159 ignore (tempfailureretry (Unix.select [sock] [] []) 0.01);
160 loop (pos + m) (n - m)
163 loop 0 n;
167 let sendstr1 s pos len sock =
168 vlog "%d => %S" state.seq s;
169 state.seq <- state.seq + 1;
170 let n = tempfailureretry (Unix.send sock s pos len) [] in
171 if n != len
172 then error "send %d returned %d" len n;
175 let updkmap sock resp =
176 let syms = r8 resp 1 in
177 let len = r32 resp 4 in
178 let data =
179 if len > 0
180 then readstr sock (4*len)
181 else ""
183 let m = len / syms in
184 state.keymap <- Array.make_matrix
185 (state.maxk - state.mink) syms 0xffffff;
186 let rec loop i = if i = m then () else
187 let k = i*4*syms in
188 let rec loop2 k l = if l = syms then () else
189 let v = r32 data k in
190 state.keymap.(i).(l) <- v;
191 loop2 (k+4) (l+1)
193 loop2 k 0;
194 loop (i+1);
196 loop 0;
199 let updmodmap sock resp =
200 let n = r8 resp 1 in
201 let len = r16 resp 4 in
202 let data =
203 if len > 0
204 then readstr sock (len*4)
205 else ""
207 let modmap = Array.make_matrix 8 n 0xffffff in
208 let rec loop l = if l = 8 then () else
209 let p = l*n in
210 let rec loop1 m = if m = n then () else
211 let p = p+m in
212 let code = r8 data p in
213 modmap.(l).(m) <- code;
214 if l = 1
215 then (
216 let ki = code - state.mink in
217 if ki >= 0
218 then
219 let a = state.keymap.(ki) in
220 let rec capsloop i = if i = Array.length a || i > 3 then () else
221 let s = a.(i) in
222 if s = 0xffe5
223 then state.capslmask <- 2
224 else capsloop (i+1)
226 capsloop 0;
228 else (
229 if l > 3
230 then (
231 let ki = code - state.mink in
232 if ki >= 0
233 then
234 let a = state.keymap.(ki) in
235 let rec lloop i = if i = Array.length a || i > 3 then () else
236 let s = a.(i) in
237 match s with
238 | 0xfe03 -> state.levl3mask <- 1 lsl l
239 | 0xfe11 -> state.levl5mask <- 1 lsl l
240 | 0xff7f -> state.numlmask <- 1 lsl l
241 | _ -> lloop (i+1)
243 lloop 0;
246 loop1 (m+1)
248 loop1 0;
249 loop (l+1)
251 loop 0;
254 let sendwithrep sock s f =
255 Queue.push f state.fifo;
256 sendstr1 s 0 (String.length s) sock;
259 let padcatl ss =
260 let b = Buffer.create 16 in
261 List.iter (Buffer.add_string b) ss;
262 let bl = Buffer.length b in
263 let pl = bl land 3 in
264 if pl != 0
265 then (
266 let pad = "123" in
267 Buffer.add_substring b pad 0 (4 - pl);
269 Buffer.contents b;
272 let padcat s1 s2 = padcatl [s1; s2];;
274 let internreq name onlyifexists =
275 let s = "\016\000\000\000\000\000\000\000" in
276 let s = padcat s name in
277 if onlyifexists then w8 s 1 1;
278 w16 s 2 (String.length s / 4);
279 w16 s 4 (String.length name);
283 let sendintern sock s onlyifexists f =
284 let s = internreq s onlyifexists in
285 sendwithrep sock s f;
288 let createwindowreq wid parent x y w h bw mask =
289 let s = "\001\000\009\000wwwwppppxxyywwhhbwccvvvvmmmmeeee" in
290 w32 s 4 wid;
291 w32 s 8 parent;
292 w16 s 12 x;
293 w16 s 14 y;
294 w16 s 16 w;
295 w16 s 18 h;
296 w16 s 20 bw;
297 w16 s 22 1;
298 w32 s 24 0;
299 w32 s 28 0x800; (* eventmask *)
300 w32 s 32 mask;
304 let getgeometryreq wid =
305 let s = "\014u\002\000dddd" in
306 w32 s 4 wid;
310 let mapreq wid =
311 let s = "\008u\002\000wwww" in
312 w32 s 4 wid;
316 let getkeymapreq first count =
317 let s = "\101u\002\000fcuu" in
318 w8 s 4 first;
319 w8 s 5 count;
323 let changepropreq wid prop typ format props =
324 let s = "\018\000llwwwwppppttttfuuuLLLL" in
325 let s = padcat s props in
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 = "\020\000\006\000wwwwppppttttooooLLLL" in
343 if delete then w8 s 1 1;
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 = "\045ullffffnnuu" 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 = "\094u\008\000ccccffffffffssmmrrggbbRRGGBB" 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 = "\002ullwwwwmmmm" 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 = "\012ullwwwwmmuu" 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 = "\033fsswwwwtttt" 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 = "\025p\011\000wwwwmmmm" in
413 let s = padcat s data in
414 w8 s 1 propagate;
415 w32 s 4 destwid;
416 w32 s 8 mask;
420 let getmodifiermappingreq () =
421 let s = "\119u\001\000" in
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 <- [||];
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;
755 let mask = 0
756 + 0x00000001 (* KeyPress *)
757 (* + 0x00000002 *) (* KeyRelease *)
758 + 0x00000004 (* ButtonPress *)
759 + 0x00000008 (* ButtonRelease *)
760 + 0x00000010 (* EnterWindow *)
761 + 0x00000020 (* LeaveWindow *)
762 + 0x00000040 (* PointerMotion *)
763 (* + 0x00000080 *) (* PointerMotionHint *)
764 (* + 0x00000100 *) (* Button1Motion *)
765 (* + 0x00000200 *) (* Button2Motion *)
766 (* + 0x00000400 *) (* Button3Motion *)
767 (* + 0x00000800 *) (* Button4Motion *)
768 (* + 0x00001000 *) (* Button5Motion *)
769 + 0x00002000 (* ButtonMotion *)
770 (* + 0x00004000 *) (* KeymapState *)
771 + 0x00008000 (* Exposure *)
772 + 0x00010000 (* VisibilityChange *)
773 + 0x00020000 (* StructureNotify *)
774 (* + 0x00040000 *) (* ResizeRedirect *)
775 (* + 0x00080000 *) (* SubstructureNotify *)
776 (* + 0x00100000 *) (* SubstructureRedirect *)
777 (* + 0x00200000 *) (* FocusChange *)
778 + 0x00400000 (* PropertyChange *)
779 (* + 0x00800000 *) (* ColormapChange *)
780 (* + 0x01000000 *) (* OwnerGrabButton *)
782 let wid = state.idbase in
783 let s = createwindowreq wid root 0 0 w h 0 mask in
784 sendstr s sock;
786 sendintern sock "WM_PROTOCOLS" false (fun resp ->
787 state.protoatom <- r32 resp 8;
788 sendintern sock "WM_DELETE_WINDOW" false (fun resp ->
789 state.deleatom <- r32 resp 8;
790 let s = s32 state.deleatom in
791 let s = changepropreq wid state.protoatom 4 32 s in
792 sendstr s sock;
796 sendintern sock "WM_CLIENT_MACHINE" false (fun resp ->
797 let atom = r32 resp 8 in
798 let empty = "" in
799 let hostname =
800 try Unix.gethostname ()
801 with exn ->
802 dolog "error getting host name: %s" (exntos exn);
803 empty
805 if hostname != empty
806 then
807 let s = changepropreq wid atom state.stringatom 8 hostname in
808 sendstr s sock;
809 sendintern sock "_NET_WM_PID" false (fun resp ->
810 let atom = r32 resp 8 in
811 let pid = Unix.getpid () in
812 let s = s32 pid in
813 let s = changepropreq wid atom (* cardinal *)6 32 s in
814 sendstr s sock;
818 state.actwin <- (fun () ->
819 let s = "\000uuu" in
820 let s = configurewindowreq state.idbase 0x40 s in
821 sendstr s state.sock;
822 let s = mapreq state.idbase in
823 sendstr s state.sock;
826 sendintern sock "_NET_ACTIVE_WINDOW" true (fun resp ->
827 let atom = r32 resp 8 in
828 state.actwin <- (fun () ->
829 let data = String.make 20 '\000' in
830 let cm = clientmessage 32 0 wid atom data in
831 let s = sendeventreq 0 root 0x180000 cm in
832 sendstr s state.sock;
836 syncsendintern sock 2.0 "WM_CLASS" false (fun resp ->
837 let atom = r32 resp 8 in
838 let llpp = "llpp\000llpp\000" in
839 let s = changepropreq wid atom 31 8 llpp in
840 sendstr s sock;
843 let s = getkeymapreq state.mink (state.maxk-state.mink) in
844 sendwithrep sock s (updkmap sock);
846 let s = getmodifiermappingreq () in
847 sendwithrep sock s (updmodmap sock);
849 let s = openfontreq (wid+1) "cursor" in
850 sendstr s sock;
852 Array.iteri (fun i glyphindex ->
853 let s = createglyphcursorreq (wid+1) (wid+2+i) glyphindex in
854 sendstr s sock;
855 ) [|34;48;50;58;128;152|];
857 sendintern sock "UTF8_STRING" true (fun resp ->
858 let atom = r32 resp 8 in
859 if atom != 0
860 then state.stringatom <- atom;
863 let setwmname s =
864 let s = changepropreq state.idbase 39 state.stringatom 8 s in
865 sendstr s state.sock;
867 state.setwmname <- setwmname;
868 sendintern sock "_NET_WM_NAME" true (fun resp ->
869 let atom = r32 resp 8 in
870 if atom != 0
871 then state.setwmname <- (fun s ->
872 setwmname s;
873 let s = changepropreq state.idbase atom state.stringatom 8 s in
874 sendstr s state.sock;
878 state.fullscreen <- (fun wid ->
879 let s = "xxuuyyuuwwuuhhuu" in
880 match state.fs with
881 | NoFs ->
882 w32 s 0 0;
883 w32 s 4 0;
884 w32 s 8 rootw;
885 w32 s 12 rooth;
886 let s = configurewindowreq wid 0x000f s in
887 sendstr s state.sock;
888 state.fs <- Fs (state.x, state.y, state.w, state.h);
890 | Fs (x, y, w, h) ->
891 w32 s 0 x;
892 w32 s 4 y;
893 w32 s 8 w;
894 w32 s 12 h;
895 let s = configurewindowreq wid 0x000f s in
896 sendstr s state.sock;
897 state.fs <- NoFs;
900 sendintern sock "_NET_WM_STATE" true (fun resp ->
901 state.nwmsatom <- r32 resp 8;
902 if state.nwmsatom != 0
903 then (
904 sendintern sock "_NET_WM_STATE_MAXIMIZED_VERT" true (fun resp ->
905 state.maxvatom <- r32 resp 8;
907 sendintern sock "_NET_WM_STATE_MAXIMIZED_HORZ" true (fun resp ->
908 state.maxhatom <- r32 resp 8;
910 sendintern sock "_NET_WM_STATE_FULLSCREEN" true (fun resp ->
911 state.fulsatom <- r32 resp 8;
912 if state.fulsatom != 0
913 then
914 state.fullscreen <-
915 (fun wid ->
916 let data = String.make 20 '\000' in
917 let fs, f =
918 match state.fs with
919 | NoFs -> Fs (-1, -1, -1, -1), 1
920 | Fs _ -> NoFs, 0
922 w32 data 0 f;
923 w32 data 4 state.fulsatom;
925 let cm = clientmessage 32 0 wid state.nwmsatom data in
926 let s = sendeventreq 0 root 0x180000 cm in
927 sendstr s sock;
928 state.fs <- fs;
933 let s = getgeometryreq wid in
934 syncsendwithrep sock 2.0 s (fun resp ->
935 glx wid;
936 let w = r16 resp 16
937 and h = r16 resp 18 in
938 state.w <- w;
939 state.h <- h;
942 | c ->
943 error "unknown conection setup response %d" (Char.code c)
946 let getauth haddr dnum =
947 let haddr =
948 if haddr = "localhost" || String.length haddr = 0
949 then
950 try Unix.gethostname ()
951 with exn ->
952 dolog "failed to resolve `%S': %s" haddr (exntos exn);
953 haddr
954 else haddr
956 let path =
957 try Sys.getenv "XAUTHORITY"
958 with Not_found ->
959 try Filename.concat (Sys.getenv "HOME") ".Xauthority"
960 with Not_found -> ""
962 let readauth ic =
963 let input_string ic len =
964 let s = String.create len in
965 really_input ic s 0 len;
968 let r16 s =
969 let rb pos = Char.code (String.get s pos) in
970 (rb 1) lor ((rb 0) lsl 8)
972 let rec find () =
973 let rs () =
974 let s = input_string ic 2 in
975 let n = r16 s in
976 input_string ic n
978 let family = input_string ic 2 in
979 let addr = rs () in
980 let nums = rs () in
981 let optnum =
982 try Some (int_of_string nums)
983 with exn ->
984 dolog
985 "display number(%S) is not an integer (corrupt %S?): %s"
986 nums path (exntos exn);
987 None
989 let name = rs () in
990 let data = rs () in
992 vlog "family %S addr %S(%S) num %S(%d) name %S data %S"
993 family addr haddr nums dnum name data;
994 match optnum with
995 | Some num when addr = haddr && num = dnum ->
996 name, data
997 | _ -> find ()
999 let name, data =
1000 try find ()
1001 with
1002 | End_of_file -> "", ""
1003 | exn ->
1004 dolog "exception while reading X authority data (%S): %s"
1005 path (exntos exn);
1006 "", ""
1008 close_in ic;
1009 name, data;
1011 let opt =
1013 if String.length path = 0
1014 then None
1015 else Some (open_in_bin path)
1016 with exn ->
1017 if Sys.file_exists path
1018 then
1019 dolog "failed to open X authority file `%S' : %s"
1020 path (exntos exn);
1021 None
1023 match opt with
1024 | None -> "", ""
1025 | Some ic -> readauth ic
1028 let init t w h osx =
1029 let d =
1030 try Sys.getenv "DISPLAY"
1031 with exn ->
1032 error "could not get DISPLAY evironment variable: %s"
1033 (exntos exn)
1035 let getnum w b e =
1036 if b = e
1037 then error "invalid DISPLAY(%s) %S" w d
1038 else
1039 let s = String.sub d b (e - b) in
1040 try int_of_string s
1041 with exn ->
1042 error "invalid DISPLAY %S can not parse %s(%S): %s"
1043 d w s (exntos exn)
1045 let rec phost pos =
1046 if pos = String.length d
1047 then error "invalid DISPLAY %S no display number specified" d
1048 else (
1049 if d.[pos] = ':'
1050 then
1051 let rec pdispnum pos1 =
1052 if pos1 = String.length d
1053 then getnum "display number" (pos+1) pos1, 0
1054 else
1055 match d.[pos1] with
1056 | '.' ->
1057 let dispnum = getnum "display number" (pos+1) pos1 in
1058 let rec pscreennum pos2 =
1059 if pos2 = String.length d
1060 then getnum "screen number" (pos1+1) pos2
1061 else
1062 match d.[pos2] with
1063 | '0' .. '9' -> pscreennum (pos2+1)
1064 | _ ->
1065 error "invalid DISPLAY %S, cannot parse screen number" d
1067 dispnum, pscreennum (pos1+1)
1068 | '0' .. '9' -> pdispnum (pos1+1)
1069 | _ ->
1070 error "invalid DISPLAY %S, cannot parse display number" d
1072 String.sub d 0 pos, pdispnum (pos+1)
1073 else phost (pos+1)
1076 let host, (dispnum, screennum) = phost 0 in
1077 let aname, adata = getauth host dispnum in
1078 let fd =
1079 let fd, addr =
1080 if osx || String.length host = 0 || host = "unix"
1081 then
1082 let addr =
1083 if osx
1084 then Unix.ADDR_UNIX d
1085 else Unix.ADDR_UNIX ("/tmp/.X11-unix/X" ^ string_of_int dispnum)
1087 let fd = Unix.socket Unix.PF_UNIX Unix.SOCK_STREAM 0 in
1088 fd, addr
1089 else
1090 let h =
1091 try Unix.gethostbyname host
1092 with exn ->
1093 error "cannot resolve %S: %s" host (exntos exn)
1095 let addr = h.Unix.h_addr_list.(0) in
1096 let port = 6000 + dispnum in
1097 let fd = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
1098 fd, (Unix.ADDR_INET (addr, port))
1100 try Unix.connect fd addr; fd
1101 with exn ->
1102 error "failed to connect to X: %s" (exntos exn)
1104 cloexec fd;
1105 let s = "luMMmmnndduu" in
1106 let s = padcat s aname in
1107 let s = padcat s adata in
1108 w16 s 2 11;
1109 w16 s 4 0;
1110 w16 s 6 (String.length aname);
1111 w16 s 8 (String.length adata);
1112 sendstr1 s 0 (String.length s) fd;
1113 state.sock <- fd;
1114 setup fd screennum w h;
1115 state.t <- t;
1116 fd, state.w, state.h;
1119 let settitle s =
1120 state.setwmname s;
1123 let setcursor cursor =
1124 if cursor != state.curcurs
1125 then
1126 let n =
1127 match cursor with
1128 | CURSOR_INHERIT -> -1
1129 | CURSOR_INFO -> 3
1130 | CURSOR_CYCLE -> 2
1131 | CURSOR_CROSSHAIR -> 0
1132 | CURSOR_TEXT -> 5
1134 let s = s32 (if n = -1 then 0 else state.idbase+2+n) in
1135 let s = changewindowattributesreq state.idbase (*cursor*)0x4000 s in
1136 sendstr s state.sock;
1137 state.curcurs <- cursor;
1140 let fullscreen () =
1141 state.fullscreen state.idbase;
1144 let metamask = 0x40;;
1145 let altmask = 8;;
1146 let shiftmask = 1;;
1147 let ctrlmask = 4;;
1149 let withalt mask = mask land altmask != 0;;
1150 let withctrl mask = mask land ctrlmask != 0;;
1151 let withshift mask = mask land shiftmask != 0;;
1152 let withmeta mask = mask land metamask != 0;;
1153 let withnone mask = mask land (altmask + ctrlmask + shiftmask + metamask) = 0;;
1155 let xlatt, xlatf =
1156 let t = Hashtbl.create 20
1157 and f = Hashtbl.create 20 in
1158 let add n nl k =
1159 List.iter (fun s -> Hashtbl.add t s k) (n::nl);
1160 Hashtbl.add f k n
1162 let addc c =
1163 let s = String.create 1 in
1164 s.[0] <- c;
1165 add s [] (Char.code c)
1167 let addcr a b =
1168 let an = Char.code a and bn = Char.code b in
1169 for i = an to bn do addc (Char.chr i) done;
1171 addcr '0' '9';
1172 addcr 'a' 'z';
1173 addcr 'A' 'Z';
1174 String.iter addc "`~!@#$%^&*()-_=+\\|[{]};:,./<>?";
1175 for i = 0 to 29 do add ("f" ^ string_of_int (i+1)) [] (0xffbe + i) done;
1176 add "space" [] 0x20;
1177 add "ret" ["return"; "enter"] 0xff0d;
1178 add "tab" [] 0xff09;
1179 add "left" [] 0xff51;
1180 add "right" [] 0xff53;
1181 add "home" [] 0xff50;
1182 add "end" [] 0xff57;
1183 add "ins" ["insert"] 0xff63;
1184 add "del" ["delete"] 0xffff;
1185 add "esc" ["escape"] 0xff1b;
1186 add "pgup" ["pageup"] 0xff55;
1187 add "pgdown" ["pagedown"] 0xff56;
1188 add "backspace" [] 0xff08;
1189 add "up" [] 0xff52;
1190 add "down" [] 0xff54;
1191 t, f;
1194 let keyname k =
1195 try Hashtbl.find xlatf k
1196 with Not_found -> Printf.sprintf "%#x" k;
1199 let namekey name =
1200 try Hashtbl.find xlatt name
1201 with Not_found ->
1202 if String.length name = 1
1203 then Char.code name.[0]
1204 else int_of_string name;