Refactor
[llpp.git] / wsi.ml
blob05835d249126c9af48b01991926a0d03483f0b8a
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
64 and fs =
65 | NoFs
66 | Fs of (int * int * int * int)
69 let state =
70 { mink = max_int
71 ; maxk = min_int
72 ; keymap = [||]
73 ; fifo = Queue.create ()
74 ; seq = 0
75 ; protoatom = -1
76 ; deleatom = -1
77 ; idbase = -1
78 ; fullscreen = (fun _ -> ())
79 ; setwmname = (fun _ -> ())
80 ; sock = Unix.stdin
81 ; t = onot
82 ; x = -1
83 ; y = -1
84 ; w = -1
85 ; h = -1
86 ; fs = NoFs
87 ; stringatom = 31
91 let w8 s pos i = String.set s pos (Char.chr (i land 0xff));;
93 let w16 s pos i =
94 w8 s pos i;
95 w8 s (pos+1) (i lsr 8);
98 let w32 s pos i =
99 w16 s pos i;
100 w16 s (pos+2) (i lsr 16);
103 let r16 s pos =
104 let rb pos1 = Char.code (String.get s (pos + pos1)) in
105 (rb 0) lor ((rb 1) lsl 8)
108 let r16s s pos =
109 let i = r16 s pos in
110 i - ((i land 0x8000) lsl 1);
113 let r8 s pos = Char.code (String.get s pos);;
115 let r32 s pos =
116 let rb pos1 = Char.code (String.get s (pos + pos1)) in
117 let l = (rb 0) lor ((rb 1) lsl 8)
118 and u = (rb 2) lor ((rb 3) lsl 8) in
119 (u lsl 16) lor l
122 let error fmt = Printf.kprintf failwith fmt;;
124 let readstr sock n =
125 let s = String.create n in
126 let rec loop pos n =
127 let m = Unix.read sock s pos n in
128 if n != m
129 then (
130 ignore (Unix.select [sock] [] [] 0.01);
131 loop (pos + m) (n - m)
134 loop 0 n;
138 let sendstr1 s pos len sock =
139 vlog "%d => %S" state.seq s;
140 state.seq <- state.seq + 1;
141 let n = Unix.send sock s pos len [] in
142 if n != len
143 then error "send %d returned %d" len n;
146 let updkmap sock resp =
147 let syms = r8 resp 1 in
148 let len = r32 resp 4 in
149 let data =
150 if len > 0
151 then readstr sock (4*len)
152 else ""
154 let m = len / syms in
155 state.keymap <- Array.make_matrix
156 (state.maxk - state.mink) syms 0xffffff;
157 let rec loop i = if i = m then () else
158 let k = i*4*syms in
159 let rec loop2 k l = if l = syms then () else
160 let v = r32 data k in
161 state.keymap.(i).(l) <- v;
162 loop2 (k+4) (l+1)
164 loop2 k 0;
165 loop (i+1);
167 loop 0;
170 let sendwithrep sock s f =
171 Queue.push f state.fifo;
172 sendstr1 s 0 (String.length s) sock;
175 let padcatl ss =
176 let b = Buffer.create 16 in
177 List.iter (Buffer.add_string b) ss;
178 let bl = Buffer.length b in
179 let pl = bl land 3 in
180 if pl != 0
181 then (
182 let pad = "123" in
183 Buffer.add_substring b pad 0 (4 - pl);
185 Buffer.contents b;
188 let padcat s1 s2 = padcatl [s1; s2];;
190 let internreq name onlyifexists =
191 let s = "\016\000\000\000\000\000\000\000" in
192 let s = padcat s name in
193 w8 s 1 (if onlyifexists then 1 else 0);
194 w16 s 2 (String.length s / 4);
195 w16 s 4 (String.length name);
199 let sendintern sock s onlyifexists f =
200 let s = internreq s onlyifexists in
201 sendwithrep sock s f;
204 let createwindowreq wid parent x y w h bw mask =
205 let s = "\001\000\009\000wwwwppppxxyywwhhbwccvvvvmmmmeeee" in
206 w32 s 4 wid;
207 w32 s 8 parent;
208 w16 s 12 x;
209 w16 s 14 y;
210 w16 s 16 w;
211 w16 s 18 h;
212 w16 s 20 bw;
213 w16 s 22 1;
214 w32 s 24 0;
215 w32 s 28 0x800; (* eventmask *)
216 w32 s 32 mask;
220 let getgeometryreq wid =
221 let s = "\014u\002\000dddd" in
222 w32 s 4 wid;
226 let mapreq wid =
227 let s = "\008u\002\000wwww" in
228 w32 s 4 wid;
232 let getkeymapreq first count =
233 let s = "\101u\002\000fcuu" in
234 w8 s 4 first;
235 w8 s 5 count;
239 let changepropreq wid prop typ format props =
240 let s = "\018\000llwwwwppppttttfuuuLLLL" in
241 let s = padcat s props in
242 w16 s 2 (String.length s / 4);
243 w32 s 4 wid;
244 w32 s 8 prop;
245 w32 s 12 typ;
246 w8 s 16 format;
247 let ful = String.length props / (match format with
248 | 8 -> 1
249 | 16 -> 2
250 | 32 -> 4
251 | n -> error "no idea what %d means" n)
253 w32 s 20 ful;
257 let openfontreq fid name =
258 let s = "\045ullffffnnuu" in
259 let s = padcat s name in
260 w16 s 2 (String.length s / 4);
261 w32 s 4 fid;
262 w16 s 8 (String.length name);
266 let createglyphcursorreq fid cid cindex =
267 let s = "\094u\008\000ccccffffffffssmmrrggbbRRGGBB" in
268 w32 s 4 cid;
269 w32 s 8 fid;
270 w32 s 12 fid;
271 w16 s 16 cindex;
272 w16 s 18 (cindex+1);
273 w16 s 20 0;
274 w16 s 22 0;
275 w16 s 24 0;
276 w16 s 26 0xffff;
277 w16 s 28 0xffff;
278 w16 s 30 0xffff;
282 let mapwindowreq wid =
283 let s = "\008u\002\000wwww" in
284 w32 s 4 wid;
288 let changewindowattributesreq wid mask attrs =
289 let s = "\002ullwwwwmmmm" in
290 let s = padcat s attrs in
291 w16 s 2 (String.length s / 4);
292 w32 s 4 wid;
293 w32 s 8 mask;
297 let configurewindowreq wid mask values =
298 let s = "\012ullwwwwmmuu" in
299 let s = padcat s values in
300 w16 s 2 (String.length s / 4);
301 w32 s 4 wid;
302 w16 s 8 mask;
306 let s32 n =
307 let s = "1234" in
308 w32 s 0 n;
312 let clientmessage format seq wid typ data =
313 let s = "\033fsswwwwtttt" in
314 let s = padcat s data in
315 w8 s 1 format;
316 w16 s 2 seq;
317 w32 s 4 wid;
318 w32 s 8 typ;
322 let sendeventreq propagate destwid mask data =
323 let s = "\025p\011\000wwwwmmmm" in
324 let s = padcat s data in
325 w8 s 1 propagate;
326 w32 s 4 destwid;
327 w32 s 8 mask;
331 let getkeysym code mask =
332 let index = (mask land 1) lxor ((mask land 2) lsr 1) in
333 let keysym = state.keymap.(code-state.mink).(index) in
334 if index = 1 && keysym = 0
335 then state.keymap.(code-state.mink).(0)
336 else keysym
339 let rec readresp sock =
340 let resp = readstr sock 32 in
341 let opcode = r8 resp 0 in
342 match opcode land lnot 0x80 with
343 | 0 -> (* error *)
344 let s = resp in
345 let code = r8 s 1
346 and serial = r16 s 2
347 and resid = r32 resp 4
348 and min = r16 s 8
349 and maj = r8 s 10 in
350 error "code=%d serial=%d resid=%#x min=%d maj=%d\n%S"
351 code serial resid min maj resp;
353 | 1 -> (* response *)
354 let rep = Queue.pop state.fifo in
355 rep resp;
357 | 2 -> (* key press *)
358 if Array.length state.keymap > 0
359 then
360 let code = r8 resp 1 in
361 let mask = r16 resp 28 in
362 let keysym = getkeysym code mask in
363 vlog "keysym = %x %c" keysym (Char.unsafe_chr keysym);
364 state.t#key keysym mask;
366 | 3 -> (* key release *)
367 if Array.length state.keymap > 0
368 then
369 let code = r8 resp 1 in
370 let mask = r16 resp 28 in
371 let keysym = getkeysym code mask in
372 vlog "release keysym = %x %c mask %#x"
373 keysym (Char.unsafe_chr keysym) mask
375 | 4 -> (* buttonpress *)
376 let n = r8 resp 1
377 and x = r16s resp 24
378 and y = r16s resp 26
379 and m = r16 resp 28 in
380 state.t#mouse n true x y m;
381 vlog "press %d" n
383 | 5 -> (* buttonrelease *)
384 let n = r8 resp 1
385 and x = r16s resp 24
386 and y = r16s resp 26
387 and m = r16 resp 28 in
388 state.t#mouse n false x y m;
389 vlog "release %d %d %d" n x y
391 | 6 -> (* motion *)
392 let x = r16s resp 24 in
393 let y = r16s resp 26 in
394 let m = r16 resp 28 in
395 if m land 0x1f00 = 0
396 then state.t#pmotion x y
397 else state.t#motion x y;
398 vlog "move %dx%d => %d" x y m
400 | 7 -> (* enter *)
401 let x = r16s resp 24
402 and y = r16s resp 26 in
403 state.t#enter x y;
404 vlog "enter %d %d" x y
406 | 8 -> (* leave *)
407 state.t#leave
409 | 18 -> vlog "unmap";
411 | 19 -> (* map *)
412 vlog "map";
414 | 12 -> (* exposure *)
415 vlog "exposure";
416 state.t#expose
418 | 15 -> (* visibility *)
419 let vis = r8 resp 8 in
420 if vis != 2 then state.t#display;
421 vlog "visibility %d" vis;
423 | 34 -> (* mapping *)
424 state.keymap <- [||];
425 let s = getkeymapreq state.mink (state.maxk-state.mink-1) in
426 sendwithrep sock s (updkmap sock);
428 | 33 -> (* clientmessage *)
429 let atom = r32 resp 8 in
430 if atom = state.protoatom
431 then (
432 let atom = r32 resp 12 in
433 if atom = state.deleatom
434 then state.t#quit;
436 vlog "atom %#x" atom
438 | 21 -> (* reparent *)
439 vlog "reparent"
441 | 22 -> (* configure *)
442 let x = r16s resp 16
443 and y = r16s resp 18
444 and w = r16 resp 20
445 and h = r16 resp 22 in
446 vlog "configure cur [%d %d %d %d] conf [%d %d %d %d]"
447 state.x state.y state.w state.h
448 x y w h
450 if w != state.w || h != state.h
451 then (
452 state.t#reshape w h;
454 state.w <- w;
455 state.h <- h;
456 state.x <- x;
457 state.y <- y;
458 state.t#display
460 | n ->
461 dolog "event %d %S" n resp
464 let readresp sock =
465 let rec loop () =
466 readresp sock;
467 if hasdata sock then loop ();
469 loop ();
472 let sendstr s ?(pos=0) ?(len=String.length s) sock =
473 sendstr1 s pos len sock;
474 if hasdata sock then readresp sock;
477 let hexstr s =
478 let b = Buffer.create 16 in
479 String.iter (fun c ->
480 Buffer.add_string b (Printf.sprintf "%02x" (Char.code c))) s;
481 Buffer.contents b;
484 let reshape w h =
485 if state.fs = NoFs
486 then
487 let s = "wwuuhhuu" in
488 w32 s 0 w;
489 w32 s 4 h;
490 let s = configurewindowreq state.idbase 0x000c s in
491 sendstr s state.sock;
492 else state.fullscreen state.idbase
495 let setup sock screennum w h =
496 let s = readstr sock 2 in
497 let n = String.length s in
498 if n != 2
499 then error "failed to read X connection setup response n=%d" n;
500 match s.[0] with
501 | '\000' ->
502 let reasonlen = r8 s 1 in
503 let s = readstr sock 6 in
504 let maj = r16 s 0
505 and min = r16 s 2
506 and add = r16 s 4 in
507 let len = add*4 in
508 let data = readstr sock len in
509 let reason = String.sub data 0 reasonlen in
510 error "X connection failed maj=%d min=%d reason=%S"
511 maj min reason
513 | '\002' -> failwith "X connection setup failed: authentication required";
515 | '\001' ->
516 let s = readstr sock 38 in
517 let maj = r16 s 0
518 and min = r16 s 2
519 and add = r16 s 4
520 and idbase = r32 s 10
521 and idmask = r32 s 14
522 and vlen = r16 s 22
523 and screens = r8 s 26
524 and formats = r8 s 27
525 and minkk = r8 s 32
526 and maxkk = r8 s 33 in
527 let data = readstr sock (4*add-32) in
528 let vendor = String.sub data 0 vlen in
529 let pos = ((vlen+3) land lnot 3) + formats*8 in
531 if screennum >= screens
532 then error "invalid screen %d, max %d" screennum (screens-1);
534 let pos =
535 let s = data in
536 let rec findscreen n pos = if n = screennum then pos else
537 let pos =
538 let ndepths = r8 s (pos+39) in
539 let rec skipdepths n pos = if n = ndepths then pos else
540 let pos =
541 let nvisiuals = r16 s (pos+2) in
542 pos + nvisiuals*24 + 8
544 skipdepths (n+1) pos
546 skipdepths n (pos+40)
548 findscreen (n+1) pos
550 findscreen 0 pos
552 let root = r32 data pos in
553 let rootw = r16 data (pos+20)
554 and rooth = r16 data (pos+22) in
555 state.mink <- minkk;
556 state.maxk <- maxkk;
557 state.idbase <- idbase;
558 vlog "vendor = %S, maj=%d min=%d" vendor maj min;
559 vlog "screens = %d formats = %d" screens formats;
560 vlog "minkk = %d maxkk = %d" minkk maxkk;
561 vlog "idbase = %#x idmask = %#x" idbase idmask;
562 vlog "root=%#x %dx%d" root rootw rooth;
564 let mask = 0
565 + 0x00000001 (* KeyPress *)
566 (* + 0x00000002 *) (* KeyRelease *)
567 + 0x00000004 (* ButtonPress *)
568 + 0x00000008 (* ButtonRelease *)
569 + 0x00000010 (* EnterWindow *)
570 + 0x00000020 (* LeaveWindow *)
571 + 0x00000040 (* PointerMotion *)
572 (* + 0x00000080 *) (* PointerMotionHint *)
573 (* + 0x00000100 *) (* Button1Motion *)
574 (* + 0x00000200 *) (* Button2Motion *)
575 (* + 0x00000400 *) (* Button3Motion *)
576 (* + 0x00000800 *) (* Button4Motion *)
577 (* + 0x00001000 *) (* Button5Motion *)
578 + 0x00002000 (* ButtonMotion *)
579 (* + 0x00004000 *) (* KeymapState *)
580 + 0x00008000 (* Exposure *)
581 + 0x00010000 (* VisibilityChange *)
582 + 0x00020000 (* StructureNotify *)
583 (* + 0x00040000 *) (* ResizeRedirect *)
584 (* + 0x00080000 *) (* SubstructureNotify *)
585 (* + 0x00100000 *) (* SubstructureRedirect *)
586 (* + 0x00200000 *) (* FocusChange *)
587 (* + 0x00400000 *) (* PropertyChange *)
588 (* + 0x00800000 *) (* ColormapChange *)
589 (* + 0x01000000 *) (* OwnerGrabButton *)
591 let wid = state.idbase in
592 let s = createwindowreq wid root 0 0 w h 0 mask in
593 sendstr s sock;
595 let s = mapreq wid in
596 sendstr s sock;
598 let s = getkeymapreq state.mink (state.maxk-state.mink) in
599 sendwithrep sock s (updkmap sock);
601 sendintern sock "WM_PROTOCOLS" false (fun resp ->
602 state.protoatom <- r32 resp 8;
603 sendintern sock "WM_DELETE_WINDOW" false (fun resp ->
604 state.deleatom <- r32 resp 8;
605 let s = s32 state.deleatom in
606 let s = changepropreq wid state.protoatom 4 32 s in
607 sendstr s sock;
611 sendintern sock "WM_CLASS" false (fun resp ->
612 let atom = r32 resp 8 in
613 let llpp = "llpp\000llpp\000" in
614 let s = changepropreq wid atom 31 8 llpp in
615 sendstr s sock;
618 let s = openfontreq (wid+1) "cursor" in
619 sendstr s sock;
621 Array.iteri (fun i glyphindex ->
622 let s = createglyphcursorreq (wid+1) (wid+2+i) glyphindex in
623 sendstr s sock;
624 ) [|34;48;50;58;128;152|];
626 sendintern sock "UTF8_STRING" true (fun resp ->
627 let atom = r32 resp 8 in
628 if atom != 0
629 then state.stringatom <- atom;
632 let setwmname s =
633 let s = changepropreq state.idbase 39 state.stringatom 8 s in
634 sendstr s state.sock;
636 state.setwmname <- setwmname;
637 sendintern sock "_NET_WM_NAME" true (fun resp ->
638 let atom = r32 resp 8 in
639 if atom != 0
640 then state.setwmname <- (fun s ->
641 setwmname s;
642 let s = changepropreq state.idbase atom state.stringatom 8 s in
643 sendstr s state.sock;
647 state.fullscreen <- (fun wid ->
648 let s = "xxuuyyuuwwuuhhuu" in
649 match state.fs with
650 | NoFs ->
651 w32 s 0 0;
652 w32 s 4 0;
653 w32 s 8 rootw;
654 w32 s 12 rooth;
655 let s = configurewindowreq wid 0x000f s in
656 sendstr s state.sock;
657 state.fs <- Fs (state.x, state.y, state.w, state.h);
659 | Fs (x, y, w, h) ->
660 w32 s 0 x;
661 w32 s 4 y;
662 w32 s 8 w;
663 w32 s 12 h;
664 let s = configurewindowreq wid 0x000f s in
665 sendstr s state.sock;
666 state.fs <- NoFs;
669 sendintern sock "_NET_WM_STATE" true (fun resp ->
670 let nwmsatom = r32 resp 8 in
671 if nwmsatom != 0
672 then
673 sendintern sock "_NET_WM_STATE_FULLSCREEN" true (fun resp ->
674 let fsatom = r32 resp 8 in
675 if fsatom != 0
676 then
677 state.fullscreen <-
678 (fun wid ->
679 let data = String.make 20 '\000' in
680 let fs, f =
681 match state.fs with
682 | NoFs -> Fs (-1, -1, -1, -1), 1
683 | Fs _ -> NoFs, 0
685 w32 data 0 f;
686 w32 data 4 fsatom;
688 let cm = clientmessage 32 0 wid nwmsatom data in
689 let s = sendeventreq 0 root 0x180000 cm in
690 sendstr s sock;
691 state.fs <- fs;
695 let s = getgeometryreq wid in
696 let completed = ref false in
697 sendwithrep sock s (fun resp ->
698 glx wid;
699 let w = r16 resp 16
700 and h = r16 resp 18 in
701 state.w <- w;
702 state.h <- h;
703 completed := true;
705 let now = Unix.gettimeofday in
706 let deadline = now () +. 2.0 in
707 let rec readtillcompletion () =
708 let r, _, _ = Unix.select [sock] [] [] (deadline -. now ()) in
709 match r with
710 | [] -> readtillcompletion ()
711 | _ ->
712 readresp sock;
713 if not !completed
714 then readtillcompletion ()
716 readtillcompletion ();
718 | c ->
719 error "unknown conection setup response %d" (Char.code c)
722 let getauth haddr dnum =
723 let haddr =
724 if haddr = "localhost" || String.length haddr = 0
725 then
726 try Unix.gethostname ()
727 with exn ->
728 dolog "failed to resolve `%S': %s" haddr (Printexc.to_string exn);
729 haddr
730 else haddr
732 let readauth ic =
733 let input_string ic len =
734 let s = String.create len in
735 really_input ic s 0 len;
738 let r16 s =
739 let rb pos = Char.code (String.get s pos) in
740 (rb 1) lor ((rb 0) lsl 8)
742 let rec find () =
743 let rs () =
744 let s = input_string ic 2 in
745 let n = r16 s in
746 input_string ic n
748 let family = input_string ic 2 in
749 let addr = rs () in
750 let nums = rs () in
751 let num = int_of_string nums in
752 let name = rs () in
753 let data = rs () in
755 vlog "family %S addr %S(%S) num %d(%d) name %S data %S"
756 family addr haddr num dnum name data;
757 if addr = haddr && num = dnum
758 then name, data
759 else find ()
761 let name, data =
762 try find () with _ -> "", ""
764 close_in ic;
765 name, data;
767 let path =
768 try Sys.getenv "XAUTHORITY"
769 with Not_found ->
770 try Filename.concat (Sys.getenv "HOME") ".Xauthority"
771 with Not_found -> ""
773 let opt =
775 if String.length path = 0
776 then None
777 else Some (open_in_bin path)
778 with exn ->
779 dolog "failed to open X authority file `%S' : %s"
780 path
781 (Printexc.to_string exn);
782 None
784 match opt with
785 | None -> "", ""
786 | Some ic -> readauth ic
789 let init t w h osx =
790 let d =
791 try Sys.getenv "DISPLAY"
792 with exn ->
793 error "Could not get DISPLAY evironment variable: %s"
794 (Printexc.to_string exn)
796 let colonpos = String.index d ':' in
797 let host = String.sub d 0 colonpos in
798 let dispnum, screennum =
800 let dotpos = String.index_from d (colonpos + 1) '.' in
801 let disp = String.sub d (colonpos + 1) (dotpos - colonpos - 1) in
802 let screen = String.sub d (dotpos + 1) (String.length d - dotpos - 1) in
803 int_of_string disp, int_of_string screen
804 with Not_found ->
805 let disp = String.sub d (colonpos + 1) (String.length d - colonpos - 1) in
806 int_of_string disp, 0
808 let aname, adata = getauth host dispnum in
809 let fd =
810 if osx || String.length host = 0 || host = "unix"
811 then
812 let addr =
813 if osx
814 then Unix.ADDR_UNIX d
815 else Unix.ADDR_UNIX ("/tmp/.X11-unix/X" ^ string_of_int dispnum)
817 let fd = Unix.socket Unix.PF_UNIX Unix.SOCK_STREAM 0 in
818 Unix.connect fd addr;
820 else
821 let h = Unix.gethostbyname host in
822 let addr = h.Unix.h_addr_list.(0) in
823 let port = 6000 + dispnum in
824 let fd = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
825 Unix.connect fd (Unix.ADDR_INET (addr, port));
828 cloexec fd;
829 let s = "luMMmmnndduu" in
830 let s = padcat s aname in
831 let s = padcat s adata in
832 w16 s 2 11;
833 w16 s 4 0;
834 w16 s 6 (String.length aname);
835 w16 s 8 (String.length adata);
836 sendstr1 s 0 (String.length s) fd;
837 state.sock <- fd;
838 setup fd screennum w h;
839 state.t <- t;
840 fd, state.w, state.h;
843 let settitle s =
844 state.setwmname s;
847 let setcursor cursor =
848 let n =
849 match cursor with
850 | CURSOR_INHERIT -> -1
851 | CURSOR_INFO -> 3
852 | CURSOR_CYCLE -> 2
853 | CURSOR_CROSSHAIR -> 0
854 | CURSOR_TEXT -> 5
856 let s = s32 (if n = -1 then 0 else state.idbase+2+n) in
857 let s = changewindowattributesreq state.idbase (*cursor*)0x4000 s in
858 sendstr s state.sock;
861 let fullscreen () =
862 state.fullscreen state.idbase;
865 let metamask = 0x40;;
866 let altmask = 8;;
867 let shiftmask = 1;;
868 let ctrlmask = 4;;
870 let withalt mask = mask land altmask != 0;;
871 let withctrl mask = mask land ctrlmask != 0;;
872 let withshift mask = mask land shiftmask != 0;;
873 let withmeta mask = mask land metamask != 0;;
874 let withnone mask = mask land (altmask + ctrlmask + shiftmask + metamask) = 0;;
876 let xlatt, xlatf =
877 let t = Hashtbl.create 20
878 and f = Hashtbl.create 20 in
879 let add n nl k =
880 List.iter (fun s -> Hashtbl.add t s k) (n::nl);
881 Hashtbl.add f k n
883 let addc c =
884 let s = String.create 1 in
885 s.[0] <- c;
886 add s [] (Char.code c)
888 let addcr a b =
889 let an = Char.code a and bn = Char.code b in
890 for i = an to bn do addc (Char.chr i) done;
892 addcr '0' '9';
893 addcr 'a' 'z';
894 addcr 'A' 'Z';
895 String.iter addc "`~!@#$%^&*()-_=+\\|[{]};:,./<>?";
896 for i = 0 to 29 do add ("f" ^ string_of_int (i+1)) [] (0xffbe + i) done;
897 add "space" [] 0x20;
898 add "ret" ["return"; "enter"] 0xff0d;
899 add "tab" [] 0xff09;
900 add "left" [] 0xff51;
901 add "right" [] 0xff53;
902 add "home" [] 0xff50;
903 add "end" [] 0xff57;
904 add "ins" ["insert"] 0xff63;
905 add "del" ["delete"] 0xffff;
906 add "esc" ["escape"] 0xff1b;
907 add "pgup" ["pageup"] 0xff55;
908 add "pgdown" ["pagedown"] 0xff56;
909 add "backspace" [] 0xff08;
910 add "up" [] 0xff52;
911 add "down" [] 0xff54;
912 t, f;
915 let keyname k =
916 try Hashtbl.find xlatf k
917 with Not_found -> Printf.sprintf "%#x" k;
920 let namekey name =
921 try Hashtbl.find xlatt name
922 with Not_found ->
923 if String.length name = 1
924 then Char.code name.[0]
925 else int_of_string name;