Expand
[llpp.git] / wsi.ml
blob109d275722d4ee78d9c401c9ba39abffdcec280a
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
66 and fs =
67 | NoFs
68 | Fs of (int * int * int * int)
71 let state =
72 { mink = max_int
73 ; maxk = min_int
74 ; keymap = [||]
75 ; fifo = Queue.create ()
76 ; seq = 0
77 ; protoatom = -1
78 ; deleatom = -1
79 ; idbase = -1
80 ; fullscreen = (fun _ -> ())
81 ; setwmname = (fun _ -> ())
82 ; sock = Unix.stdin
83 ; t = onot
84 ; x = -1
85 ; y = -1
86 ; w = -1
87 ; h = -1
88 ; fs = NoFs
89 ; stringatom = 31
90 ; curcurs = CURSOR_INHERIT
94 let w8 s pos i = String.set s pos (Char.chr (i land 0xff));;
96 let w16 s pos i =
97 w8 s pos i;
98 w8 s (pos+1) (i lsr 8);
101 let w32 s pos i =
102 w16 s pos i;
103 w16 s (pos+2) (i lsr 16);
106 let r16 s pos =
107 let rb pos1 = Char.code (String.get s (pos + pos1)) in
108 (rb 0) lor ((rb 1) lsl 8)
111 let r16s s pos =
112 let i = r16 s pos in
113 i - ((i land 0x8000) lsl 1);
116 let r8 s pos = Char.code (String.get s pos);;
118 let r32 s pos =
119 let rb pos1 = Char.code (String.get s (pos + pos1)) in
120 let l = (rb 0) lor ((rb 1) lsl 8)
121 and u = (rb 2) lor ((rb 3) lsl 8) in
122 (u lsl 16) lor l
125 let error fmt = Printf.kprintf failwith fmt;;
127 let readstr sock n =
128 let s = String.create n in
129 let rec loop pos n =
130 let m = Unix.read sock s pos n in
131 if n != m
132 then (
133 ignore (Unix.select [sock] [] [] 0.01);
134 loop (pos + m) (n - m)
137 loop 0 n;
141 let sendstr1 s pos len sock =
142 vlog "%d => %S" state.seq s;
143 state.seq <- state.seq + 1;
144 let n = Unix.send sock s pos len [] in
145 if n != len
146 then error "send %d returned %d" len n;
149 let updkmap sock resp =
150 let syms = r8 resp 1 in
151 let len = r32 resp 4 in
152 let data =
153 if len > 0
154 then readstr sock (4*len)
155 else ""
157 let m = len / syms in
158 state.keymap <- Array.make_matrix
159 (state.maxk - state.mink) syms 0xffffff;
160 let rec loop i = if i = m then () else
161 let k = i*4*syms in
162 let rec loop2 k l = if l = syms then () else
163 let v = r32 data k in
164 state.keymap.(i).(l) <- v;
165 loop2 (k+4) (l+1)
167 loop2 k 0;
168 loop (i+1);
170 loop 0;
173 let sendwithrep sock s f =
174 Queue.push f state.fifo;
175 sendstr1 s 0 (String.length s) sock;
178 let padcatl ss =
179 let b = Buffer.create 16 in
180 List.iter (Buffer.add_string b) ss;
181 let bl = Buffer.length b in
182 let pl = bl land 3 in
183 if pl != 0
184 then (
185 let pad = "123" in
186 Buffer.add_substring b pad 0 (4 - pl);
188 Buffer.contents b;
191 let padcat s1 s2 = padcatl [s1; s2];;
193 let internreq name onlyifexists =
194 let s = "\016\000\000\000\000\000\000\000" in
195 let s = padcat s name in
196 w8 s 1 (if onlyifexists then 1 else 0);
197 w16 s 2 (String.length s / 4);
198 w16 s 4 (String.length name);
202 let sendintern sock s onlyifexists f =
203 let s = internreq s onlyifexists in
204 sendwithrep sock s f;
207 let createwindowreq wid parent x y w h bw mask =
208 let s = "\001\000\009\000wwwwppppxxyywwhhbwccvvvvmmmmeeee" in
209 w32 s 4 wid;
210 w32 s 8 parent;
211 w16 s 12 x;
212 w16 s 14 y;
213 w16 s 16 w;
214 w16 s 18 h;
215 w16 s 20 bw;
216 w16 s 22 1;
217 w32 s 24 0;
218 w32 s 28 0x800; (* eventmask *)
219 w32 s 32 mask;
223 let getgeometryreq wid =
224 let s = "\014u\002\000dddd" in
225 w32 s 4 wid;
229 let mapreq wid =
230 let s = "\008u\002\000wwww" in
231 w32 s 4 wid;
235 let getkeymapreq first count =
236 let s = "\101u\002\000fcuu" in
237 w8 s 4 first;
238 w8 s 5 count;
242 let changepropreq wid prop typ format props =
243 let s = "\018\000llwwwwppppttttfuuuLLLL" in
244 let s = padcat s props in
245 w16 s 2 (String.length s / 4);
246 w32 s 4 wid;
247 w32 s 8 prop;
248 w32 s 12 typ;
249 w8 s 16 format;
250 let ful = String.length props / (match format with
251 | 8 -> 1
252 | 16 -> 2
253 | 32 -> 4
254 | n -> error "no idea what %d means" n)
256 w32 s 20 ful;
260 let openfontreq fid name =
261 let s = "\045ullffffnnuu" in
262 let s = padcat s name in
263 w16 s 2 (String.length s / 4);
264 w32 s 4 fid;
265 w16 s 8 (String.length name);
269 let createglyphcursorreq fid cid cindex =
270 let s = "\094u\008\000ccccffffffffssmmrrggbbRRGGBB" in
271 w32 s 4 cid;
272 w32 s 8 fid;
273 w32 s 12 fid;
274 w16 s 16 cindex;
275 w16 s 18 (cindex+1);
276 w16 s 20 0;
277 w16 s 22 0;
278 w16 s 24 0;
279 w16 s 26 0xffff;
280 w16 s 28 0xffff;
281 w16 s 30 0xffff;
285 let mapwindowreq wid =
286 let s = "\008u\002\000wwww" in
287 w32 s 4 wid;
291 let changewindowattributesreq wid mask attrs =
292 let s = "\002ullwwwwmmmm" in
293 let s = padcat s attrs in
294 w16 s 2 (String.length s / 4);
295 w32 s 4 wid;
296 w32 s 8 mask;
300 let configurewindowreq wid mask values =
301 let s = "\012ullwwwwmmuu" in
302 let s = padcat s values in
303 w16 s 2 (String.length s / 4);
304 w32 s 4 wid;
305 w16 s 8 mask;
309 let s32 n =
310 let s = "1234" in
311 w32 s 0 n;
315 let clientmessage format seq wid typ data =
316 let s = "\033fsswwwwtttt" in
317 let s = padcat s data in
318 w8 s 1 format;
319 w16 s 2 seq;
320 w32 s 4 wid;
321 w32 s 8 typ;
325 let sendeventreq propagate destwid mask data =
326 let s = "\025p\011\000wwwwmmmm" in
327 let s = padcat s data in
328 w8 s 1 propagate;
329 w32 s 4 destwid;
330 w32 s 8 mask;
334 let getkeysym code mask =
335 let index = (mask land 1) lxor ((mask land 2) lsr 1) in
336 let keysym = state.keymap.(code-state.mink).(index) in
337 if index = 1 && keysym = 0
338 then state.keymap.(code-state.mink).(0)
339 else keysym
342 let rec readresp sock =
343 let resp = readstr sock 32 in
344 let opcode = r8 resp 0 in
345 match opcode land lnot 0x80 with
346 | 0 -> (* error *)
347 let s = resp in
348 let code = r8 s 1
349 and serial = r16 s 2
350 and resid = r32 resp 4
351 and min = r16 s 8
352 and maj = r8 s 10 in
353 error "code=%d serial=%d resid=%#x min=%d maj=%d\n%S"
354 code serial resid min maj resp;
356 | 1 -> (* response *)
357 let rep = Queue.pop state.fifo in
358 rep resp;
360 | 2 -> (* key press *)
361 if Array.length state.keymap > 0
362 then
363 let code = r8 resp 1 in
364 let mask = r16 resp 28 in
365 let keysym = getkeysym code mask in
366 vlog "keysym = %x %c" keysym (Char.unsafe_chr keysym);
367 state.t#key keysym mask;
369 | 3 -> (* key release *)
370 if Array.length state.keymap > 0
371 then
372 let code = r8 resp 1 in
373 let mask = r16 resp 28 in
374 let keysym = getkeysym code mask in
375 vlog "release keysym = %x %c mask %#x"
376 keysym (Char.unsafe_chr keysym) mask
378 | 4 -> (* buttonpress *)
379 let n = r8 resp 1
380 and x = r16s resp 24
381 and y = r16s resp 26
382 and m = r16 resp 28 in
383 state.t#mouse n true x y m;
384 vlog "press %d" n
386 | 5 -> (* buttonrelease *)
387 let n = r8 resp 1
388 and x = r16s resp 24
389 and y = r16s resp 26
390 and m = r16 resp 28 in
391 state.t#mouse n false x y m;
392 vlog "release %d %d %d" n x y
394 | 6 -> (* motion *)
395 let x = r16s resp 24 in
396 let y = r16s resp 26 in
397 let m = r16 resp 28 in
398 if m land 0x1f00 = 0
399 then state.t#pmotion x y
400 else state.t#motion x y;
401 vlog "move %dx%d => %d" x y m
403 | 7 -> (* enter *)
404 let x = r16s resp 24
405 and y = r16s resp 26 in
406 state.t#enter x y;
407 vlog "enter %d %d" x y
409 | 8 -> (* leave *)
410 state.t#leave
412 | 18 -> vlog "unmap";
414 | 19 -> (* map *)
415 vlog "map";
417 | 12 -> (* exposure *)
418 vlog "exposure";
419 state.t#expose
421 | 15 -> (* visibility *)
422 let vis = r8 resp 8 in
423 if vis != 2 then state.t#expose;
424 vlog "visibility %d" vis;
426 | 34 -> (* mapping *)
427 state.keymap <- [||];
428 let s = getkeymapreq state.mink (state.maxk-state.mink-1) in
429 sendwithrep sock s (updkmap sock);
431 | 33 -> (* clientmessage *)
432 let atom = r32 resp 8 in
433 if atom = state.protoatom
434 then (
435 let atom = r32 resp 12 in
436 if atom = state.deleatom
437 then state.t#quit;
439 vlog "atom %#x" atom
441 | 21 -> (* reparent *)
442 vlog "reparent"
444 | 22 -> (* configure *)
445 let x = r16s resp 16
446 and y = r16s resp 18
447 and w = r16 resp 20
448 and h = r16 resp 22 in
449 vlog "configure cur [%d %d %d %d] conf [%d %d %d %d]"
450 state.x state.y state.w state.h
451 x y w h
453 glxsync ();
454 if w != state.w || h != state.h
455 then (
456 state.t#reshape w h;
458 state.w <- w;
459 state.h <- h;
460 state.x <- x;
461 state.y <- y;
462 state.t#expose
464 | n ->
465 dolog "event %d %S" n resp
468 let readresp sock =
469 let rec loop () =
470 readresp sock;
471 if hasdata sock then loop ();
473 loop ();
476 let sendstr s ?(pos=0) ?(len=String.length s) sock =
477 sendstr1 s pos len sock;
478 if hasdata sock then readresp sock;
481 let hexstr s =
482 let b = Buffer.create 16 in
483 String.iter (fun c ->
484 Buffer.add_string b (Printf.sprintf "%02x" (Char.code c))) s;
485 Buffer.contents b;
488 let reshape w h =
489 if state.fs = NoFs
490 then
491 let s = "wwuuhhuu" in
492 w32 s 0 w;
493 w32 s 4 h;
494 let s = configurewindowreq state.idbase 0x000c s in
495 sendstr s state.sock;
496 else state.fullscreen state.idbase
499 let setup sock screennum w h =
500 let s = readstr sock 2 in
501 let n = String.length s in
502 if n != 2
503 then error "failed to read X connection setup response n=%d" n;
504 match s.[0] with
505 | '\000' ->
506 let reasonlen = r8 s 1 in
507 let s = readstr sock 6 in
508 let maj = r16 s 0
509 and min = r16 s 2
510 and add = r16 s 4 in
511 let len = add*4 in
512 let data = readstr sock len in
513 let reason = String.sub data 0 reasonlen in
514 error "X connection failed maj=%d min=%d reason=%S"
515 maj min reason
517 | '\002' -> failwith "X connection setup failed: authentication required";
519 | '\001' ->
520 let s = readstr sock 38 in
521 let maj = r16 s 0
522 and min = r16 s 2
523 and add = r16 s 4
524 and idbase = r32 s 10
525 and idmask = r32 s 14
526 and vlen = r16 s 22
527 and screens = r8 s 26
528 and formats = r8 s 27
529 and minkk = r8 s 32
530 and maxkk = r8 s 33 in
531 let data = readstr sock (4*add-32) in
532 let vendor = String.sub data 0 vlen in
533 let pos = ((vlen+3) land lnot 3) + formats*8 in
535 if screennum >= screens
536 then error "invalid screen %d, max %d" screennum (screens-1);
538 let pos =
539 let s = data in
540 let rec findscreen n pos = if n = screennum then pos else
541 let pos =
542 let ndepths = r8 s (pos+39) in
543 let rec skipdepths n pos = if n = ndepths then pos else
544 let pos =
545 let nvisiuals = r16 s (pos+2) in
546 pos + nvisiuals*24 + 8
548 skipdepths (n+1) pos
550 skipdepths n (pos+40)
552 findscreen (n+1) pos
554 findscreen 0 pos
556 let root = r32 data pos in
557 let rootw = r16 data (pos+20)
558 and rooth = r16 data (pos+22) in
559 state.mink <- minkk;
560 state.maxk <- maxkk;
561 state.idbase <- idbase;
562 vlog "vendor = %S, maj=%d min=%d" vendor maj min;
563 vlog "screens = %d formats = %d" screens formats;
564 vlog "minkk = %d maxkk = %d" minkk maxkk;
565 vlog "idbase = %#x idmask = %#x" idbase idmask;
566 vlog "root=%#x %dx%d" root rootw rooth;
568 let mask = 0
569 + 0x00000001 (* KeyPress *)
570 (* + 0x00000002 *) (* KeyRelease *)
571 + 0x00000004 (* ButtonPress *)
572 + 0x00000008 (* ButtonRelease *)
573 + 0x00000010 (* EnterWindow *)
574 + 0x00000020 (* LeaveWindow *)
575 + 0x00000040 (* PointerMotion *)
576 (* + 0x00000080 *) (* PointerMotionHint *)
577 (* + 0x00000100 *) (* Button1Motion *)
578 (* + 0x00000200 *) (* Button2Motion *)
579 (* + 0x00000400 *) (* Button3Motion *)
580 (* + 0x00000800 *) (* Button4Motion *)
581 (* + 0x00001000 *) (* Button5Motion *)
582 + 0x00002000 (* ButtonMotion *)
583 (* + 0x00004000 *) (* KeymapState *)
584 + 0x00008000 (* Exposure *)
585 + 0x00010000 (* VisibilityChange *)
586 + 0x00020000 (* StructureNotify *)
587 (* + 0x00040000 *) (* ResizeRedirect *)
588 (* + 0x00080000 *) (* SubstructureNotify *)
589 (* + 0x00100000 *) (* SubstructureRedirect *)
590 (* + 0x00200000 *) (* FocusChange *)
591 (* + 0x00400000 *) (* PropertyChange *)
592 (* + 0x00800000 *) (* ColormapChange *)
593 (* + 0x01000000 *) (* OwnerGrabButton *)
595 let wid = state.idbase in
596 let s = createwindowreq wid root 0 0 w h 0 mask in
597 sendstr s sock;
599 let s = mapreq wid in
600 sendstr s sock;
602 let s = getkeymapreq state.mink (state.maxk-state.mink) in
603 sendwithrep sock s (updkmap sock);
605 sendintern sock "WM_PROTOCOLS" false (fun resp ->
606 state.protoatom <- r32 resp 8;
607 sendintern sock "WM_DELETE_WINDOW" false (fun resp ->
608 state.deleatom <- r32 resp 8;
609 let s = s32 state.deleatom in
610 let s = changepropreq wid state.protoatom 4 32 s in
611 sendstr s sock;
615 sendintern sock "WM_CLASS" false (fun resp ->
616 let atom = r32 resp 8 in
617 let llpp = "llpp\000llpp\000" in
618 let s = changepropreq wid atom 31 8 llpp in
619 sendstr s sock;
622 let s = openfontreq (wid+1) "cursor" in
623 sendstr s sock;
625 Array.iteri (fun i glyphindex ->
626 let s = createglyphcursorreq (wid+1) (wid+2+i) glyphindex in
627 sendstr s sock;
628 ) [|34;48;50;58;128;152|];
630 sendintern sock "UTF8_STRING" true (fun resp ->
631 let atom = r32 resp 8 in
632 if atom != 0
633 then state.stringatom <- atom;
636 let setwmname s =
637 let s = changepropreq state.idbase 39 state.stringatom 8 s in
638 sendstr s state.sock;
640 state.setwmname <- setwmname;
641 sendintern sock "_NET_WM_NAME" true (fun resp ->
642 let atom = r32 resp 8 in
643 if atom != 0
644 then state.setwmname <- (fun s ->
645 setwmname s;
646 let s = changepropreq state.idbase atom state.stringatom 8 s in
647 sendstr s state.sock;
651 state.fullscreen <- (fun wid ->
652 let s = "xxuuyyuuwwuuhhuu" in
653 match state.fs with
654 | NoFs ->
655 w32 s 0 0;
656 w32 s 4 0;
657 w32 s 8 rootw;
658 w32 s 12 rooth;
659 let s = configurewindowreq wid 0x000f s in
660 sendstr s state.sock;
661 state.fs <- Fs (state.x, state.y, state.w, state.h);
663 | Fs (x, y, w, h) ->
664 w32 s 0 x;
665 w32 s 4 y;
666 w32 s 8 w;
667 w32 s 12 h;
668 let s = configurewindowreq wid 0x000f s in
669 sendstr s state.sock;
670 state.fs <- NoFs;
673 sendintern sock "_NET_WM_STATE" true (fun resp ->
674 let nwmsatom = r32 resp 8 in
675 if nwmsatom != 0
676 then
677 sendintern sock "_NET_WM_STATE_FULLSCREEN" true (fun resp ->
678 let fsatom = r32 resp 8 in
679 if fsatom != 0
680 then
681 state.fullscreen <-
682 (fun wid ->
683 let data = String.make 20 '\000' in
684 let fs, f =
685 match state.fs with
686 | NoFs -> Fs (-1, -1, -1, -1), 1
687 | Fs _ -> NoFs, 0
689 w32 data 0 f;
690 w32 data 4 fsatom;
692 let cm = clientmessage 32 0 wid nwmsatom data in
693 let s = sendeventreq 0 root 0x180000 cm in
694 sendstr s sock;
695 state.fs <- fs;
699 let s = getgeometryreq wid in
700 let completed = ref false in
701 sendwithrep sock s (fun resp ->
702 glx wid;
703 let w = r16 resp 16
704 and h = r16 resp 18 in
705 state.w <- w;
706 state.h <- h;
707 completed := true;
709 let now = Unix.gettimeofday in
710 let deadline = now () +. 2.0 in
711 let rec readtillcompletion () =
712 let r, _, _ = Unix.select [sock] [] [] (deadline -. now ()) in
713 match r with
714 | [] -> readtillcompletion ()
715 | _ ->
716 readresp sock;
717 if not !completed
718 then readtillcompletion ()
720 readtillcompletion ();
722 | c ->
723 error "unknown conection setup response %d" (Char.code c)
726 let getauth haddr dnum =
727 let haddr =
728 if haddr = "localhost" || String.length haddr = 0
729 then
730 try Unix.gethostname ()
731 with exn ->
732 dolog "failed to resolve `%S': %s" haddr (Printexc.to_string exn);
733 haddr
734 else haddr
736 let readauth ic =
737 let input_string ic len =
738 let s = String.create len in
739 really_input ic s 0 len;
742 let r16 s =
743 let rb pos = Char.code (String.get s pos) in
744 (rb 1) lor ((rb 0) lsl 8)
746 let rec find () =
747 let rs () =
748 let s = input_string ic 2 in
749 let n = r16 s in
750 input_string ic n
752 let family = input_string ic 2 in
753 let addr = rs () in
754 let nums = rs () in
755 let num = int_of_string nums in
756 let name = rs () in
757 let data = rs () in
759 vlog "family %S addr %S(%S) num %d(%d) name %S data %S"
760 family addr haddr num dnum name data;
761 if addr = haddr && num = dnum
762 then name, data
763 else find ()
765 let name, data =
766 try find () with _ -> "", ""
768 close_in ic;
769 name, data;
771 let path =
772 try Sys.getenv "XAUTHORITY"
773 with Not_found ->
774 try Filename.concat (Sys.getenv "HOME") ".Xauthority"
775 with Not_found -> ""
777 let opt =
779 if String.length path = 0
780 then None
781 else Some (open_in_bin path)
782 with exn ->
783 if Sys.file_exists path
784 then
785 dolog "failed to open X authority file `%S' : %s"
786 path (Printexc.to_string exn);
787 None
789 match opt with
790 | None -> "", ""
791 | Some ic -> readauth ic
794 let init t w h osx =
795 let d =
796 try Sys.getenv "DISPLAY"
797 with exn ->
798 error "Could not get DISPLAY evironment variable: %s"
799 (Printexc.to_string exn)
801 let colonpos = String.index d ':' in
802 let host = String.sub d 0 colonpos in
803 let dispnum, screennum =
805 let dotpos = String.index_from d (colonpos + 1) '.' in
806 let disp = String.sub d (colonpos + 1) (dotpos - colonpos - 1) in
807 let screen = String.sub d (dotpos + 1) (String.length d - dotpos - 1) in
808 int_of_string disp, int_of_string screen
809 with Not_found ->
810 let disp = String.sub d (colonpos + 1) (String.length d - colonpos - 1) in
811 int_of_string disp, 0
813 let aname, adata = getauth host dispnum in
814 let fd =
815 if osx || String.length host = 0 || host = "unix"
816 then
817 let addr =
818 if osx
819 then Unix.ADDR_UNIX d
820 else Unix.ADDR_UNIX ("/tmp/.X11-unix/X" ^ string_of_int dispnum)
822 let fd = Unix.socket Unix.PF_UNIX Unix.SOCK_STREAM 0 in
823 Unix.connect fd addr;
825 else
826 let h = Unix.gethostbyname host in
827 let addr = h.Unix.h_addr_list.(0) in
828 let port = 6000 + dispnum in
829 let fd = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
830 Unix.connect fd (Unix.ADDR_INET (addr, port));
833 cloexec fd;
834 let s = "luMMmmnndduu" in
835 let s = padcat s aname in
836 let s = padcat s adata in
837 w16 s 2 11;
838 w16 s 4 0;
839 w16 s 6 (String.length aname);
840 w16 s 8 (String.length adata);
841 sendstr1 s 0 (String.length s) fd;
842 state.sock <- fd;
843 setup fd screennum w h;
844 state.t <- t;
845 fd, state.w, state.h;
848 let settitle s =
849 state.setwmname s;
852 let setcursor cursor =
853 if cursor != state.curcurs
854 then
855 let n =
856 match cursor with
857 | CURSOR_INHERIT -> -1
858 | CURSOR_INFO -> 3
859 | CURSOR_CYCLE -> 2
860 | CURSOR_CROSSHAIR -> 0
861 | CURSOR_TEXT -> 5
863 let s = s32 (if n = -1 then 0 else state.idbase+2+n) in
864 let s = changewindowattributesreq state.idbase (*cursor*)0x4000 s in
865 sendstr s state.sock;
866 state.curcurs <- cursor;
869 let fullscreen () =
870 state.fullscreen state.idbase;
873 let metamask = 0x40;;
874 let altmask = 8;;
875 let shiftmask = 1;;
876 let ctrlmask = 4;;
878 let withalt mask = mask land altmask != 0;;
879 let withctrl mask = mask land ctrlmask != 0;;
880 let withshift mask = mask land shiftmask != 0;;
881 let withmeta mask = mask land metamask != 0;;
882 let withnone mask = mask land (altmask + ctrlmask + shiftmask + metamask) = 0;;
884 let xlatt, xlatf =
885 let t = Hashtbl.create 20
886 and f = Hashtbl.create 20 in
887 let add n nl k =
888 List.iter (fun s -> Hashtbl.add t s k) (n::nl);
889 Hashtbl.add f k n
891 let addc c =
892 let s = String.create 1 in
893 s.[0] <- c;
894 add s [] (Char.code c)
896 let addcr a b =
897 let an = Char.code a and bn = Char.code b in
898 for i = an to bn do addc (Char.chr i) done;
900 addcr '0' '9';
901 addcr 'a' 'z';
902 addcr 'A' 'Z';
903 String.iter addc "`~!@#$%^&*()-_=+\\|[{]};:,./<>?";
904 for i = 0 to 29 do add ("f" ^ string_of_int (i+1)) [] (0xffbe + i) done;
905 add "space" [] 0x20;
906 add "ret" ["return"; "enter"] 0xff0d;
907 add "tab" [] 0xff09;
908 add "left" [] 0xff51;
909 add "right" [] 0xff53;
910 add "home" [] 0xff50;
911 add "end" [] 0xff57;
912 add "ins" ["insert"] 0xff63;
913 add "del" ["delete"] 0xffff;
914 add "esc" ["escape"] 0xff1b;
915 add "pgup" ["pageup"] 0xff55;
916 add "pgdown" ["pagedown"] 0xff56;
917 add "backspace" [] 0xff08;
918 add "up" [] 0xff52;
919 add "down" [] 0xff54;
920 t, f;
923 let keyname k =
924 try Hashtbl.find xlatf k
925 with Not_found -> Printf.sprintf "%#x" k;
928 let namekey name =
929 try Hashtbl.find xlatt name
930 with Not_found ->
931 if String.length name = 1
932 then Char.code name.[0]
933 else int_of_string name;