Hummm...
[llpp.git] / wsi.ml
blobba4c9429c8d74bdfd47af2ed3231974e49f42d76
1 open Utils;;
3 let (~>) = Bytes.unsafe_of_string;;
5 type cursor =
6 | CURSOR_INHERIT
7 | CURSOR_INFO
8 | CURSOR_CYCLE
9 | CURSOR_CROSSHAIR
10 | CURSOR_TEXT
13 type winstate =
14 | MaxVert
15 | MaxHorz
16 | Fullscreen
19 type visiblestate =
20 | Unobscured
21 | PartiallyObscured
22 | FullyObscured
25 type wid = int and screenno = int and vid = int and atom = int;;
27 external glxinit : string -> wid -> screenno -> vid = "ml_glxinit";;
28 external glxcompleteinit : unit -> unit = "ml_glxcompleteinit";;
29 external swapb : unit -> unit = "ml_swapb";;
31 let vlog fmt = Format.ksprintf ignore fmt;;
33 let onot = object
34 method display = ()
35 method map _ = ()
36 method expose = ()
37 method visible _ = ()
38 method reshape _ _ = ()
39 method mouse _ _ _ _ _ = ()
40 method motion _ _ = ()
41 method pmotion _ _ = ()
42 method key _ _ = ()
43 method enter _ _ = ()
44 method leave = ()
45 method winstate _ = ()
46 method quit = exit 0
47 end;;
49 class type t = object
50 method display : unit
51 method map : bool -> unit
52 method expose : unit
53 method visible : visiblestate -> unit
54 method reshape : int -> int -> unit
55 method mouse : int -> bool -> int -> int -> int -> unit
56 method motion : int -> int -> unit
57 method pmotion : int -> int -> unit
58 method key : int -> int -> unit
59 method enter : int -> int -> unit
60 method leave : unit
61 method winstate : winstate list -> unit
62 method quit : unit
63 end;;
65 type state =
66 { mutable mink : int
67 ; mutable maxk : int
68 ; mutable keymap : int array array
69 ; fifo : (bytes -> unit) Queue.t
70 ; mutable seq : int
71 ; mutable protoatom : atom
72 ; mutable deleatom : atom
73 ; mutable nwmsatom : atom
74 ; mutable maxvatom : atom
75 ; mutable maxhatom : atom
76 ; mutable fulsatom : atom
77 ; mutable idbase : int
78 ; mutable wid : int
79 ; mutable fid : int
80 ; mutable fullscreen : (int -> unit)
81 ; mutable setwmname : (bytes -> unit)
82 ; mutable actwin : (unit -> unit)
83 ; mutable stringatom : int
84 ; mutable t : t
85 ; mutable sock : Unix.file_descr
86 ; mutable x : int
87 ; mutable y : int
88 ; mutable w : int
89 ; mutable h : int
90 ; mutable fs : fs
91 ; mutable curcurs : cursor
92 ; mutable capslmask : int
93 ; mutable numlmask : int
94 ; mutable levl3mask : int
95 ; mutable levl5mask : int
97 and fs =
98 | NoFs
99 | Fs of (int * int * int * int)
102 let state =
103 { mink = max_int
104 ; maxk = min_int
105 ; keymap = E.a
106 ; fifo = Queue.create ()
107 ; seq = 0
108 ; protoatom = -1
109 ; deleatom = -1
110 ; nwmsatom = -1
111 ; maxvatom = -1
112 ; maxhatom = -1
113 ; fulsatom = -1
114 ; idbase = -1
115 ; wid = -1
116 ; fid = -1
117 ; fullscreen = (fun _ -> ())
118 ; setwmname = (fun _ -> ())
119 ; actwin = (fun _ -> ())
120 ; sock = Unix.stdin
121 ; t = onot
122 ; x = -1
123 ; y = -1
124 ; w = -1
125 ; h = -1
126 ; fs = NoFs
127 ; stringatom = 31
128 ; curcurs = CURSOR_INHERIT
129 ; capslmask = 0
130 ; numlmask = 0
131 ; levl3mask = 0
132 ; levl5mask = 0
136 include Bo;;
138 let makereq opcode len reqlen =
139 let s = Bytes.create len in
140 w8 s 0 opcode;
141 w16 s 2 reqlen;
145 let recv fd s pos len =
146 Unix.read fd s pos len;
149 let readstr sock n =
150 let s = Bytes.create n in
151 let rec loop pos n =
152 let m = tempfailureretry (recv 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 let s = Bytes.unsafe_to_string s in
167 vlog "%d <= %S" state.seq s;
168 state.seq <- state.seq + 1;
169 let n = tempfailureretry (Unix.write_substring sock s pos) len in
170 if n != len
171 then error "send %d returned %d" len n;
174 let updkmap sock resp =
175 let syms = r8 resp 1 in
176 let len = r32 resp 4 in
177 let data =
178 if len > 0
179 then readstr sock (4*len)
180 else E.b
182 let m = len / syms in
183 state.keymap <- Array.make_matrix
184 (state.maxk - state.mink) syms 0xffffff;
185 let rec loop i = if i = m then () else
186 let k = i*4*syms in
187 let rec loop2 k l = if l = syms then () else
188 let v = r32 data k in
189 state.keymap.(i).(l) <- v;
190 loop2 (k+4) (l+1)
192 loop2 k 0;
193 loop (i+1);
195 loop 0;
198 let updmodmap sock resp =
199 let n = r8 resp 1 in
200 let len = r16 resp 4 in
201 let data =
202 if len > 0
203 then readstr sock (len*4)
204 else E.b
206 if len > 0 then (*???*)
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 (Bytes.length s) sock;
259 let padcatl =
260 let pad = "123" in
261 fun ss ->
262 let b = Buffer.create 16 in
263 List.iter (Buffer.add_bytes b) ss;
264 let bl = Buffer.length b in
265 let pl = bl land 3 in
266 if pl != 0
267 then (
268 Buffer.add_substring b pad 0 (4 - pl);
270 Buffer.to_bytes b;
273 let padcat s1 s2 = padcatl [s1; s2];;
275 let internreq name onlyifexists =
276 let s = makereq 16 8 8 in
277 let s = padcat s name in
278 w8 s 1 (if onlyifexists then 1 else 0);
279 w16 s 2 (Bytes.length s / 4);
280 w16 s 4 (Bytes.length name);
284 let sendintern sock s onlyifexists f =
285 let s = internreq s onlyifexists in
286 sendwithrep sock s f;
289 let createwindowreq wid parent x y w h bw eventmask vid depth mid =
290 let s = makereq 1 48 12 in
291 w8 s 1 depth;
292 w32 s 4 wid;
293 w32 s 8 parent;
294 w16 s 12 x;
295 w16 s 14 y;
296 w16 s 16 w;
297 w16 s 18 h;
298 w16 s 20 bw;
299 w16 s 22 0; (* inputoutput *)
300 w32 s 24 vid; (* visual *)
301 w32 s 28 0x280a; (* eventmask*)
302 w32 s 32 0;
303 w32 s 36 0;
304 w32 s 40 eventmask;
305 w32 s 44 mid;
309 let createcolormapreq mid wid vid =
310 let s = makereq 78 16 4 in
311 w8 s 1 0;
312 w32 s 4 mid;
313 w32 s 8 wid;
314 w32 s 12 vid;
318 let getgeometryreq wid =
319 let s = makereq 14 8 2 in
320 w32 s 4 wid;
324 let mapreq wid =
325 let s = makereq 8 8 2 in
326 w32 s 4 wid;
330 let getkeymapreq first count =
331 let s = makereq 101 8 2 in
332 w8 s 4 first;
333 w8 s 5 count;
337 let changepropreq wid prop typ format props =
338 let s = makereq 18 24 0 in
339 let s = padcat s props in
340 w8 s 1 0;
341 w16 s 2 (Bytes.length s / 4);
342 w32 s 4 wid;
343 w32 s 8 prop;
344 w32 s 12 typ;
345 w8 s 16 format;
346 let ful = Bytes.length props / (match format with
347 | 8 -> 1
348 | 16 -> 2
349 | 32 -> 4
350 | n -> error "no idea what %d means" n)
352 w32 s 20 ful;
356 let getpropreq delete wid prop typ =
357 let s = makereq 20 24 6 in
358 w8 s 1 (if delete then 1 else 0);
359 w32 s 4 wid;
360 w32 s 8 prop;
361 w32 s 12 typ;
362 w32 s 16 0;
363 w32 s 20 2;
367 let openfontreq fid name =
368 let s = makereq 45 12 0 in
369 let s = padcat s name in
370 w16 s 2 (Bytes.length s / 4);
371 w32 s 4 fid;
372 w16 s 8 (Bytes.length name);
376 let createglyphcursorreq fid cid cindex =
377 let s = makereq 94 32 8 in
378 w32 s 4 cid;
379 w32 s 8 fid;
380 w32 s 12 fid;
381 w16 s 16 cindex;
382 w16 s 18 (cindex+1);
383 w16 s 20 0;
384 w16 s 22 0;
385 w16 s 24 0;
386 w16 s 26 0xffff;
387 w16 s 28 0xffff;
388 w16 s 30 0xffff;
392 let changewindowattributesreq wid mask attrs =
393 let s = makereq 2 12 0 in
394 let s = padcat s attrs in
395 w16 s 2 (Bytes.length s / 4);
396 w32 s 4 wid;
397 w32 s 8 mask;
401 let configurewindowreq wid mask values =
402 let s = makereq 12 12 0 in
403 let s = padcat s values in
404 w16 s 2 (Bytes.length s / 4);
405 w32 s 4 wid;
406 w16 s 8 mask;
410 let s32 n =
411 let s = Bytes.create 4 in
412 w32 s 0 n;
416 let clientmessage format seq wid typ data =
417 let s = makereq 33 12 0 in
418 let s = padcat s data in
419 w8 s 1 format;
420 w16 s 2 seq;
421 w32 s 4 wid;
422 w32 s 8 typ;
426 let sendeventreq propagate destwid mask data =
427 let s = makereq 25 12 11 in
428 let s = padcat s data in
429 w8 s 1 propagate;
430 w16 s 2 11;
431 w32 s 4 destwid;
432 w32 s 8 mask;
436 let getmodifiermappingreq () =
437 makereq 119 4 1;
440 let getkeysym code mask =
441 let pkpk = state.keymap.(code-state.mink).(0) in
442 if (pkpk >= 0xff80 && pkpk <= 0xffbd)
443 || (pkpk >= 0x11000000 && pkpk <= 0x1100ffff)
444 then (
445 if mask land state.numlmask != 0
446 then
447 let keysym = state.keymap.(code-state.mink).(1) in
448 if keysym = 0 then pkpk else keysym
449 else pkpk
451 else (
452 let shift =
453 if pkpk land 0xf000 = 0xf000
454 then 0
455 else (mask land 1) lxor ((mask land state.capslmask) lsr 1)
457 let index =
458 let l3 = (mask land state.levl3mask) != 0 in
459 let l4 = (mask land state.levl5mask) != 0 in
460 shift +
461 if l3 then (if l4 then 8 else 4) else (if l4 then 6 else 0)
463 let keysym = state.keymap.(code-state.mink).(index) in
464 if index land 1 = 1 && keysym = 0
465 then state.keymap.(code-state.mink).(index - 1)
466 else keysym
470 let readresp sock =
471 let resp = readstr sock 32 in
472 let opcode = r8 resp 0 in
473 match opcode land lnot 0x80 with
474 | 0 -> (* error *)
475 let s = resp in
476 let code = r8 s 1
477 and serial = r16 s 2
478 and resid = r32 resp 4
479 and min = r16 s 8
480 and maj = r8 s 10 in
481 error "code=%d serial=%d resid=%#x min=%d maj=%d\n%S"
482 code serial resid min maj (Bytes.unsafe_to_string resp);
484 | 1 -> (* response *)
485 let rep = Queue.pop state.fifo in
486 rep resp;
488 | 2 -> (* key press *)
489 if Array.length state.keymap > 0
490 then
491 let code = r8 resp 1 in
492 let mask = r16 resp 28 in
493 let keysym = getkeysym code mask in
494 vlog "keysym = %x %c mask %#x code %d"
495 keysym (Char.unsafe_chr keysym) mask code;
496 state.t#key keysym mask;
498 | 3 -> (* key release *)
499 if Array.length state.keymap > 0
500 then
501 let code = r8 resp 1 in
502 let mask = r16 resp 28 in
503 let keysym = getkeysym code mask in
504 vlog "release keysym = %x %c mask %#x code %#d"
505 keysym (Char.unsafe_chr keysym) mask code
507 | 4 -> (* buttonpress *)
508 let n = r8 resp 1
509 and x = r16s resp 24
510 and y = r16s resp 26
511 and m = r16 resp 28 in
512 state.t#mouse n true x y m;
513 vlog "press %d" n
515 | 5 -> (* buttonrelease *)
516 let n = r8 resp 1
517 and x = r16s resp 24
518 and y = r16s resp 26
519 and m = r16 resp 28 in
520 state.t#mouse n false x y m;
521 vlog "release %d %d %d" n x y
523 | 6 -> (* motion *)
524 let x = r16s resp 24 in
525 let y = r16s resp 26 in
526 let m = r16 resp 28 in
527 if m land 0x1f00 = 0
528 then state.t#pmotion x y
529 else state.t#motion x y;
530 vlog "move %dx%d => %d" x y m
532 | 7 -> (* enter *)
533 let x = r16s resp 24
534 and y = r16s resp 26 in
535 state.t#enter x y;
536 vlog "enter %d %d" x y
538 | 8 -> (* leave *)
539 state.t#leave
541 | 18 -> (* unmap *)
542 state.t#map false;
543 vlog "unmap";
545 | 19 -> (* map *)
546 state.t#map true;
547 vlog "map";
549 | 12 -> (* exposure *)
550 vlog "exposure";
551 state.t#expose
553 | 15 -> (* visibility *)
554 let v = r8 resp 8 in
555 let vis =
556 match v with
557 | 0 -> Unobscured
558 | 1 -> PartiallyObscured
559 | 2 -> FullyObscured
560 | _ ->
561 dolog "unknown visibility %d" v;
562 Unobscured
564 state.t#visible vis;
565 vlog "visibility %d" v;
567 | 34 -> (* mapping *)
568 state.keymap <- E.a;
569 let s = getkeymapreq state.mink (state.maxk-state.mink-1) in
570 sendwithrep sock s (updkmap sock);
571 state.capslmask <- 0;
572 state.levl3mask <- 0;
573 state.levl5mask <- 0;
574 state.numlmask <- 0;
575 let s = getmodifiermappingreq () in
576 sendwithrep sock s (updmodmap sock);
578 | 33 -> (* clientmessage *)
579 let atom = r32 resp 8 in
580 if atom = state.protoatom
581 then (
582 let atom = r32 resp 12 in
583 if atom = state.deleatom
584 then state.t#quit;
586 vlog "atom %#x" atom
588 | 21 -> (* reparent *)
589 vlog "reparent"
591 | 22 -> (* configure *)
592 let x = r16s resp 16
593 and y = r16s resp 18
594 and w = r16 resp 20
595 and h = r16 resp 22 in
596 vlog "configure cur [%d %d %d %d] conf [%d %d %d %d]"
597 state.x state.y state.w state.h
598 x y w h
600 if w != state.w || h != state.h
601 then (
602 state.t#reshape w h;
604 state.w <- w;
605 state.h <- h;
606 state.x <- x;
607 state.y <- y;
608 state.t#expose
610 | 28 ->
611 let atom = r32 resp 8 in
612 if atom = state.nwmsatom
613 then
614 let s = getpropreq false state.wid atom 4 in
615 sendwithrep sock s (fun resp ->
616 let len = r32 resp 4 in
617 let nitems = r32 resp 16 in
618 let wsl =
619 if len = 0
620 then []
621 else
622 let s = readstr sock (len*4) in
623 let rec loop wsl i = if i = nitems then wsl else
624 let atom = r32 s (i*4) in
625 let wsl =
626 if atom = state.maxhatom
627 then MaxHorz::wsl
628 else (
629 if atom = state.maxvatom
630 then MaxVert::wsl
631 else (
632 if atom = state.fulsatom
633 then (
634 state.fs <- Fs (state.x, state.y, state.w, state.h);
635 Fullscreen::wsl
637 else wsl
640 in loop wsl (i+1)
642 loop [] 0
644 state.t#winstate (List.sort compare wsl)
647 | n ->
648 dolog "event %d %S" n (Bytes.unsafe_to_string resp)
651 let readresp sock =
652 let rec loop () =
653 readresp sock;
654 if hasdata sock then loop ();
656 loop ();
659 let sendstr s ?(pos=0) ?(len=Bytes.length s) sock =
660 sendstr1 s pos len sock;
661 if hasdata sock then readresp sock;
664 let reshape w h =
665 if state.fs = NoFs
666 then
667 let s = Bytes.create 8 in
668 w32 s 0 w;
669 w32 s 4 h;
670 let s = configurewindowreq state.wid 0x000c s in
671 sendstr s state.sock;
672 else state.fullscreen state.wid
675 let activatewin () =
676 state.actwin ();
679 let syncsendwithrep sock secstowait s f =
680 let completed = ref false in
681 sendwithrep sock s (fun resp -> f resp; completed := true);
682 let now = Unix.gettimeofday in
683 let deadline = now () +. secstowait in
684 let rec readtillcompletion () =
685 let sf deadline =
686 let timeout = deadline -. now () in
687 if timeout <= 0.0
688 then [], [], []
689 else Unix.select [sock] [] [] timeout
691 let r, _, _ = tempfailureretry sf deadline in
692 match r with
693 | [] -> error "didn't get X response in %f seconds, aborting" secstowait
694 | _ ->
695 readresp sock;
696 if not !completed
697 then readtillcompletion ()
699 readtillcompletion ();
702 let mapwin () =
703 let s = mapreq state.wid in
704 sendstr s state.sock;
707 let syncsendintern sock secstowait s onlyifexists f =
708 let s = internreq s onlyifexists in
709 syncsendwithrep sock secstowait s f;
712 let setup disp sock rootwid screennum w h =
713 let s = readstr sock 2 in
714 let n = Bytes.length s in
715 if n != 2
716 then error "failed to read X connection setup response n=%d" n;
717 match Bytes.get s 0 with
718 | '\000' ->
719 let reasonlen = r8 s 1 in
720 let s = readstr sock 6 in
721 let maj = r16 s 0
722 and min = r16 s 2
723 and add = r16 s 4 in
724 let len = add*4 in
725 let data = readstr sock len in
726 let reason = Bytes.sub data 0 reasonlen in
727 error "X connection failed maj=%d min=%d reason=%S"
728 maj min (Bytes.unsafe_to_string reason)
730 | '\002' -> failwith "X connection setup failed: authentication required";
732 | '\001' ->
733 let s = readstr sock 38 in
734 let maj = r16 s 0
735 and min = r16 s 2
736 and add = r16 s 4
737 and idbase = r32 s 10
738 and idmask = r32 s 14
739 and vlen = r16 s 22
740 and screens = r8 s 26
741 and formats = r8 s 27
742 and minkk = r8 s 32
743 and maxkk = r8 s 33 in
744 let data = readstr sock (4*add-32) in
745 let vendor = Bytes.sub data 0 vlen in
746 let pos = ((vlen+3) land lnot 3) + formats*8 in
748 if screennum >= screens
749 then error "invalid screen %d, max %d" screennum (screens-1);
751 let pos =
752 let rec findscreen n pos = if n = screennum then pos else
753 let pos =
754 let ndepths = r8 data (pos+39) in
755 let rec skipdepths n pos = if n = ndepths then pos else
756 let pos =
757 let nvisiuals = r16 data (pos+2) in
758 pos + nvisiuals*24 + 8
760 skipdepths (n+1) pos
762 skipdepths n (pos+40)
764 findscreen (n+1) pos
766 findscreen 0 pos
768 let root = if rootwid = 0 then r32 data pos else rootwid in
769 let rootw = r16 data (pos+20)
770 and rooth = r16 data (pos+22)
771 and rootdepth = r8 data (pos+38)in
773 state.mink <- minkk;
774 state.maxk <- maxkk;
775 state.idbase <- idbase;
776 vlog "vendor = %S, maj=%d min=%d" (Bytes.unsafe_to_string vendor) maj min;
777 vlog "screens = %d formats = %d" screens formats;
778 vlog "minkk = %d maxkk = %d" minkk maxkk;
779 vlog "idbase = %#x idmask = %#x" idbase idmask;
780 vlog "root=%#x %dx%d" root rootw rooth;
781 vlog "wmm = %d, hmm = %d" (r16 data (pos+24)) (r16 data (pos+26));
782 vlog "visualid = %#x " (r32 data (pos+32));
783 vlog "root depth = %d" rootdepth;
785 let wid = state.idbase in
786 let mid = wid+1 in
787 let fid = mid+1 in
789 state.wid <- wid;
790 state.fid <- fid;
792 let vid = glxinit disp wid screennum in
793 let ndepths = r8 data (pos+39) in
794 let rec finddepth n' pos =
795 if n' = ndepths
796 then error "couldn't find depth for visual %#x" vid;
797 let depth = r8 data pos in
798 let nvisuals = r16 data (pos+2) in
799 let rec findvisual n pos =
800 if n = nvisuals
801 then finddepth (n'+1) pos
802 else
803 let id = r32 data pos in
804 if id = vid
805 then depth
806 else findvisual (n+1) (pos+24)
808 findvisual 0 (pos+8)
810 let depth = finddepth 0 (pos+40) in
812 let s = createcolormapreq mid root vid in
813 sendstr s sock;
815 let mask = 0
816 + 0x00000001 (* KeyPress *)
817 (* + 0x00000002 *) (* KeyRelease *)
818 + 0x00000004 (* ButtonPress *)
819 + 0x00000008 (* ButtonRelease *)
820 + 0x00000010 (* EnterWindow *)
821 + 0x00000020 (* LeaveWindow *)
822 + 0x00000040 (* PointerMotion *)
823 (* + 0x00000080 *) (* PointerMotionHint *)
824 (* + 0x00000100 *) (* Button1Motion *)
825 (* + 0x00000200 *) (* Button2Motion *)
826 (* + 0x00000400 *) (* Button3Motion *)
827 (* + 0x00000800 *) (* Button4Motion *)
828 (* + 0x00001000 *) (* Button5Motion *)
829 + 0x00002000 (* ButtonMotion *)
830 (* + 0x00004000 *) (* KeymapState *)
831 + 0x00008000 (* Exposure *)
832 + 0x00010000 (* VisibilityChange *)
833 + 0x00020000 (* StructureNotify *)
834 (* + 0x00040000 *) (* ResizeRedirect *)
835 (* + 0x00080000 *) (* SubstructureNotify *)
836 (* + 0x00100000 *) (* SubstructureRedirect *)
837 (* + 0x00200000 *) (* FocusChange *)
838 + 0x00400000 (* PropertyChange *)
839 (* + 0x00800000 *) (* ColormapChange *)
840 (* + 0x01000000 *) (* OwnerGrabButton *)
843 let s = createwindowreq wid root 0 0 w h 0 mask vid depth mid in
844 sendstr s sock;
846 sendintern sock (~> "WM_PROTOCOLS") false (fun resp ->
847 state.protoatom <- r32 resp 8;
848 sendintern
849 sock (~> "WM_DELETE_WINDOW") false (fun resp ->
850 state.deleatom <- r32 resp 8;
851 let s = s32 state.deleatom in
852 let s = changepropreq wid state.protoatom 4 32 s in
853 sendstr s sock;
857 sendintern sock (~> "WM_CLIENT_MACHINE") false (fun resp ->
858 let atom = r32 resp 8 in
859 let empty = E.s in
860 let hostname =
861 try Unix.gethostname ()
862 with exn ->
863 dolog "error getting host name: %s" (exntos exn);
864 empty
866 if hostname != empty
867 then
868 let s = changepropreq wid atom state.stringatom 8
869 (~> 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 = Bytes.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 = Bytes.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 = Bytes.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 = Bytes.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 r16be s =
1026 let rb pos = Char.code (Bytes.get s pos) in
1027 (rb 1) lor ((rb 0) lsl 8)
1029 let rec find () =
1030 let rs () =
1031 let s = really_input_string ic 2 in
1032 let n = r16be (~> s) in
1033 really_input_string ic n
1035 let family = really_input_string ic 2 in
1036 let addr = rs () in
1037 let nums = rs () in
1038 let optnum =
1039 try Some (int_of_string nums)
1040 with exn ->
1041 dolog
1042 "display number(%S) is not an integer (corrupt %S?): %s"
1043 nums path (exntos exn);
1044 None
1046 let name = rs () in
1047 let data = rs () in
1049 vlog "family %S addr %S(%S) num %S(%d) name %S data %S"
1050 family addr haddr nums dnum name data;
1051 match optnum with
1052 | Some num when addr = haddr && num = dnum ->
1053 name, data
1054 | _ -> find ()
1056 let name, data =
1057 try find ()
1058 with
1059 | End_of_file -> E.s, E.s
1060 | exn ->
1061 dolog "exception while reading X authority data (%S): %s"
1062 path (exntos exn);
1063 E.s, E.s
1065 close_in ic;
1066 name, data;
1068 let opt =
1070 if String.length path = 0
1071 then None
1072 else Some (open_in_bin path)
1073 with exn ->
1074 if Sys.file_exists path
1075 then
1076 dolog "failed to open X authority file `%S' : %s"
1077 path (exntos exn);
1078 None
1080 match opt with
1081 | None -> E.s, E.s
1082 | Some ic -> readauth ic
1085 let init t rootwid w h osx =
1086 let d =
1087 try Sys.getenv "DISPLAY"
1088 with exn ->
1089 error "could not get DISPLAY evironment variable: %s"
1090 (exntos exn)
1092 let getnum w b e =
1093 if b = e
1094 then error "invalid DISPLAY(%s) %S" w d
1095 else
1096 let s = String.sub d b (e - b) in
1097 try int_of_string s
1098 with exn ->
1099 error "invalid DISPLAY %S can not parse %s(%S): %s"
1100 d w s (exntos exn)
1102 let rec phost pos =
1103 if pos = String.length d
1104 then error "invalid DISPLAY %S no display number specified" d
1105 else (
1106 if d.[pos] = ':'
1107 then
1108 let rec pdispnum pos1 =
1109 if pos1 = String.length d
1110 then getnum "display number" (pos+1) pos1, 0
1111 else
1112 match d.[pos1] with
1113 | '.' ->
1114 let dispnum = getnum "display number" (pos+1) pos1 in
1115 let rec pscreennum pos2 =
1116 if pos2 = String.length d
1117 then getnum "screen number" (pos1+1) pos2
1118 else
1119 match d.[pos2] with
1120 | '0' .. '9' -> pscreennum (pos2+1)
1121 | _ ->
1122 error "invalid DISPLAY %S, cannot parse screen number" d
1124 dispnum, pscreennum (pos1+1)
1125 | '0' .. '9' -> pdispnum (pos1+1)
1126 | _ ->
1127 error "invalid DISPLAY %S, cannot parse display number" d
1129 String.sub d 0 pos, pdispnum (pos+1)
1130 else phost (pos+1)
1133 let host, (dispnum, screennum) = phost 0 in
1134 let aname, adata = getauth host dispnum in
1135 let fd =
1136 let fd, addr =
1137 if osx || String.length host = 0 || host = "unix"
1138 then
1139 let addr =
1140 if osx
1141 then Unix.ADDR_UNIX d
1142 else Unix.ADDR_UNIX ("/tmp/.X11-unix/X" ^ string_of_int dispnum)
1144 let fd = Unix.socket Unix.PF_UNIX Unix.SOCK_STREAM 0 in
1145 fd, addr
1146 else
1147 let h =
1148 try Unix.gethostbyname host
1149 with exn ->
1150 error "cannot resolve %S: %s" host (exntos exn)
1152 let addr = h.Unix.h_addr_list.(0) in
1153 let port = 6000 + dispnum in
1154 let fd = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
1155 fd, (Unix.ADDR_INET (addr, port))
1157 try Unix.connect fd addr; fd
1158 with exn ->
1159 error "failed to connect to X: %s" (exntos exn)
1161 cloexec fd;
1162 let s = Bytes.create 12 in
1163 let s = padcat s (~> aname) in
1164 let s = padcat s (~> adata) in
1165 Bytes.set s 0 ordermagic;
1166 w16 s 2 11;
1167 w16 s 4 0;
1168 w16 s 6 (String.length aname);
1169 w16 s 8 (String.length adata);
1170 sendstr1 s 0 (Bytes.length s) fd;
1171 state.sock <- fd;
1172 setup d fd rootwid screennum w h;
1173 state.t <- t;
1174 fd, state.w, state.h;
1177 let settitle s =
1178 state.setwmname (~> s);
1181 let setcursor cursor =
1182 if cursor != state.curcurs
1183 then
1184 let n =
1185 match cursor with
1186 | CURSOR_INHERIT -> -1
1187 | CURSOR_INFO -> 3
1188 | CURSOR_CYCLE -> 2
1189 | CURSOR_CROSSHAIR -> 0
1190 | CURSOR_TEXT -> 5
1192 let s = s32 (if n = -1 then 0 else state.fid+1+n) in
1193 let s = changewindowattributesreq state.wid 0x4000(*cursor*) s in
1194 sendstr s state.sock;
1195 state.curcurs <- cursor;
1198 let fullscreen () =
1199 state.fullscreen state.wid;
1202 let metamask = 0x40;;
1203 let altmask = 8;;
1204 let shiftmask = 1;;
1205 let ctrlmask = 4;;
1207 let withalt mask = mask land altmask != 0;;
1208 let withctrl mask = mask land ctrlmask != 0;;
1209 let withshift mask = mask land shiftmask != 0;;
1210 let withmeta mask = mask land metamask != 0;;
1211 let withnone mask = mask land (altmask + ctrlmask + shiftmask + metamask) = 0;;
1213 let xlatt, xlatf =
1214 let t = Hashtbl.create 20
1215 and f = Hashtbl.create 20 in
1216 let add n nl k =
1217 List.iter (fun s -> Hashtbl.add t s k) (n::nl);
1218 Hashtbl.add f k n
1220 let addc c =
1221 let s = String.make 1 c in
1222 add s [] (Char.code c)
1224 let addcr a b =
1225 let an = Char.code a and bn = Char.code b in
1226 for i = an to bn do addc (Char.chr i) done;
1228 addcr '0' '9';
1229 addcr 'a' 'z';
1230 addcr 'A' 'Z';
1231 String.iter addc "`~!@#$%^&*()-_=+\\|[{]};:,./<>?";
1232 for i = 0 to 29 do add ("f" ^ string_of_int (i+1)) [] (0xffbe + i) done;
1233 add "space" [] 0x20;
1234 add "ret" ["return"; "enter"] 0xff0d;
1235 add "tab" [] 0xff09;
1236 add "left" [] 0xff51;
1237 add "right" [] 0xff53;
1238 add "home" [] 0xff50;
1239 add "end" [] 0xff57;
1240 add "ins" ["insert"] 0xff63;
1241 add "del" ["delete"] 0xffff;
1242 add "esc" ["escape"] 0xff1b;
1243 add "pgup" ["pageup"] 0xff55;
1244 add "pgdown" ["pagedown"] 0xff56;
1245 add "backspace" [] 0xff08;
1246 add "up" [] 0xff52;
1247 add "down" [] 0xff54;
1248 add "menu" [] 0xff67;
1249 t, f;
1252 let keyname k =
1253 try Hashtbl.find xlatf k
1254 with Not_found -> Printf.sprintf "%#x" k;
1257 let namekey name =
1258 try Hashtbl.find xlatt name
1259 with Not_found ->
1260 if String.length name = 1
1261 then Char.code name.[0]
1262 else int_of_string name;