Cosmetics
[llpp.git] / wsi.ml
blob241b4fdd31cff61344a08bfe42050449ce61b176
1 type cursor =
2 | CURSOR_INHERIT
3 | CURSOR_INFO
4 | CURSOR_CYCLE
5 | CURSOR_CROSSHAIR
6 | CURSOR_TEXT
7 ;;
9 external cloexec : Unix.file_descr -> unit = "ml_cloexec";;
10 external glx : int -> unit = "ml_glx";;
11 external glxsync : unit -> unit = "ml_glxsync";;
12 external swapb : unit -> unit = "ml_swapb";;
13 external hasdata : Unix.file_descr -> bool = "ml_hasdata";;
14 external toutf8 : int -> string = "ml_keysymtoutf8";;
16 let dolog fmt = Format.kprintf prerr_endline fmt;;
17 let vlog fmt = Format.kprintf ignore fmt;;
19 let onot = object
20 method display = ()
21 method expose = ()
22 method reshape _ _ = ()
23 method mouse _ _ _ _ _ = ()
24 method motion _ _ = ()
25 method pmotion _ _ = ()
26 method key _ _ = ()
27 method enter _ _ = ()
28 method leave = ()
29 method quit = exit 0
30 end;;
32 class type t = object
33 method display : unit
34 method expose : unit
35 method reshape : int -> int -> unit
36 method mouse : int -> bool -> int -> int -> int -> unit
37 method motion : int -> int -> unit
38 method pmotion : int -> int -> unit
39 method key : int -> int -> unit
40 method enter : int -> int -> unit
41 method leave : unit
42 method quit : unit
43 end;;
45 type state =
46 { mutable mink : int
47 ; mutable maxk : int
48 ; mutable keymap : int array array
49 ; fifo : (string -> unit) Queue.t
50 ; mutable seq : int
51 ; mutable protoatom : int
52 ; mutable deleatom : int
53 ; mutable idbase : int
54 ; mutable fullscreen : (int -> unit)
55 ; mutable setwmname : (string -> unit)
56 ; mutable stringatom : int
57 ; mutable t : t
58 ; mutable sock : Unix.file_descr
59 ; mutable x : int
60 ; mutable y : int
61 ; mutable w : int
62 ; mutable h : int
63 ; mutable fs : fs
64 ; mutable curcurs : cursor
65 ; mutable capslmask : int
66 ; mutable numlmask : int
67 ; mutable levl3mask : int
68 ; mutable levl5mask : int
70 and fs =
71 | NoFs
72 | Fs of (int * int * int * int)
75 let state =
76 { mink = max_int
77 ; maxk = min_int
78 ; keymap = [||]
79 ; fifo = Queue.create ()
80 ; seq = 0
81 ; protoatom = -1
82 ; deleatom = -1
83 ; idbase = -1
84 ; fullscreen = (fun _ -> ())
85 ; setwmname = (fun _ -> ())
86 ; sock = Unix.stdin
87 ; t = onot
88 ; x = -1
89 ; y = -1
90 ; w = -1
91 ; h = -1
92 ; fs = NoFs
93 ; stringatom = 31
94 ; curcurs = CURSOR_INHERIT
95 ; capslmask = 0
96 ; numlmask = 0
97 ; levl3mask = 0
98 ; levl5mask = 0
102 let w8 s pos i = String.set s pos (Char.chr (i land 0xff));;
104 let w16 s pos i =
105 w8 s pos i;
106 w8 s (pos+1) (i lsr 8);
109 let w32 s pos i =
110 w16 s pos i;
111 w16 s (pos+2) (i lsr 16);
114 let r16 s pos =
115 let rb pos1 = Char.code (String.get s (pos + pos1)) in
116 (rb 0) lor ((rb 1) lsl 8)
119 let r16s s pos =
120 let i = r16 s pos in
121 i - ((i land 0x8000) lsl 1);
124 let r8 s pos = Char.code (String.get s pos);;
126 let r32 s pos =
127 let rb pos1 = Char.code (String.get s (pos + pos1)) in
128 let l = (rb 0) lor ((rb 1) lsl 8)
129 and u = (rb 2) lor ((rb 3) lsl 8) in
130 (u lsl 16) lor l
133 let exntos = function
134 | Unix.Unix_error (e, s, a) -> Printf.sprintf "%s(%s):%s(%d)"
135 s a (Unix.error_message e) (Obj.magic e)
136 | exn -> Printexc.to_string exn;
139 let error fmt = Printf.kprintf failwith fmt;;
141 let readstr sock n =
142 let s = String.create n in
143 let rec loop pos n =
144 let m = Unix.read sock s pos n in
145 if m = 0
146 then state.t#quit;
147 if n != m
148 then (
149 ignore (Unix.select [sock] [] [] 0.01);
150 loop (pos + m) (n - m)
153 loop 0 n;
157 let sendstr1 s pos len sock =
158 vlog "%d => %S" state.seq s;
159 state.seq <- state.seq + 1;
160 let n = Unix.send sock s pos len [] in
161 if n != len
162 then error "send %d returned %d" len n;
165 let updkmap sock resp =
166 let syms = r8 resp 1 in
167 let len = r32 resp 4 in
168 let data =
169 if len > 0
170 then readstr sock (4*len)
171 else ""
173 let m = len / syms in
174 state.keymap <- Array.make_matrix
175 (state.maxk - state.mink) syms 0xffffff;
176 let rec loop i = if i = m then () else
177 let k = i*4*syms in
178 let rec loop2 k l = if l = syms then () else
179 let v = r32 data k in
180 state.keymap.(i).(l) <- v;
181 loop2 (k+4) (l+1)
183 loop2 k 0;
184 loop (i+1);
186 loop 0;
189 let updmodmap sock resp =
190 let n = r8 resp 1 in
191 let len = r16 resp 4 in
192 let data =
193 if len > 0
194 then readstr sock (len*4)
195 else ""
197 let modmap = Array.make_matrix 8 n 0xffffff in
198 let rec loop l = if l = 8 then () else
199 let p = l*n in
200 let rec loop1 m = if m = n then () else
201 let p = p+m in
202 let code = r8 data p in
203 modmap.(l).(m) <- code;
204 if l = 1
205 then (
206 let ki = code - state.mink in
207 if ki >= 0
208 then
209 let a = state.keymap.(ki) in
210 let rec capsloop i = if i = Array.length a || i > 3 then () else
211 let s = a.(i) in
212 if s = 0xffe5
213 then state.capslmask <- 2
214 else capsloop (i+1)
216 capsloop 0;
218 else (
219 if l > 3
220 then (
221 let ki = code - state.mink in
222 if ki >= 0
223 then
224 let a = state.keymap.(ki) in
225 let rec lloop i = if i = Array.length a || i > 3 then () else
226 let s = a.(i) in
227 match s with
228 | 0xfe03 -> state.levl3mask <- 1 lsl l
229 | 0xfe11 -> state.levl5mask <- 1 lsl l
230 | 0xff7f -> state.numlmask <- 1 lsl l
231 | _ -> lloop (i+1)
233 lloop 0;
236 loop1 (m+1)
238 loop1 0;
239 loop (l+1)
241 loop 0;
244 let sendwithrep sock s f =
245 Queue.push f state.fifo;
246 sendstr1 s 0 (String.length s) sock;
249 let padcatl ss =
250 let b = Buffer.create 16 in
251 List.iter (Buffer.add_string b) ss;
252 let bl = Buffer.length b in
253 let pl = bl land 3 in
254 if pl != 0
255 then (
256 let pad = "123" in
257 Buffer.add_substring b pad 0 (4 - pl);
259 Buffer.contents b;
262 let padcat s1 s2 = padcatl [s1; s2];;
264 let internreq name onlyifexists =
265 let s = "\016\000\000\000\000\000\000\000" in
266 let s = padcat s name in
267 if onlyifexists then w8 s 1 1;
268 w16 s 2 (String.length s / 4);
269 w16 s 4 (String.length name);
273 let sendintern sock s onlyifexists f =
274 let s = internreq s onlyifexists in
275 sendwithrep sock s f;
278 let createwindowreq wid parent x y w h bw mask =
279 let s = "\001\000\009\000wwwwppppxxyywwhhbwccvvvvmmmmeeee" in
280 w32 s 4 wid;
281 w32 s 8 parent;
282 w16 s 12 x;
283 w16 s 14 y;
284 w16 s 16 w;
285 w16 s 18 h;
286 w16 s 20 bw;
287 w16 s 22 1;
288 w32 s 24 0;
289 w32 s 28 0x800; (* eventmask *)
290 w32 s 32 mask;
294 let getgeometryreq wid =
295 let s = "\014u\002\000dddd" in
296 w32 s 4 wid;
300 let mapreq wid =
301 let s = "\008u\002\000wwww" in
302 w32 s 4 wid;
306 let getkeymapreq first count =
307 let s = "\101u\002\000fcuu" in
308 w8 s 4 first;
309 w8 s 5 count;
313 let changepropreq wid prop typ format props =
314 let s = "\018\000llwwwwppppttttfuuuLLLL" in
315 let s = padcat s props in
316 w16 s 2 (String.length s / 4);
317 w32 s 4 wid;
318 w32 s 8 prop;
319 w32 s 12 typ;
320 w8 s 16 format;
321 let ful = String.length props / (match format with
322 | 8 -> 1
323 | 16 -> 2
324 | 32 -> 4
325 | n -> error "no idea what %d means" n)
327 w32 s 20 ful;
331 let openfontreq fid name =
332 let s = "\045ullffffnnuu" in
333 let s = padcat s name in
334 w16 s 2 (String.length s / 4);
335 w32 s 4 fid;
336 w16 s 8 (String.length name);
340 let createglyphcursorreq fid cid cindex =
341 let s = "\094u\008\000ccccffffffffssmmrrggbbRRGGBB" in
342 w32 s 4 cid;
343 w32 s 8 fid;
344 w32 s 12 fid;
345 w16 s 16 cindex;
346 w16 s 18 (cindex+1);
347 w16 s 20 0;
348 w16 s 22 0;
349 w16 s 24 0;
350 w16 s 26 0xffff;
351 w16 s 28 0xffff;
352 w16 s 30 0xffff;
356 let changewindowattributesreq wid mask attrs =
357 let s = "\002ullwwwwmmmm" in
358 let s = padcat s attrs in
359 w16 s 2 (String.length s / 4);
360 w32 s 4 wid;
361 w32 s 8 mask;
365 let configurewindowreq wid mask values =
366 let s = "\012ullwwwwmmuu" in
367 let s = padcat s values in
368 w16 s 2 (String.length s / 4);
369 w32 s 4 wid;
370 w16 s 8 mask;
374 let s32 n =
375 let s = "1234" in
376 w32 s 0 n;
380 let clientmessage format seq wid typ data =
381 let s = "\033fsswwwwtttt" in
382 let s = padcat s data in
383 w8 s 1 format;
384 w16 s 2 seq;
385 w32 s 4 wid;
386 w32 s 8 typ;
390 let sendeventreq propagate destwid mask data =
391 let s = "\025p\011\000wwwwmmmm" in
392 let s = padcat s data in
393 w8 s 1 propagate;
394 w32 s 4 destwid;
395 w32 s 8 mask;
399 let getmodifiermappingreq () =
400 let s = "\119u\001\000" in
404 let getkeysym code mask =
405 let pkpk = state.keymap.(code-state.mink).(0) in
406 if (pkpk >= 0xff80 && pkpk <= 0xffbd)
407 || (pkpk >= 0x11000000 && pkpk <= 0x1100ffff)
408 then (
409 if mask land state.numlmask != 0
410 then
411 let keysym = state.keymap.(code-state.mink).(1) in
412 if keysym = 0 then pkpk else keysym
413 else pkpk
415 else (
416 let shift = (mask land 1) lxor ((mask land state.capslmask) lsr 1) in
417 let index =
418 let l3 = (mask land state.levl3mask) != 0 in
419 let l4 = (mask land state.levl5mask) != 0 in
420 shift +
421 if l3 then (if l4 then 8 else 4) else (if l4 then 6 else 0)
423 let keysym = state.keymap.(code-state.mink).(index) in
424 if index land 1 = 1 && keysym = 0
425 then state.keymap.(code-state.mink).(index - 1)
426 else keysym
430 let readresp sock =
431 let resp = readstr sock 32 in
432 let opcode = r8 resp 0 in
433 match opcode land lnot 0x80 with
434 | 0 -> (* error *)
435 let s = resp in
436 let code = r8 s 1
437 and serial = r16 s 2
438 and resid = r32 resp 4
439 and min = r16 s 8
440 and maj = r8 s 10 in
441 error "code=%d serial=%d resid=%#x min=%d maj=%d\n%S"
442 code serial resid min maj resp;
444 | 1 -> (* response *)
445 let rep = Queue.pop state.fifo in
446 rep resp;
448 | 2 -> (* key press *)
449 if Array.length state.keymap > 0
450 then
451 let code = r8 resp 1 in
452 let mask = r16 resp 28 in
453 let keysym = getkeysym code mask in
454 vlog "keysym = %x %c mask %#x code %d"
455 keysym (Char.unsafe_chr keysym) mask code;
456 state.t#key keysym mask;
458 | 3 -> (* key release *)
459 if Array.length state.keymap > 0
460 then
461 let code = r8 resp 1 in
462 let mask = r16 resp 28 in
463 let keysym = getkeysym code mask in
464 vlog "release keysym = %x %c mask %#x code %#d"
465 keysym (Char.unsafe_chr keysym) mask code
467 | 4 -> (* buttonpress *)
468 let n = r8 resp 1
469 and x = r16s resp 24
470 and y = r16s resp 26
471 and m = r16 resp 28 in
472 state.t#mouse n true x y m;
473 vlog "press %d" n
475 | 5 -> (* buttonrelease *)
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 false x y m;
481 vlog "release %d %d %d" n x y
483 | 6 -> (* motion *)
484 let x = r16s resp 24 in
485 let y = r16s resp 26 in
486 let m = r16 resp 28 in
487 if m land 0x1f00 = 0
488 then state.t#pmotion x y
489 else state.t#motion x y;
490 vlog "move %dx%d => %d" x y m
492 | 7 -> (* enter *)
493 let x = r16s resp 24
494 and y = r16s resp 26 in
495 state.t#enter x y;
496 vlog "enter %d %d" x y
498 | 8 -> (* leave *)
499 state.t#leave
501 | 18 -> vlog "unmap";
503 | 19 -> (* map *)
504 vlog "map";
506 | 12 -> (* exposure *)
507 vlog "exposure";
508 state.t#expose
510 | 15 -> (* visibility *)
511 let vis = r8 resp 8 in
512 if vis != 2 then state.t#expose;
513 vlog "visibility %d" vis;
515 | 34 -> (* mapping *)
516 state.keymap <- [||];
517 let s = getkeymapreq state.mink (state.maxk-state.mink-1) in
518 sendwithrep sock s (updkmap sock);
519 state.capslmask <- 0;
520 state.levl3mask <- 0;
521 state.levl5mask <- 0;
522 state.numlmask <- 0;
523 let s = getmodifiermappingreq () in
524 sendwithrep sock s (updmodmap sock);
527 | 33 -> (* clientmessage *)
528 let atom = r32 resp 8 in
529 if atom = state.protoatom
530 then (
531 let atom = r32 resp 12 in
532 if atom = state.deleatom
533 then state.t#quit;
535 vlog "atom %#x" atom
537 | 21 -> (* reparent *)
538 vlog "reparent"
540 | 22 -> (* configure *)
541 let x = r16s resp 16
542 and y = r16s resp 18
543 and w = r16 resp 20
544 and h = r16 resp 22 in
545 vlog "configure cur [%d %d %d %d] conf [%d %d %d %d]"
546 state.x state.y state.w state.h
547 x y w h
549 glxsync ();
550 if w != state.w || h != state.h
551 then (
552 state.t#reshape w h;
554 state.w <- w;
555 state.h <- h;
556 state.x <- x;
557 state.y <- y;
558 state.t#expose
560 | n ->
561 dolog "event %d %S" n resp
564 let readresp sock =
565 let rec loop () =
566 readresp sock;
567 if hasdata sock then loop ();
569 loop ();
572 let sendstr s ?(pos=0) ?(len=String.length s) sock =
573 sendstr1 s pos len sock;
574 if hasdata sock then readresp sock;
577 let reshape w h =
578 if state.fs = NoFs
579 then
580 let s = "wwuuhhuu" in
581 w32 s 0 w;
582 w32 s 4 h;
583 let s = configurewindowreq state.idbase 0x000c s in
584 sendstr s state.sock;
585 else state.fullscreen state.idbase
588 let syncsendwithrep sock secstowait s f =
589 let completed = ref false in
590 sendwithrep sock s (fun resp -> f resp; completed := true);
591 let now = Unix.gettimeofday in
592 let deadline = now () +. secstowait in
593 let rec readtillcompletion () =
594 let r, _, _ = Unix.select [sock] [] [] (deadline -. now ()) in
595 match r with
596 | [] -> error "didn't get X response in %f seconds, aborting" secstowait
597 | _ ->
598 readresp sock;
599 if not !completed
600 then readtillcompletion ()
602 readtillcompletion ();
605 let syncsendintern sock secstowait s onlyifexists f =
606 let s = internreq s onlyifexists in
607 syncsendwithrep sock secstowait s f;
610 let setup sock screennum w h =
611 let s = readstr sock 2 in
612 let n = String.length s in
613 if n != 2
614 then error "failed to read X connection setup response n=%d" n;
615 match s.[0] with
616 | '\000' ->
617 let reasonlen = r8 s 1 in
618 let s = readstr sock 6 in
619 let maj = r16 s 0
620 and min = r16 s 2
621 and add = r16 s 4 in
622 let len = add*4 in
623 let data = readstr sock len in
624 let reason = String.sub data 0 reasonlen in
625 error "X connection failed maj=%d min=%d reason=%S"
626 maj min reason
628 | '\002' -> failwith "X connection setup failed: authentication required";
630 | '\001' ->
631 let s = readstr sock 38 in
632 let maj = r16 s 0
633 and min = r16 s 2
634 and add = r16 s 4
635 and idbase = r32 s 10
636 and idmask = r32 s 14
637 and vlen = r16 s 22
638 and screens = r8 s 26
639 and formats = r8 s 27
640 and minkk = r8 s 32
641 and maxkk = r8 s 33 in
642 let data = readstr sock (4*add-32) in
643 let vendor = String.sub data 0 vlen in
644 let pos = ((vlen+3) land lnot 3) + formats*8 in
646 if screennum >= screens
647 then error "invalid screen %d, max %d" screennum (screens-1);
649 let pos =
650 let s = data in
651 let rec findscreen n pos = if n = screennum then pos else
652 let pos =
653 let ndepths = r8 s (pos+39) in
654 let rec skipdepths n pos = if n = ndepths then pos else
655 let pos =
656 let nvisiuals = r16 s (pos+2) in
657 pos + nvisiuals*24 + 8
659 skipdepths (n+1) pos
661 skipdepths n (pos+40)
663 findscreen (n+1) pos
665 findscreen 0 pos
667 let root = r32 data pos in
668 let rootw = r16 data (pos+20)
669 and rooth = r16 data (pos+22) in
670 state.mink <- minkk;
671 state.maxk <- maxkk;
672 state.idbase <- idbase;
673 vlog "vendor = %S, maj=%d min=%d" vendor maj min;
674 vlog "screens = %d formats = %d" screens formats;
675 vlog "minkk = %d maxkk = %d" minkk maxkk;
676 vlog "idbase = %#x idmask = %#x" idbase idmask;
677 vlog "root=%#x %dx%d" root rootw rooth;
679 let mask = 0
680 + 0x00000001 (* KeyPress *)
681 (* + 0x00000002 *) (* KeyRelease *)
682 + 0x00000004 (* ButtonPress *)
683 + 0x00000008 (* ButtonRelease *)
684 + 0x00000010 (* EnterWindow *)
685 + 0x00000020 (* LeaveWindow *)
686 + 0x00000040 (* PointerMotion *)
687 (* + 0x00000080 *) (* PointerMotionHint *)
688 (* + 0x00000100 *) (* Button1Motion *)
689 (* + 0x00000200 *) (* Button2Motion *)
690 (* + 0x00000400 *) (* Button3Motion *)
691 (* + 0x00000800 *) (* Button4Motion *)
692 (* + 0x00001000 *) (* Button5Motion *)
693 + 0x00002000 (* ButtonMotion *)
694 (* + 0x00004000 *) (* KeymapState *)
695 + 0x00008000 (* Exposure *)
696 + 0x00010000 (* VisibilityChange *)
697 + 0x00020000 (* StructureNotify *)
698 (* + 0x00040000 *) (* ResizeRedirect *)
699 (* + 0x00080000 *) (* SubstructureNotify *)
700 (* + 0x00100000 *) (* SubstructureRedirect *)
701 (* + 0x00200000 *) (* FocusChange *)
702 (* + 0x00400000 *) (* PropertyChange *)
703 (* + 0x00800000 *) (* ColormapChange *)
704 (* + 0x01000000 *) (* OwnerGrabButton *)
706 let wid = state.idbase in
707 let s = createwindowreq wid root 0 0 w h 0 mask in
708 sendstr s sock;
710 sendintern sock "WM_PROTOCOLS" false (fun resp ->
711 state.protoatom <- r32 resp 8;
712 sendintern sock "WM_DELETE_WINDOW" false (fun resp ->
713 state.deleatom <- r32 resp 8;
714 let s = s32 state.deleatom in
715 let s = changepropreq wid state.protoatom 4 32 s in
716 sendstr s sock;
720 syncsendintern sock 2.0 "WM_CLASS" false (fun resp ->
721 let atom = r32 resp 8 in
722 let llpp = "llpp\000llpp\000" in
723 let s = changepropreq wid atom 31 8 llpp in
724 sendstr s sock;
727 let s = mapreq wid in
728 sendstr s sock;
730 let s = getkeymapreq state.mink (state.maxk-state.mink) in
731 sendwithrep sock s (updkmap sock);
733 let s = getmodifiermappingreq () in
734 sendwithrep sock s (updmodmap sock);
736 let s = openfontreq (wid+1) "cursor" in
737 sendstr s sock;
739 Array.iteri (fun i glyphindex ->
740 let s = createglyphcursorreq (wid+1) (wid+2+i) glyphindex in
741 sendstr s sock;
742 ) [|34;48;50;58;128;152|];
744 sendintern sock "UTF8_STRING" true (fun resp ->
745 let atom = r32 resp 8 in
746 if atom != 0
747 then state.stringatom <- atom;
750 let setwmname s =
751 let s = changepropreq state.idbase 39 state.stringatom 8 s in
752 sendstr s state.sock;
754 state.setwmname <- setwmname;
755 sendintern sock "_NET_WM_NAME" true (fun resp ->
756 let atom = r32 resp 8 in
757 if atom != 0
758 then state.setwmname <- (fun s ->
759 setwmname s;
760 let s = changepropreq state.idbase atom state.stringatom 8 s in
761 sendstr s state.sock;
765 state.fullscreen <- (fun wid ->
766 let s = "xxuuyyuuwwuuhhuu" in
767 match state.fs with
768 | NoFs ->
769 w32 s 0 0;
770 w32 s 4 0;
771 w32 s 8 rootw;
772 w32 s 12 rooth;
773 let s = configurewindowreq wid 0x000f s in
774 sendstr s state.sock;
775 state.fs <- Fs (state.x, state.y, state.w, state.h);
777 | Fs (x, y, w, h) ->
778 w32 s 0 x;
779 w32 s 4 y;
780 w32 s 8 w;
781 w32 s 12 h;
782 let s = configurewindowreq wid 0x000f s in
783 sendstr s state.sock;
784 state.fs <- NoFs;
787 sendintern sock "_NET_WM_STATE" true (fun resp ->
788 let nwmsatom = r32 resp 8 in
789 if nwmsatom != 0
790 then
791 sendintern sock "_NET_WM_STATE_FULLSCREEN" true (fun resp ->
792 let fsatom = r32 resp 8 in
793 if fsatom != 0
794 then
795 state.fullscreen <-
796 (fun wid ->
797 let data = String.make 20 '\000' in
798 let fs, f =
799 match state.fs with
800 | NoFs -> Fs (-1, -1, -1, -1), 1
801 | Fs _ -> NoFs, 0
803 w32 data 0 f;
804 w32 data 4 fsatom;
806 let cm = clientmessage 32 0 wid nwmsatom data in
807 let s = sendeventreq 0 root 0x180000 cm in
808 sendstr s sock;
809 state.fs <- fs;
813 let s = getgeometryreq wid in
814 syncsendwithrep sock 2.0 s (fun resp ->
815 glx wid;
816 let w = r16 resp 16
817 and h = r16 resp 18 in
818 state.w <- w;
819 state.h <- h;
822 | c ->
823 error "unknown conection setup response %d" (Char.code c)
826 let getauth haddr dnum =
827 let haddr =
828 if haddr = "localhost" || String.length haddr = 0
829 then
830 try Unix.gethostname ()
831 with exn ->
832 dolog "failed to resolve `%S': %s" haddr (exntos exn);
833 haddr
834 else haddr
836 let path =
837 try Sys.getenv "XAUTHORITY"
838 with Not_found ->
839 try Filename.concat (Sys.getenv "HOME") ".Xauthority"
840 with Not_found -> ""
842 let readauth ic =
843 let input_string ic len =
844 let s = String.create len in
845 really_input ic s 0 len;
848 let r16 s =
849 let rb pos = Char.code (String.get s pos) in
850 (rb 1) lor ((rb 0) lsl 8)
852 let rec find () =
853 let rs () =
854 let s = input_string ic 2 in
855 let n = r16 s in
856 input_string ic n
858 let family = input_string ic 2 in
859 let addr = rs () in
860 let nums = rs () in
861 let optnum =
862 try Some (int_of_string nums)
863 with exn ->
864 dolog
865 "display number(%S) is not an integer (corrupt %S?): %s"
866 nums path (exntos exn);
867 None
869 let name = rs () in
870 let data = rs () in
872 vlog "family %S addr %S(%S) num %S(%d) name %S data %S"
873 family addr haddr nums dnum name data;
874 match optnum with
875 | Some num when addr = haddr && num = dnum ->
876 name, data
877 | _ -> find ()
879 let name, data =
880 try find ()
881 with
882 | End_of_file -> "", ""
883 | exn ->
884 dolog "exception while reading X authority data (%S): %s"
885 path (exntos exn);
886 "", ""
888 close_in ic;
889 name, data;
891 let opt =
893 if String.length path = 0
894 then None
895 else Some (open_in_bin path)
896 with exn ->
897 if Sys.file_exists path
898 then
899 dolog "failed to open X authority file `%S' : %s"
900 path (exntos exn);
901 None
903 match opt with
904 | None -> "", ""
905 | Some ic -> readauth ic
908 let init t w h osx =
909 let d =
910 try Sys.getenv "DISPLAY"
911 with exn ->
912 error "could not get DISPLAY evironment variable: %s"
913 (exntos exn)
915 let getnum w b e =
916 if b = e
917 then error "invalid DISPLAY(%s) %S" w d
918 else
919 let s = String.sub d b (e - b) in
920 try int_of_string s
921 with exn ->
922 error "invalid DISPLAY %S can not parse %s(%S): %s"
923 d w s (exntos exn)
925 let rec phost pos =
926 if pos = String.length d
927 then error "invalid DISPLAY %S no display number specified" d
928 else (
929 if d.[pos] = ':'
930 then
931 let rec pdispnum pos1 =
932 if pos1 = String.length d
933 then getnum "display number" (pos+1) pos1, 0
934 else
935 match d.[pos1] with
936 | '.' ->
937 let dispnum = getnum "display number" (pos+1) pos1 in
938 let rec pscreennum pos2 =
939 if pos2 = String.length d
940 then getnum "screen number" (pos1+1) pos2
941 else
942 match d.[pos2] with
943 | '0' .. '9' -> pscreennum (pos2+1)
944 | _ ->
945 error "invalid DISPLAY %S, cannot parse screen number" d
947 dispnum, pscreennum (pos1+1)
948 | '0' .. '9' -> pdispnum (pos1+1)
949 | _ ->
950 error "invalid DISPLAY %S, cannot parse display number" d
952 String.sub d 0 pos, pdispnum (pos+1)
953 else phost (pos+1)
956 let host, (dispnum, screennum) = phost 0 in
957 let aname, adata = getauth host dispnum in
958 let fd =
959 let fd, addr =
960 if osx || String.length host = 0 || host = "unix"
961 then
962 let addr =
963 if osx
964 then Unix.ADDR_UNIX d
965 else Unix.ADDR_UNIX ("/tmp/.X11-unix/X" ^ string_of_int dispnum)
967 let fd = Unix.socket Unix.PF_UNIX Unix.SOCK_STREAM 0 in
968 fd, addr
969 else
970 let h =
971 try Unix.gethostbyname host
972 with exn ->
973 error "cannot resolve %S: %s" host (exntos exn)
975 let addr = h.Unix.h_addr_list.(0) in
976 let port = 6000 + dispnum in
977 let fd = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
978 fd, (Unix.ADDR_INET (addr, port))
980 try Unix.connect fd addr; fd
981 with exn ->
982 error "failed to connect to X: %s" (exntos exn)
984 cloexec fd;
985 let s = "luMMmmnndduu" in
986 let s = padcat s aname in
987 let s = padcat s adata in
988 w16 s 2 11;
989 w16 s 4 0;
990 w16 s 6 (String.length aname);
991 w16 s 8 (String.length adata);
992 sendstr1 s 0 (String.length s) fd;
993 state.sock <- fd;
994 setup fd screennum w h;
995 state.t <- t;
996 fd, state.w, state.h;
999 let settitle s =
1000 state.setwmname s;
1003 let setcursor cursor =
1004 if cursor != state.curcurs
1005 then
1006 let n =
1007 match cursor with
1008 | CURSOR_INHERIT -> -1
1009 | CURSOR_INFO -> 3
1010 | CURSOR_CYCLE -> 2
1011 | CURSOR_CROSSHAIR -> 0
1012 | CURSOR_TEXT -> 5
1014 let s = s32 (if n = -1 then 0 else state.idbase+2+n) in
1015 let s = changewindowattributesreq state.idbase (*cursor*)0x4000 s in
1016 sendstr s state.sock;
1017 state.curcurs <- cursor;
1020 let fullscreen () =
1021 state.fullscreen state.idbase;
1024 let metamask = 0x40;;
1025 let altmask = 8;;
1026 let shiftmask = 1;;
1027 let ctrlmask = 4;;
1029 let withalt mask = mask land altmask != 0;;
1030 let withctrl mask = mask land ctrlmask != 0;;
1031 let withshift mask = mask land shiftmask != 0;;
1032 let withmeta mask = mask land metamask != 0;;
1033 let withnone mask = mask land (altmask + ctrlmask + shiftmask + metamask) = 0;;
1035 let xlatt, xlatf =
1036 let t = Hashtbl.create 20
1037 and f = Hashtbl.create 20 in
1038 let add n nl k =
1039 List.iter (fun s -> Hashtbl.add t s k) (n::nl);
1040 Hashtbl.add f k n
1042 let addc c =
1043 let s = String.create 1 in
1044 s.[0] <- c;
1045 add s [] (Char.code c)
1047 let addcr a b =
1048 let an = Char.code a and bn = Char.code b in
1049 for i = an to bn do addc (Char.chr i) done;
1051 addcr '0' '9';
1052 addcr 'a' 'z';
1053 addcr 'A' 'Z';
1054 String.iter addc "`~!@#$%^&*()-_=+\\|[{]};:,./<>?";
1055 for i = 0 to 29 do add ("f" ^ string_of_int (i+1)) [] (0xffbe + i) done;
1056 add "space" [] 0x20;
1057 add "ret" ["return"; "enter"] 0xff0d;
1058 add "tab" [] 0xff09;
1059 add "left" [] 0xff51;
1060 add "right" [] 0xff53;
1061 add "home" [] 0xff50;
1062 add "end" [] 0xff57;
1063 add "ins" ["insert"] 0xff63;
1064 add "del" ["delete"] 0xffff;
1065 add "esc" ["escape"] 0xff1b;
1066 add "pgup" ["pageup"] 0xff55;
1067 add "pgdown" ["pagedown"] 0xff56;
1068 add "backspace" [] 0xff08;
1069 add "up" [] 0xff52;
1070 add "down" [] 0xff54;
1071 t, f;
1074 let keyname k =
1075 try Hashtbl.find xlatf k
1076 with Not_found -> Printf.sprintf "%#x" k;
1079 let namekey name =
1080 try Hashtbl.find xlatt name
1081 with Not_found ->
1082 if String.length name = 1
1083 then Char.code name.[0]
1084 else int_of_string name;