Conserve bandwidth
[llpp.git] / wsi.ml
blobeb7a5c423c3b3c1e223617fee931bbce3a227489
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 swapb : unit -> unit = "ml_swapb";;
12 external hasdata : Unix.file_descr -> bool = "ml_hasdata";;
13 external toutf8 : int -> string = "ml_keysymtoutf8";;
15 let dolog fmt = Format.kprintf prerr_endline fmt;;
16 let vlog fmt = Format.kprintf ignore fmt;;
18 let onot = object
19 method display = ()
20 method expose = ()
21 method reshape _ _ = ()
22 method mouse _ _ _ _ _ = ()
23 method motion _ _ = ()
24 method pmotion _ _ = ()
25 method key _ _ = ()
26 method enter _ _ = ()
27 method leave = ()
28 method quit = exit 0
29 end;;
31 class type t = object
32 method display : unit
33 method expose : unit
34 method reshape : int -> int -> unit
35 method mouse : int -> bool -> int -> int -> int -> unit
36 method motion : int -> int -> unit
37 method pmotion : int -> int -> unit
38 method key : int -> int -> unit
39 method enter : int -> int -> unit
40 method leave : unit
41 method quit : unit
42 end;;
44 type state =
45 { mutable mink : int
46 ; mutable maxk : int
47 ; mutable keymap : int array array
48 ; fifo : (string -> unit) Queue.t
49 ; mutable seq : int
50 ; mutable protoatom : int
51 ; mutable deleatom : int
52 ; mutable idbase : int
53 ; mutable fullscreen : (int -> unit)
54 ; mutable setwmname : (string -> unit)
55 ; mutable stringatom : int
56 ; mutable t : t
57 ; mutable sock : Unix.file_descr
58 ; mutable x : int
59 ; mutable y : int
60 ; mutable w : int
61 ; mutable h : int
62 ; mutable fs : fs
63 ; mutable curcurs : cursor
65 and fs =
66 | NoFs
67 | Fs of (int * int * int * int)
70 let state =
71 { mink = max_int
72 ; maxk = min_int
73 ; keymap = [||]
74 ; fifo = Queue.create ()
75 ; seq = 0
76 ; protoatom = -1
77 ; deleatom = -1
78 ; idbase = -1
79 ; fullscreen = (fun _ -> ())
80 ; setwmname = (fun _ -> ())
81 ; sock = Unix.stdin
82 ; t = onot
83 ; x = -1
84 ; y = -1
85 ; w = -1
86 ; h = -1
87 ; fs = NoFs
88 ; stringatom = 31
89 ; curcurs = CURSOR_INHERIT
93 let w8 s pos i = String.set s pos (Char.chr (i land 0xff));;
95 let w16 s pos i =
96 w8 s pos i;
97 w8 s (pos+1) (i lsr 8);
100 let w32 s pos i =
101 w16 s pos i;
102 w16 s (pos+2) (i lsr 16);
105 let r16 s pos =
106 let rb pos1 = Char.code (String.get s (pos + pos1)) in
107 (rb 0) lor ((rb 1) lsl 8)
110 let r16s s pos =
111 let i = r16 s pos in
112 i - ((i land 0x8000) lsl 1);
115 let r8 s pos = Char.code (String.get s pos);;
117 let r32 s pos =
118 let rb pos1 = Char.code (String.get s (pos + pos1)) in
119 let l = (rb 0) lor ((rb 1) lsl 8)
120 and u = (rb 2) lor ((rb 3) lsl 8) in
121 (u lsl 16) lor l
124 let error fmt = Printf.kprintf failwith fmt;;
126 let readstr sock n =
127 let s = String.create n in
128 let rec loop pos n =
129 let m = Unix.read sock s pos n in
130 if n != m
131 then (
132 ignore (Unix.select [sock] [] [] 0.01);
133 loop (pos + m) (n - m)
136 loop 0 n;
140 let sendstr1 s pos len sock =
141 vlog "%d => %S" state.seq s;
142 state.seq <- state.seq + 1;
143 let n = Unix.send sock s pos len [] in
144 if n != len
145 then error "send %d returned %d" len n;
148 let updkmap sock resp =
149 let syms = r8 resp 1 in
150 let len = r32 resp 4 in
151 let data =
152 if len > 0
153 then readstr sock (4*len)
154 else ""
156 let m = len / syms in
157 state.keymap <- Array.make_matrix
158 (state.maxk - state.mink) syms 0xffffff;
159 let rec loop i = if i = m then () else
160 let k = i*4*syms in
161 let rec loop2 k l = if l = syms then () else
162 let v = r32 data k in
163 state.keymap.(i).(l) <- v;
164 loop2 (k+4) (l+1)
166 loop2 k 0;
167 loop (i+1);
169 loop 0;
172 let sendwithrep sock s f =
173 Queue.push f state.fifo;
174 sendstr1 s 0 (String.length s) sock;
177 let padcatl ss =
178 let b = Buffer.create 16 in
179 List.iter (Buffer.add_string b) ss;
180 let bl = Buffer.length b in
181 let pl = bl land 3 in
182 if pl != 0
183 then (
184 let pad = "123" in
185 Buffer.add_substring b pad 0 (4 - pl);
187 Buffer.contents b;
190 let padcat s1 s2 = padcatl [s1; s2];;
192 let internreq name onlyifexists =
193 let s = "\016\000\000\000\000\000\000\000" in
194 let s = padcat s name in
195 w8 s 1 (if onlyifexists then 1 else 0);
196 w16 s 2 (String.length s / 4);
197 w16 s 4 (String.length name);
201 let sendintern sock s onlyifexists f =
202 let s = internreq s onlyifexists in
203 sendwithrep sock s f;
206 let createwindowreq wid parent x y w h bw mask =
207 let s = "\001\000\009\000wwwwppppxxyywwhhbwccvvvvmmmmeeee" in
208 w32 s 4 wid;
209 w32 s 8 parent;
210 w16 s 12 x;
211 w16 s 14 y;
212 w16 s 16 w;
213 w16 s 18 h;
214 w16 s 20 bw;
215 w16 s 22 1;
216 w32 s 24 0;
217 w32 s 28 0x800; (* eventmask *)
218 w32 s 32 mask;
222 let getgeometryreq wid =
223 let s = "\014u\002\000dddd" in
224 w32 s 4 wid;
228 let mapreq wid =
229 let s = "\008u\002\000wwww" in
230 w32 s 4 wid;
234 let getkeymapreq first count =
235 let s = "\101u\002\000fcuu" in
236 w8 s 4 first;
237 w8 s 5 count;
241 let changepropreq wid prop typ format props =
242 let s = "\018\000llwwwwppppttttfuuuLLLL" in
243 let s = padcat s props in
244 w16 s 2 (String.length s / 4);
245 w32 s 4 wid;
246 w32 s 8 prop;
247 w32 s 12 typ;
248 w8 s 16 format;
249 let ful = String.length props / (match format with
250 | 8 -> 1
251 | 16 -> 2
252 | 32 -> 4
253 | n -> error "no idea what %d means" n)
255 w32 s 20 ful;
259 let openfontreq fid name =
260 let s = "\045ullffffnnuu" in
261 let s = padcat s name in
262 w16 s 2 (String.length s / 4);
263 w32 s 4 fid;
264 w16 s 8 (String.length name);
268 let createglyphcursorreq fid cid cindex =
269 let s = "\094u\008\000ccccffffffffssmmrrggbbRRGGBB" in
270 w32 s 4 cid;
271 w32 s 8 fid;
272 w32 s 12 fid;
273 w16 s 16 cindex;
274 w16 s 18 (cindex+1);
275 w16 s 20 0;
276 w16 s 22 0;
277 w16 s 24 0;
278 w16 s 26 0xffff;
279 w16 s 28 0xffff;
280 w16 s 30 0xffff;
284 let mapwindowreq wid =
285 let s = "\008u\002\000wwww" in
286 w32 s 4 wid;
290 let changewindowattributesreq wid mask attrs =
291 let s = "\002ullwwwwmmmm" in
292 let s = padcat s attrs in
293 w16 s 2 (String.length s / 4);
294 w32 s 4 wid;
295 w32 s 8 mask;
299 let configurewindowreq wid mask values =
300 let s = "\012ullwwwwmmuu" in
301 let s = padcat s values in
302 w16 s 2 (String.length s / 4);
303 w32 s 4 wid;
304 w16 s 8 mask;
308 let s32 n =
309 let s = "1234" in
310 w32 s 0 n;
314 let clientmessage format seq wid typ data =
315 let s = "\033fsswwwwtttt" in
316 let s = padcat s data in
317 w8 s 1 format;
318 w16 s 2 seq;
319 w32 s 4 wid;
320 w32 s 8 typ;
324 let sendeventreq propagate destwid mask data =
325 let s = "\025p\011\000wwwwmmmm" in
326 let s = padcat s data in
327 w8 s 1 propagate;
328 w32 s 4 destwid;
329 w32 s 8 mask;
333 let getkeysym code mask =
334 let index = (mask land 1) lxor ((mask land 2) lsr 1) in
335 let keysym = state.keymap.(code-state.mink).(index) in
336 if index = 1 && keysym = 0
337 then state.keymap.(code-state.mink).(0)
338 else keysym
341 let rec readresp sock =
342 let resp = readstr sock 32 in
343 let opcode = r8 resp 0 in
344 match opcode land lnot 0x80 with
345 | 0 -> (* error *)
346 let s = resp in
347 let code = r8 s 1
348 and serial = r16 s 2
349 and resid = r32 resp 4
350 and min = r16 s 8
351 and maj = r8 s 10 in
352 error "code=%d serial=%d resid=%#x min=%d maj=%d\n%S"
353 code serial resid min maj resp;
355 | 1 -> (* response *)
356 let rep = Queue.pop state.fifo in
357 rep resp;
359 | 2 -> (* key press *)
360 if Array.length state.keymap > 0
361 then
362 let code = r8 resp 1 in
363 let mask = r16 resp 28 in
364 let keysym = getkeysym code mask in
365 vlog "keysym = %x %c" keysym (Char.unsafe_chr keysym);
366 state.t#key keysym mask;
368 | 3 -> (* key release *)
369 if Array.length state.keymap > 0
370 then
371 let code = r8 resp 1 in
372 let mask = r16 resp 28 in
373 let keysym = getkeysym code mask in
374 vlog "release keysym = %x %c mask %#x"
375 keysym (Char.unsafe_chr keysym) mask
377 | 4 -> (* buttonpress *)
378 let n = r8 resp 1
379 and x = r16s resp 24
380 and y = r16s resp 26
381 and m = r16 resp 28 in
382 state.t#mouse n true x y m;
383 vlog "press %d" n
385 | 5 -> (* buttonrelease *)
386 let n = r8 resp 1
387 and x = r16s resp 24
388 and y = r16s resp 26
389 and m = r16 resp 28 in
390 state.t#mouse n false x y m;
391 vlog "release %d %d %d" n x y
393 | 6 -> (* motion *)
394 let x = r16s resp 24 in
395 let y = r16s resp 26 in
396 let m = r16 resp 28 in
397 if m land 0x1f00 = 0
398 then state.t#pmotion x y
399 else state.t#motion x y;
400 vlog "move %dx%d => %d" x y m
402 | 7 -> (* enter *)
403 let x = r16s resp 24
404 and y = r16s resp 26 in
405 state.t#enter x y;
406 vlog "enter %d %d" x y
408 | 8 -> (* leave *)
409 state.t#leave
411 | 18 -> vlog "unmap";
413 | 19 -> (* map *)
414 vlog "map";
416 | 12 -> (* exposure *)
417 vlog "exposure";
418 state.t#expose
420 | 15 -> (* visibility *)
421 let vis = r8 resp 8 in
422 if vis != 2 then state.t#display;
423 vlog "visibility %d" vis;
425 | 34 -> (* mapping *)
426 state.keymap <- [||];
427 let s = getkeymapreq state.mink (state.maxk-state.mink-1) in
428 sendwithrep sock s (updkmap sock);
430 | 33 -> (* clientmessage *)
431 let atom = r32 resp 8 in
432 if atom = state.protoatom
433 then (
434 let atom = r32 resp 12 in
435 if atom = state.deleatom
436 then state.t#quit;
438 vlog "atom %#x" atom
440 | 21 -> (* reparent *)
441 vlog "reparent"
443 | 22 -> (* configure *)
444 let x = r16s resp 16
445 and y = r16s resp 18
446 and w = r16 resp 20
447 and h = r16 resp 22 in
448 vlog "configure cur [%d %d %d %d] conf [%d %d %d %d]"
449 state.x state.y state.w state.h
450 x y w h
452 if w != state.w || h != state.h
453 then (
454 state.t#reshape w h;
456 state.w <- w;
457 state.h <- h;
458 state.x <- x;
459 state.y <- y;
460 state.t#display
462 | n ->
463 dolog "event %d %S" n resp
466 let readresp sock =
467 let rec loop () =
468 readresp sock;
469 if hasdata sock then loop ();
471 loop ();
474 let sendstr s ?(pos=0) ?(len=String.length s) sock =
475 sendstr1 s pos len sock;
476 if hasdata sock then readresp sock;
479 let hexstr s =
480 let b = Buffer.create 16 in
481 String.iter (fun c ->
482 Buffer.add_string b (Printf.sprintf "%02x" (Char.code c))) s;
483 Buffer.contents b;
486 let reshape w h =
487 if state.fs = NoFs
488 then
489 let s = "wwuuhhuu" in
490 w32 s 0 w;
491 w32 s 4 h;
492 let s = configurewindowreq state.idbase 0x000c s in
493 sendstr s state.sock;
494 else state.fullscreen state.idbase
497 let setup sock screennum w h =
498 let s = readstr sock 2 in
499 let n = String.length s in
500 if n != 2
501 then error "failed to read X connection setup response n=%d" n;
502 match s.[0] with
503 | '\000' ->
504 let reasonlen = r8 s 1 in
505 let s = readstr sock 6 in
506 let maj = r16 s 0
507 and min = r16 s 2
508 and add = r16 s 4 in
509 let len = add*4 in
510 let data = readstr sock len in
511 let reason = String.sub data 0 reasonlen in
512 error "X connection failed maj=%d min=%d reason=%S"
513 maj min reason
515 | '\002' -> failwith "X connection setup failed: authentication required";
517 | '\001' ->
518 let s = readstr sock 38 in
519 let maj = r16 s 0
520 and min = r16 s 2
521 and add = r16 s 4
522 and idbase = r32 s 10
523 and idmask = r32 s 14
524 and vlen = r16 s 22
525 and screens = r8 s 26
526 and formats = r8 s 27
527 and minkk = r8 s 32
528 and maxkk = r8 s 33 in
529 let data = readstr sock (4*add-32) in
530 let vendor = String.sub data 0 vlen in
531 let pos = ((vlen+3) land lnot 3) + formats*8 in
533 if screennum >= screens
534 then error "invalid screen %d, max %d" screennum (screens-1);
536 let pos =
537 let s = data in
538 let rec findscreen n pos = if n = screennum then pos else
539 let pos =
540 let ndepths = r8 s (pos+39) in
541 let rec skipdepths n pos = if n = ndepths then pos else
542 let pos =
543 let nvisiuals = r16 s (pos+2) in
544 pos + nvisiuals*24 + 8
546 skipdepths (n+1) pos
548 skipdepths n (pos+40)
550 findscreen (n+1) pos
552 findscreen 0 pos
554 let root = r32 data pos in
555 let rootw = r16 data (pos+20)
556 and rooth = r16 data (pos+22) in
557 state.mink <- minkk;
558 state.maxk <- maxkk;
559 state.idbase <- idbase;
560 vlog "vendor = %S, maj=%d min=%d" vendor maj min;
561 vlog "screens = %d formats = %d" screens formats;
562 vlog "minkk = %d maxkk = %d" minkk maxkk;
563 vlog "idbase = %#x idmask = %#x" idbase idmask;
564 vlog "root=%#x %dx%d" root rootw rooth;
566 let mask = 0
567 + 0x00000001 (* KeyPress *)
568 (* + 0x00000002 *) (* KeyRelease *)
569 + 0x00000004 (* ButtonPress *)
570 + 0x00000008 (* ButtonRelease *)
571 + 0x00000010 (* EnterWindow *)
572 + 0x00000020 (* LeaveWindow *)
573 + 0x00000040 (* PointerMotion *)
574 (* + 0x00000080 *) (* PointerMotionHint *)
575 (* + 0x00000100 *) (* Button1Motion *)
576 (* + 0x00000200 *) (* Button2Motion *)
577 (* + 0x00000400 *) (* Button3Motion *)
578 (* + 0x00000800 *) (* Button4Motion *)
579 (* + 0x00001000 *) (* Button5Motion *)
580 + 0x00002000 (* ButtonMotion *)
581 (* + 0x00004000 *) (* KeymapState *)
582 + 0x00008000 (* Exposure *)
583 + 0x00010000 (* VisibilityChange *)
584 + 0x00020000 (* StructureNotify *)
585 (* + 0x00040000 *) (* ResizeRedirect *)
586 (* + 0x00080000 *) (* SubstructureNotify *)
587 (* + 0x00100000 *) (* SubstructureRedirect *)
588 (* + 0x00200000 *) (* FocusChange *)
589 (* + 0x00400000 *) (* PropertyChange *)
590 (* + 0x00800000 *) (* ColormapChange *)
591 (* + 0x01000000 *) (* OwnerGrabButton *)
593 let wid = state.idbase in
594 let s = createwindowreq wid root 0 0 w h 0 mask in
595 sendstr s sock;
597 let s = mapreq wid in
598 sendstr s sock;
600 let s = getkeymapreq state.mink (state.maxk-state.mink) in
601 sendwithrep sock s (updkmap sock);
603 sendintern sock "WM_PROTOCOLS" false (fun resp ->
604 state.protoatom <- r32 resp 8;
605 sendintern sock "WM_DELETE_WINDOW" false (fun resp ->
606 state.deleatom <- r32 resp 8;
607 let s = s32 state.deleatom in
608 let s = changepropreq wid state.protoatom 4 32 s in
609 sendstr s sock;
613 sendintern sock "WM_CLASS" false (fun resp ->
614 let atom = r32 resp 8 in
615 let llpp = "llpp\000llpp\000" in
616 let s = changepropreq wid atom 31 8 llpp in
617 sendstr s sock;
620 let s = openfontreq (wid+1) "cursor" in
621 sendstr s sock;
623 Array.iteri (fun i glyphindex ->
624 let s = createglyphcursorreq (wid+1) (wid+2+i) glyphindex in
625 sendstr s sock;
626 ) [|34;48;50;58;128;152|];
628 sendintern sock "UTF8_STRING" true (fun resp ->
629 let atom = r32 resp 8 in
630 if atom != 0
631 then state.stringatom <- atom;
634 let setwmname s =
635 let s = changepropreq state.idbase 39 state.stringatom 8 s in
636 sendstr s state.sock;
638 state.setwmname <- setwmname;
639 sendintern sock "_NET_WM_NAME" true (fun resp ->
640 let atom = r32 resp 8 in
641 if atom != 0
642 then state.setwmname <- (fun s ->
643 setwmname s;
644 let s = changepropreq state.idbase atom state.stringatom 8 s in
645 sendstr s state.sock;
649 state.fullscreen <- (fun wid ->
650 let s = "xxuuyyuuwwuuhhuu" in
651 match state.fs with
652 | NoFs ->
653 w32 s 0 0;
654 w32 s 4 0;
655 w32 s 8 rootw;
656 w32 s 12 rooth;
657 let s = configurewindowreq wid 0x000f s in
658 sendstr s state.sock;
659 state.fs <- Fs (state.x, state.y, state.w, state.h);
661 | Fs (x, y, w, h) ->
662 w32 s 0 x;
663 w32 s 4 y;
664 w32 s 8 w;
665 w32 s 12 h;
666 let s = configurewindowreq wid 0x000f s in
667 sendstr s state.sock;
668 state.fs <- NoFs;
671 sendintern sock "_NET_WM_STATE" true (fun resp ->
672 let nwmsatom = r32 resp 8 in
673 if nwmsatom != 0
674 then
675 sendintern sock "_NET_WM_STATE_FULLSCREEN" true (fun resp ->
676 let fsatom = r32 resp 8 in
677 if fsatom != 0
678 then
679 state.fullscreen <-
680 (fun wid ->
681 let data = String.make 20 '\000' in
682 let fs, f =
683 match state.fs with
684 | NoFs -> Fs (-1, -1, -1, -1), 1
685 | Fs _ -> NoFs, 0
687 w32 data 0 f;
688 w32 data 4 fsatom;
690 let cm = clientmessage 32 0 wid nwmsatom data in
691 let s = sendeventreq 0 root 0x180000 cm in
692 sendstr s sock;
693 state.fs <- fs;
697 let s = getgeometryreq wid in
698 let completed = ref false in
699 sendwithrep sock s (fun resp ->
700 glx wid;
701 let w = r16 resp 16
702 and h = r16 resp 18 in
703 state.w <- w;
704 state.h <- h;
705 completed := true;
707 let now = Unix.gettimeofday in
708 let deadline = now () +. 2.0 in
709 let rec readtillcompletion () =
710 let r, _, _ = Unix.select [sock] [] [] (deadline -. now ()) in
711 match r with
712 | [] -> readtillcompletion ()
713 | _ ->
714 readresp sock;
715 if not !completed
716 then readtillcompletion ()
718 readtillcompletion ();
720 | c ->
721 error "unknown conection setup response %d" (Char.code c)
724 let getauth haddr dnum =
725 let haddr =
726 if haddr = "localhost" || String.length haddr = 0
727 then
728 try Unix.gethostname ()
729 with exn ->
730 dolog "failed to resolve `%S': %s" haddr (Printexc.to_string exn);
731 haddr
732 else haddr
734 let readauth ic =
735 let input_string ic len =
736 let s = String.create len in
737 really_input ic s 0 len;
740 let r16 s =
741 let rb pos = Char.code (String.get s pos) in
742 (rb 1) lor ((rb 0) lsl 8)
744 let rec find () =
745 let rs () =
746 let s = input_string ic 2 in
747 let n = r16 s in
748 input_string ic n
750 let family = input_string ic 2 in
751 let addr = rs () in
752 let nums = rs () in
753 let num = int_of_string nums in
754 let name = rs () in
755 let data = rs () in
757 vlog "family %S addr %S(%S) num %d(%d) name %S data %S"
758 family addr haddr num dnum name data;
759 if addr = haddr && num = dnum
760 then name, data
761 else find ()
763 let name, data =
764 try find () with _ -> "", ""
766 close_in ic;
767 name, data;
769 let path =
770 try Sys.getenv "XAUTHORITY"
771 with Not_found ->
772 try Filename.concat (Sys.getenv "HOME") ".Xauthority"
773 with Not_found -> ""
775 let opt =
777 if String.length path = 0
778 then None
779 else Some (open_in_bin path)
780 with exn ->
781 if Sys.file_exists path
782 then
783 dolog "failed to open X authority file `%S' : %s"
784 path (Printexc.to_string exn);
785 None
787 match opt with
788 | None -> "", ""
789 | Some ic -> readauth ic
792 let init t w h osx =
793 let d =
794 try Sys.getenv "DISPLAY"
795 with exn ->
796 error "Could not get DISPLAY evironment variable: %s"
797 (Printexc.to_string exn)
799 let colonpos = String.index d ':' in
800 let host = String.sub d 0 colonpos in
801 let dispnum, screennum =
803 let dotpos = String.index_from d (colonpos + 1) '.' in
804 let disp = String.sub d (colonpos + 1) (dotpos - colonpos - 1) in
805 let screen = String.sub d (dotpos + 1) (String.length d - dotpos - 1) in
806 int_of_string disp, int_of_string screen
807 with Not_found ->
808 let disp = String.sub d (colonpos + 1) (String.length d - colonpos - 1) in
809 int_of_string disp, 0
811 let aname, adata = getauth host dispnum in
812 let fd =
813 if osx || String.length host = 0 || host = "unix"
814 then
815 let addr =
816 if osx
817 then Unix.ADDR_UNIX d
818 else Unix.ADDR_UNIX ("/tmp/.X11-unix/X" ^ string_of_int dispnum)
820 let fd = Unix.socket Unix.PF_UNIX Unix.SOCK_STREAM 0 in
821 Unix.connect fd addr;
823 else
824 let h = Unix.gethostbyname host in
825 let addr = h.Unix.h_addr_list.(0) in
826 let port = 6000 + dispnum in
827 let fd = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
828 Unix.connect fd (Unix.ADDR_INET (addr, port));
831 cloexec fd;
832 let s = "luMMmmnndduu" in
833 let s = padcat s aname in
834 let s = padcat s adata in
835 w16 s 2 11;
836 w16 s 4 0;
837 w16 s 6 (String.length aname);
838 w16 s 8 (String.length adata);
839 sendstr1 s 0 (String.length s) fd;
840 state.sock <- fd;
841 setup fd screennum w h;
842 state.t <- t;
843 fd, state.w, state.h;
846 let settitle s =
847 state.setwmname s;
850 let setcursor cursor =
851 if cursor != state.curcurs
852 then
853 let n =
854 match cursor with
855 | CURSOR_INHERIT -> -1
856 | CURSOR_INFO -> 3
857 | CURSOR_CYCLE -> 2
858 | CURSOR_CROSSHAIR -> 0
859 | CURSOR_TEXT -> 5
861 let s = s32 (if n = -1 then 0 else state.idbase+2+n) in
862 let s = changewindowattributesreq state.idbase (*cursor*)0x4000 s in
863 sendstr s state.sock;
864 state.curcurs <- cursor;
867 let fullscreen () =
868 state.fullscreen state.idbase;
871 let metamask = 0x40;;
872 let altmask = 8;;
873 let shiftmask = 1;;
874 let ctrlmask = 4;;
876 let withalt mask = mask land altmask != 0;;
877 let withctrl mask = mask land ctrlmask != 0;;
878 let withshift mask = mask land shiftmask != 0;;
879 let withmeta mask = mask land metamask != 0;;
880 let withnone mask = mask land (altmask + ctrlmask + shiftmask + metamask) = 0;;
882 let xlatt, xlatf =
883 let t = Hashtbl.create 20
884 and f = Hashtbl.create 20 in
885 let add n nl k =
886 List.iter (fun s -> Hashtbl.add t s k) (n::nl);
887 Hashtbl.add f k n
889 let addc c =
890 let s = String.create 1 in
891 s.[0] <- c;
892 add s [] (Char.code c)
894 let addcr a b =
895 let an = Char.code a and bn = Char.code b in
896 for i = an to bn do addc (Char.chr i) done;
898 addcr '0' '9';
899 addcr 'a' 'z';
900 addcr 'A' 'Z';
901 String.iter addc "`~!@#$%^&*()-_=+\\|[{]};:,./<>?";
902 for i = 0 to 29 do add ("f" ^ string_of_int (i+1)) [] (0xffbe + i) done;
903 add "space" [] 0x20;
904 add "ret" ["return"; "enter"] 0xff0d;
905 add "tab" [] 0xff09;
906 add "left" [] 0xff51;
907 add "right" [] 0xff53;
908 add "home" [] 0xff50;
909 add "end" [] 0xff57;
910 add "ins" ["insert"] 0xff63;
911 add "del" ["delete"] 0xffff;
912 add "esc" ["escape"] 0xff1b;
913 add "pgup" ["pageup"] 0xff55;
914 add "pgdown" ["pagedown"] 0xff56;
915 add "backspace" [] 0xff08;
916 add "up" [] 0xff52;
917 add "down" [] 0xff54;
918 t, f;
921 let keyname k =
922 try Hashtbl.find xlatf k
923 with Not_found -> Printf.sprintf "%#x" k;
926 let namekey name =
927 try Hashtbl.find xlatt name
928 with Not_found ->
929 if String.length name = 1
930 then Char.code name.[0]
931 else int_of_string name;