ISO_Level3_Shift support
[llpp.git] / wsi.ml
blobe315e28c9b7a47f6a5eac3a222ba556753009b4a
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 il3s : int
67 and fs =
68 | NoFs
69 | Fs of (int * int * int * int)
72 let state =
73 { mink = max_int
74 ; maxk = min_int
75 ; keymap = [||]
76 ; fifo = Queue.create ()
77 ; seq = 0
78 ; protoatom = -1
79 ; deleatom = -1
80 ; idbase = -1
81 ; fullscreen = (fun _ -> ())
82 ; setwmname = (fun _ -> ())
83 ; sock = Unix.stdin
84 ; t = onot
85 ; x = -1
86 ; y = -1
87 ; w = -1
88 ; h = -1
89 ; fs = NoFs
90 ; stringatom = 31
91 ; curcurs = CURSOR_INHERIT
92 ; il3s = 0
96 let w8 s pos i = String.set s pos (Char.chr (i land 0xff));;
98 let w16 s pos i =
99 w8 s pos i;
100 w8 s (pos+1) (i lsr 8);
103 let w32 s pos i =
104 w16 s pos i;
105 w16 s (pos+2) (i lsr 16);
108 let r16 s pos =
109 let rb pos1 = Char.code (String.get s (pos + pos1)) in
110 (rb 0) lor ((rb 1) lsl 8)
113 let r16s s pos =
114 let i = r16 s pos in
115 i - ((i land 0x8000) lsl 1);
118 let r8 s pos = Char.code (String.get s pos);;
120 let r32 s pos =
121 let rb pos1 = Char.code (String.get s (pos + pos1)) in
122 let l = (rb 0) lor ((rb 1) lsl 8)
123 and u = (rb 2) lor ((rb 3) lsl 8) in
124 (u lsl 16) lor l
127 let exntos = function
128 | Unix.Unix_error (e, s, a) -> Printf.sprintf "%s(%s):%s(%d)"
129 s a (Unix.error_message e) (Obj.magic e)
130 | exn -> Printexc.to_string exn;
133 let error fmt = Printf.kprintf failwith fmt;;
135 let readstr sock n =
136 let s = String.create n in
137 let rec loop pos n =
138 let m = Unix.read sock s pos n in
139 if m = 0
140 then state.t#quit;
141 if n != m
142 then (
143 ignore (Unix.select [sock] [] [] 0.01);
144 loop (pos + m) (n - m)
147 loop 0 n;
151 let sendstr1 s pos len sock =
152 vlog "%d => %S" state.seq s;
153 state.seq <- state.seq + 1;
154 let n = Unix.send sock s pos len [] in
155 if n != len
156 then error "send %d returned %d" len n;
159 let updkmap sock resp =
160 let syms = r8 resp 1 in
161 let len = r32 resp 4 in
162 let data =
163 if len > 0
164 then readstr sock (4*len)
165 else ""
167 let m = len / syms in
168 state.keymap <- Array.make_matrix
169 (state.maxk - state.mink) syms 0xffffff;
170 let rec loop i = if i = m then () else
171 let k = i*4*syms in
172 let rec loop2 k l = if l = syms then () else
173 let v = r32 data k in
174 state.keymap.(i).(l) <- v;
175 loop2 (k+4) (l+1)
177 loop2 k 0;
178 loop (i+1);
180 loop 0;
183 let sendwithrep sock s f =
184 Queue.push f state.fifo;
185 sendstr1 s 0 (String.length s) sock;
188 let padcatl ss =
189 let b = Buffer.create 16 in
190 List.iter (Buffer.add_string b) ss;
191 let bl = Buffer.length b in
192 let pl = bl land 3 in
193 if pl != 0
194 then (
195 let pad = "123" in
196 Buffer.add_substring b pad 0 (4 - pl);
198 Buffer.contents b;
201 let padcat s1 s2 = padcatl [s1; s2];;
203 let internreq name onlyifexists =
204 let s = "\016\000\000\000\000\000\000\000" in
205 let s = padcat s name in
206 if onlyifexists then w8 s 1 1;
207 w16 s 2 (String.length s / 4);
208 w16 s 4 (String.length name);
212 let sendintern sock s onlyifexists f =
213 let s = internreq s onlyifexists in
214 sendwithrep sock s f;
217 let createwindowreq wid parent x y w h bw mask =
218 let s = "\001\000\009\000wwwwppppxxyywwhhbwccvvvvmmmmeeee" in
219 w32 s 4 wid;
220 w32 s 8 parent;
221 w16 s 12 x;
222 w16 s 14 y;
223 w16 s 16 w;
224 w16 s 18 h;
225 w16 s 20 bw;
226 w16 s 22 1;
227 w32 s 24 0;
228 w32 s 28 0x800; (* eventmask *)
229 w32 s 32 mask;
233 let getgeometryreq wid =
234 let s = "\014u\002\000dddd" in
235 w32 s 4 wid;
239 let mapreq wid =
240 let s = "\008u\002\000wwww" in
241 w32 s 4 wid;
245 let getkeymapreq first count =
246 let s = "\101u\002\000fcuu" in
247 w8 s 4 first;
248 w8 s 5 count;
252 let changepropreq wid prop typ format props =
253 let s = "\018\000llwwwwppppttttfuuuLLLL" in
254 let s = padcat s props in
255 w16 s 2 (String.length s / 4);
256 w32 s 4 wid;
257 w32 s 8 prop;
258 w32 s 12 typ;
259 w8 s 16 format;
260 let ful = String.length props / (match format with
261 | 8 -> 1
262 | 16 -> 2
263 | 32 -> 4
264 | n -> error "no idea what %d means" n)
266 w32 s 20 ful;
270 let openfontreq fid name =
271 let s = "\045ullffffnnuu" in
272 let s = padcat s name in
273 w16 s 2 (String.length s / 4);
274 w32 s 4 fid;
275 w16 s 8 (String.length name);
279 let createglyphcursorreq fid cid cindex =
280 let s = "\094u\008\000ccccffffffffssmmrrggbbRRGGBB" in
281 w32 s 4 cid;
282 w32 s 8 fid;
283 w32 s 12 fid;
284 w16 s 16 cindex;
285 w16 s 18 (cindex+1);
286 w16 s 20 0;
287 w16 s 22 0;
288 w16 s 24 0;
289 w16 s 26 0xffff;
290 w16 s 28 0xffff;
291 w16 s 30 0xffff;
295 let changewindowattributesreq wid mask attrs =
296 let s = "\002ullwwwwmmmm" in
297 let s = padcat s attrs in
298 w16 s 2 (String.length s / 4);
299 w32 s 4 wid;
300 w32 s 8 mask;
304 let configurewindowreq wid mask values =
305 let s = "\012ullwwwwmmuu" in
306 let s = padcat s values in
307 w16 s 2 (String.length s / 4);
308 w32 s 4 wid;
309 w16 s 8 mask;
313 let s32 n =
314 let s = "1234" in
315 w32 s 0 n;
319 let clientmessage format seq wid typ data =
320 let s = "\033fsswwwwtttt" in
321 let s = padcat s data in
322 w8 s 1 format;
323 w16 s 2 seq;
324 w32 s 4 wid;
325 w32 s 8 typ;
329 let sendeventreq propagate destwid mask data =
330 let s = "\025p\011\000wwwwmmmm" in
331 let s = padcat s data in
332 w8 s 1 propagate;
333 w32 s 4 destwid;
334 w32 s 8 mask;
338 let getkeysym code mask =
339 let index = (mask land 1) lxor ((mask land 2) lsr 1) in
340 let keysym = state.keymap.(code-state.mink).(index) in
341 if keysym = 0xfe03
342 then (
343 state.il3s <- state.il3s lxor 4;
346 else (
347 let index = index + state.il3s in
348 let keysym = state.keymap.(code-state.mink).(index) in
349 if index = 1 && keysym = 0
350 then state.keymap.(code-state.mink).(0)
351 else keysym
355 let readresp sock =
356 let resp = readstr sock 32 in
357 let opcode = r8 resp 0 in
358 match opcode land lnot 0x80 with
359 | 0 -> (* error *)
360 let s = resp in
361 let code = r8 s 1
362 and serial = r16 s 2
363 and resid = r32 resp 4
364 and min = r16 s 8
365 and maj = r8 s 10 in
366 error "code=%d serial=%d resid=%#x min=%d maj=%d\n%S"
367 code serial resid min maj resp;
369 | 1 -> (* response *)
370 let rep = Queue.pop state.fifo in
371 rep resp;
373 | 2 -> (* key press *)
374 if Array.length state.keymap > 0
375 then
376 let code = r8 resp 1 in
377 let mask = r16 resp 28 in
378 let keysym = getkeysym code mask in
379 vlog "keysym = %x %c mask %#x code %d"
380 keysym (Char.unsafe_chr keysym) mask code;
381 state.t#key keysym mask;
383 | 3 -> (* key release *)
384 if Array.length state.keymap > 0
385 then
386 let code = r8 resp 1 in
387 let mask = r16 resp 28 in
388 let keysym = getkeysym code mask in
389 vlog "release keysym = %x %c mask %#x code %#d"
390 keysym (Char.unsafe_chr keysym) mask code
392 | 4 -> (* buttonpress *)
393 let n = r8 resp 1
394 and x = r16s resp 24
395 and y = r16s resp 26
396 and m = r16 resp 28 in
397 state.t#mouse n true x y m;
398 vlog "press %d" n
400 | 5 -> (* buttonrelease *)
401 let n = r8 resp 1
402 and x = r16s resp 24
403 and y = r16s resp 26
404 and m = r16 resp 28 in
405 state.t#mouse n false x y m;
406 vlog "release %d %d %d" n x y
408 | 6 -> (* motion *)
409 let x = r16s resp 24 in
410 let y = r16s resp 26 in
411 let m = r16 resp 28 in
412 if m land 0x1f00 = 0
413 then state.t#pmotion x y
414 else state.t#motion x y;
415 vlog "move %dx%d => %d" x y m
417 | 7 -> (* enter *)
418 let x = r16s resp 24
419 and y = r16s resp 26 in
420 state.t#enter x y;
421 vlog "enter %d %d" x y
423 | 8 -> (* leave *)
424 state.t#leave
426 | 18 -> vlog "unmap";
428 | 19 -> (* map *)
429 vlog "map";
431 | 12 -> (* exposure *)
432 vlog "exposure";
433 state.t#expose
435 | 15 -> (* visibility *)
436 let vis = r8 resp 8 in
437 if vis != 2 then state.t#expose;
438 vlog "visibility %d" vis;
440 | 34 -> (* mapping *)
441 state.keymap <- [||];
442 let s = getkeymapreq state.mink (state.maxk-state.mink-1) in
443 sendwithrep sock s (updkmap sock);
445 | 33 -> (* clientmessage *)
446 let atom = r32 resp 8 in
447 if atom = state.protoatom
448 then (
449 let atom = r32 resp 12 in
450 if atom = state.deleatom
451 then state.t#quit;
453 vlog "atom %#x" atom
455 | 21 -> (* reparent *)
456 vlog "reparent"
458 | 22 -> (* configure *)
459 let x = r16s resp 16
460 and y = r16s resp 18
461 and w = r16 resp 20
462 and h = r16 resp 22 in
463 vlog "configure cur [%d %d %d %d] conf [%d %d %d %d]"
464 state.x state.y state.w state.h
465 x y w h
467 glxsync ();
468 if w != state.w || h != state.h
469 then (
470 state.t#reshape w h;
472 state.w <- w;
473 state.h <- h;
474 state.x <- x;
475 state.y <- y;
476 state.t#expose
478 | n ->
479 dolog "event %d %S" n resp
482 let readresp sock =
483 let rec loop () =
484 readresp sock;
485 if hasdata sock then loop ();
487 loop ();
490 let sendstr s ?(pos=0) ?(len=String.length s) sock =
491 sendstr1 s pos len sock;
492 if hasdata sock then readresp sock;
495 let reshape w h =
496 if state.fs = NoFs
497 then
498 let s = "wwuuhhuu" in
499 w32 s 0 w;
500 w32 s 4 h;
501 let s = configurewindowreq state.idbase 0x000c s in
502 sendstr s state.sock;
503 else state.fullscreen state.idbase
506 let syncsendwithrep sock secstowait s f =
507 let completed = ref false in
508 sendwithrep sock s (fun resp -> f resp; completed := true);
509 let now = Unix.gettimeofday in
510 let deadline = now () +. secstowait in
511 let rec readtillcompletion () =
512 let r, _, _ = Unix.select [sock] [] [] (deadline -. now ()) in
513 match r with
514 | [] -> error "didn't get X response in %f seconds, aborting" secstowait
515 | _ ->
516 readresp sock;
517 if not !completed
518 then readtillcompletion ()
520 readtillcompletion ();
523 let syncsendintern sock secstowait s onlyifexists f =
524 let s = internreq s onlyifexists in
525 syncsendwithrep sock secstowait s f;
528 let setup sock screennum w h =
529 let s = readstr sock 2 in
530 let n = String.length s in
531 if n != 2
532 then error "failed to read X connection setup response n=%d" n;
533 match s.[0] with
534 | '\000' ->
535 let reasonlen = r8 s 1 in
536 let s = readstr sock 6 in
537 let maj = r16 s 0
538 and min = r16 s 2
539 and add = r16 s 4 in
540 let len = add*4 in
541 let data = readstr sock len in
542 let reason = String.sub data 0 reasonlen in
543 error "X connection failed maj=%d min=%d reason=%S"
544 maj min reason
546 | '\002' -> failwith "X connection setup failed: authentication required";
548 | '\001' ->
549 let s = readstr sock 38 in
550 let maj = r16 s 0
551 and min = r16 s 2
552 and add = r16 s 4
553 and idbase = r32 s 10
554 and idmask = r32 s 14
555 and vlen = r16 s 22
556 and screens = r8 s 26
557 and formats = r8 s 27
558 and minkk = r8 s 32
559 and maxkk = r8 s 33 in
560 let data = readstr sock (4*add-32) in
561 let vendor = String.sub data 0 vlen in
562 let pos = ((vlen+3) land lnot 3) + formats*8 in
564 if screennum >= screens
565 then error "invalid screen %d, max %d" screennum (screens-1);
567 let pos =
568 let s = data in
569 let rec findscreen n pos = if n = screennum then pos else
570 let pos =
571 let ndepths = r8 s (pos+39) in
572 let rec skipdepths n pos = if n = ndepths then pos else
573 let pos =
574 let nvisiuals = r16 s (pos+2) in
575 pos + nvisiuals*24 + 8
577 skipdepths (n+1) pos
579 skipdepths n (pos+40)
581 findscreen (n+1) pos
583 findscreen 0 pos
585 let root = r32 data pos in
586 let rootw = r16 data (pos+20)
587 and rooth = r16 data (pos+22) in
588 state.mink <- minkk;
589 state.maxk <- maxkk;
590 state.idbase <- idbase;
591 vlog "vendor = %S, maj=%d min=%d" vendor maj min;
592 vlog "screens = %d formats = %d" screens formats;
593 vlog "minkk = %d maxkk = %d" minkk maxkk;
594 vlog "idbase = %#x idmask = %#x" idbase idmask;
595 vlog "root=%#x %dx%d" root rootw rooth;
597 let mask = 0
598 + 0x00000001 (* KeyPress *)
599 + 0x00000002 (* KeyRelease *)
600 + 0x00000004 (* ButtonPress *)
601 + 0x00000008 (* ButtonRelease *)
602 + 0x00000010 (* EnterWindow *)
603 + 0x00000020 (* LeaveWindow *)
604 + 0x00000040 (* PointerMotion *)
605 (* + 0x00000080 *) (* PointerMotionHint *)
606 (* + 0x00000100 *) (* Button1Motion *)
607 (* + 0x00000200 *) (* Button2Motion *)
608 (* + 0x00000400 *) (* Button3Motion *)
609 (* + 0x00000800 *) (* Button4Motion *)
610 (* + 0x00001000 *) (* Button5Motion *)
611 + 0x00002000 (* ButtonMotion *)
612 (* + 0x00004000 *) (* KeymapState *)
613 + 0x00008000 (* Exposure *)
614 + 0x00010000 (* VisibilityChange *)
615 + 0x00020000 (* StructureNotify *)
616 (* + 0x00040000 *) (* ResizeRedirect *)
617 (* + 0x00080000 *) (* SubstructureNotify *)
618 (* + 0x00100000 *) (* SubstructureRedirect *)
619 (* + 0x00200000 *) (* FocusChange *)
620 (* + 0x00400000 *) (* PropertyChange *)
621 (* + 0x00800000 *) (* ColormapChange *)
622 (* + 0x01000000 *) (* OwnerGrabButton *)
624 let wid = state.idbase in
625 let s = createwindowreq wid root 0 0 w h 0 mask in
626 sendstr s sock;
628 sendintern sock "WM_PROTOCOLS" false (fun resp ->
629 state.protoatom <- r32 resp 8;
630 sendintern sock "WM_DELETE_WINDOW" false (fun resp ->
631 state.deleatom <- r32 resp 8;
632 let s = s32 state.deleatom in
633 let s = changepropreq wid state.protoatom 4 32 s in
634 sendstr s sock;
638 syncsendintern sock 2.0 "WM_CLASS" false (fun resp ->
639 let atom = r32 resp 8 in
640 let llpp = "llpp\000llpp\000" in
641 let s = changepropreq wid atom 31 8 llpp in
642 sendstr s sock;
645 let s = mapreq wid in
646 sendstr s sock;
648 let s = getkeymapreq state.mink (state.maxk-state.mink) in
649 sendwithrep sock s (updkmap sock);
651 let s = openfontreq (wid+1) "cursor" in
652 sendstr s sock;
654 Array.iteri (fun i glyphindex ->
655 let s = createglyphcursorreq (wid+1) (wid+2+i) glyphindex in
656 sendstr s sock;
657 ) [|34;48;50;58;128;152|];
659 sendintern sock "UTF8_STRING" true (fun resp ->
660 let atom = r32 resp 8 in
661 if atom != 0
662 then state.stringatom <- atom;
665 let setwmname s =
666 let s = changepropreq state.idbase 39 state.stringatom 8 s in
667 sendstr s state.sock;
669 state.setwmname <- setwmname;
670 sendintern sock "_NET_WM_NAME" true (fun resp ->
671 let atom = r32 resp 8 in
672 if atom != 0
673 then state.setwmname <- (fun s ->
674 setwmname s;
675 let s = changepropreq state.idbase atom state.stringatom 8 s in
676 sendstr s state.sock;
680 state.fullscreen <- (fun wid ->
681 let s = "xxuuyyuuwwuuhhuu" in
682 match state.fs with
683 | NoFs ->
684 w32 s 0 0;
685 w32 s 4 0;
686 w32 s 8 rootw;
687 w32 s 12 rooth;
688 let s = configurewindowreq wid 0x000f s in
689 sendstr s state.sock;
690 state.fs <- Fs (state.x, state.y, state.w, state.h);
692 | Fs (x, y, w, h) ->
693 w32 s 0 x;
694 w32 s 4 y;
695 w32 s 8 w;
696 w32 s 12 h;
697 let s = configurewindowreq wid 0x000f s in
698 sendstr s state.sock;
699 state.fs <- NoFs;
702 sendintern sock "_NET_WM_STATE" true (fun resp ->
703 let nwmsatom = r32 resp 8 in
704 if nwmsatom != 0
705 then
706 sendintern sock "_NET_WM_STATE_FULLSCREEN" true (fun resp ->
707 let fsatom = r32 resp 8 in
708 if fsatom != 0
709 then
710 state.fullscreen <-
711 (fun wid ->
712 let data = String.make 20 '\000' in
713 let fs, f =
714 match state.fs with
715 | NoFs -> Fs (-1, -1, -1, -1), 1
716 | Fs _ -> NoFs, 0
718 w32 data 0 f;
719 w32 data 4 fsatom;
721 let cm = clientmessage 32 0 wid nwmsatom data in
722 let s = sendeventreq 0 root 0x180000 cm in
723 sendstr s sock;
724 state.fs <- fs;
728 let s = getgeometryreq wid in
729 syncsendwithrep sock 2.0 s (fun resp ->
730 glx wid;
731 let w = r16 resp 16
732 and h = r16 resp 18 in
733 state.w <- w;
734 state.h <- h;
737 | c ->
738 error "unknown conection setup response %d" (Char.code c)
741 let getauth haddr dnum =
742 let haddr =
743 if haddr = "localhost" || String.length haddr = 0
744 then
745 try Unix.gethostname ()
746 with exn ->
747 dolog "failed to resolve `%S': %s" haddr (exntos exn);
748 haddr
749 else haddr
751 let path =
752 try Sys.getenv "XAUTHORITY"
753 with Not_found ->
754 try Filename.concat (Sys.getenv "HOME") ".Xauthority"
755 with Not_found -> ""
757 let readauth ic =
758 let input_string ic len =
759 let s = String.create len in
760 really_input ic s 0 len;
763 let r16 s =
764 let rb pos = Char.code (String.get s pos) in
765 (rb 1) lor ((rb 0) lsl 8)
767 let rec find () =
768 let rs () =
769 let s = input_string ic 2 in
770 let n = r16 s in
771 input_string ic n
773 let family = input_string ic 2 in
774 let addr = rs () in
775 let nums = rs () in
776 let optnum =
777 try Some (int_of_string nums)
778 with exn ->
779 dolog
780 "display number(%S) is not an integer (corrupt %S?): %s"
781 nums path (exntos exn);
782 None
784 let name = rs () in
785 let data = rs () in
787 vlog "family %S addr %S(%S) num %S(%d) name %S data %S"
788 family addr haddr nums dnum name data;
789 match optnum with
790 | Some num when addr = haddr && num = dnum ->
791 name, data
792 | _ -> find ()
794 let name, data =
795 try find ()
796 with
797 | End_of_file -> "", ""
798 | exn ->
799 dolog "exception while reading X authority data (%S): %s"
800 path (exntos exn);
801 "", ""
803 close_in ic;
804 name, data;
806 let opt =
808 if String.length path = 0
809 then None
810 else Some (open_in_bin path)
811 with exn ->
812 if Sys.file_exists path
813 then
814 dolog "failed to open X authority file `%S' : %s"
815 path (exntos exn);
816 None
818 match opt with
819 | None -> "", ""
820 | Some ic -> readauth ic
823 let init t w h osx =
824 let d =
825 try Sys.getenv "DISPLAY"
826 with exn ->
827 error "could not get DISPLAY evironment variable: %s"
828 (exntos exn)
830 let getnum w b e =
831 if b = e
832 then error "invalid DISPLAY(%s) %S" w d
833 else
834 let s = String.sub d b (e - b) in
835 try int_of_string s
836 with exn ->
837 error "invalid DISPLAY %S can not parse %s(%S): %s"
838 d w s (exntos exn)
840 let rec phost pos =
841 if pos = String.length d
842 then error "invalid DISPLAY %S no display number specified" d
843 else (
844 if d.[pos] = ':'
845 then
846 let rec pdispnum pos1 =
847 if pos1 = String.length d
848 then getnum "display number" (pos+1) pos1, 0
849 else
850 match d.[pos1] with
851 | '.' ->
852 let dispnum = getnum "display number" (pos+1) pos1 in
853 let rec pscreennum pos2 =
854 if pos2 = String.length d
855 then getnum "screen number" (pos1+1) pos2
856 else
857 match d.[pos2] with
858 | '0' .. '9' -> pscreennum (pos2+1)
859 | _ ->
860 error "invalid DISPLAY %S, cannot parse screen number" d
862 dispnum, pscreennum (pos1+1)
863 | '0' .. '9' -> pdispnum (pos1+1)
864 | _ ->
865 error "invalid DISPLAY %S, cannot parse display number" d
867 String.sub d 0 pos, pdispnum (pos+1)
868 else phost (pos+1)
871 let host, (dispnum, screennum) = phost 0 in
872 let aname, adata = getauth host dispnum in
873 let fd =
874 let fd, addr =
875 if osx || String.length host = 0 || host = "unix"
876 then
877 let addr =
878 if osx
879 then Unix.ADDR_UNIX d
880 else Unix.ADDR_UNIX ("/tmp/.X11-unix/X" ^ string_of_int dispnum)
882 let fd = Unix.socket Unix.PF_UNIX Unix.SOCK_STREAM 0 in
883 fd, addr
884 else
885 let h =
886 try Unix.gethostbyname host
887 with exn ->
888 error "cannot resolve %S: %s" host (exntos exn)
890 let addr = h.Unix.h_addr_list.(0) in
891 let port = 6000 + dispnum in
892 let fd = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
893 fd, (Unix.ADDR_INET (addr, port))
895 try Unix.connect fd addr; fd
896 with exn ->
897 error "failed to connect to X: %s" (exntos exn)
899 cloexec fd;
900 let s = "luMMmmnndduu" in
901 let s = padcat s aname in
902 let s = padcat s adata in
903 w16 s 2 11;
904 w16 s 4 0;
905 w16 s 6 (String.length aname);
906 w16 s 8 (String.length adata);
907 sendstr1 s 0 (String.length s) fd;
908 state.sock <- fd;
909 setup fd screennum w h;
910 state.t <- t;
911 fd, state.w, state.h;
914 let settitle s =
915 state.setwmname s;
918 let setcursor cursor =
919 if cursor != state.curcurs
920 then
921 let n =
922 match cursor with
923 | CURSOR_INHERIT -> -1
924 | CURSOR_INFO -> 3
925 | CURSOR_CYCLE -> 2
926 | CURSOR_CROSSHAIR -> 0
927 | CURSOR_TEXT -> 5
929 let s = s32 (if n = -1 then 0 else state.idbase+2+n) in
930 let s = changewindowattributesreq state.idbase (*cursor*)0x4000 s in
931 sendstr s state.sock;
932 state.curcurs <- cursor;
935 let fullscreen () =
936 state.fullscreen state.idbase;
939 let metamask = 0x40;;
940 let altmask = 8;;
941 let shiftmask = 1;;
942 let ctrlmask = 4;;
944 let withalt mask = mask land altmask != 0;;
945 let withctrl mask = mask land ctrlmask != 0;;
946 let withshift mask = mask land shiftmask != 0;;
947 let withmeta mask = mask land metamask != 0;;
948 let withnone mask = mask land (altmask + ctrlmask + shiftmask + metamask) = 0;;
950 let xlatt, xlatf =
951 let t = Hashtbl.create 20
952 and f = Hashtbl.create 20 in
953 let add n nl k =
954 List.iter (fun s -> Hashtbl.add t s k) (n::nl);
955 Hashtbl.add f k n
957 let addc c =
958 let s = String.create 1 in
959 s.[0] <- c;
960 add s [] (Char.code c)
962 let addcr a b =
963 let an = Char.code a and bn = Char.code b in
964 for i = an to bn do addc (Char.chr i) done;
966 addcr '0' '9';
967 addcr 'a' 'z';
968 addcr 'A' 'Z';
969 String.iter addc "`~!@#$%^&*()-_=+\\|[{]};:,./<>?";
970 for i = 0 to 29 do add ("f" ^ string_of_int (i+1)) [] (0xffbe + i) done;
971 add "space" [] 0x20;
972 add "ret" ["return"; "enter"] 0xff0d;
973 add "tab" [] 0xff09;
974 add "left" [] 0xff51;
975 add "right" [] 0xff53;
976 add "home" [] 0xff50;
977 add "end" [] 0xff57;
978 add "ins" ["insert"] 0xff63;
979 add "del" ["delete"] 0xffff;
980 add "esc" ["escape"] 0xff1b;
981 add "pgup" ["pageup"] 0xff55;
982 add "pgdown" ["pagedown"] 0xff56;
983 add "backspace" [] 0xff08;
984 add "up" [] 0xff52;
985 add "down" [] 0xff54;
986 t, f;
989 let keyname k =
990 try Hashtbl.find xlatf k
991 with Not_found -> Printf.sprintf "%#x" k;
994 let namekey name =
995 try Hashtbl.find xlatt name
996 with Not_found ->
997 if String.length name = 1
998 then Char.code name.[0]
999 else int_of_string name;