Emit a warning message if strdup of trimcachepath fails
[llpp.git] / wsi.ml
blobd8d1e8d7260bcf35bcdbe8e0f9fd728a31be05fa
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 exntos = function
126 | Unix.Unix_error (e, s, a) -> Printf.sprintf "%s(%s):%s(%d)"
127 s a (Unix.error_message e) (Obj.magic e)
128 | exn -> Printexc.to_string exn;
131 let error fmt = Printf.kprintf failwith fmt;;
133 let readstr sock n =
134 let s = String.create n in
135 let rec loop pos n =
136 let m = Unix.read sock s pos n in
137 if m = 0
138 then state.t#quit;
139 if n != m
140 then (
141 ignore (Unix.select [sock] [] [] 0.01);
142 loop (pos + m) (n - m)
145 loop 0 n;
149 let sendstr1 s pos len sock =
150 vlog "%d => %S" state.seq s;
151 state.seq <- state.seq + 1;
152 let n = Unix.send sock s pos len [] in
153 if n != len
154 then error "send %d returned %d" len n;
157 let updkmap sock resp =
158 let syms = r8 resp 1 in
159 let len = r32 resp 4 in
160 let data =
161 if len > 0
162 then readstr sock (4*len)
163 else ""
165 let m = len / syms in
166 state.keymap <- Array.make_matrix
167 (state.maxk - state.mink) syms 0xffffff;
168 let rec loop i = if i = m then () else
169 let k = i*4*syms in
170 let rec loop2 k l = if l = syms then () else
171 let v = r32 data k in
172 state.keymap.(i).(l) <- v;
173 loop2 (k+4) (l+1)
175 loop2 k 0;
176 loop (i+1);
178 loop 0;
181 let sendwithrep sock s f =
182 Queue.push f state.fifo;
183 sendstr1 s 0 (String.length s) sock;
186 let padcatl ss =
187 let b = Buffer.create 16 in
188 List.iter (Buffer.add_string b) ss;
189 let bl = Buffer.length b in
190 let pl = bl land 3 in
191 if pl != 0
192 then (
193 let pad = "123" in
194 Buffer.add_substring b pad 0 (4 - pl);
196 Buffer.contents b;
199 let padcat s1 s2 = padcatl [s1; s2];;
201 let internreq name onlyifexists =
202 let s = "\016\000\000\000\000\000\000\000" in
203 let s = padcat s name in
204 if onlyifexists then w8 s 1 1;
205 w16 s 2 (String.length s / 4);
206 w16 s 4 (String.length name);
210 let sendintern sock s onlyifexists f =
211 let s = internreq s onlyifexists in
212 sendwithrep sock s f;
215 let createwindowreq wid parent x y w h bw mask =
216 let s = "\001\000\009\000wwwwppppxxyywwhhbwccvvvvmmmmeeee" in
217 w32 s 4 wid;
218 w32 s 8 parent;
219 w16 s 12 x;
220 w16 s 14 y;
221 w16 s 16 w;
222 w16 s 18 h;
223 w16 s 20 bw;
224 w16 s 22 1;
225 w32 s 24 0;
226 w32 s 28 0x800; (* eventmask *)
227 w32 s 32 mask;
231 let getgeometryreq wid =
232 let s = "\014u\002\000dddd" in
233 w32 s 4 wid;
237 let mapreq wid =
238 let s = "\008u\002\000wwww" in
239 w32 s 4 wid;
243 let getkeymapreq first count =
244 let s = "\101u\002\000fcuu" in
245 w8 s 4 first;
246 w8 s 5 count;
250 let changepropreq wid prop typ format props =
251 let s = "\018\000llwwwwppppttttfuuuLLLL" in
252 let s = padcat s props in
253 w16 s 2 (String.length s / 4);
254 w32 s 4 wid;
255 w32 s 8 prop;
256 w32 s 12 typ;
257 w8 s 16 format;
258 let ful = String.length props / (match format with
259 | 8 -> 1
260 | 16 -> 2
261 | 32 -> 4
262 | n -> error "no idea what %d means" n)
264 w32 s 20 ful;
268 let openfontreq fid name =
269 let s = "\045ullffffnnuu" in
270 let s = padcat s name in
271 w16 s 2 (String.length s / 4);
272 w32 s 4 fid;
273 w16 s 8 (String.length name);
277 let createglyphcursorreq fid cid cindex =
278 let s = "\094u\008\000ccccffffffffssmmrrggbbRRGGBB" in
279 w32 s 4 cid;
280 w32 s 8 fid;
281 w32 s 12 fid;
282 w16 s 16 cindex;
283 w16 s 18 (cindex+1);
284 w16 s 20 0;
285 w16 s 22 0;
286 w16 s 24 0;
287 w16 s 26 0xffff;
288 w16 s 28 0xffff;
289 w16 s 30 0xffff;
293 let changewindowattributesreq wid mask attrs =
294 let s = "\002ullwwwwmmmm" in
295 let s = padcat s attrs in
296 w16 s 2 (String.length s / 4);
297 w32 s 4 wid;
298 w32 s 8 mask;
302 let configurewindowreq wid mask values =
303 let s = "\012ullwwwwmmuu" in
304 let s = padcat s values in
305 w16 s 2 (String.length s / 4);
306 w32 s 4 wid;
307 w16 s 8 mask;
311 let s32 n =
312 let s = "1234" in
313 w32 s 0 n;
317 let clientmessage format seq wid typ data =
318 let s = "\033fsswwwwtttt" in
319 let s = padcat s data in
320 w8 s 1 format;
321 w16 s 2 seq;
322 w32 s 4 wid;
323 w32 s 8 typ;
327 let sendeventreq propagate destwid mask data =
328 let s = "\025p\011\000wwwwmmmm" in
329 let s = padcat s data in
330 w8 s 1 propagate;
331 w32 s 4 destwid;
332 w32 s 8 mask;
336 let getkeysym code mask =
337 let index = (mask land 1) lxor ((mask land 2) lsr 1) in
338 let index = index lor ((mask land 0x80) lsr 5) in
339 let keysym = state.keymap.(code-state.mink).(index) in
340 if index = 1 && keysym = 0
341 then state.keymap.(code-state.mink).(0)
342 else keysym
345 let readresp sock =
346 let resp = readstr sock 32 in
347 let opcode = r8 resp 0 in
348 match opcode land lnot 0x80 with
349 | 0 -> (* error *)
350 let s = resp in
351 let code = r8 s 1
352 and serial = r16 s 2
353 and resid = r32 resp 4
354 and min = r16 s 8
355 and maj = r8 s 10 in
356 error "code=%d serial=%d resid=%#x min=%d maj=%d\n%S"
357 code serial resid min maj resp;
359 | 1 -> (* response *)
360 let rep = Queue.pop state.fifo in
361 rep resp;
363 | 2 -> (* key press *)
364 if Array.length state.keymap > 0
365 then
366 let code = r8 resp 1 in
367 let mask = r16 resp 28 in
368 let keysym = getkeysym code mask in
369 vlog "keysym = %x %c mask %#x code %d"
370 keysym (Char.unsafe_chr keysym) mask code;
371 state.t#key keysym mask;
373 | 3 -> (* key release *)
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 "release keysym = %x %c mask %#x code %#d"
380 keysym (Char.unsafe_chr keysym) mask code
382 | 4 -> (* buttonpress *)
383 let n = r8 resp 1
384 and x = r16s resp 24
385 and y = r16s resp 26
386 and m = r16 resp 28 in
387 state.t#mouse n true x y m;
388 vlog "press %d" n
390 | 5 -> (* buttonrelease *)
391 let n = r8 resp 1
392 and x = r16s resp 24
393 and y = r16s resp 26
394 and m = r16 resp 28 in
395 state.t#mouse n false x y m;
396 vlog "release %d %d %d" n x y
398 | 6 -> (* motion *)
399 let x = r16s resp 24 in
400 let y = r16s resp 26 in
401 let m = r16 resp 28 in
402 if m land 0x1f00 = 0
403 then state.t#pmotion x y
404 else state.t#motion x y;
405 vlog "move %dx%d => %d" x y m
407 | 7 -> (* enter *)
408 let x = r16s resp 24
409 and y = r16s resp 26 in
410 state.t#enter x y;
411 vlog "enter %d %d" x y
413 | 8 -> (* leave *)
414 state.t#leave
416 | 18 -> vlog "unmap";
418 | 19 -> (* map *)
419 vlog "map";
421 | 12 -> (* exposure *)
422 vlog "exposure";
423 state.t#expose
425 | 15 -> (* visibility *)
426 let vis = r8 resp 8 in
427 if vis != 2 then state.t#expose;
428 vlog "visibility %d" vis;
430 | 34 -> (* mapping *)
431 state.keymap <- [||];
432 let s = getkeymapreq state.mink (state.maxk-state.mink-1) in
433 sendwithrep sock s (updkmap sock);
435 | 33 -> (* clientmessage *)
436 let atom = r32 resp 8 in
437 if atom = state.protoatom
438 then (
439 let atom = r32 resp 12 in
440 if atom = state.deleatom
441 then state.t#quit;
443 vlog "atom %#x" atom
445 | 21 -> (* reparent *)
446 vlog "reparent"
448 | 22 -> (* configure *)
449 let x = r16s resp 16
450 and y = r16s resp 18
451 and w = r16 resp 20
452 and h = r16 resp 22 in
453 vlog "configure cur [%d %d %d %d] conf [%d %d %d %d]"
454 state.x state.y state.w state.h
455 x y w h
457 glxsync ();
458 if w != state.w || h != state.h
459 then (
460 state.t#reshape w h;
462 state.w <- w;
463 state.h <- h;
464 state.x <- x;
465 state.y <- y;
466 state.t#expose
468 | n ->
469 dolog "event %d %S" n resp
472 let readresp sock =
473 let rec loop () =
474 readresp sock;
475 if hasdata sock then loop ();
477 loop ();
480 let sendstr s ?(pos=0) ?(len=String.length s) sock =
481 sendstr1 s pos len sock;
482 if hasdata sock then readresp sock;
485 let reshape w h =
486 if state.fs = NoFs
487 then
488 let s = "wwuuhhuu" in
489 w32 s 0 w;
490 w32 s 4 h;
491 let s = configurewindowreq state.idbase 0x000c s in
492 sendstr s state.sock;
493 else state.fullscreen state.idbase
496 let syncsendwithrep sock secstowait s f =
497 let completed = ref false in
498 sendwithrep sock s (fun resp -> f resp; completed := true);
499 let now = Unix.gettimeofday in
500 let deadline = now () +. secstowait in
501 let rec readtillcompletion () =
502 let r, _, _ = Unix.select [sock] [] [] (deadline -. now ()) in
503 match r with
504 | [] -> error "didn't get X response in %f seconds, aborting" secstowait
505 | _ ->
506 readresp sock;
507 if not !completed
508 then readtillcompletion ()
510 readtillcompletion ();
513 let syncsendintern sock secstowait s onlyifexists f =
514 let s = internreq s onlyifexists in
515 syncsendwithrep sock secstowait s f;
518 let setup sock screennum w h =
519 let s = readstr sock 2 in
520 let n = String.length s in
521 if n != 2
522 then error "failed to read X connection setup response n=%d" n;
523 match s.[0] with
524 | '\000' ->
525 let reasonlen = r8 s 1 in
526 let s = readstr sock 6 in
527 let maj = r16 s 0
528 and min = r16 s 2
529 and add = r16 s 4 in
530 let len = add*4 in
531 let data = readstr sock len in
532 let reason = String.sub data 0 reasonlen in
533 error "X connection failed maj=%d min=%d reason=%S"
534 maj min reason
536 | '\002' -> failwith "X connection setup failed: authentication required";
538 | '\001' ->
539 let s = readstr sock 38 in
540 let maj = r16 s 0
541 and min = r16 s 2
542 and add = r16 s 4
543 and idbase = r32 s 10
544 and idmask = r32 s 14
545 and vlen = r16 s 22
546 and screens = r8 s 26
547 and formats = r8 s 27
548 and minkk = r8 s 32
549 and maxkk = r8 s 33 in
550 let data = readstr sock (4*add-32) in
551 let vendor = String.sub data 0 vlen in
552 let pos = ((vlen+3) land lnot 3) + formats*8 in
554 if screennum >= screens
555 then error "invalid screen %d, max %d" screennum (screens-1);
557 let pos =
558 let s = data in
559 let rec findscreen n pos = if n = screennum then pos else
560 let pos =
561 let ndepths = r8 s (pos+39) in
562 let rec skipdepths n pos = if n = ndepths then pos else
563 let pos =
564 let nvisiuals = r16 s (pos+2) in
565 pos + nvisiuals*24 + 8
567 skipdepths (n+1) pos
569 skipdepths n (pos+40)
571 findscreen (n+1) pos
573 findscreen 0 pos
575 let root = r32 data pos in
576 let rootw = r16 data (pos+20)
577 and rooth = r16 data (pos+22) in
578 state.mink <- minkk;
579 state.maxk <- maxkk;
580 state.idbase <- idbase;
581 vlog "vendor = %S, maj=%d min=%d" vendor maj min;
582 vlog "screens = %d formats = %d" screens formats;
583 vlog "minkk = %d maxkk = %d" minkk maxkk;
584 vlog "idbase = %#x idmask = %#x" idbase idmask;
585 vlog "root=%#x %dx%d" root rootw rooth;
587 let mask = 0
588 + 0x00000001 (* KeyPress *)
589 (* + 0x00000002 *) (* KeyRelease *)
590 + 0x00000004 (* ButtonPress *)
591 + 0x00000008 (* ButtonRelease *)
592 + 0x00000010 (* EnterWindow *)
593 + 0x00000020 (* LeaveWindow *)
594 + 0x00000040 (* PointerMotion *)
595 (* + 0x00000080 *) (* PointerMotionHint *)
596 (* + 0x00000100 *) (* Button1Motion *)
597 (* + 0x00000200 *) (* Button2Motion *)
598 (* + 0x00000400 *) (* Button3Motion *)
599 (* + 0x00000800 *) (* Button4Motion *)
600 (* + 0x00001000 *) (* Button5Motion *)
601 + 0x00002000 (* ButtonMotion *)
602 (* + 0x00004000 *) (* KeymapState *)
603 + 0x00008000 (* Exposure *)
604 + 0x00010000 (* VisibilityChange *)
605 + 0x00020000 (* StructureNotify *)
606 (* + 0x00040000 *) (* ResizeRedirect *)
607 (* + 0x00080000 *) (* SubstructureNotify *)
608 (* + 0x00100000 *) (* SubstructureRedirect *)
609 (* + 0x00200000 *) (* FocusChange *)
610 (* + 0x00400000 *) (* PropertyChange *)
611 (* + 0x00800000 *) (* ColormapChange *)
612 (* + 0x01000000 *) (* OwnerGrabButton *)
614 let wid = state.idbase in
615 let s = createwindowreq wid root 0 0 w h 0 mask in
616 sendstr s sock;
618 sendintern sock "WM_PROTOCOLS" false (fun resp ->
619 state.protoatom <- r32 resp 8;
620 sendintern sock "WM_DELETE_WINDOW" false (fun resp ->
621 state.deleatom <- r32 resp 8;
622 let s = s32 state.deleatom in
623 let s = changepropreq wid state.protoatom 4 32 s in
624 sendstr s sock;
628 syncsendintern sock 2.0 "WM_CLASS" false (fun resp ->
629 let atom = r32 resp 8 in
630 let llpp = "llpp\000llpp\000" in
631 let s = changepropreq wid atom 31 8 llpp in
632 sendstr s sock;
635 let s = mapreq wid in
636 sendstr s sock;
638 let s = getkeymapreq state.mink (state.maxk-state.mink) in
639 sendwithrep sock s (updkmap sock);
641 let s = openfontreq (wid+1) "cursor" in
642 sendstr s sock;
644 Array.iteri (fun i glyphindex ->
645 let s = createglyphcursorreq (wid+1) (wid+2+i) glyphindex in
646 sendstr s sock;
647 ) [|34;48;50;58;128;152|];
649 sendintern sock "UTF8_STRING" true (fun resp ->
650 let atom = r32 resp 8 in
651 if atom != 0
652 then state.stringatom <- atom;
655 let setwmname s =
656 let s = changepropreq state.idbase 39 state.stringatom 8 s in
657 sendstr s state.sock;
659 state.setwmname <- setwmname;
660 sendintern sock "_NET_WM_NAME" true (fun resp ->
661 let atom = r32 resp 8 in
662 if atom != 0
663 then state.setwmname <- (fun s ->
664 setwmname s;
665 let s = changepropreq state.idbase atom state.stringatom 8 s in
666 sendstr s state.sock;
670 state.fullscreen <- (fun wid ->
671 let s = "xxuuyyuuwwuuhhuu" in
672 match state.fs with
673 | NoFs ->
674 w32 s 0 0;
675 w32 s 4 0;
676 w32 s 8 rootw;
677 w32 s 12 rooth;
678 let s = configurewindowreq wid 0x000f s in
679 sendstr s state.sock;
680 state.fs <- Fs (state.x, state.y, state.w, state.h);
682 | Fs (x, y, w, h) ->
683 w32 s 0 x;
684 w32 s 4 y;
685 w32 s 8 w;
686 w32 s 12 h;
687 let s = configurewindowreq wid 0x000f s in
688 sendstr s state.sock;
689 state.fs <- NoFs;
692 sendintern sock "_NET_WM_STATE" true (fun resp ->
693 let nwmsatom = r32 resp 8 in
694 if nwmsatom != 0
695 then
696 sendintern sock "_NET_WM_STATE_FULLSCREEN" true (fun resp ->
697 let fsatom = r32 resp 8 in
698 if fsatom != 0
699 then
700 state.fullscreen <-
701 (fun wid ->
702 let data = String.make 20 '\000' in
703 let fs, f =
704 match state.fs with
705 | NoFs -> Fs (-1, -1, -1, -1), 1
706 | Fs _ -> NoFs, 0
708 w32 data 0 f;
709 w32 data 4 fsatom;
711 let cm = clientmessage 32 0 wid nwmsatom data in
712 let s = sendeventreq 0 root 0x180000 cm in
713 sendstr s sock;
714 state.fs <- fs;
718 let s = getgeometryreq wid in
719 syncsendwithrep sock 2.0 s (fun resp ->
720 glx wid;
721 let w = r16 resp 16
722 and h = r16 resp 18 in
723 state.w <- w;
724 state.h <- h;
727 | c ->
728 error "unknown conection setup response %d" (Char.code c)
731 let getauth haddr dnum =
732 let haddr =
733 if haddr = "localhost" || String.length haddr = 0
734 then
735 try Unix.gethostname ()
736 with exn ->
737 dolog "failed to resolve `%S': %s" haddr (exntos exn);
738 haddr
739 else haddr
741 let path =
742 try Sys.getenv "XAUTHORITY"
743 with Not_found ->
744 try Filename.concat (Sys.getenv "HOME") ".Xauthority"
745 with Not_found -> ""
747 let readauth ic =
748 let input_string ic len =
749 let s = String.create len in
750 really_input ic s 0 len;
753 let r16 s =
754 let rb pos = Char.code (String.get s pos) in
755 (rb 1) lor ((rb 0) lsl 8)
757 let rec find () =
758 let rs () =
759 let s = input_string ic 2 in
760 let n = r16 s in
761 input_string ic n
763 let family = input_string ic 2 in
764 let addr = rs () in
765 let nums = rs () in
766 let optnum =
767 try Some (int_of_string nums)
768 with exn ->
769 dolog
770 "display number(%S) is not an integer (corrupt %S?): %s"
771 nums path (exntos exn);
772 None
774 let name = rs () in
775 let data = rs () in
777 vlog "family %S addr %S(%S) num %S(%d) name %S data %S"
778 family addr haddr nums dnum name data;
779 match optnum with
780 | Some num when addr = haddr && num = dnum ->
781 name, data
782 | _ -> find ()
784 let name, data =
785 try find ()
786 with
787 | End_of_file -> "", ""
788 | exn ->
789 dolog "exception while reading X authority data (%S): %s"
790 path (exntos exn);
791 "", ""
793 close_in ic;
794 name, data;
796 let opt =
798 if String.length path = 0
799 then None
800 else Some (open_in_bin path)
801 with exn ->
802 if Sys.file_exists path
803 then
804 dolog "failed to open X authority file `%S' : %s"
805 path (exntos exn);
806 None
808 match opt with
809 | None -> "", ""
810 | Some ic -> readauth ic
813 let init t w h osx =
814 let d =
815 try Sys.getenv "DISPLAY"
816 with exn ->
817 error "could not get DISPLAY evironment variable: %s"
818 (exntos exn)
820 let getnum w b e =
821 if b = e
822 then error "invalid DISPLAY(%s) %S" w d
823 else
824 let s = String.sub d b (e - b) in
825 try int_of_string s
826 with exn ->
827 error "invalid DISPLAY %S can not parse %s(%S): %s"
828 d w s (exntos exn)
830 let rec phost pos =
831 if pos = String.length d
832 then error "invalid DISPLAY %S no display number specified" d
833 else (
834 if d.[pos] = ':'
835 then
836 let rec pdispnum pos1 =
837 if pos1 = String.length d
838 then getnum "display number" (pos+1) pos1, 0
839 else
840 match d.[pos1] with
841 | '.' ->
842 let dispnum = getnum "display number" (pos+1) pos1 in
843 let rec pscreennum pos2 =
844 if pos2 = String.length d
845 then getnum "screen number" (pos1+1) pos2
846 else
847 match d.[pos2] with
848 | '0' .. '9' -> pscreennum (pos2+1)
849 | _ ->
850 error "invalid DISPLAY %S, cannot parse screen number" d
852 dispnum, pscreennum (pos1+1)
853 | '0' .. '9' -> pdispnum (pos1+1)
854 | _ ->
855 error "invalid DISPLAY %S, cannot parse display number" d
857 String.sub d 0 pos, pdispnum (pos+1)
858 else phost (pos+1)
861 let host, (dispnum, screennum) = phost 0 in
862 let aname, adata = getauth host dispnum in
863 let fd =
864 let fd, addr =
865 if osx || String.length host = 0 || host = "unix"
866 then
867 let addr =
868 if osx
869 then Unix.ADDR_UNIX d
870 else Unix.ADDR_UNIX ("/tmp/.X11-unix/X" ^ string_of_int dispnum)
872 let fd = Unix.socket Unix.PF_UNIX Unix.SOCK_STREAM 0 in
873 fd, addr
874 else
875 let h =
876 try Unix.gethostbyname host
877 with exn ->
878 error "cannot resolve %S: %s" host (exntos exn)
880 let addr = h.Unix.h_addr_list.(0) in
881 let port = 6000 + dispnum in
882 let fd = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
883 fd, (Unix.ADDR_INET (addr, port))
885 try Unix.connect fd addr; fd
886 with exn ->
887 error "failed to connect to X: %s" (exntos exn)
889 cloexec fd;
890 let s = "luMMmmnndduu" in
891 let s = padcat s aname in
892 let s = padcat s adata in
893 w16 s 2 11;
894 w16 s 4 0;
895 w16 s 6 (String.length aname);
896 w16 s 8 (String.length adata);
897 sendstr1 s 0 (String.length s) fd;
898 state.sock <- fd;
899 setup fd screennum w h;
900 state.t <- t;
901 fd, state.w, state.h;
904 let settitle s =
905 state.setwmname s;
908 let setcursor cursor =
909 if cursor != state.curcurs
910 then
911 let n =
912 match cursor with
913 | CURSOR_INHERIT -> -1
914 | CURSOR_INFO -> 3
915 | CURSOR_CYCLE -> 2
916 | CURSOR_CROSSHAIR -> 0
917 | CURSOR_TEXT -> 5
919 let s = s32 (if n = -1 then 0 else state.idbase+2+n) in
920 let s = changewindowattributesreq state.idbase (*cursor*)0x4000 s in
921 sendstr s state.sock;
922 state.curcurs <- cursor;
925 let fullscreen () =
926 state.fullscreen state.idbase;
929 let metamask = 0x40;;
930 let altmask = 8;;
931 let shiftmask = 1;;
932 let ctrlmask = 4;;
934 let withalt mask = mask land altmask != 0;;
935 let withctrl mask = mask land ctrlmask != 0;;
936 let withshift mask = mask land shiftmask != 0;;
937 let withmeta mask = mask land metamask != 0;;
938 let withnone mask = mask land (altmask + ctrlmask + shiftmask + metamask) = 0;;
940 let xlatt, xlatf =
941 let t = Hashtbl.create 20
942 and f = Hashtbl.create 20 in
943 let add n nl k =
944 List.iter (fun s -> Hashtbl.add t s k) (n::nl);
945 Hashtbl.add f k n
947 let addc c =
948 let s = String.create 1 in
949 s.[0] <- c;
950 add s [] (Char.code c)
952 let addcr a b =
953 let an = Char.code a and bn = Char.code b in
954 for i = an to bn do addc (Char.chr i) done;
956 addcr '0' '9';
957 addcr 'a' 'z';
958 addcr 'A' 'Z';
959 String.iter addc "`~!@#$%^&*()-_=+\\|[{]};:,./<>?";
960 for i = 0 to 29 do add ("f" ^ string_of_int (i+1)) [] (0xffbe + i) done;
961 add "space" [] 0x20;
962 add "ret" ["return"; "enter"] 0xff0d;
963 add "tab" [] 0xff09;
964 add "left" [] 0xff51;
965 add "right" [] 0xff53;
966 add "home" [] 0xff50;
967 add "end" [] 0xff57;
968 add "ins" ["insert"] 0xff63;
969 add "del" ["delete"] 0xffff;
970 add "esc" ["escape"] 0xff1b;
971 add "pgup" ["pageup"] 0xff55;
972 add "pgdown" ["pagedown"] 0xff56;
973 add "backspace" [] 0xff08;
974 add "up" [] 0xff52;
975 add "down" [] 0xff54;
976 t, f;
979 let keyname k =
980 try Hashtbl.find xlatf k
981 with Not_found -> Printf.sprintf "%#x" k;
984 let namekey name =
985 try Hashtbl.find xlatt name
986 with Not_found ->
987 if String.length name = 1
988 then Char.code name.[0]
989 else int_of_string name;