Fix trimming
[llpp.git] / wsi.ml
blobb4481d18474f2ecddab3ecb98b145888a8b820bc
1 type cursor =
2 | CURSOR_INHERIT
3 | CURSOR_INFO
4 | CURSOR_CYCLE
5 | CURSOR_CROSSHAIR
6 | CURSOR_TEXT
7 ;;
9 type winstate =
10 | MaxVert
11 | MaxHorz
12 | Fullscreen
15 let tempfailureretry f a =
16 let rec g () =
17 try f a with Unix.Unix_error (Unix.EINTR, _, _) -> g ()
18 in g ()
21 external cloexec : Unix.file_descr -> unit = "ml_cloexec";;
22 external glx : int -> unit = "ml_glx";;
23 external glxsync : unit -> unit = "ml_glxsync";;
24 external swapb : unit -> unit = "ml_swapb";;
25 external hasdata : Unix.file_descr -> bool = "ml_hasdata";;
26 external toutf8 : int -> string = "ml_keysymtoutf8";;
28 let dolog fmt = Format.kprintf prerr_endline fmt;;
29 let vlog fmt = Format.kprintf ignore fmt;;
31 let onot = object
32 method display = ()
33 method expose = ()
34 method reshape _ _ = ()
35 method mouse _ _ _ _ _ = ()
36 method motion _ _ = ()
37 method pmotion _ _ = ()
38 method key _ _ = ()
39 method enter _ _ = ()
40 method leave = ()
41 method winstate _ = ()
42 method quit = exit 0
43 end;;
45 class type t = object
46 method display : unit
47 method expose : unit
48 method reshape : int -> int -> unit
49 method mouse : int -> bool -> int -> int -> int -> unit
50 method motion : int -> int -> unit
51 method pmotion : int -> int -> unit
52 method key : int -> int -> unit
53 method enter : int -> int -> unit
54 method leave : unit
55 method winstate : winstate list -> unit
56 method quit : unit
57 end;;
59 type state =
60 { mutable mink : int
61 ; mutable maxk : int
62 ; mutable keymap : int array array
63 ; fifo : (string -> unit) Queue.t
64 ; mutable seq : int
65 ; mutable protoatom : int
66 ; mutable deleatom : int
67 ; mutable nwmsatom : int
68 ; mutable maxvatom : int
69 ; mutable maxhatom : int
70 ; mutable fulsatom : int
71 ; mutable idbase : int
72 ; mutable fullscreen : (int -> unit)
73 ; mutable setwmname : (string -> unit)
74 ; mutable actwin : (unit -> unit)
75 ; mutable stringatom : int
76 ; mutable t : t
77 ; mutable sock : Unix.file_descr
78 ; mutable x : int
79 ; mutable y : int
80 ; mutable w : int
81 ; mutable h : int
82 ; mutable fs : fs
83 ; mutable curcurs : cursor
84 ; mutable capslmask : int
85 ; mutable numlmask : int
86 ; mutable levl3mask : int
87 ; mutable levl5mask : int
89 and fs =
90 | NoFs
91 | Fs of (int * int * int * int)
94 let state =
95 { mink = max_int
96 ; maxk = min_int
97 ; keymap = [||]
98 ; fifo = Queue.create ()
99 ; seq = 0
100 ; protoatom = -1
101 ; deleatom = -1
102 ; nwmsatom = -1
103 ; maxvatom = -1
104 ; maxhatom = -1
105 ; fulsatom = -1
106 ; idbase = -1
107 ; fullscreen = (fun _ -> ())
108 ; setwmname = (fun _ -> ())
109 ; actwin = (fun _ -> ())
110 ; sock = Unix.stdin
111 ; t = onot
112 ; x = -1
113 ; y = -1
114 ; w = -1
115 ; h = -1
116 ; fs = NoFs
117 ; stringatom = 31
118 ; curcurs = CURSOR_INHERIT
119 ; capslmask = 0
120 ; numlmask = 0
121 ; levl3mask = 0
122 ; levl5mask = 0
126 let w8 s pos i = String.set s pos (Char.chr (i land 0xff));;
128 let w16 s pos i =
129 w8 s pos i;
130 w8 s (pos+1) (i lsr 8);
133 let w32 s pos i =
134 w16 s pos i;
135 w16 s (pos+2) (i lsr 16);
138 let r16 s pos =
139 let rb pos1 = Char.code (String.get s (pos + pos1)) in
140 (rb 0) lor ((rb 1) lsl 8)
143 let r16s s pos =
144 let i = r16 s pos in
145 i - ((i land 0x8000) lsl 1);
148 let r8 s pos = Char.code (String.get s pos);;
150 let r32 s pos =
151 let rb pos1 = Char.code (String.get s (pos + pos1)) in
152 let l = (rb 0) lor ((rb 1) lsl 8)
153 and u = (rb 2) lor ((rb 3) lsl 8) in
154 (u lsl 16) lor l
157 let exntos = function
158 | Unix.Unix_error (e, s, a) -> Printf.sprintf "%s(%s):%s(%d)"
159 s a (Unix.error_message e) (Obj.magic e)
160 | exn -> Printexc.to_string exn;
163 let error fmt = Printf.kprintf failwith fmt;;
165 let readstr sock n =
166 let s = String.create n in
167 let rec loop pos n =
168 let m = tempfailureretry (Unix.read sock s pos) n in
169 if m = 0
170 then state.t#quit;
171 if n != m
172 then (
173 ignore (tempfailureretry (Unix.select [sock] [] []) 0.01);
174 loop (pos + m) (n - m)
177 loop 0 n;
181 let sendstr1 s pos len sock =
182 vlog "%d => %S" state.seq s;
183 state.seq <- state.seq + 1;
184 let n = tempfailureretry (Unix.send sock s pos len) [] in
185 if n != len
186 then error "send %d returned %d" len n;
189 let updkmap sock resp =
190 let syms = r8 resp 1 in
191 let len = r32 resp 4 in
192 let data =
193 if len > 0
194 then readstr sock (4*len)
195 else ""
197 let m = len / syms in
198 state.keymap <- Array.make_matrix
199 (state.maxk - state.mink) syms 0xffffff;
200 let rec loop i = if i = m then () else
201 let k = i*4*syms in
202 let rec loop2 k l = if l = syms then () else
203 let v = r32 data k in
204 state.keymap.(i).(l) <- v;
205 loop2 (k+4) (l+1)
207 loop2 k 0;
208 loop (i+1);
210 loop 0;
213 let updmodmap sock resp =
214 let n = r8 resp 1 in
215 let len = r16 resp 4 in
216 let data =
217 if len > 0
218 then readstr sock (len*4)
219 else ""
221 let modmap = Array.make_matrix 8 n 0xffffff in
222 let rec loop l = if l = 8 then () else
223 let p = l*n in
224 let rec loop1 m = if m = n then () else
225 let p = p+m in
226 let code = r8 data p in
227 modmap.(l).(m) <- code;
228 if l = 1
229 then (
230 let ki = code - state.mink in
231 if ki >= 0
232 then
233 let a = state.keymap.(ki) in
234 let rec capsloop i = if i = Array.length a || i > 3 then () else
235 let s = a.(i) in
236 if s = 0xffe5
237 then state.capslmask <- 2
238 else capsloop (i+1)
240 capsloop 0;
242 else (
243 if l > 3
244 then (
245 let ki = code - state.mink in
246 if ki >= 0
247 then
248 let a = state.keymap.(ki) in
249 let rec lloop i = if i = Array.length a || i > 3 then () else
250 let s = a.(i) in
251 match s with
252 | 0xfe03 -> state.levl3mask <- 1 lsl l
253 | 0xfe11 -> state.levl5mask <- 1 lsl l
254 | 0xff7f -> state.numlmask <- 1 lsl l
255 | _ -> lloop (i+1)
257 lloop 0;
260 loop1 (m+1)
262 loop1 0;
263 loop (l+1)
265 loop 0;
268 let sendwithrep sock s f =
269 Queue.push f state.fifo;
270 sendstr1 s 0 (String.length s) sock;
273 let padcatl ss =
274 let b = Buffer.create 16 in
275 List.iter (Buffer.add_string b) ss;
276 let bl = Buffer.length b in
277 let pl = bl land 3 in
278 if pl != 0
279 then (
280 let pad = "123" in
281 Buffer.add_substring b pad 0 (4 - pl);
283 Buffer.contents b;
286 let padcat s1 s2 = padcatl [s1; s2];;
288 let internreq name onlyifexists =
289 let s = "\016\000\000\000\000\000\000\000" in
290 let s = padcat s name in
291 if onlyifexists then w8 s 1 1;
292 w16 s 2 (String.length s / 4);
293 w16 s 4 (String.length name);
297 let sendintern sock s onlyifexists f =
298 let s = internreq s onlyifexists in
299 sendwithrep sock s f;
302 let createwindowreq wid parent x y w h bw mask =
303 let s = "\001\000\009\000wwwwppppxxyywwhhbwccvvvvmmmmeeee" in
304 w32 s 4 wid;
305 w32 s 8 parent;
306 w16 s 12 x;
307 w16 s 14 y;
308 w16 s 16 w;
309 w16 s 18 h;
310 w16 s 20 bw;
311 w16 s 22 1;
312 w32 s 24 0;
313 w32 s 28 0x800; (* eventmask *)
314 w32 s 32 mask;
318 let getgeometryreq wid =
319 let s = "\014u\002\000dddd" in
320 w32 s 4 wid;
324 let mapreq wid =
325 let s = "\008u\002\000wwww" in
326 w32 s 4 wid;
330 let getkeymapreq first count =
331 let s = "\101u\002\000fcuu" in
332 w8 s 4 first;
333 w8 s 5 count;
337 let changepropreq wid prop typ format props =
338 let s = "\018\000llwwwwppppttttfuuuLLLL" in
339 let s = padcat s props in
340 w16 s 2 (String.length s / 4);
341 w32 s 4 wid;
342 w32 s 8 prop;
343 w32 s 12 typ;
344 w8 s 16 format;
345 let ful = String.length props / (match format with
346 | 8 -> 1
347 | 16 -> 2
348 | 32 -> 4
349 | n -> error "no idea what %d means" n)
351 w32 s 20 ful;
355 let getpropreq delete wid prop typ =
356 let s = "\020\000\006\000wwwwppppttttooooLLLL" in
357 if delete then w8 s 1 1;
358 w32 s 4 wid;
359 w32 s 8 prop;
360 w32 s 12 typ;
361 w32 s 16 0;
362 w32 s 20 2;
366 let openfontreq fid name =
367 let s = "\045ullffffnnuu" in
368 let s = padcat s name in
369 w16 s 2 (String.length s / 4);
370 w32 s 4 fid;
371 w16 s 8 (String.length name);
375 let createglyphcursorreq fid cid cindex =
376 let s = "\094u\008\000ccccffffffffssmmrrggbbRRGGBB" in
377 w32 s 4 cid;
378 w32 s 8 fid;
379 w32 s 12 fid;
380 w16 s 16 cindex;
381 w16 s 18 (cindex+1);
382 w16 s 20 0;
383 w16 s 22 0;
384 w16 s 24 0;
385 w16 s 26 0xffff;
386 w16 s 28 0xffff;
387 w16 s 30 0xffff;
391 let changewindowattributesreq wid mask attrs =
392 let s = "\002ullwwwwmmmm" in
393 let s = padcat s attrs in
394 w16 s 2 (String.length s / 4);
395 w32 s 4 wid;
396 w32 s 8 mask;
400 let configurewindowreq wid mask values =
401 let s = "\012ullwwwwmmuu" in
402 let s = padcat s values in
403 w16 s 2 (String.length s / 4);
404 w32 s 4 wid;
405 w16 s 8 mask;
409 let s32 n =
410 let s = "1234" in
411 w32 s 0 n;
415 let clientmessage format seq wid typ data =
416 let s = "\033fsswwwwtttt" in
417 let s = padcat s data in
418 w8 s 1 format;
419 w16 s 2 seq;
420 w32 s 4 wid;
421 w32 s 8 typ;
425 let sendeventreq propagate destwid mask data =
426 let s = "\025p\011\000wwwwmmmm" in
427 let s = padcat s data in
428 w8 s 1 propagate;
429 w32 s 4 destwid;
430 w32 s 8 mask;
434 let getmodifiermappingreq () =
435 let s = "\119u\001\000" in
439 let getkeysym code mask =
440 let pkpk = state.keymap.(code-state.mink).(0) in
441 if (pkpk >= 0xff80 && pkpk <= 0xffbd)
442 || (pkpk >= 0x11000000 && pkpk <= 0x1100ffff)
443 then (
444 if mask land state.numlmask != 0
445 then
446 let keysym = state.keymap.(code-state.mink).(1) in
447 if keysym = 0 then pkpk else keysym
448 else pkpk
450 else (
451 let shift = (mask land 1) lxor ((mask land state.capslmask) lsr 1) in
452 let index =
453 let l3 = (mask land state.levl3mask) != 0 in
454 let l4 = (mask land state.levl5mask) != 0 in
455 shift +
456 if l3 then (if l4 then 8 else 4) else (if l4 then 6 else 0)
458 let keysym = state.keymap.(code-state.mink).(index) in
459 if index land 1 = 1 && keysym = 0
460 then state.keymap.(code-state.mink).(index - 1)
461 else keysym
465 let readresp sock =
466 let resp = readstr sock 32 in
467 let opcode = r8 resp 0 in
468 match opcode land lnot 0x80 with
469 | 0 -> (* error *)
470 let s = resp in
471 let code = r8 s 1
472 and serial = r16 s 2
473 and resid = r32 resp 4
474 and min = r16 s 8
475 and maj = r8 s 10 in
476 error "code=%d serial=%d resid=%#x min=%d maj=%d\n%S"
477 code serial resid min maj resp;
479 | 1 -> (* response *)
480 let rep = Queue.pop state.fifo in
481 rep resp;
483 | 2 -> (* key press *)
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 "keysym = %x %c mask %#x code %d"
490 keysym (Char.unsafe_chr keysym) mask code;
491 state.t#key keysym mask;
493 | 3 -> (* key release *)
494 if Array.length state.keymap > 0
495 then
496 let code = r8 resp 1 in
497 let mask = r16 resp 28 in
498 let keysym = getkeysym code mask in
499 vlog "release keysym = %x %c mask %#x code %#d"
500 keysym (Char.unsafe_chr keysym) mask code
502 | 4 -> (* buttonpress *)
503 let n = r8 resp 1
504 and x = r16s resp 24
505 and y = r16s resp 26
506 and m = r16 resp 28 in
507 state.t#mouse n true x y m;
508 vlog "press %d" n
510 | 5 -> (* buttonrelease *)
511 let n = r8 resp 1
512 and x = r16s resp 24
513 and y = r16s resp 26
514 and m = r16 resp 28 in
515 state.t#mouse n false x y m;
516 vlog "release %d %d %d" n x y
518 | 6 -> (* motion *)
519 let x = r16s resp 24 in
520 let y = r16s resp 26 in
521 let m = r16 resp 28 in
522 if m land 0x1f00 = 0
523 then state.t#pmotion x y
524 else state.t#motion x y;
525 vlog "move %dx%d => %d" x y m
527 | 7 -> (* enter *)
528 let x = r16s resp 24
529 and y = r16s resp 26 in
530 state.t#enter x y;
531 vlog "enter %d %d" x y
533 | 8 -> (* leave *)
534 state.t#leave
536 | 18 -> vlog "unmap";
538 | 19 -> (* map *)
539 vlog "map";
541 | 12 -> (* exposure *)
542 vlog "exposure";
543 state.t#expose
545 | 15 -> (* visibility *)
546 let vis = r8 resp 8 in
547 if vis != 2 then state.t#expose;
548 vlog "visibility %d" vis;
550 | 34 -> (* mapping *)
551 state.keymap <- [||];
552 let s = getkeymapreq state.mink (state.maxk-state.mink-1) in
553 sendwithrep sock s (updkmap sock);
554 state.capslmask <- 0;
555 state.levl3mask <- 0;
556 state.levl5mask <- 0;
557 state.numlmask <- 0;
558 let s = getmodifiermappingreq () in
559 sendwithrep sock s (updmodmap sock);
562 | 33 -> (* clientmessage *)
563 let atom = r32 resp 8 in
564 if atom = state.protoatom
565 then (
566 let atom = r32 resp 12 in
567 if atom = state.deleatom
568 then state.t#quit;
570 vlog "atom %#x" atom
572 | 21 -> (* reparent *)
573 vlog "reparent"
575 | 22 -> (* configure *)
576 let x = r16s resp 16
577 and y = r16s resp 18
578 and w = r16 resp 20
579 and h = r16 resp 22 in
580 vlog "configure cur [%d %d %d %d] conf [%d %d %d %d]"
581 state.x state.y state.w state.h
582 x y w h
584 glxsync ();
585 if w != state.w || h != state.h
586 then (
587 state.t#reshape w h;
589 state.w <- w;
590 state.h <- h;
591 state.x <- x;
592 state.y <- y;
593 state.t#expose
595 | 28 ->
596 let atom = r32 resp 8 in
597 if atom = state.nwmsatom
598 then
599 let s = getpropreq false state.idbase atom 4 in
600 sendwithrep sock s (fun resp ->
601 let len = r32 resp 4 in
602 let nitems = r32 resp 16 in
603 let wsl =
604 if len = 0
605 then []
606 else
607 let s = readstr sock (len*4) in
608 let rec loop wsl i = if i = nitems then wsl else
609 let atom = r32 s (i*4) in
610 let wsl =
611 if atom = state.maxhatom then (MaxHorz::wsl)
612 else (
613 if atom = state.maxvatom then (MaxVert::wsl)
614 else (
615 if atom = state.fulsatom then (Fullscreen::wsl) else wsl
618 in loop wsl (i+1)
620 loop [] 0
622 state.t#winstate (List.sort compare wsl)
625 | n ->
626 dolog "event %d %S" n resp
629 let readresp sock =
630 let rec loop () =
631 readresp sock;
632 if hasdata sock then loop ();
634 loop ();
637 let sendstr s ?(pos=0) ?(len=String.length s) sock =
638 sendstr1 s pos len sock;
639 if hasdata sock then readresp sock;
642 let reshape w h =
643 if state.fs = NoFs
644 then
645 let s = "wwuuhhuu" in
646 w32 s 0 w;
647 w32 s 4 h;
648 let s = configurewindowreq state.idbase 0x000c s in
649 sendstr s state.sock;
650 else state.fullscreen state.idbase
653 let activatewin () =
654 state.actwin ();
657 let syncsendwithrep sock secstowait s f =
658 let completed = ref false in
659 sendwithrep sock s (fun resp -> f resp; completed := true);
660 let now = Unix.gettimeofday in
661 let deadline = now () +. secstowait in
662 let rec readtillcompletion () =
663 let sf deadline =
664 let timeout = deadline -. now () in
665 if timeout <= 0.0
666 then [], [], []
667 else Unix.select [sock] [] [] timeout
669 let r, _, _ = tempfailureretry sf deadline in
670 match r with
671 | [] -> error "didn't get X response in %f seconds, aborting" secstowait
672 | _ ->
673 readresp sock;
674 if not !completed
675 then readtillcompletion ()
677 readtillcompletion ();
680 let syncsendintern sock secstowait s onlyifexists f =
681 let s = internreq s onlyifexists in
682 syncsendwithrep sock secstowait s f;
685 let setup sock screennum w h =
686 let s = readstr sock 2 in
687 let n = String.length s in
688 if n != 2
689 then error "failed to read X connection setup response n=%d" n;
690 match s.[0] with
691 | '\000' ->
692 let reasonlen = r8 s 1 in
693 let s = readstr sock 6 in
694 let maj = r16 s 0
695 and min = r16 s 2
696 and add = r16 s 4 in
697 let len = add*4 in
698 let data = readstr sock len in
699 let reason = String.sub data 0 reasonlen in
700 error "X connection failed maj=%d min=%d reason=%S"
701 maj min reason
703 | '\002' -> failwith "X connection setup failed: authentication required";
705 | '\001' ->
706 let s = readstr sock 38 in
707 let maj = r16 s 0
708 and min = r16 s 2
709 and add = r16 s 4
710 and idbase = r32 s 10
711 and idmask = r32 s 14
712 and vlen = r16 s 22
713 and screens = r8 s 26
714 and formats = r8 s 27
715 and minkk = r8 s 32
716 and maxkk = r8 s 33 in
717 let data = readstr sock (4*add-32) in
718 let vendor = String.sub data 0 vlen in
719 let pos = ((vlen+3) land lnot 3) + formats*8 in
721 if screennum >= screens
722 then error "invalid screen %d, max %d" screennum (screens-1);
724 let pos =
725 let s = data in
726 let rec findscreen n pos = if n = screennum then pos else
727 let pos =
728 let ndepths = r8 s (pos+39) in
729 let rec skipdepths n pos = if n = ndepths then pos else
730 let pos =
731 let nvisiuals = r16 s (pos+2) in
732 pos + nvisiuals*24 + 8
734 skipdepths (n+1) pos
736 skipdepths n (pos+40)
738 findscreen (n+1) pos
740 findscreen 0 pos
742 let root = r32 data pos in
743 let rootw = r16 data (pos+20)
744 and rooth = r16 data (pos+22) in
745 state.mink <- minkk;
746 state.maxk <- maxkk;
747 state.idbase <- idbase;
748 vlog "vendor = %S, maj=%d min=%d" vendor maj min;
749 vlog "screens = %d formats = %d" screens formats;
750 vlog "minkk = %d maxkk = %d" minkk maxkk;
751 vlog "idbase = %#x idmask = %#x" idbase idmask;
752 vlog "root=%#x %dx%d" root rootw rooth;
754 let mask = 0
755 + 0x00000001 (* KeyPress *)
756 (* + 0x00000002 *) (* KeyRelease *)
757 + 0x00000004 (* ButtonPress *)
758 + 0x00000008 (* ButtonRelease *)
759 + 0x00000010 (* EnterWindow *)
760 + 0x00000020 (* LeaveWindow *)
761 + 0x00000040 (* PointerMotion *)
762 (* + 0x00000080 *) (* PointerMotionHint *)
763 (* + 0x00000100 *) (* Button1Motion *)
764 (* + 0x00000200 *) (* Button2Motion *)
765 (* + 0x00000400 *) (* Button3Motion *)
766 (* + 0x00000800 *) (* Button4Motion *)
767 (* + 0x00001000 *) (* Button5Motion *)
768 + 0x00002000 (* ButtonMotion *)
769 (* + 0x00004000 *) (* KeymapState *)
770 + 0x00008000 (* Exposure *)
771 + 0x00010000 (* VisibilityChange *)
772 + 0x00020000 (* StructureNotify *)
773 (* + 0x00040000 *) (* ResizeRedirect *)
774 (* + 0x00080000 *) (* SubstructureNotify *)
775 (* + 0x00100000 *) (* SubstructureRedirect *)
776 (* + 0x00200000 *) (* FocusChange *)
777 + 0x00400000 (* PropertyChange *)
778 (* + 0x00800000 *) (* ColormapChange *)
779 (* + 0x01000000 *) (* OwnerGrabButton *)
781 let wid = state.idbase in
782 let s = createwindowreq wid root 0 0 w h 0 mask in
783 sendstr s sock;
785 sendintern sock "WM_PROTOCOLS" false (fun resp ->
786 state.protoatom <- r32 resp 8;
787 sendintern sock "WM_DELETE_WINDOW" false (fun resp ->
788 state.deleatom <- r32 resp 8;
789 let s = s32 state.deleatom in
790 let s = changepropreq wid state.protoatom 4 32 s in
791 sendstr s sock;
795 sendintern sock "WM_CLIENT_MACHINE" false (fun resp ->
796 let atom = r32 resp 8 in
797 let empty = "" in
798 let hostname =
799 try Unix.gethostname ()
800 with exn ->
801 dolog "error getting host name: %s" (exntos exn);
802 empty
804 if hostname != empty
805 then
806 let s = changepropreq wid atom state.stringatom 8 hostname in
807 sendstr s sock;
808 sendintern sock "_NET_WM_PID" false (fun resp ->
809 let atom = r32 resp 8 in
810 let pid = Unix.getpid () in
811 let s = s32 pid in
812 let s = changepropreq wid atom (* cardinal *)6 32 s in
813 sendstr s sock;
817 state.actwin <- (fun () ->
818 let s = "\000uuu" in
819 let s = configurewindowreq state.idbase 0x40 s in
820 sendstr s state.sock;
821 let s = mapreq state.idbase in
822 sendstr s state.sock;
825 sendintern sock "_NET_ACTIVE_WINDOW" true (fun resp ->
826 let atom = r32 resp 8 in
827 state.actwin <- (fun () ->
828 let data = String.make 20 '\000' in
829 let cm = clientmessage 32 0 wid atom data in
830 let s = sendeventreq 0 root 0x180000 cm in
831 sendstr s state.sock;
835 syncsendintern sock 2.0 "WM_CLASS" false (fun resp ->
836 let atom = r32 resp 8 in
837 let llpp = "llpp\000llpp\000" in
838 let s = changepropreq wid atom 31 8 llpp in
839 sendstr s sock;
842 let s = mapreq wid in
843 sendstr s sock;
845 let s = getkeymapreq state.mink (state.maxk-state.mink) in
846 sendwithrep sock s (updkmap sock);
848 let s = getmodifiermappingreq () in
849 sendwithrep sock s (updmodmap sock);
851 let s = openfontreq (wid+1) "cursor" in
852 sendstr s sock;
854 Array.iteri (fun i glyphindex ->
855 let s = createglyphcursorreq (wid+1) (wid+2+i) glyphindex in
856 sendstr s sock;
857 ) [|34;48;50;58;128;152|];
859 sendintern sock "UTF8_STRING" true (fun resp ->
860 let atom = r32 resp 8 in
861 if atom != 0
862 then state.stringatom <- atom;
865 let setwmname s =
866 let s = changepropreq state.idbase 39 state.stringatom 8 s in
867 sendstr s state.sock;
869 state.setwmname <- setwmname;
870 sendintern sock "_NET_WM_NAME" true (fun resp ->
871 let atom = r32 resp 8 in
872 if atom != 0
873 then state.setwmname <- (fun s ->
874 setwmname s;
875 let s = changepropreq state.idbase atom state.stringatom 8 s in
876 sendstr s state.sock;
880 state.fullscreen <- (fun wid ->
881 let s = "xxuuyyuuwwuuhhuu" in
882 match state.fs with
883 | NoFs ->
884 w32 s 0 0;
885 w32 s 4 0;
886 w32 s 8 rootw;
887 w32 s 12 rooth;
888 let s = configurewindowreq wid 0x000f s in
889 sendstr s state.sock;
890 state.fs <- Fs (state.x, state.y, state.w, state.h);
892 | Fs (x, y, w, h) ->
893 w32 s 0 x;
894 w32 s 4 y;
895 w32 s 8 w;
896 w32 s 12 h;
897 let s = configurewindowreq wid 0x000f s in
898 sendstr s state.sock;
899 state.fs <- NoFs;
902 sendintern sock "_NET_WM_STATE" true (fun resp ->
903 state.nwmsatom <- r32 resp 8;
904 if state.nwmsatom != 0
905 then (
906 sendintern sock "_NET_WM_STATE_MAXIMIZED_VERT" true (fun resp ->
907 state.maxvatom <- r32 resp 8;
909 sendintern sock "_NET_WM_STATE_MAXIMIZED_HORZ" true (fun resp ->
910 state.maxhatom <- r32 resp 8;
912 sendintern sock "_NET_WM_STATE_FULLSCREEN" true (fun resp ->
913 state.fulsatom <- r32 resp 8;
914 if state.fulsatom != 0
915 then
916 state.fullscreen <-
917 (fun wid ->
918 let data = String.make 20 '\000' in
919 let fs, f =
920 match state.fs with
921 | NoFs -> Fs (-1, -1, -1, -1), 1
922 | Fs _ -> NoFs, 0
924 w32 data 0 f;
925 w32 data 4 state.fulsatom;
927 let cm = clientmessage 32 0 wid state.nwmsatom data in
928 let s = sendeventreq 0 root 0x180000 cm in
929 sendstr s sock;
930 state.fs <- fs;
935 let s = getgeometryreq wid in
936 syncsendwithrep sock 2.0 s (fun resp ->
937 glx wid;
938 let w = r16 resp 16
939 and h = r16 resp 18 in
940 state.w <- w;
941 state.h <- h;
944 | c ->
945 error "unknown conection setup response %d" (Char.code c)
948 let getauth haddr dnum =
949 let haddr =
950 if haddr = "localhost" || String.length haddr = 0
951 then
952 try Unix.gethostname ()
953 with exn ->
954 dolog "failed to resolve `%S': %s" haddr (exntos exn);
955 haddr
956 else haddr
958 let path =
959 try Sys.getenv "XAUTHORITY"
960 with Not_found ->
961 try Filename.concat (Sys.getenv "HOME") ".Xauthority"
962 with Not_found -> ""
964 let readauth ic =
965 let input_string ic len =
966 let s = String.create len in
967 really_input ic s 0 len;
970 let r16 s =
971 let rb pos = Char.code (String.get s pos) in
972 (rb 1) lor ((rb 0) lsl 8)
974 let rec find () =
975 let rs () =
976 let s = input_string ic 2 in
977 let n = r16 s in
978 input_string ic n
980 let family = input_string ic 2 in
981 let addr = rs () in
982 let nums = rs () in
983 let optnum =
984 try Some (int_of_string nums)
985 with exn ->
986 dolog
987 "display number(%S) is not an integer (corrupt %S?): %s"
988 nums path (exntos exn);
989 None
991 let name = rs () in
992 let data = rs () in
994 vlog "family %S addr %S(%S) num %S(%d) name %S data %S"
995 family addr haddr nums dnum name data;
996 match optnum with
997 | Some num when addr = haddr && num = dnum ->
998 name, data
999 | _ -> find ()
1001 let name, data =
1002 try find ()
1003 with
1004 | End_of_file -> "", ""
1005 | exn ->
1006 dolog "exception while reading X authority data (%S): %s"
1007 path (exntos exn);
1008 "", ""
1010 close_in ic;
1011 name, data;
1013 let opt =
1015 if String.length path = 0
1016 then None
1017 else Some (open_in_bin path)
1018 with exn ->
1019 if Sys.file_exists path
1020 then
1021 dolog "failed to open X authority file `%S' : %s"
1022 path (exntos exn);
1023 None
1025 match opt with
1026 | None -> "", ""
1027 | Some ic -> readauth ic
1030 let init t w h osx =
1031 let d =
1032 try Sys.getenv "DISPLAY"
1033 with exn ->
1034 error "could not get DISPLAY evironment variable: %s"
1035 (exntos exn)
1037 let getnum w b e =
1038 if b = e
1039 then error "invalid DISPLAY(%s) %S" w d
1040 else
1041 let s = String.sub d b (e - b) in
1042 try int_of_string s
1043 with exn ->
1044 error "invalid DISPLAY %S can not parse %s(%S): %s"
1045 d w s (exntos exn)
1047 let rec phost pos =
1048 if pos = String.length d
1049 then error "invalid DISPLAY %S no display number specified" d
1050 else (
1051 if d.[pos] = ':'
1052 then
1053 let rec pdispnum pos1 =
1054 if pos1 = String.length d
1055 then getnum "display number" (pos+1) pos1, 0
1056 else
1057 match d.[pos1] with
1058 | '.' ->
1059 let dispnum = getnum "display number" (pos+1) pos1 in
1060 let rec pscreennum pos2 =
1061 if pos2 = String.length d
1062 then getnum "screen number" (pos1+1) pos2
1063 else
1064 match d.[pos2] with
1065 | '0' .. '9' -> pscreennum (pos2+1)
1066 | _ ->
1067 error "invalid DISPLAY %S, cannot parse screen number" d
1069 dispnum, pscreennum (pos1+1)
1070 | '0' .. '9' -> pdispnum (pos1+1)
1071 | _ ->
1072 error "invalid DISPLAY %S, cannot parse display number" d
1074 String.sub d 0 pos, pdispnum (pos+1)
1075 else phost (pos+1)
1078 let host, (dispnum, screennum) = phost 0 in
1079 let aname, adata = getauth host dispnum in
1080 let fd =
1081 let fd, addr =
1082 if osx || String.length host = 0 || host = "unix"
1083 then
1084 let addr =
1085 if osx
1086 then Unix.ADDR_UNIX d
1087 else Unix.ADDR_UNIX ("/tmp/.X11-unix/X" ^ string_of_int dispnum)
1089 let fd = Unix.socket Unix.PF_UNIX Unix.SOCK_STREAM 0 in
1090 fd, addr
1091 else
1092 let h =
1093 try Unix.gethostbyname host
1094 with exn ->
1095 error "cannot resolve %S: %s" host (exntos exn)
1097 let addr = h.Unix.h_addr_list.(0) in
1098 let port = 6000 + dispnum in
1099 let fd = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
1100 fd, (Unix.ADDR_INET (addr, port))
1102 try Unix.connect fd addr; fd
1103 with exn ->
1104 error "failed to connect to X: %s" (exntos exn)
1106 cloexec fd;
1107 let s = "luMMmmnndduu" in
1108 let s = padcat s aname in
1109 let s = padcat s adata in
1110 w16 s 2 11;
1111 w16 s 4 0;
1112 w16 s 6 (String.length aname);
1113 w16 s 8 (String.length adata);
1114 sendstr1 s 0 (String.length s) fd;
1115 state.sock <- fd;
1116 setup fd screennum w h;
1117 state.t <- t;
1118 fd, state.w, state.h;
1121 let settitle s =
1122 state.setwmname s;
1125 let setcursor cursor =
1126 if cursor != state.curcurs
1127 then
1128 let n =
1129 match cursor with
1130 | CURSOR_INHERIT -> -1
1131 | CURSOR_INFO -> 3
1132 | CURSOR_CYCLE -> 2
1133 | CURSOR_CROSSHAIR -> 0
1134 | CURSOR_TEXT -> 5
1136 let s = s32 (if n = -1 then 0 else state.idbase+2+n) in
1137 let s = changewindowattributesreq state.idbase (*cursor*)0x4000 s in
1138 sendstr s state.sock;
1139 state.curcurs <- cursor;
1142 let fullscreen () =
1143 state.fullscreen state.idbase;
1146 let metamask = 0x40;;
1147 let altmask = 8;;
1148 let shiftmask = 1;;
1149 let ctrlmask = 4;;
1151 let withalt mask = mask land altmask != 0;;
1152 let withctrl mask = mask land ctrlmask != 0;;
1153 let withshift mask = mask land shiftmask != 0;;
1154 let withmeta mask = mask land metamask != 0;;
1155 let withnone mask = mask land (altmask + ctrlmask + shiftmask + metamask) = 0;;
1157 let xlatt, xlatf =
1158 let t = Hashtbl.create 20
1159 and f = Hashtbl.create 20 in
1160 let add n nl k =
1161 List.iter (fun s -> Hashtbl.add t s k) (n::nl);
1162 Hashtbl.add f k n
1164 let addc c =
1165 let s = String.create 1 in
1166 s.[0] <- c;
1167 add s [] (Char.code c)
1169 let addcr a b =
1170 let an = Char.code a and bn = Char.code b in
1171 for i = an to bn do addc (Char.chr i) done;
1173 addcr '0' '9';
1174 addcr 'a' 'z';
1175 addcr 'A' 'Z';
1176 String.iter addc "`~!@#$%^&*()-_=+\\|[{]};:,./<>?";
1177 for i = 0 to 29 do add ("f" ^ string_of_int (i+1)) [] (0xffbe + i) done;
1178 add "space" [] 0x20;
1179 add "ret" ["return"; "enter"] 0xff0d;
1180 add "tab" [] 0xff09;
1181 add "left" [] 0xff51;
1182 add "right" [] 0xff53;
1183 add "home" [] 0xff50;
1184 add "end" [] 0xff57;
1185 add "ins" ["insert"] 0xff63;
1186 add "del" ["delete"] 0xffff;
1187 add "esc" ["escape"] 0xff1b;
1188 add "pgup" ["pageup"] 0xff55;
1189 add "pgdown" ["pagedown"] 0xff56;
1190 add "backspace" [] 0xff08;
1191 add "up" [] 0xff52;
1192 add "down" [] 0xff54;
1193 t, f;
1196 let keyname k =
1197 try Hashtbl.find xlatf k
1198 with Not_found -> Printf.sprintf "%#x" k;
1201 let namekey name =
1202 try Hashtbl.find xlatt name
1203 with Not_found ->
1204 if String.length name = 1
1205 then Char.code name.[0]
1206 else int_of_string name;