Whoops
[llpp.git] / wsi.ml
blob65bb3ff330079bec3c9a366e6f807924f9c03d1b
1 type cursor =
2 | CURSOR_INHERIT
3 | CURSOR_INFO
4 | CURSOR_CYCLE
5 | CURSOR_CROSSHAIR
6 | CURSOR_TEXT
7 ;;
9 let tempfailureretry f a =
10 let rec g () =
11 try f a with Unix.Unix_error (Unix.EINTR, _, _) -> g ()
12 in g ()
15 external cloexec : Unix.file_descr -> unit = "ml_cloexec";;
16 external glx : int -> unit = "ml_glx";;
17 external glxsync : unit -> unit = "ml_glxsync";;
18 external swapb : unit -> unit = "ml_swapb";;
19 external hasdata : Unix.file_descr -> bool = "ml_hasdata";;
20 external toutf8 : int -> string = "ml_keysymtoutf8";;
22 let dolog fmt = Format.kprintf prerr_endline fmt;;
23 let vlog fmt = Format.kprintf ignore fmt;;
25 let onot = object
26 method display = ()
27 method expose = ()
28 method reshape _ _ = ()
29 method mouse _ _ _ _ _ = ()
30 method motion _ _ = ()
31 method pmotion _ _ = ()
32 method key _ _ = ()
33 method enter _ _ = ()
34 method leave = ()
35 method quit = exit 0
36 end;;
38 class type t = object
39 method display : unit
40 method expose : unit
41 method reshape : int -> int -> unit
42 method mouse : int -> bool -> int -> int -> int -> unit
43 method motion : int -> int -> unit
44 method pmotion : int -> int -> unit
45 method key : int -> int -> unit
46 method enter : int -> int -> unit
47 method leave : unit
48 method quit : unit
49 end;;
51 type state =
52 { mutable mink : int
53 ; mutable maxk : int
54 ; mutable keymap : int array array
55 ; fifo : (string -> unit) Queue.t
56 ; mutable seq : int
57 ; mutable protoatom : int
58 ; mutable deleatom : int
59 ; mutable idbase : int
60 ; mutable fullscreen : (int -> unit)
61 ; mutable setwmname : (string -> unit)
62 ; mutable actwin : (unit -> unit)
63 ; mutable stringatom : int
64 ; mutable t : t
65 ; mutable sock : Unix.file_descr
66 ; mutable x : int
67 ; mutable y : int
68 ; mutable w : int
69 ; mutable h : int
70 ; mutable fs : fs
71 ; mutable curcurs : cursor
72 ; mutable capslmask : int
73 ; mutable numlmask : int
74 ; mutable levl3mask : int
75 ; mutable levl5mask : int
77 and fs =
78 | NoFs
79 | Fs of (int * int * int * int)
82 let state =
83 { mink = max_int
84 ; maxk = min_int
85 ; keymap = [||]
86 ; fifo = Queue.create ()
87 ; seq = 0
88 ; protoatom = -1
89 ; deleatom = -1
90 ; idbase = -1
91 ; fullscreen = (fun _ -> ())
92 ; setwmname = (fun _ -> ())
93 ; actwin = (fun _ -> ())
94 ; sock = Unix.stdin
95 ; t = onot
96 ; x = -1
97 ; y = -1
98 ; w = -1
99 ; h = -1
100 ; fs = NoFs
101 ; stringatom = 31
102 ; curcurs = CURSOR_INHERIT
103 ; capslmask = 0
104 ; numlmask = 0
105 ; levl3mask = 0
106 ; levl5mask = 0
110 let w8 s pos i = String.set s pos (Char.chr (i land 0xff));;
112 let w16 s pos i =
113 w8 s pos i;
114 w8 s (pos+1) (i lsr 8);
117 let w32 s pos i =
118 w16 s pos i;
119 w16 s (pos+2) (i lsr 16);
122 let r16 s pos =
123 let rb pos1 = Char.code (String.get s (pos + pos1)) in
124 (rb 0) lor ((rb 1) lsl 8)
127 let r16s s pos =
128 let i = r16 s pos in
129 i - ((i land 0x8000) lsl 1);
132 let r8 s pos = Char.code (String.get s pos);;
134 let r32 s pos =
135 let rb pos1 = Char.code (String.get s (pos + pos1)) in
136 let l = (rb 0) lor ((rb 1) lsl 8)
137 and u = (rb 2) lor ((rb 3) lsl 8) in
138 (u lsl 16) lor l
141 let exntos = function
142 | Unix.Unix_error (e, s, a) -> Printf.sprintf "%s(%s):%s(%d)"
143 s a (Unix.error_message e) (Obj.magic e)
144 | exn -> Printexc.to_string exn;
147 let error fmt = Printf.kprintf failwith fmt;;
149 let readstr sock n =
150 let s = String.create n in
151 let rec loop pos n =
152 let m = tempfailureretry (Unix.read sock s pos) n in
153 if m = 0
154 then state.t#quit;
155 if n != m
156 then (
157 ignore (tempfailureretry (Unix.select [sock] [] []) 0.01);
158 loop (pos + m) (n - m)
161 loop 0 n;
165 let sendstr1 s pos len sock =
166 vlog "%d => %S" state.seq s;
167 state.seq <- state.seq + 1;
168 let n = tempfailureretry (Unix.send sock s pos len) [] in
169 if n != len
170 then error "send %d returned %d" len n;
173 let updkmap sock resp =
174 let syms = r8 resp 1 in
175 let len = r32 resp 4 in
176 let data =
177 if len > 0
178 then readstr sock (4*len)
179 else ""
181 let m = len / syms in
182 state.keymap <- Array.make_matrix
183 (state.maxk - state.mink) syms 0xffffff;
184 let rec loop i = if i = m then () else
185 let k = i*4*syms in
186 let rec loop2 k l = if l = syms then () else
187 let v = r32 data k in
188 state.keymap.(i).(l) <- v;
189 loop2 (k+4) (l+1)
191 loop2 k 0;
192 loop (i+1);
194 loop 0;
197 let updmodmap sock resp =
198 let n = r8 resp 1 in
199 let len = r16 resp 4 in
200 let data =
201 if len > 0
202 then readstr sock (len*4)
203 else ""
205 let modmap = Array.make_matrix 8 n 0xffffff in
206 let rec loop l = if l = 8 then () else
207 let p = l*n in
208 let rec loop1 m = if m = n then () else
209 let p = p+m in
210 let code = r8 data p in
211 modmap.(l).(m) <- code;
212 if l = 1
213 then (
214 let ki = code - state.mink in
215 if ki >= 0
216 then
217 let a = state.keymap.(ki) in
218 let rec capsloop i = if i = Array.length a || i > 3 then () else
219 let s = a.(i) in
220 if s = 0xffe5
221 then state.capslmask <- 2
222 else capsloop (i+1)
224 capsloop 0;
226 else (
227 if l > 3
228 then (
229 let ki = code - state.mink in
230 if ki >= 0
231 then
232 let a = state.keymap.(ki) in
233 let rec lloop i = if i = Array.length a || i > 3 then () else
234 let s = a.(i) in
235 match s with
236 | 0xfe03 -> state.levl3mask <- 1 lsl l
237 | 0xfe11 -> state.levl5mask <- 1 lsl l
238 | 0xff7f -> state.numlmask <- 1 lsl l
239 | _ -> lloop (i+1)
241 lloop 0;
244 loop1 (m+1)
246 loop1 0;
247 loop (l+1)
249 loop 0;
252 let sendwithrep sock s f =
253 Queue.push f state.fifo;
254 sendstr1 s 0 (String.length s) sock;
257 let padcatl ss =
258 let b = Buffer.create 16 in
259 List.iter (Buffer.add_string b) ss;
260 let bl = Buffer.length b in
261 let pl = bl land 3 in
262 if pl != 0
263 then (
264 let pad = "123" in
265 Buffer.add_substring b pad 0 (4 - pl);
267 Buffer.contents b;
270 let padcat s1 s2 = padcatl [s1; s2];;
272 let internreq name onlyifexists =
273 let s = "\016\000\000\000\000\000\000\000" in
274 let s = padcat s name in
275 if onlyifexists then w8 s 1 1;
276 w16 s 2 (String.length s / 4);
277 w16 s 4 (String.length name);
281 let sendintern sock s onlyifexists f =
282 let s = internreq s onlyifexists in
283 sendwithrep sock s f;
286 let createwindowreq wid parent x y w h bw mask =
287 let s = "\001\000\009\000wwwwppppxxyywwhhbwccvvvvmmmmeeee" in
288 w32 s 4 wid;
289 w32 s 8 parent;
290 w16 s 12 x;
291 w16 s 14 y;
292 w16 s 16 w;
293 w16 s 18 h;
294 w16 s 20 bw;
295 w16 s 22 1;
296 w32 s 24 0;
297 w32 s 28 0x800; (* eventmask *)
298 w32 s 32 mask;
302 let getgeometryreq wid =
303 let s = "\014u\002\000dddd" in
304 w32 s 4 wid;
308 let mapreq wid =
309 let s = "\008u\002\000wwww" in
310 w32 s 4 wid;
314 let getkeymapreq first count =
315 let s = "\101u\002\000fcuu" in
316 w8 s 4 first;
317 w8 s 5 count;
321 let changepropreq wid prop typ format props =
322 let s = "\018\000llwwwwppppttttfuuuLLLL" in
323 let s = padcat s props in
324 w16 s 2 (String.length s / 4);
325 w32 s 4 wid;
326 w32 s 8 prop;
327 w32 s 12 typ;
328 w8 s 16 format;
329 let ful = String.length props / (match format with
330 | 8 -> 1
331 | 16 -> 2
332 | 32 -> 4
333 | n -> error "no idea what %d means" n)
335 w32 s 20 ful;
339 let openfontreq fid name =
340 let s = "\045ullffffnnuu" in
341 let s = padcat s name in
342 w16 s 2 (String.length s / 4);
343 w32 s 4 fid;
344 w16 s 8 (String.length name);
348 let createglyphcursorreq fid cid cindex =
349 let s = "\094u\008\000ccccffffffffssmmrrggbbRRGGBB" in
350 w32 s 4 cid;
351 w32 s 8 fid;
352 w32 s 12 fid;
353 w16 s 16 cindex;
354 w16 s 18 (cindex+1);
355 w16 s 20 0;
356 w16 s 22 0;
357 w16 s 24 0;
358 w16 s 26 0xffff;
359 w16 s 28 0xffff;
360 w16 s 30 0xffff;
364 let changewindowattributesreq wid mask attrs =
365 let s = "\002ullwwwwmmmm" in
366 let s = padcat s attrs in
367 w16 s 2 (String.length s / 4);
368 w32 s 4 wid;
369 w32 s 8 mask;
373 let configurewindowreq wid mask values =
374 let s = "\012ullwwwwmmuu" in
375 let s = padcat s values in
376 w16 s 2 (String.length s / 4);
377 w32 s 4 wid;
378 w16 s 8 mask;
382 let s32 n =
383 let s = "1234" in
384 w32 s 0 n;
388 let clientmessage format seq wid typ data =
389 let s = "\033fsswwwwtttt" in
390 let s = padcat s data in
391 w8 s 1 format;
392 w16 s 2 seq;
393 w32 s 4 wid;
394 w32 s 8 typ;
398 let sendeventreq propagate destwid mask data =
399 let s = "\025p\011\000wwwwmmmm" in
400 let s = padcat s data in
401 w8 s 1 propagate;
402 w32 s 4 destwid;
403 w32 s 8 mask;
407 let getmodifiermappingreq () =
408 let s = "\119u\001\000" in
412 let getkeysym code mask =
413 let pkpk = state.keymap.(code-state.mink).(0) in
414 if (pkpk >= 0xff80 && pkpk <= 0xffbd)
415 || (pkpk >= 0x11000000 && pkpk <= 0x1100ffff)
416 then (
417 if mask land state.numlmask != 0
418 then
419 let keysym = state.keymap.(code-state.mink).(1) in
420 if keysym = 0 then pkpk else keysym
421 else pkpk
423 else (
424 let shift = (mask land 1) lxor ((mask land state.capslmask) lsr 1) in
425 let index =
426 let l3 = (mask land state.levl3mask) != 0 in
427 let l4 = (mask land state.levl5mask) != 0 in
428 shift +
429 if l3 then (if l4 then 8 else 4) else (if l4 then 6 else 0)
431 let keysym = state.keymap.(code-state.mink).(index) in
432 if index land 1 = 1 && keysym = 0
433 then state.keymap.(code-state.mink).(index - 1)
434 else keysym
438 let readresp sock =
439 let resp = readstr sock 32 in
440 let opcode = r8 resp 0 in
441 match opcode land lnot 0x80 with
442 | 0 -> (* error *)
443 let s = resp in
444 let code = r8 s 1
445 and serial = r16 s 2
446 and resid = r32 resp 4
447 and min = r16 s 8
448 and maj = r8 s 10 in
449 error "code=%d serial=%d resid=%#x min=%d maj=%d\n%S"
450 code serial resid min maj resp;
452 | 1 -> (* response *)
453 let rep = Queue.pop state.fifo in
454 rep resp;
456 | 2 -> (* key press *)
457 if Array.length state.keymap > 0
458 then
459 let code = r8 resp 1 in
460 let mask = r16 resp 28 in
461 let keysym = getkeysym code mask in
462 vlog "keysym = %x %c mask %#x code %d"
463 keysym (Char.unsafe_chr keysym) mask code;
464 state.t#key keysym mask;
466 | 3 -> (* key release *)
467 if Array.length state.keymap > 0
468 then
469 let code = r8 resp 1 in
470 let mask = r16 resp 28 in
471 let keysym = getkeysym code mask in
472 vlog "release keysym = %x %c mask %#x code %#d"
473 keysym (Char.unsafe_chr keysym) mask code
475 | 4 -> (* buttonpress *)
476 let n = r8 resp 1
477 and x = r16s resp 24
478 and y = r16s resp 26
479 and m = r16 resp 28 in
480 state.t#mouse n true x y m;
481 vlog "press %d" n
483 | 5 -> (* buttonrelease *)
484 let n = r8 resp 1
485 and x = r16s resp 24
486 and y = r16s resp 26
487 and m = r16 resp 28 in
488 state.t#mouse n false x y m;
489 vlog "release %d %d %d" n x y
491 | 6 -> (* motion *)
492 let x = r16s resp 24 in
493 let y = r16s resp 26 in
494 let m = r16 resp 28 in
495 if m land 0x1f00 = 0
496 then state.t#pmotion x y
497 else state.t#motion x y;
498 vlog "move %dx%d => %d" x y m
500 | 7 -> (* enter *)
501 let x = r16s resp 24
502 and y = r16s resp 26 in
503 state.t#enter x y;
504 vlog "enter %d %d" x y
506 | 8 -> (* leave *)
507 state.t#leave
509 | 18 -> vlog "unmap";
511 | 19 -> (* map *)
512 vlog "map";
514 | 12 -> (* exposure *)
515 vlog "exposure";
516 state.t#expose
518 | 15 -> (* visibility *)
519 let vis = r8 resp 8 in
520 if vis != 2 then state.t#expose;
521 vlog "visibility %d" vis;
523 | 34 -> (* mapping *)
524 state.keymap <- [||];
525 let s = getkeymapreq state.mink (state.maxk-state.mink-1) in
526 sendwithrep sock s (updkmap sock);
527 state.capslmask <- 0;
528 state.levl3mask <- 0;
529 state.levl5mask <- 0;
530 state.numlmask <- 0;
531 let s = getmodifiermappingreq () in
532 sendwithrep sock s (updmodmap sock);
535 | 33 -> (* clientmessage *)
536 let atom = r32 resp 8 in
537 if atom = state.protoatom
538 then (
539 let atom = r32 resp 12 in
540 if atom = state.deleatom
541 then state.t#quit;
543 vlog "atom %#x" atom
545 | 21 -> (* reparent *)
546 vlog "reparent"
548 | 22 -> (* configure *)
549 let x = r16s resp 16
550 and y = r16s resp 18
551 and w = r16 resp 20
552 and h = r16 resp 22 in
553 vlog "configure cur [%d %d %d %d] conf [%d %d %d %d]"
554 state.x state.y state.w state.h
555 x y w h
557 glxsync ();
558 if w != state.w || h != state.h
559 then (
560 state.t#reshape w h;
562 state.w <- w;
563 state.h <- h;
564 state.x <- x;
565 state.y <- y;
566 state.t#expose
568 | n ->
569 dolog "event %d %S" n resp
572 let readresp sock =
573 let rec loop () =
574 readresp sock;
575 if hasdata sock then loop ();
577 loop ();
580 let sendstr s ?(pos=0) ?(len=String.length s) sock =
581 sendstr1 s pos len sock;
582 if hasdata sock then readresp sock;
585 let reshape w h =
586 if state.fs = NoFs
587 then
588 let s = "wwuuhhuu" in
589 w32 s 0 w;
590 w32 s 4 h;
591 let s = configurewindowreq state.idbase 0x000c s in
592 sendstr s state.sock;
593 else state.fullscreen state.idbase
596 let activatewin () =
597 state.actwin ();
600 let syncsendwithrep sock secstowait s f =
601 let completed = ref false in
602 sendwithrep sock s (fun resp -> f resp; completed := true);
603 let now = Unix.gettimeofday in
604 let deadline = now () +. secstowait in
605 let rec readtillcompletion () =
606 let sf deadline =
607 let timeout = deadline -. now () in
608 if timeout <= 0.0
609 then [], [], []
610 else Unix.select [sock] [] [] timeout
612 let r, _, _ = tempfailureretry sf deadline in
613 match r with
614 | [] -> error "didn't get X response in %f seconds, aborting" secstowait
615 | _ ->
616 readresp sock;
617 if not !completed
618 then readtillcompletion ()
620 readtillcompletion ();
623 let syncsendintern sock secstowait s onlyifexists f =
624 let s = internreq s onlyifexists in
625 syncsendwithrep sock secstowait s f;
628 let setup sock screennum w h =
629 let s = readstr sock 2 in
630 let n = String.length s in
631 if n != 2
632 then error "failed to read X connection setup response n=%d" n;
633 match s.[0] with
634 | '\000' ->
635 let reasonlen = r8 s 1 in
636 let s = readstr sock 6 in
637 let maj = r16 s 0
638 and min = r16 s 2
639 and add = r16 s 4 in
640 let len = add*4 in
641 let data = readstr sock len in
642 let reason = String.sub data 0 reasonlen in
643 error "X connection failed maj=%d min=%d reason=%S"
644 maj min reason
646 | '\002' -> failwith "X connection setup failed: authentication required";
648 | '\001' ->
649 let s = readstr sock 38 in
650 let maj = r16 s 0
651 and min = r16 s 2
652 and add = r16 s 4
653 and idbase = r32 s 10
654 and idmask = r32 s 14
655 and vlen = r16 s 22
656 and screens = r8 s 26
657 and formats = r8 s 27
658 and minkk = r8 s 32
659 and maxkk = r8 s 33 in
660 let data = readstr sock (4*add-32) in
661 let vendor = String.sub data 0 vlen in
662 let pos = ((vlen+3) land lnot 3) + formats*8 in
664 if screennum >= screens
665 then error "invalid screen %d, max %d" screennum (screens-1);
667 let pos =
668 let s = data in
669 let rec findscreen n pos = if n = screennum then pos else
670 let pos =
671 let ndepths = r8 s (pos+39) in
672 let rec skipdepths n pos = if n = ndepths then pos else
673 let pos =
674 let nvisiuals = r16 s (pos+2) in
675 pos + nvisiuals*24 + 8
677 skipdepths (n+1) pos
679 skipdepths n (pos+40)
681 findscreen (n+1) pos
683 findscreen 0 pos
685 let root = r32 data pos in
686 let rootw = r16 data (pos+20)
687 and rooth = r16 data (pos+22) in
688 state.mink <- minkk;
689 state.maxk <- maxkk;
690 state.idbase <- idbase;
691 vlog "vendor = %S, maj=%d min=%d" vendor maj min;
692 vlog "screens = %d formats = %d" screens formats;
693 vlog "minkk = %d maxkk = %d" minkk maxkk;
694 vlog "idbase = %#x idmask = %#x" idbase idmask;
695 vlog "root=%#x %dx%d" root rootw rooth;
697 let mask = 0
698 + 0x00000001 (* KeyPress *)
699 (* + 0x00000002 *) (* KeyRelease *)
700 + 0x00000004 (* ButtonPress *)
701 + 0x00000008 (* ButtonRelease *)
702 + 0x00000010 (* EnterWindow *)
703 + 0x00000020 (* LeaveWindow *)
704 + 0x00000040 (* PointerMotion *)
705 (* + 0x00000080 *) (* PointerMotionHint *)
706 (* + 0x00000100 *) (* Button1Motion *)
707 (* + 0x00000200 *) (* Button2Motion *)
708 (* + 0x00000400 *) (* Button3Motion *)
709 (* + 0x00000800 *) (* Button4Motion *)
710 (* + 0x00001000 *) (* Button5Motion *)
711 + 0x00002000 (* ButtonMotion *)
712 (* + 0x00004000 *) (* KeymapState *)
713 + 0x00008000 (* Exposure *)
714 + 0x00010000 (* VisibilityChange *)
715 + 0x00020000 (* StructureNotify *)
716 (* + 0x00040000 *) (* ResizeRedirect *)
717 (* + 0x00080000 *) (* SubstructureNotify *)
718 (* + 0x00100000 *) (* SubstructureRedirect *)
719 (* + 0x00200000 *) (* FocusChange *)
720 (* + 0x00400000 *) (* PropertyChange *)
721 (* + 0x00800000 *) (* ColormapChange *)
722 (* + 0x01000000 *) (* OwnerGrabButton *)
724 let wid = state.idbase in
725 let s = createwindowreq wid root 0 0 w h 0 mask in
726 sendstr s sock;
728 sendintern sock "WM_PROTOCOLS" false (fun resp ->
729 state.protoatom <- r32 resp 8;
730 sendintern sock "WM_DELETE_WINDOW" false (fun resp ->
731 state.deleatom <- r32 resp 8;
732 let s = s32 state.deleatom in
733 let s = changepropreq wid state.protoatom 4 32 s in
734 sendstr s sock;
738 sendintern sock "WM_CLIENT_MACHINE" false (fun resp ->
739 let atom = r32 resp 8 in
740 let empty = "" in
741 let hostname =
742 try Unix.gethostname ()
743 with exn ->
744 dolog "error getting host name: %s" (exntos exn);
745 empty
747 if hostname != empty
748 then
749 let s = changepropreq wid atom state.stringatom 8 hostname in
750 sendstr s sock;
751 sendintern sock "_NET_WM_PID" false (fun resp ->
752 let atom = r32 resp 8 in
753 let pid = Unix.getpid () in
754 let s = s32 pid in
755 let s = changepropreq wid atom (* cardinal *)6 32 s in
756 sendstr s sock;
760 state.actwin <- (fun () ->
761 let s = "\000uuu" in
762 let s = configurewindowreq state.idbase 0x40 s in
763 sendstr s state.sock;
764 let s = mapreq state.idbase in
765 sendstr s state.sock;
768 sendintern sock "_NET_ACTIVE_WINDOW" true (fun resp ->
769 let atom = r32 resp 8 in
770 state.actwin <- (fun () ->
771 let data = String.make 20 '\000' in
772 let cm = clientmessage 32 0 wid atom data in
773 let s = sendeventreq 0 root 0x180000 cm in
774 sendstr s state.sock;
778 syncsendintern sock 2.0 "WM_CLASS" false (fun resp ->
779 let atom = r32 resp 8 in
780 let llpp = "llpp\000llpp\000" in
781 let s = changepropreq wid atom 31 8 llpp in
782 sendstr s sock;
785 let s = mapreq wid in
786 sendstr s sock;
788 let s = getkeymapreq state.mink (state.maxk-state.mink) in
789 sendwithrep sock s (updkmap sock);
791 let s = getmodifiermappingreq () in
792 sendwithrep sock s (updmodmap sock);
794 let s = openfontreq (wid+1) "cursor" in
795 sendstr s sock;
797 Array.iteri (fun i glyphindex ->
798 let s = createglyphcursorreq (wid+1) (wid+2+i) glyphindex in
799 sendstr s sock;
800 ) [|34;48;50;58;128;152|];
802 sendintern sock "UTF8_STRING" true (fun resp ->
803 let atom = r32 resp 8 in
804 if atom != 0
805 then state.stringatom <- atom;
808 let setwmname s =
809 let s = changepropreq state.idbase 39 state.stringatom 8 s in
810 sendstr s state.sock;
812 state.setwmname <- setwmname;
813 sendintern sock "_NET_WM_NAME" true (fun resp ->
814 let atom = r32 resp 8 in
815 if atom != 0
816 then state.setwmname <- (fun s ->
817 setwmname s;
818 let s = changepropreq state.idbase atom state.stringatom 8 s in
819 sendstr s state.sock;
823 state.fullscreen <- (fun wid ->
824 let s = "xxuuyyuuwwuuhhuu" in
825 match state.fs with
826 | NoFs ->
827 w32 s 0 0;
828 w32 s 4 0;
829 w32 s 8 rootw;
830 w32 s 12 rooth;
831 let s = configurewindowreq wid 0x000f s in
832 sendstr s state.sock;
833 state.fs <- Fs (state.x, state.y, state.w, state.h);
835 | Fs (x, y, w, h) ->
836 w32 s 0 x;
837 w32 s 4 y;
838 w32 s 8 w;
839 w32 s 12 h;
840 let s = configurewindowreq wid 0x000f s in
841 sendstr s state.sock;
842 state.fs <- NoFs;
845 sendintern sock "_NET_WM_STATE" true (fun resp ->
846 let nwmsatom = r32 resp 8 in
847 if nwmsatom != 0
848 then
849 sendintern sock "_NET_WM_STATE_FULLSCREEN" true (fun resp ->
850 let fsatom = r32 resp 8 in
851 if fsatom != 0
852 then
853 state.fullscreen <-
854 (fun wid ->
855 let data = String.make 20 '\000' in
856 let fs, f =
857 match state.fs with
858 | NoFs -> Fs (-1, -1, -1, -1), 1
859 | Fs _ -> NoFs, 0
861 w32 data 0 f;
862 w32 data 4 fsatom;
864 let cm = clientmessage 32 0 wid nwmsatom data in
865 let s = sendeventreq 0 root 0x180000 cm in
866 sendstr s sock;
867 state.fs <- fs;
871 let s = getgeometryreq wid in
872 syncsendwithrep sock 2.0 s (fun resp ->
873 glx wid;
874 let w = r16 resp 16
875 and h = r16 resp 18 in
876 state.w <- w;
877 state.h <- h;
880 | c ->
881 error "unknown conection setup response %d" (Char.code c)
884 let getauth haddr dnum =
885 let haddr =
886 if haddr = "localhost" || String.length haddr = 0
887 then
888 try Unix.gethostname ()
889 with exn ->
890 dolog "failed to resolve `%S': %s" haddr (exntos exn);
891 haddr
892 else haddr
894 let path =
895 try Sys.getenv "XAUTHORITY"
896 with Not_found ->
897 try Filename.concat (Sys.getenv "HOME") ".Xauthority"
898 with Not_found -> ""
900 let readauth ic =
901 let input_string ic len =
902 let s = String.create len in
903 really_input ic s 0 len;
906 let r16 s =
907 let rb pos = Char.code (String.get s pos) in
908 (rb 1) lor ((rb 0) lsl 8)
910 let rec find () =
911 let rs () =
912 let s = input_string ic 2 in
913 let n = r16 s in
914 input_string ic n
916 let family = input_string ic 2 in
917 let addr = rs () in
918 let nums = rs () in
919 let optnum =
920 try Some (int_of_string nums)
921 with exn ->
922 dolog
923 "display number(%S) is not an integer (corrupt %S?): %s"
924 nums path (exntos exn);
925 None
927 let name = rs () in
928 let data = rs () in
930 vlog "family %S addr %S(%S) num %S(%d) name %S data %S"
931 family addr haddr nums dnum name data;
932 match optnum with
933 | Some num when addr = haddr && num = dnum ->
934 name, data
935 | _ -> find ()
937 let name, data =
938 try find ()
939 with
940 | End_of_file -> "", ""
941 | exn ->
942 dolog "exception while reading X authority data (%S): %s"
943 path (exntos exn);
944 "", ""
946 close_in ic;
947 name, data;
949 let opt =
951 if String.length path = 0
952 then None
953 else Some (open_in_bin path)
954 with exn ->
955 if Sys.file_exists path
956 then
957 dolog "failed to open X authority file `%S' : %s"
958 path (exntos exn);
959 None
961 match opt with
962 | None -> "", ""
963 | Some ic -> readauth ic
966 let init t w h osx =
967 let d =
968 try Sys.getenv "DISPLAY"
969 with exn ->
970 error "could not get DISPLAY evironment variable: %s"
971 (exntos exn)
973 let getnum w b e =
974 if b = e
975 then error "invalid DISPLAY(%s) %S" w d
976 else
977 let s = String.sub d b (e - b) in
978 try int_of_string s
979 with exn ->
980 error "invalid DISPLAY %S can not parse %s(%S): %s"
981 d w s (exntos exn)
983 let rec phost pos =
984 if pos = String.length d
985 then error "invalid DISPLAY %S no display number specified" d
986 else (
987 if d.[pos] = ':'
988 then
989 let rec pdispnum pos1 =
990 if pos1 = String.length d
991 then getnum "display number" (pos+1) pos1, 0
992 else
993 match d.[pos1] with
994 | '.' ->
995 let dispnum = getnum "display number" (pos+1) pos1 in
996 let rec pscreennum pos2 =
997 if pos2 = String.length d
998 then getnum "screen number" (pos1+1) pos2
999 else
1000 match d.[pos2] with
1001 | '0' .. '9' -> pscreennum (pos2+1)
1002 | _ ->
1003 error "invalid DISPLAY %S, cannot parse screen number" d
1005 dispnum, pscreennum (pos1+1)
1006 | '0' .. '9' -> pdispnum (pos1+1)
1007 | _ ->
1008 error "invalid DISPLAY %S, cannot parse display number" d
1010 String.sub d 0 pos, pdispnum (pos+1)
1011 else phost (pos+1)
1014 let host, (dispnum, screennum) = phost 0 in
1015 let aname, adata = getauth host dispnum in
1016 let fd =
1017 let fd, addr =
1018 if osx || String.length host = 0 || host = "unix"
1019 then
1020 let addr =
1021 if osx
1022 then Unix.ADDR_UNIX d
1023 else Unix.ADDR_UNIX ("/tmp/.X11-unix/X" ^ string_of_int dispnum)
1025 let fd = Unix.socket Unix.PF_UNIX Unix.SOCK_STREAM 0 in
1026 fd, addr
1027 else
1028 let h =
1029 try Unix.gethostbyname host
1030 with exn ->
1031 error "cannot resolve %S: %s" host (exntos exn)
1033 let addr = h.Unix.h_addr_list.(0) in
1034 let port = 6000 + dispnum in
1035 let fd = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
1036 fd, (Unix.ADDR_INET (addr, port))
1038 try Unix.connect fd addr; fd
1039 with exn ->
1040 error "failed to connect to X: %s" (exntos exn)
1042 cloexec fd;
1043 let s = "luMMmmnndduu" in
1044 let s = padcat s aname in
1045 let s = padcat s adata in
1046 w16 s 2 11;
1047 w16 s 4 0;
1048 w16 s 6 (String.length aname);
1049 w16 s 8 (String.length adata);
1050 sendstr1 s 0 (String.length s) fd;
1051 state.sock <- fd;
1052 setup fd screennum w h;
1053 state.t <- t;
1054 fd, state.w, state.h;
1057 let settitle s =
1058 state.setwmname s;
1061 let setcursor cursor =
1062 if cursor != state.curcurs
1063 then
1064 let n =
1065 match cursor with
1066 | CURSOR_INHERIT -> -1
1067 | CURSOR_INFO -> 3
1068 | CURSOR_CYCLE -> 2
1069 | CURSOR_CROSSHAIR -> 0
1070 | CURSOR_TEXT -> 5
1072 let s = s32 (if n = -1 then 0 else state.idbase+2+n) in
1073 let s = changewindowattributesreq state.idbase (*cursor*)0x4000 s in
1074 sendstr s state.sock;
1075 state.curcurs <- cursor;
1078 let fullscreen () =
1079 state.fullscreen state.idbase;
1082 let metamask = 0x40;;
1083 let altmask = 8;;
1084 let shiftmask = 1;;
1085 let ctrlmask = 4;;
1087 let withalt mask = mask land altmask != 0;;
1088 let withctrl mask = mask land ctrlmask != 0;;
1089 let withshift mask = mask land shiftmask != 0;;
1090 let withmeta mask = mask land metamask != 0;;
1091 let withnone mask = mask land (altmask + ctrlmask + shiftmask + metamask) = 0;;
1093 let xlatt, xlatf =
1094 let t = Hashtbl.create 20
1095 and f = Hashtbl.create 20 in
1096 let add n nl k =
1097 List.iter (fun s -> Hashtbl.add t s k) (n::nl);
1098 Hashtbl.add f k n
1100 let addc c =
1101 let s = String.create 1 in
1102 s.[0] <- c;
1103 add s [] (Char.code c)
1105 let addcr a b =
1106 let an = Char.code a and bn = Char.code b in
1107 for i = an to bn do addc (Char.chr i) done;
1109 addcr '0' '9';
1110 addcr 'a' 'z';
1111 addcr 'A' 'Z';
1112 String.iter addc "`~!@#$%^&*()-_=+\\|[{]};:,./<>?";
1113 for i = 0 to 29 do add ("f" ^ string_of_int (i+1)) [] (0xffbe + i) done;
1114 add "space" [] 0x20;
1115 add "ret" ["return"; "enter"] 0xff0d;
1116 add "tab" [] 0xff09;
1117 add "left" [] 0xff51;
1118 add "right" [] 0xff53;
1119 add "home" [] 0xff50;
1120 add "end" [] 0xff57;
1121 add "ins" ["insert"] 0xff63;
1122 add "del" ["delete"] 0xffff;
1123 add "esc" ["escape"] 0xff1b;
1124 add "pgup" ["pageup"] 0xff55;
1125 add "pgdown" ["pagedown"] 0xff56;
1126 add "backspace" [] 0xff08;
1127 add "up" [] 0xff52;
1128 add "down" [] 0xff54;
1129 t, f;
1132 let keyname k =
1133 try Hashtbl.find xlatf k
1134 with Not_found -> Printf.sprintf "%#x" k;
1137 let namekey name =
1138 try Hashtbl.find xlatt name
1139 with Not_found ->
1140 if String.length name = 1
1141 then Char.code name.[0]
1142 else int_of_string name;