Allow embedding
[llpp.git] / wsi.ml
blob7e0c1697c3b01522e623534eccde44c3b2a5acb4
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 type visiblestate =
18 | Unobscured
19 | PartiallyObscured
20 | FullyObscured
23 type wid = int and screenno = int and vid = int and atom = int;;
25 external glxinit : string -> wid -> screenno -> vid = "ml_glxinit";;
26 external glxcompleteinit : unit -> unit = "ml_glxcompleteinit";;
27 external swapb : unit -> unit = "ml_swapb";;
29 let vlog fmt = Format.kprintf ignore fmt;;
31 let onot = object
32 method display = ()
33 method map _ = ()
34 method expose = ()
35 method visible _ = ()
36 method reshape _ _ = ()
37 method mouse _ _ _ _ _ = ()
38 method motion _ _ = ()
39 method pmotion _ _ = ()
40 method key _ _ = ()
41 method enter _ _ = ()
42 method leave = ()
43 method winstate _ = ()
44 method quit = exit 0
45 end;;
47 class type t = object
48 method display : unit
49 method map : bool -> unit
50 method expose : unit
51 method visible : visiblestate -> unit
52 method reshape : int -> int -> unit
53 method mouse : int -> bool -> int -> int -> int -> unit
54 method motion : int -> int -> unit
55 method pmotion : int -> int -> unit
56 method key : int -> int -> unit
57 method enter : int -> int -> unit
58 method leave : unit
59 method winstate : winstate list -> unit
60 method quit : unit
61 end;;
63 type state =
64 { mutable mink : int
65 ; mutable maxk : int
66 ; mutable keymap : int array array
67 ; fifo : (string -> unit) Queue.t
68 ; mutable seq : int
69 ; mutable protoatom : atom
70 ; mutable deleatom : atom
71 ; mutable nwmsatom : atom
72 ; mutable maxvatom : atom
73 ; mutable maxhatom : atom
74 ; mutable fulsatom : atom
75 ; mutable idbase : int
76 ; mutable wid : int
77 ; mutable fid : int
78 ; mutable fullscreen : (int -> unit)
79 ; mutable setwmname : (string -> unit)
80 ; mutable actwin : (unit -> unit)
81 ; mutable stringatom : int
82 ; mutable t : t
83 ; mutable sock : Unix.file_descr
84 ; mutable x : int
85 ; mutable y : int
86 ; mutable w : int
87 ; mutable h : int
88 ; mutable fs : fs
89 ; mutable curcurs : cursor
90 ; mutable capslmask : int
91 ; mutable numlmask : int
92 ; mutable levl3mask : int
93 ; mutable levl5mask : int
95 and fs =
96 | NoFs
97 | Fs of (int * int * int * int)
100 let state =
101 { mink = max_int
102 ; maxk = min_int
103 ; keymap = E.a
104 ; fifo = Queue.create ()
105 ; seq = 0
106 ; protoatom = -1
107 ; deleatom = -1
108 ; nwmsatom = -1
109 ; maxvatom = -1
110 ; maxhatom = -1
111 ; fulsatom = -1
112 ; idbase = -1
113 ; wid = -1
114 ; fid = -1
115 ; fullscreen = (fun _ -> ())
116 ; setwmname = (fun _ -> ())
117 ; actwin = (fun _ -> ())
118 ; sock = Unix.stdin
119 ; t = onot
120 ; x = -1
121 ; y = -1
122 ; w = -1
123 ; h = -1
124 ; fs = NoFs
125 ; stringatom = 31
126 ; curcurs = CURSOR_INHERIT
127 ; capslmask = 0
128 ; numlmask = 0
129 ; levl3mask = 0
130 ; levl5mask = 0
134 include Bo;;
136 let makereq opcode len reqlen =
137 let s = String.create len in
138 w8 s 0 opcode;
139 w16 s 2 reqlen;
143 let recv fd s pos len =
144 Unix.read fd s pos len;
147 let readstr sock n =
148 let s = String.create n in
149 let rec loop pos n =
150 let m = tempfailureretry (recv sock s pos) n in
151 if m = 0
152 then state.t#quit;
153 if n != m
154 then (
155 ignore (tempfailureretry (Unix.select [sock] [] []) 0.01);
156 loop (pos + m) (n - m)
159 loop 0 n;
163 let sendstr1 s pos len sock =
164 vlog "%d <= %S" state.seq s;
165 state.seq <- state.seq + 1;
166 let n = tempfailureretry (Unix.write sock s pos) len in
167 if n != len
168 then error "send %d returned %d" len n;
171 let updkmap sock resp =
172 let syms = r8 resp 1 in
173 let len = r32 resp 4 in
174 let data =
175 if len > 0
176 then readstr sock (4*len)
177 else E.s
179 let m = len / syms in
180 state.keymap <- Array.make_matrix
181 (state.maxk - state.mink) syms 0xffffff;
182 let rec loop i = if i = m then () else
183 let k = i*4*syms in
184 let rec loop2 k l = if l = syms then () else
185 let v = r32 data k in
186 state.keymap.(i).(l) <- v;
187 loop2 (k+4) (l+1)
189 loop2 k 0;
190 loop (i+1);
192 loop 0;
195 let updmodmap sock resp =
196 let n = r8 resp 1 in
197 let len = r16 resp 4 in
198 let data =
199 if len > 0
200 then readstr sock (len*4)
201 else E.s
203 if len > 0 then (*???*)
204 let modmap = Array.make_matrix 8 n 0xffffff in
205 let rec loop l = if l = 8 then () else
206 let p = l*n in
207 let rec loop1 m = if m = n then () else
208 let p = p+m in
209 let code = r8 data p in
210 modmap.(l).(m) <- code;
211 if l = 1
212 then (
213 let ki = code - state.mink in
214 if ki >= 0
215 then
216 let a = state.keymap.(ki) in
217 let rec capsloop i = if i = Array.length a || i > 3 then () else
218 let s = a.(i) in
219 if s = 0xffe5
220 then state.capslmask <- 2
221 else capsloop (i+1)
223 capsloop 0;
225 else (
226 if l > 3
227 then (
228 let ki = code - state.mink in
229 if ki >= 0
230 then
231 let a = state.keymap.(ki) in
232 let rec lloop i = if i = Array.length a || i > 3 then () else
233 let s = a.(i) in
234 match s with
235 | 0xfe03 -> state.levl3mask <- 1 lsl l
236 | 0xfe11 -> state.levl5mask <- 1 lsl l
237 | 0xff7f -> state.numlmask <- 1 lsl l
238 | _ -> lloop (i+1)
240 lloop 0;
243 loop1 (m+1)
245 loop1 0;
246 loop (l+1)
248 loop 0;
251 let sendwithrep sock s f =
252 Queue.push f state.fifo;
253 sendstr1 s 0 (String.length s) sock;
256 let padcatl ss =
257 let b = Buffer.create 16 in
258 List.iter (Buffer.add_string b) ss;
259 let bl = Buffer.length b in
260 let pl = bl land 3 in
261 if pl != 0
262 then (
263 let pad = String.create 3 in
264 Buffer.add_substring b pad 0 (4 - pl);
266 Buffer.contents b;
269 let padcat s1 s2 = padcatl [s1; s2];;
271 let internreq name onlyifexists =
272 let s = makereq 16 8 8 in
273 let s = padcat s name in
274 w8 s 1 (if onlyifexists then 1 else 0);
275 w16 s 2 (String.length s / 4);
276 w16 s 4 (String.length name);
280 let sendintern sock s onlyifexists f =
281 let s = internreq s onlyifexists in
282 sendwithrep sock s f;
285 let createwindowreq wid parent x y w h bw eventmask vid depth mid =
286 let s = makereq 1 48 12 in
287 w8 s 1 depth;
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 0; (* inputoutput *)
296 w32 s 24 vid; (* visual *)
297 w32 s 28 0x280a; (* eventmask*)
298 w32 s 32 0;
299 w32 s 36 0;
300 w32 s 40 eventmask;
301 w32 s 44 mid;
305 let createcolormapreq mid wid vid =
306 let s = makereq 78 16 4 in
307 w8 s 1 0;
308 w32 s 4 mid;
309 w32 s 8 wid;
310 w32 s 12 vid;
314 let getgeometryreq wid =
315 let s = makereq 14 8 2 in
316 w32 s 4 wid;
320 let mapreq wid =
321 let s = makereq 8 8 2 in
322 w32 s 4 wid;
326 let getkeymapreq first count =
327 let s = makereq 101 8 2 in
328 w8 s 4 first;
329 w8 s 5 count;
333 let changepropreq wid prop typ format props =
334 let s = makereq 18 24 0 in
335 let s = padcat s props in
336 w8 s 1 0;
337 w16 s 2 (String.length s / 4);
338 w32 s 4 wid;
339 w32 s 8 prop;
340 w32 s 12 typ;
341 w8 s 16 format;
342 let ful = String.length props / (match format with
343 | 8 -> 1
344 | 16 -> 2
345 | 32 -> 4
346 | n -> error "no idea what %d means" n)
348 w32 s 20 ful;
352 let getpropreq delete wid prop typ =
353 let s = makereq 20 24 6 in
354 w8 s 1 (if delete then 1 else 0);
355 w32 s 4 wid;
356 w32 s 8 prop;
357 w32 s 12 typ;
358 w32 s 16 0;
359 w32 s 20 2;
363 let openfontreq fid name =
364 let s = makereq 45 12 0 in
365 let s = padcat s name in
366 w16 s 2 (String.length s / 4);
367 w32 s 4 fid;
368 w16 s 8 (String.length name);
372 let createglyphcursorreq fid cid cindex =
373 let s = makereq 94 32 8 in
374 w32 s 4 cid;
375 w32 s 8 fid;
376 w32 s 12 fid;
377 w16 s 16 cindex;
378 w16 s 18 (cindex+1);
379 w16 s 20 0;
380 w16 s 22 0;
381 w16 s 24 0;
382 w16 s 26 0xffff;
383 w16 s 28 0xffff;
384 w16 s 30 0xffff;
388 let changewindowattributesreq wid mask attrs =
389 let s = makereq 2 12 0 in
390 let s = padcat s attrs in
391 w16 s 2 (String.length s / 4);
392 w32 s 4 wid;
393 w32 s 8 mask;
397 let configurewindowreq wid mask values =
398 let s = makereq 2 12 0 in
399 let s = padcat s values in
400 w16 s 2 (String.length s / 4);
401 w32 s 4 wid;
402 w16 s 8 mask;
406 let s32 n =
407 let s = String.create 4 in
408 w32 s 0 n;
412 let clientmessage format seq wid typ data =
413 let s = makereq 33 12 0 in
414 let s = padcat s data in
415 w8 s 1 format;
416 w16 s 2 seq;
417 w32 s 4 wid;
418 w32 s 8 typ;
422 let sendeventreq propagate destwid mask data =
423 let s = makereq 25 12 11 in
424 let s = padcat s data in
425 w8 s 1 propagate;
426 w16 s 2 11;
427 w32 s 4 destwid;
428 w32 s 8 mask;
432 let getmodifiermappingreq () =
433 makereq 119 4 1;
436 let getkeysym code mask =
437 let pkpk = state.keymap.(code-state.mink).(0) in
438 if (pkpk >= 0xff80 && pkpk <= 0xffbd)
439 || (pkpk >= 0x11000000 && pkpk <= 0x1100ffff)
440 then (
441 if mask land state.numlmask != 0
442 then
443 let keysym = state.keymap.(code-state.mink).(1) in
444 if keysym = 0 then pkpk else keysym
445 else pkpk
447 else (
448 let shift =
449 if pkpk land 0xf000 = 0xf000
450 then 0
451 else (mask land 1) lxor ((mask land state.capslmask) lsr 1)
453 let index =
454 let l3 = (mask land state.levl3mask) != 0 in
455 let l4 = (mask land state.levl5mask) != 0 in
456 shift +
457 if l3 then (if l4 then 8 else 4) else (if l4 then 6 else 0)
459 let keysym = state.keymap.(code-state.mink).(index) in
460 if index land 1 = 1 && keysym = 0
461 then state.keymap.(code-state.mink).(index - 1)
462 else keysym
466 let readresp sock =
467 let resp = readstr sock 32 in
468 let opcode = r8 resp 0 in
469 match opcode land lnot 0x80 with
470 | 0 -> (* error *)
471 let s = resp in
472 let code = r8 s 1
473 and serial = r16 s 2
474 and resid = r32 resp 4
475 and min = r16 s 8
476 and maj = r8 s 10 in
477 error "code=%d serial=%d resid=%#x min=%d maj=%d\n%S"
478 code serial resid min maj resp;
480 | 1 -> (* response *)
481 let rep = Queue.pop state.fifo in
482 rep resp;
484 | 2 -> (* key press *)
485 if Array.length state.keymap > 0
486 then
487 let code = r8 resp 1 in
488 let mask = r16 resp 28 in
489 let keysym = getkeysym code mask in
490 vlog "keysym = %x %c mask %#x code %d"
491 keysym (Char.unsafe_chr keysym) mask code;
492 state.t#key keysym mask;
494 | 3 -> (* key release *)
495 if Array.length state.keymap > 0
496 then
497 let code = r8 resp 1 in
498 let mask = r16 resp 28 in
499 let keysym = getkeysym code mask in
500 vlog "release keysym = %x %c mask %#x code %#d"
501 keysym (Char.unsafe_chr keysym) mask code
503 | 4 -> (* buttonpress *)
504 let n = r8 resp 1
505 and x = r16s resp 24
506 and y = r16s resp 26
507 and m = r16 resp 28 in
508 state.t#mouse n true x y m;
509 vlog "press %d" n
511 | 5 -> (* buttonrelease *)
512 let n = r8 resp 1
513 and x = r16s resp 24
514 and y = r16s resp 26
515 and m = r16 resp 28 in
516 state.t#mouse n false x y m;
517 vlog "release %d %d %d" n x y
519 | 6 -> (* motion *)
520 let x = r16s resp 24 in
521 let y = r16s resp 26 in
522 let m = r16 resp 28 in
523 if m land 0x1f00 = 0
524 then state.t#pmotion x y
525 else state.t#motion x y;
526 vlog "move %dx%d => %d" x y m
528 | 7 -> (* enter *)
529 let x = r16s resp 24
530 and y = r16s resp 26 in
531 state.t#enter x y;
532 vlog "enter %d %d" x y
534 | 8 -> (* leave *)
535 state.t#leave
537 | 18 -> (* unmap *)
538 state.t#map false;
539 vlog "unmap";
541 | 19 -> (* map *)
542 state.t#map true;
543 vlog "map";
545 | 12 -> (* exposure *)
546 vlog "exposure";
547 state.t#expose
549 | 15 -> (* visibility *)
550 let v = r8 resp 8 in
551 let vis =
552 match v with
553 | 0 -> Unobscured
554 | 1 -> PartiallyObscured
555 | 2 -> FullyObscured
556 | _ ->
557 dolog "unknown visibility %d" v;
558 Unobscured
560 state.t#visible vis;
561 vlog "visibility %d" v;
563 | 34 -> (* mapping *)
564 state.keymap <- E.a;
565 let s = getkeymapreq state.mink (state.maxk-state.mink-1) in
566 sendwithrep sock s (updkmap sock);
567 state.capslmask <- 0;
568 state.levl3mask <- 0;
569 state.levl5mask <- 0;
570 state.numlmask <- 0;
571 let s = getmodifiermappingreq () in
572 sendwithrep sock s (updmodmap sock);
574 | 33 -> (* clientmessage *)
575 let atom = r32 resp 8 in
576 if atom = state.protoatom
577 then (
578 let atom = r32 resp 12 in
579 if atom = state.deleatom
580 then state.t#quit;
582 vlog "atom %#x" atom
584 | 21 -> (* reparent *)
585 vlog "reparent"
587 | 22 -> (* configure *)
588 let x = r16s resp 16
589 and y = r16s resp 18
590 and w = r16 resp 20
591 and h = r16 resp 22 in
592 vlog "configure cur [%d %d %d %d] conf [%d %d %d %d]"
593 state.x state.y state.w state.h
594 x y w h
596 if w != state.w || h != state.h
597 then (
598 state.t#reshape w h;
600 state.w <- w;
601 state.h <- h;
602 state.x <- x;
603 state.y <- y;
604 state.t#expose
606 | 28 ->
607 let atom = r32 resp 8 in
608 if atom = state.nwmsatom
609 then
610 let s = getpropreq false state.wid atom 4 in
611 sendwithrep sock s (fun resp ->
612 let len = r32 resp 4 in
613 let nitems = r32 resp 16 in
614 let wsl =
615 if len = 0
616 then []
617 else
618 let s = readstr sock (len*4) in
619 let rec loop wsl i = if i = nitems then wsl else
620 let atom = r32 s (i*4) in
621 let wsl =
622 if atom = state.maxhatom
623 then MaxHorz::wsl
624 else (
625 if atom = state.maxvatom
626 then MaxVert::wsl
627 else (
628 if atom = state.fulsatom
629 then (
630 state.fs <- Fs (state.x, state.y, state.w, state.h);
631 Fullscreen::wsl
633 else wsl
636 in loop wsl (i+1)
638 loop [] 0
640 state.t#winstate (List.sort compare wsl)
643 | n ->
644 dolog "event %d %S" n resp
647 let readresp sock =
648 let rec loop () =
649 readresp sock;
650 if hasdata sock then loop ();
652 loop ();
655 let sendstr s ?(pos=0) ?(len=String.length s) sock =
656 sendstr1 s pos len sock;
657 if hasdata sock then readresp sock;
660 let reshape w h =
661 if state.fs = NoFs
662 then
663 let s = String.create 8 in
664 w32 s 0 w;
665 w32 s 4 h;
666 let s = configurewindowreq state.wid 0x000c s in
667 sendstr s state.sock;
668 else state.fullscreen state.wid
671 let activatewin () =
672 state.actwin ();
675 let syncsendwithrep sock secstowait s f =
676 let completed = ref false in
677 sendwithrep sock s (fun resp -> f resp; completed := true);
678 let now = Unix.gettimeofday in
679 let deadline = now () +. secstowait in
680 let rec readtillcompletion () =
681 let sf deadline =
682 let timeout = deadline -. now () in
683 if timeout <= 0.0
684 then [], [], []
685 else Unix.select [sock] [] [] timeout
687 let r, _, _ = tempfailureretry sf deadline in
688 match r with
689 | [] -> error "didn't get X response in %f seconds, aborting" secstowait
690 | _ ->
691 readresp sock;
692 if not !completed
693 then readtillcompletion ()
695 readtillcompletion ();
698 let mapwin () =
699 let s = mapreq state.wid in
700 sendstr s state.sock;
703 let syncsendintern sock secstowait s onlyifexists f =
704 let s = internreq s onlyifexists in
705 syncsendwithrep sock secstowait s f;
708 let setup sock rootwid screennum w h =
709 let s = readstr sock 2 in
710 let n = String.length s in
711 if n != 2
712 then error "failed to read X connection setup response n=%d" n;
713 match s.[0] with
714 | '\000' ->
715 let reasonlen = r8 s 1 in
716 let s = readstr sock 6 in
717 let maj = r16 s 0
718 and min = r16 s 2
719 and add = r16 s 4 in
720 let len = add*4 in
721 let data = readstr sock len in
722 let reason = String.sub data 0 reasonlen in
723 error "X connection failed maj=%d min=%d reason=%S"
724 maj min reason
726 | '\002' -> failwith "X connection setup failed: authentication required";
728 | '\001' ->
729 let s = readstr sock 38 in
730 let maj = r16 s 0
731 and min = r16 s 2
732 and add = r16 s 4
733 and idbase = r32 s 10
734 and idmask = r32 s 14
735 and vlen = r16 s 22
736 and screens = r8 s 26
737 and formats = r8 s 27
738 and minkk = r8 s 32
739 and maxkk = r8 s 33 in
740 let data = readstr sock (4*add-32) in
741 let vendor = String.sub data 0 vlen in
742 let pos = ((vlen+3) land lnot 3) + formats*8 in
744 if screennum >= screens
745 then error "invalid screen %d, max %d" screennum (screens-1);
747 let pos =
748 let rec findscreen n pos = if n = screennum then pos else
749 let pos =
750 let ndepths = r8 data (pos+39) in
751 let rec skipdepths n pos = if n = ndepths then pos else
752 let pos =
753 let nvisiuals = r16 data (pos+2) in
754 pos + nvisiuals*24 + 8
756 skipdepths (n+1) pos
758 skipdepths n (pos+40)
760 findscreen (n+1) pos
762 findscreen 0 pos
764 let root = if rootwid = 0 then r32 data pos else rootwid in
765 let rootw = r16 data (pos+20)
766 and rooth = r16 data (pos+22)
767 and rootdepth = r8 data (pos+38)in
769 state.mink <- minkk;
770 state.maxk <- maxkk;
771 state.idbase <- idbase;
772 vlog "vendor = %S, maj=%d min=%d" vendor maj min;
773 vlog "screens = %d formats = %d" screens formats;
774 vlog "minkk = %d maxkk = %d" minkk maxkk;
775 vlog "idbase = %#x idmask = %#x" idbase idmask;
776 vlog "root=%#x %dx%d" root rootw rooth;
777 vlog "wmm = %d, hmm = %d" (r16 data (pos+24)) (r16 data (pos+26));
778 vlog "visualid = %#x " (r32 data (pos+32));
779 vlog "root depth = %d" rootdepth;
781 let wid = state.idbase in
782 let mid = wid+1 in
783 let fid = mid+1 in
785 state.wid <- wid;
786 state.fid <- fid;
788 let vid =
789 let disp =
790 try Sys.getenv "DISPLAY"
791 with Not_found -> E.s
793 glxinit disp wid screennum
795 let ndepths = r8 data (pos+39) in
796 let rec finddepth n' pos =
797 if n' = ndepths
798 then error "couldn't find depth for visual %#x" vid;
799 let depth = r8 data pos in
800 let nvisuals = r16 data (pos+2) in
801 let rec findvisual n pos =
802 if n = nvisuals
803 then finddepth (n'+1) pos
804 else
805 let id = r32 data pos in
806 if id = vid
807 then depth
808 else findvisual (n+1) (pos+24)
810 findvisual 0 (pos+8)
812 let depth = finddepth 0 (pos+40) in
814 let s = createcolormapreq mid root vid in
815 sendstr s sock;
817 let mask = 0
818 + 0x00000001 (* KeyPress *)
819 (* + 0x00000002 *) (* KeyRelease *)
820 + 0x00000004 (* ButtonPress *)
821 + 0x00000008 (* ButtonRelease *)
822 + 0x00000010 (* EnterWindow *)
823 + 0x00000020 (* LeaveWindow *)
824 + 0x00000040 (* PointerMotion *)
825 (* + 0x00000080 *) (* PointerMotionHint *)
826 (* + 0x00000100 *) (* Button1Motion *)
827 (* + 0x00000200 *) (* Button2Motion *)
828 (* + 0x00000400 *) (* Button3Motion *)
829 (* + 0x00000800 *) (* Button4Motion *)
830 (* + 0x00001000 *) (* Button5Motion *)
831 + 0x00002000 (* ButtonMotion *)
832 (* + 0x00004000 *) (* KeymapState *)
833 + 0x00008000 (* Exposure *)
834 + 0x00010000 (* VisibilityChange *)
835 + 0x00020000 (* StructureNotify *)
836 (* + 0x00040000 *) (* ResizeRedirect *)
837 (* + 0x00080000 *) (* SubstructureNotify *)
838 (* + 0x00100000 *) (* SubstructureRedirect *)
839 (* + 0x00200000 *) (* FocusChange *)
840 + 0x00400000 (* PropertyChange *)
841 (* + 0x00800000 *) (* ColormapChange *)
842 (* + 0x01000000 *) (* OwnerGrabButton *)
845 let s = createwindowreq wid root 0 0 w h 0 mask vid depth mid in
846 sendstr s sock;
848 sendintern sock "WM_PROTOCOLS" false (fun resp ->
849 state.protoatom <- r32 resp 8;
850 sendintern sock "WM_DELETE_WINDOW" false (fun resp ->
851 state.deleatom <- r32 resp 8;
852 let s = s32 state.deleatom in
853 let s = changepropreq wid state.protoatom 4 32 s in
854 sendstr s sock;
858 sendintern sock "WM_CLIENT_MACHINE" false (fun resp ->
859 let atom = r32 resp 8 in
860 let empty = E.s in
861 let hostname =
862 try Unix.gethostname ()
863 with exn ->
864 dolog "error getting host name: %s" (exntos exn);
865 empty
867 if hostname != empty
868 then
869 let s = changepropreq wid atom state.stringatom 8 hostname in
870 sendstr s sock;
871 sendintern sock "_NET_WM_PID" false (fun resp ->
872 let atom = r32 resp 8 in
873 let pid = Unix.getpid () in
874 let s = s32 pid in
875 let s = changepropreq wid atom 6(*cardinal*) 32 s in
876 sendstr s sock;
880 state.actwin <- (fun () ->
881 let s = String.create 4 in
882 let s = configurewindowreq wid 0x40 s in
883 sendstr s state.sock;
884 let s = mapreq wid in
885 sendstr s state.sock;
888 sendintern sock "_NET_ACTIVE_WINDOW" true (fun resp ->
889 let atom = r32 resp 8 in
890 state.actwin <- (fun () ->
891 let data = String.make 20 '\000' in
892 let cm = clientmessage 32 0 wid atom data in
893 let s = sendeventreq 0 root 0x180000 cm in
894 sendstr s state.sock;
898 syncsendintern sock 2.0 "WM_CLASS" false (fun resp ->
899 let atom = r32 resp 8 in
900 let llpp = "llpp\000llpp\000" in
901 let s = changepropreq wid atom 31 8 llpp in
902 sendstr s sock;
905 let s = getkeymapreq state.mink (state.maxk-state.mink) in
906 sendwithrep sock s (updkmap sock);
908 let s = getmodifiermappingreq () in
909 sendwithrep sock s (updmodmap sock);
911 let s = openfontreq fid "cursor" in
912 sendstr s sock;
914 Array.iteri (fun i glyphindex ->
915 let s = createglyphcursorreq fid (fid+1+i) glyphindex in
916 sendstr s sock;
917 ) [|34;48;50;58;128;152|];
919 sendintern sock "UTF8_STRING" true (fun resp ->
920 let atom = r32 resp 8 in
921 if atom != 0
922 then state.stringatom <- atom;
925 let setwmname s =
926 let s = changepropreq wid 39 state.stringatom 8 s in
927 sendstr s state.sock;
929 state.setwmname <- setwmname;
930 sendintern sock "_NET_WM_NAME" true (fun resp ->
931 let atom = r32 resp 8 in
932 if atom != 0
933 then state.setwmname <- (fun s ->
934 setwmname s;
935 let s = changepropreq wid atom state.stringatom 8 s in
936 sendstr s state.sock;
940 state.fullscreen <- (fun wid ->
941 let s = String.create 16 in
942 match state.fs with
943 | NoFs ->
944 w32 s 0 0;
945 w32 s 4 0;
946 w32 s 8 rootw;
947 w32 s 12 rooth;
948 let s = configurewindowreq wid 0x000f s in
949 sendstr s state.sock;
950 state.fs <- Fs (state.x, state.y, state.w, state.h);
952 | Fs (x, y, w, h) ->
953 w32 s 0 x;
954 w32 s 4 y;
955 w32 s 8 w;
956 w32 s 12 h;
957 let s = configurewindowreq wid 0x000f s in
958 sendstr s state.sock;
959 state.fs <- NoFs;
962 sendintern sock "_NET_WM_STATE" true (fun resp ->
963 state.nwmsatom <- r32 resp 8;
964 if state.nwmsatom != 0
965 then (
966 sendintern sock "_NET_WM_STATE_MAXIMIZED_VERT" true (fun resp ->
967 state.maxvatom <- r32 resp 8;
969 sendintern sock "_NET_WM_STATE_MAXIMIZED_HORZ" true (fun resp ->
970 state.maxhatom <- r32 resp 8;
972 sendintern sock "_NET_WM_STATE_FULLSCREEN" true (fun resp ->
973 state.fulsatom <- r32 resp 8;
974 if state.fulsatom != 0
975 then
976 state.fullscreen <-
977 (fun wid ->
978 let data = String.make 20 '\000' in
979 let fs, f =
980 match state.fs with
981 | NoFs -> Fs (-1, -1, -1, -1), 1
982 | Fs _ -> NoFs, 0
984 w32 data 0 f;
985 w32 data 4 state.fulsatom;
987 let cm = clientmessage 32 0 wid state.nwmsatom data in
988 let s = sendeventreq 0 root 0x180000 cm in
989 sendstr s sock;
990 state.fs <- fs;
995 let s = getgeometryreq wid in
996 syncsendwithrep sock 2.0 s (fun resp ->
997 glxcompleteinit ();
998 let w = r16 resp 16
999 and h = r16 resp 18 in
1000 state.w <- w;
1001 state.h <- h;
1004 | c ->
1005 error "unknown conection setup response %d" (Char.code c)
1008 let getauth haddr dnum =
1009 let haddr =
1010 if haddr = "localhost" || String.length haddr = 0
1011 then
1012 try Unix.gethostname ()
1013 with exn ->
1014 dolog "failed to resolve `%S': %s" haddr (exntos exn);
1015 haddr
1016 else haddr
1018 let path =
1019 try Sys.getenv "XAUTHORITY"
1020 with Not_found ->
1021 try Filename.concat (Sys.getenv "HOME") ".Xauthority"
1022 with Not_found -> E.s
1024 let readauth ic =
1025 let input_string ic len =
1026 let s = String.create len in
1027 really_input ic s 0 len;
1030 let r16be s =
1031 let rb pos = Char.code (String.get s pos) in
1032 (rb 1) lor ((rb 0) lsl 8)
1034 let rec find () =
1035 let rs () =
1036 let s = input_string ic 2 in
1037 let n = r16be s in
1038 input_string ic n
1040 let family = input_string ic 2 in
1041 let addr = rs () in
1042 let nums = rs () in
1043 let optnum =
1044 try Some (int_of_string nums)
1045 with exn ->
1046 dolog
1047 "display number(%S) is not an integer (corrupt %S?): %s"
1048 nums path (exntos exn);
1049 None
1051 let name = rs () in
1052 let data = rs () in
1054 vlog "family %S addr %S(%S) num %S(%d) name %S data %S"
1055 family addr haddr nums dnum name data;
1056 match optnum with
1057 | Some num when addr = haddr && num = dnum ->
1058 name, data
1059 | _ -> find ()
1061 let name, data =
1062 try find ()
1063 with
1064 | End_of_file -> E.s, E.s
1065 | exn ->
1066 dolog "exception while reading X authority data (%S): %s"
1067 path (exntos exn);
1068 E.s, E.s
1070 close_in ic;
1071 name, data;
1073 let opt =
1075 if String.length path = 0
1076 then None
1077 else Some (open_in_bin path)
1078 with exn ->
1079 if Sys.file_exists path
1080 then
1081 dolog "failed to open X authority file `%S' : %s"
1082 path (exntos exn);
1083 None
1085 match opt with
1086 | None -> E.s, E.s
1087 | Some ic -> readauth ic
1090 let init t rootwid w h osx =
1091 let d =
1092 try Sys.getenv "DISPLAY"
1093 with exn ->
1094 error "could not get DISPLAY evironment variable: %s"
1095 (exntos exn)
1097 let getnum w b e =
1098 if b = e
1099 then error "invalid DISPLAY(%s) %S" w d
1100 else
1101 let s = String.sub d b (e - b) in
1102 try int_of_string s
1103 with exn ->
1104 error "invalid DISPLAY %S can not parse %s(%S): %s"
1105 d w s (exntos exn)
1107 let rec phost pos =
1108 if pos = String.length d
1109 then error "invalid DISPLAY %S no display number specified" d
1110 else (
1111 if d.[pos] = ':'
1112 then
1113 let rec pdispnum pos1 =
1114 if pos1 = String.length d
1115 then getnum "display number" (pos+1) pos1, 0
1116 else
1117 match d.[pos1] with
1118 | '.' ->
1119 let dispnum = getnum "display number" (pos+1) pos1 in
1120 let rec pscreennum pos2 =
1121 if pos2 = String.length d
1122 then getnum "screen number" (pos1+1) pos2
1123 else
1124 match d.[pos2] with
1125 | '0' .. '9' -> pscreennum (pos2+1)
1126 | _ ->
1127 error "invalid DISPLAY %S, cannot parse screen number" d
1129 dispnum, pscreennum (pos1+1)
1130 | '0' .. '9' -> pdispnum (pos1+1)
1131 | _ ->
1132 error "invalid DISPLAY %S, cannot parse display number" d
1134 String.sub d 0 pos, pdispnum (pos+1)
1135 else phost (pos+1)
1138 let host, (dispnum, screennum) = phost 0 in
1139 let aname, adata = getauth host dispnum in
1140 let fd =
1141 let fd, addr =
1142 if osx || String.length host = 0 || host = "unix"
1143 then
1144 let addr =
1145 if osx
1146 then Unix.ADDR_UNIX d
1147 else Unix.ADDR_UNIX ("/tmp/.X11-unix/X" ^ string_of_int dispnum)
1149 let fd = Unix.socket Unix.PF_UNIX Unix.SOCK_STREAM 0 in
1150 fd, addr
1151 else
1152 let h =
1153 try Unix.gethostbyname host
1154 with exn ->
1155 error "cannot resolve %S: %s" host (exntos exn)
1157 let addr = h.Unix.h_addr_list.(0) in
1158 let port = 6000 + dispnum in
1159 let fd = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
1160 fd, (Unix.ADDR_INET (addr, port))
1162 try Unix.connect fd addr; fd
1163 with exn ->
1164 error "failed to connect to X: %s" (exntos exn)
1166 cloexec fd;
1167 let s = String.create 12 in
1168 let s = padcat s aname in
1169 let s = padcat s adata in
1170 s.[0] <- ordermagic;
1171 w16 s 2 11;
1172 w16 s 4 0;
1173 w16 s 6 (String.length aname);
1174 w16 s 8 (String.length adata);
1175 sendstr1 s 0 (String.length s) fd;
1176 state.sock <- fd;
1177 setup fd rootwid screennum w h;
1178 state.t <- t;
1179 fd, state.w, state.h;
1182 let settitle s =
1183 state.setwmname s;
1186 let setcursor cursor =
1187 if cursor != state.curcurs
1188 then
1189 let n =
1190 match cursor with
1191 | CURSOR_INHERIT -> -1
1192 | CURSOR_INFO -> 3
1193 | CURSOR_CYCLE -> 2
1194 | CURSOR_CROSSHAIR -> 0
1195 | CURSOR_TEXT -> 5
1197 let s = s32 (if n = -1 then 0 else state.fid+1+n) in
1198 let s = changewindowattributesreq state.wid 0x4000(*cursor*) s in
1199 sendstr s state.sock;
1200 state.curcurs <- cursor;
1203 let fullscreen () =
1204 state.fullscreen state.wid;
1207 let metamask = 0x40;;
1208 let altmask = 8;;
1209 let shiftmask = 1;;
1210 let ctrlmask = 4;;
1212 let withalt mask = mask land altmask != 0;;
1213 let withctrl mask = mask land ctrlmask != 0;;
1214 let withshift mask = mask land shiftmask != 0;;
1215 let withmeta mask = mask land metamask != 0;;
1216 let withnone mask = mask land (altmask + ctrlmask + shiftmask + metamask) = 0;;
1218 let xlatt, xlatf =
1219 let t = Hashtbl.create 20
1220 and f = Hashtbl.create 20 in
1221 let add n nl k =
1222 List.iter (fun s -> Hashtbl.add t s k) (n::nl);
1223 Hashtbl.add f k n
1225 let addc c =
1226 let s = String.create 1 in
1227 s.[0] <- c;
1228 add s [] (Char.code c)
1230 let addcr a b =
1231 let an = Char.code a and bn = Char.code b in
1232 for i = an to bn do addc (Char.chr i) done;
1234 addcr '0' '9';
1235 addcr 'a' 'z';
1236 addcr 'A' 'Z';
1237 String.iter addc "`~!@#$%^&*()-_=+\\|[{]};:,./<>?";
1238 for i = 0 to 29 do add ("f" ^ string_of_int (i+1)) [] (0xffbe + i) done;
1239 add "space" [] 0x20;
1240 add "ret" ["return"; "enter"] 0xff0d;
1241 add "tab" [] 0xff09;
1242 add "left" [] 0xff51;
1243 add "right" [] 0xff53;
1244 add "home" [] 0xff50;
1245 add "end" [] 0xff57;
1246 add "ins" ["insert"] 0xff63;
1247 add "del" ["delete"] 0xffff;
1248 add "esc" ["escape"] 0xff1b;
1249 add "pgup" ["pageup"] 0xff55;
1250 add "pgdown" ["pagedown"] 0xff56;
1251 add "backspace" [] 0xff08;
1252 add "up" [] 0xff52;
1253 add "down" [] 0xff54;
1254 add "menu" [] 0xff67;
1255 t, f;
1258 let keyname k =
1259 try Hashtbl.find xlatf k
1260 with Not_found -> Printf.sprintf "%#x" k;
1263 let namekey name =
1264 try Hashtbl.find xlatt name
1265 with Not_found ->
1266 if String.length name = 1
1267 then Char.code name.[0]
1268 else int_of_string name;