9e31c686d75a873ca6f66d20d00adac6f921c3ac
[llpp.git] / wsi.ml
blob9e31c686d75a873ca6f66d20d00adac6f921c3ac
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 capslmask : 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 ; capslmask = 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 updmodmap sock resp =
184 let n = r8 resp 1 in
185 let len = r16 resp 4 in
186 let data =
187 if len > 0
188 then readstr sock (len*4)
189 else ""
191 let modmap = Array.make_matrix 8 n 0xffffff in
192 let rec loop l = if l = 8 then () else
193 let p = l*n in
194 let rec loop1 m = if m = n then () else
195 let p = p+m in
196 let code = r8 data p in
197 modmap.(l).(m) <- code;
198 if l = 1
199 then (
200 let ki = code - state.mink in
201 if ki >= 0
202 then
203 let a = state.keymap.(ki) in
204 let rec capsloop i = if i = Array.length a || i > 3 then () else
205 let s = a.(i) in
206 if s = 0xffe5
207 then state.capslmask <- 2
208 else capsloop (i+1)
210 capsloop 0;
212 loop1 (m+1)
214 loop1 0;
215 loop (l+1)
217 loop 0;
220 let sendwithrep sock s f =
221 Queue.push f state.fifo;
222 sendstr1 s 0 (String.length s) sock;
225 let padcatl ss =
226 let b = Buffer.create 16 in
227 List.iter (Buffer.add_string b) ss;
228 let bl = Buffer.length b in
229 let pl = bl land 3 in
230 if pl != 0
231 then (
232 let pad = "123" in
233 Buffer.add_substring b pad 0 (4 - pl);
235 Buffer.contents b;
238 let padcat s1 s2 = padcatl [s1; s2];;
240 let internreq name onlyifexists =
241 let s = "\016\000\000\000\000\000\000\000" in
242 let s = padcat s name in
243 if onlyifexists then w8 s 1 1;
244 w16 s 2 (String.length s / 4);
245 w16 s 4 (String.length name);
249 let sendintern sock s onlyifexists f =
250 let s = internreq s onlyifexists in
251 sendwithrep sock s f;
254 let createwindowreq wid parent x y w h bw mask =
255 let s = "\001\000\009\000wwwwppppxxyywwhhbwccvvvvmmmmeeee" in
256 w32 s 4 wid;
257 w32 s 8 parent;
258 w16 s 12 x;
259 w16 s 14 y;
260 w16 s 16 w;
261 w16 s 18 h;
262 w16 s 20 bw;
263 w16 s 22 1;
264 w32 s 24 0;
265 w32 s 28 0x800; (* eventmask *)
266 w32 s 32 mask;
270 let getgeometryreq wid =
271 let s = "\014u\002\000dddd" in
272 w32 s 4 wid;
276 let mapreq wid =
277 let s = "\008u\002\000wwww" in
278 w32 s 4 wid;
282 let getkeymapreq first count =
283 let s = "\101u\002\000fcuu" in
284 w8 s 4 first;
285 w8 s 5 count;
289 let changepropreq wid prop typ format props =
290 let s = "\018\000llwwwwppppttttfuuuLLLL" in
291 let s = padcat s props in
292 w16 s 2 (String.length s / 4);
293 w32 s 4 wid;
294 w32 s 8 prop;
295 w32 s 12 typ;
296 w8 s 16 format;
297 let ful = String.length props / (match format with
298 | 8 -> 1
299 | 16 -> 2
300 | 32 -> 4
301 | n -> error "no idea what %d means" n)
303 w32 s 20 ful;
307 let openfontreq fid name =
308 let s = "\045ullffffnnuu" in
309 let s = padcat s name in
310 w16 s 2 (String.length s / 4);
311 w32 s 4 fid;
312 w16 s 8 (String.length name);
316 let createglyphcursorreq fid cid cindex =
317 let s = "\094u\008\000ccccffffffffssmmrrggbbRRGGBB" in
318 w32 s 4 cid;
319 w32 s 8 fid;
320 w32 s 12 fid;
321 w16 s 16 cindex;
322 w16 s 18 (cindex+1);
323 w16 s 20 0;
324 w16 s 22 0;
325 w16 s 24 0;
326 w16 s 26 0xffff;
327 w16 s 28 0xffff;
328 w16 s 30 0xffff;
332 let changewindowattributesreq wid mask attrs =
333 let s = "\002ullwwwwmmmm" in
334 let s = padcat s attrs in
335 w16 s 2 (String.length s / 4);
336 w32 s 4 wid;
337 w32 s 8 mask;
341 let configurewindowreq wid mask values =
342 let s = "\012ullwwwwmmuu" in
343 let s = padcat s values in
344 w16 s 2 (String.length s / 4);
345 w32 s 4 wid;
346 w16 s 8 mask;
350 let s32 n =
351 let s = "1234" in
352 w32 s 0 n;
356 let clientmessage format seq wid typ data =
357 let s = "\033fsswwwwtttt" in
358 let s = padcat s data in
359 w8 s 1 format;
360 w16 s 2 seq;
361 w32 s 4 wid;
362 w32 s 8 typ;
366 let sendeventreq propagate destwid mask data =
367 let s = "\025p\011\000wwwwmmmm" in
368 let s = padcat s data in
369 w8 s 1 propagate;
370 w32 s 4 destwid;
371 w32 s 8 mask;
375 let getmodifiermappingreq () =
376 let s = "\119u\001\000" in
380 let getkeysym code mask =
381 let shinc = (mask land 1) lxor ((mask land state.capslmask) lsr 1) in
382 let msh = ((mask land 0x10) lsr 4)
383 + ((mask land 0x20) lsr 5)
384 + ((mask land 0x40) lsr 6)
385 + ((mask land 0x80) lsr 7)
387 let index = (1 lsl msh) + shinc in
388 let keysym = state.keymap.(code-state.mink).(index) in
389 if shinc = 1 && keysym = 0
390 then state.keymap.(code-state.mink).(index - 1)
391 else keysym
394 let readresp sock =
395 let resp = readstr sock 32 in
396 let opcode = r8 resp 0 in
397 match opcode land lnot 0x80 with
398 | 0 -> (* error *)
399 let s = resp in
400 let code = r8 s 1
401 and serial = r16 s 2
402 and resid = r32 resp 4
403 and min = r16 s 8
404 and maj = r8 s 10 in
405 error "code=%d serial=%d resid=%#x min=%d maj=%d\n%S"
406 code serial resid min maj resp;
408 | 1 -> (* response *)
409 let rep = Queue.pop state.fifo in
410 rep resp;
412 | 2 -> (* key press *)
413 if Array.length state.keymap > 0
414 then
415 let code = r8 resp 1 in
416 let mask = r16 resp 28 in
417 let keysym = getkeysym code mask in
418 vlog "keysym = %x %c mask %#x code %d"
419 keysym (Char.unsafe_chr keysym) mask code;
420 state.t#key keysym mask;
422 | 3 -> (* key release *)
423 if Array.length state.keymap > 0
424 then
425 let code = r8 resp 1 in
426 let mask = r16 resp 28 in
427 let keysym = getkeysym code mask in
428 vlog "release keysym = %x %c mask %#x code %#d"
429 keysym (Char.unsafe_chr keysym) mask code
431 | 4 -> (* buttonpress *)
432 let n = r8 resp 1
433 and x = r16s resp 24
434 and y = r16s resp 26
435 and m = r16 resp 28 in
436 state.t#mouse n true x y m;
437 vlog "press %d" n
439 | 5 -> (* buttonrelease *)
440 let n = r8 resp 1
441 and x = r16s resp 24
442 and y = r16s resp 26
443 and m = r16 resp 28 in
444 state.t#mouse n false x y m;
445 vlog "release %d %d %d" n x y
447 | 6 -> (* motion *)
448 let x = r16s resp 24 in
449 let y = r16s resp 26 in
450 let m = r16 resp 28 in
451 if m land 0x1f00 = 0
452 then state.t#pmotion x y
453 else state.t#motion x y;
454 vlog "move %dx%d => %d" x y m
456 | 7 -> (* enter *)
457 let x = r16s resp 24
458 and y = r16s resp 26 in
459 state.t#enter x y;
460 vlog "enter %d %d" x y
462 | 8 -> (* leave *)
463 state.t#leave
465 | 18 -> vlog "unmap";
467 | 19 -> (* map *)
468 vlog "map";
470 | 12 -> (* exposure *)
471 vlog "exposure";
472 state.t#expose
474 | 15 -> (* visibility *)
475 let vis = r8 resp 8 in
476 if vis != 2 then state.t#expose;
477 vlog "visibility %d" vis;
479 | 34 -> (* mapping *)
480 state.keymap <- [||];
481 let s = getkeymapreq state.mink (state.maxk-state.mink-1) in
482 sendwithrep sock s (updkmap sock);
483 state.capslmask <- 0;
484 let s = getmodifiermappingreq () in
485 sendwithrep sock s (updmodmap sock);
488 | 33 -> (* clientmessage *)
489 let atom = r32 resp 8 in
490 if atom = state.protoatom
491 then (
492 let atom = r32 resp 12 in
493 if atom = state.deleatom
494 then state.t#quit;
496 vlog "atom %#x" atom
498 | 21 -> (* reparent *)
499 vlog "reparent"
501 | 22 -> (* configure *)
502 let x = r16s resp 16
503 and y = r16s resp 18
504 and w = r16 resp 20
505 and h = r16 resp 22 in
506 vlog "configure cur [%d %d %d %d] conf [%d %d %d %d]"
507 state.x state.y state.w state.h
508 x y w h
510 glxsync ();
511 if w != state.w || h != state.h
512 then (
513 state.t#reshape w h;
515 state.w <- w;
516 state.h <- h;
517 state.x <- x;
518 state.y <- y;
519 state.t#expose
521 | n ->
522 dolog "event %d %S" n resp
525 let readresp sock =
526 let rec loop () =
527 readresp sock;
528 if hasdata sock then loop ();
530 loop ();
533 let sendstr s ?(pos=0) ?(len=String.length s) sock =
534 sendstr1 s pos len sock;
535 if hasdata sock then readresp sock;
538 let reshape w h =
539 if state.fs = NoFs
540 then
541 let s = "wwuuhhuu" in
542 w32 s 0 w;
543 w32 s 4 h;
544 let s = configurewindowreq state.idbase 0x000c s in
545 sendstr s state.sock;
546 else state.fullscreen state.idbase
549 let syncsendwithrep sock secstowait s f =
550 let completed = ref false in
551 sendwithrep sock s (fun resp -> f resp; completed := true);
552 let now = Unix.gettimeofday in
553 let deadline = now () +. secstowait in
554 let rec readtillcompletion () =
555 let r, _, _ = Unix.select [sock] [] [] (deadline -. now ()) in
556 match r with
557 | [] -> error "didn't get X response in %f seconds, aborting" secstowait
558 | _ ->
559 readresp sock;
560 if not !completed
561 then readtillcompletion ()
563 readtillcompletion ();
566 let syncsendintern sock secstowait s onlyifexists f =
567 let s = internreq s onlyifexists in
568 syncsendwithrep sock secstowait s f;
571 let setup sock screennum w h =
572 let s = readstr sock 2 in
573 let n = String.length s in
574 if n != 2
575 then error "failed to read X connection setup response n=%d" n;
576 match s.[0] with
577 | '\000' ->
578 let reasonlen = r8 s 1 in
579 let s = readstr sock 6 in
580 let maj = r16 s 0
581 and min = r16 s 2
582 and add = r16 s 4 in
583 let len = add*4 in
584 let data = readstr sock len in
585 let reason = String.sub data 0 reasonlen in
586 error "X connection failed maj=%d min=%d reason=%S"
587 maj min reason
589 | '\002' -> failwith "X connection setup failed: authentication required";
591 | '\001' ->
592 let s = readstr sock 38 in
593 let maj = r16 s 0
594 and min = r16 s 2
595 and add = r16 s 4
596 and idbase = r32 s 10
597 and idmask = r32 s 14
598 and vlen = r16 s 22
599 and screens = r8 s 26
600 and formats = r8 s 27
601 and minkk = r8 s 32
602 and maxkk = r8 s 33 in
603 let data = readstr sock (4*add-32) in
604 let vendor = String.sub data 0 vlen in
605 let pos = ((vlen+3) land lnot 3) + formats*8 in
607 if screennum >= screens
608 then error "invalid screen %d, max %d" screennum (screens-1);
610 let pos =
611 let s = data in
612 let rec findscreen n pos = if n = screennum then pos else
613 let pos =
614 let ndepths = r8 s (pos+39) in
615 let rec skipdepths n pos = if n = ndepths then pos else
616 let pos =
617 let nvisiuals = r16 s (pos+2) in
618 pos + nvisiuals*24 + 8
620 skipdepths (n+1) pos
622 skipdepths n (pos+40)
624 findscreen (n+1) pos
626 findscreen 0 pos
628 let root = r32 data pos in
629 let rootw = r16 data (pos+20)
630 and rooth = r16 data (pos+22) in
631 state.mink <- minkk;
632 state.maxk <- maxkk;
633 state.idbase <- idbase;
634 vlog "vendor = %S, maj=%d min=%d" vendor maj min;
635 vlog "screens = %d formats = %d" screens formats;
636 vlog "minkk = %d maxkk = %d" minkk maxkk;
637 vlog "idbase = %#x idmask = %#x" idbase idmask;
638 vlog "root=%#x %dx%d" root rootw rooth;
640 let mask = 0
641 + 0x00000001 (* KeyPress *)
642 (* + 0x00000002 *) (* KeyRelease *)
643 + 0x00000004 (* ButtonPress *)
644 + 0x00000008 (* ButtonRelease *)
645 + 0x00000010 (* EnterWindow *)
646 + 0x00000020 (* LeaveWindow *)
647 + 0x00000040 (* PointerMotion *)
648 (* + 0x00000080 *) (* PointerMotionHint *)
649 (* + 0x00000100 *) (* Button1Motion *)
650 (* + 0x00000200 *) (* Button2Motion *)
651 (* + 0x00000400 *) (* Button3Motion *)
652 (* + 0x00000800 *) (* Button4Motion *)
653 (* + 0x00001000 *) (* Button5Motion *)
654 + 0x00002000 (* ButtonMotion *)
655 (* + 0x00004000 *) (* KeymapState *)
656 + 0x00008000 (* Exposure *)
657 + 0x00010000 (* VisibilityChange *)
658 + 0x00020000 (* StructureNotify *)
659 (* + 0x00040000 *) (* ResizeRedirect *)
660 (* + 0x00080000 *) (* SubstructureNotify *)
661 (* + 0x00100000 *) (* SubstructureRedirect *)
662 (* + 0x00200000 *) (* FocusChange *)
663 (* + 0x00400000 *) (* PropertyChange *)
664 (* + 0x00800000 *) (* ColormapChange *)
665 (* + 0x01000000 *) (* OwnerGrabButton *)
667 let wid = state.idbase in
668 let s = createwindowreq wid root 0 0 w h 0 mask in
669 sendstr s sock;
671 sendintern sock "WM_PROTOCOLS" false (fun resp ->
672 state.protoatom <- r32 resp 8;
673 sendintern sock "WM_DELETE_WINDOW" false (fun resp ->
674 state.deleatom <- r32 resp 8;
675 let s = s32 state.deleatom in
676 let s = changepropreq wid state.protoatom 4 32 s in
677 sendstr s sock;
681 syncsendintern sock 2.0 "WM_CLASS" false (fun resp ->
682 let atom = r32 resp 8 in
683 let llpp = "llpp\000llpp\000" in
684 let s = changepropreq wid atom 31 8 llpp in
685 sendstr s sock;
688 let s = mapreq wid in
689 sendstr s sock;
691 let s = getkeymapreq state.mink (state.maxk-state.mink) in
692 sendwithrep sock s (updkmap sock);
694 let s = getmodifiermappingreq () in
695 sendwithrep sock s (updmodmap sock);
697 let s = openfontreq (wid+1) "cursor" in
698 sendstr s sock;
700 Array.iteri (fun i glyphindex ->
701 let s = createglyphcursorreq (wid+1) (wid+2+i) glyphindex in
702 sendstr s sock;
703 ) [|34;48;50;58;128;152|];
705 sendintern sock "UTF8_STRING" true (fun resp ->
706 let atom = r32 resp 8 in
707 if atom != 0
708 then state.stringatom <- atom;
711 let setwmname s =
712 let s = changepropreq state.idbase 39 state.stringatom 8 s in
713 sendstr s state.sock;
715 state.setwmname <- setwmname;
716 sendintern sock "_NET_WM_NAME" true (fun resp ->
717 let atom = r32 resp 8 in
718 if atom != 0
719 then state.setwmname <- (fun s ->
720 setwmname s;
721 let s = changepropreq state.idbase atom state.stringatom 8 s in
722 sendstr s state.sock;
726 state.fullscreen <- (fun wid ->
727 let s = "xxuuyyuuwwuuhhuu" in
728 match state.fs with
729 | NoFs ->
730 w32 s 0 0;
731 w32 s 4 0;
732 w32 s 8 rootw;
733 w32 s 12 rooth;
734 let s = configurewindowreq wid 0x000f s in
735 sendstr s state.sock;
736 state.fs <- Fs (state.x, state.y, state.w, state.h);
738 | Fs (x, y, w, h) ->
739 w32 s 0 x;
740 w32 s 4 y;
741 w32 s 8 w;
742 w32 s 12 h;
743 let s = configurewindowreq wid 0x000f s in
744 sendstr s state.sock;
745 state.fs <- NoFs;
748 sendintern sock "_NET_WM_STATE" true (fun resp ->
749 let nwmsatom = r32 resp 8 in
750 if nwmsatom != 0
751 then
752 sendintern sock "_NET_WM_STATE_FULLSCREEN" true (fun resp ->
753 let fsatom = r32 resp 8 in
754 if fsatom != 0
755 then
756 state.fullscreen <-
757 (fun wid ->
758 let data = String.make 20 '\000' in
759 let fs, f =
760 match state.fs with
761 | NoFs -> Fs (-1, -1, -1, -1), 1
762 | Fs _ -> NoFs, 0
764 w32 data 0 f;
765 w32 data 4 fsatom;
767 let cm = clientmessage 32 0 wid nwmsatom data in
768 let s = sendeventreq 0 root 0x180000 cm in
769 sendstr s sock;
770 state.fs <- fs;
774 let s = getgeometryreq wid in
775 syncsendwithrep sock 2.0 s (fun resp ->
776 glx wid;
777 let w = r16 resp 16
778 and h = r16 resp 18 in
779 state.w <- w;
780 state.h <- h;
783 | c ->
784 error "unknown conection setup response %d" (Char.code c)
787 let getauth haddr dnum =
788 let haddr =
789 if haddr = "localhost" || String.length haddr = 0
790 then
791 try Unix.gethostname ()
792 with exn ->
793 dolog "failed to resolve `%S': %s" haddr (exntos exn);
794 haddr
795 else haddr
797 let path =
798 try Sys.getenv "XAUTHORITY"
799 with Not_found ->
800 try Filename.concat (Sys.getenv "HOME") ".Xauthority"
801 with Not_found -> ""
803 let readauth ic =
804 let input_string ic len =
805 let s = String.create len in
806 really_input ic s 0 len;
809 let r16 s =
810 let rb pos = Char.code (String.get s pos) in
811 (rb 1) lor ((rb 0) lsl 8)
813 let rec find () =
814 let rs () =
815 let s = input_string ic 2 in
816 let n = r16 s in
817 input_string ic n
819 let family = input_string ic 2 in
820 let addr = rs () in
821 let nums = rs () in
822 let optnum =
823 try Some (int_of_string nums)
824 with exn ->
825 dolog
826 "display number(%S) is not an integer (corrupt %S?): %s"
827 nums path (exntos exn);
828 None
830 let name = rs () in
831 let data = rs () in
833 vlog "family %S addr %S(%S) num %S(%d) name %S data %S"
834 family addr haddr nums dnum name data;
835 match optnum with
836 | Some num when addr = haddr && num = dnum ->
837 name, data
838 | _ -> find ()
840 let name, data =
841 try find ()
842 with
843 | End_of_file -> "", ""
844 | exn ->
845 dolog "exception while reading X authority data (%S): %s"
846 path (exntos exn);
847 "", ""
849 close_in ic;
850 name, data;
852 let opt =
854 if String.length path = 0
855 then None
856 else Some (open_in_bin path)
857 with exn ->
858 if Sys.file_exists path
859 then
860 dolog "failed to open X authority file `%S' : %s"
861 path (exntos exn);
862 None
864 match opt with
865 | None -> "", ""
866 | Some ic -> readauth ic
869 let init t w h osx =
870 let d =
871 try Sys.getenv "DISPLAY"
872 with exn ->
873 error "could not get DISPLAY evironment variable: %s"
874 (exntos exn)
876 let getnum w b e =
877 if b = e
878 then error "invalid DISPLAY(%s) %S" w d
879 else
880 let s = String.sub d b (e - b) in
881 try int_of_string s
882 with exn ->
883 error "invalid DISPLAY %S can not parse %s(%S): %s"
884 d w s (exntos exn)
886 let rec phost pos =
887 if pos = String.length d
888 then error "invalid DISPLAY %S no display number specified" d
889 else (
890 if d.[pos] = ':'
891 then
892 let rec pdispnum pos1 =
893 if pos1 = String.length d
894 then getnum "display number" (pos+1) pos1, 0
895 else
896 match d.[pos1] with
897 | '.' ->
898 let dispnum = getnum "display number" (pos+1) pos1 in
899 let rec pscreennum pos2 =
900 if pos2 = String.length d
901 then getnum "screen number" (pos1+1) pos2
902 else
903 match d.[pos2] with
904 | '0' .. '9' -> pscreennum (pos2+1)
905 | _ ->
906 error "invalid DISPLAY %S, cannot parse screen number" d
908 dispnum, pscreennum (pos1+1)
909 | '0' .. '9' -> pdispnum (pos1+1)
910 | _ ->
911 error "invalid DISPLAY %S, cannot parse display number" d
913 String.sub d 0 pos, pdispnum (pos+1)
914 else phost (pos+1)
917 let host, (dispnum, screennum) = phost 0 in
918 let aname, adata = getauth host dispnum in
919 let fd =
920 let fd, addr =
921 if osx || String.length host = 0 || host = "unix"
922 then
923 let addr =
924 if osx
925 then Unix.ADDR_UNIX d
926 else Unix.ADDR_UNIX ("/tmp/.X11-unix/X" ^ string_of_int dispnum)
928 let fd = Unix.socket Unix.PF_UNIX Unix.SOCK_STREAM 0 in
929 fd, addr
930 else
931 let h =
932 try Unix.gethostbyname host
933 with exn ->
934 error "cannot resolve %S: %s" host (exntos exn)
936 let addr = h.Unix.h_addr_list.(0) in
937 let port = 6000 + dispnum in
938 let fd = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
939 fd, (Unix.ADDR_INET (addr, port))
941 try Unix.connect fd addr; fd
942 with exn ->
943 error "failed to connect to X: %s" (exntos exn)
945 cloexec fd;
946 let s = "luMMmmnndduu" in
947 let s = padcat s aname in
948 let s = padcat s adata in
949 w16 s 2 11;
950 w16 s 4 0;
951 w16 s 6 (String.length aname);
952 w16 s 8 (String.length adata);
953 sendstr1 s 0 (String.length s) fd;
954 state.sock <- fd;
955 setup fd screennum w h;
956 state.t <- t;
957 fd, state.w, state.h;
960 let settitle s =
961 state.setwmname s;
964 let setcursor cursor =
965 if cursor != state.curcurs
966 then
967 let n =
968 match cursor with
969 | CURSOR_INHERIT -> -1
970 | CURSOR_INFO -> 3
971 | CURSOR_CYCLE -> 2
972 | CURSOR_CROSSHAIR -> 0
973 | CURSOR_TEXT -> 5
975 let s = s32 (if n = -1 then 0 else state.idbase+2+n) in
976 let s = changewindowattributesreq state.idbase (*cursor*)0x4000 s in
977 sendstr s state.sock;
978 state.curcurs <- cursor;
981 let fullscreen () =
982 state.fullscreen state.idbase;
985 let metamask = 0x40;;
986 let altmask = 8;;
987 let shiftmask = 1;;
988 let ctrlmask = 4;;
990 let withalt mask = mask land altmask != 0;;
991 let withctrl mask = mask land ctrlmask != 0;;
992 let withshift mask = mask land shiftmask != 0;;
993 let withmeta mask = mask land metamask != 0;;
994 let withnone mask = mask land (altmask + ctrlmask + shiftmask + metamask) = 0;;
996 let xlatt, xlatf =
997 let t = Hashtbl.create 20
998 and f = Hashtbl.create 20 in
999 let add n nl k =
1000 List.iter (fun s -> Hashtbl.add t s k) (n::nl);
1001 Hashtbl.add f k n
1003 let addc c =
1004 let s = String.create 1 in
1005 s.[0] <- c;
1006 add s [] (Char.code c)
1008 let addcr a b =
1009 let an = Char.code a and bn = Char.code b in
1010 for i = an to bn do addc (Char.chr i) done;
1012 addcr '0' '9';
1013 addcr 'a' 'z';
1014 addcr 'A' 'Z';
1015 String.iter addc "`~!@#$%^&*()-_=+\\|[{]};:,./<>?";
1016 for i = 0 to 29 do add ("f" ^ string_of_int (i+1)) [] (0xffbe + i) done;
1017 add "space" [] 0x20;
1018 add "ret" ["return"; "enter"] 0xff0d;
1019 add "tab" [] 0xff09;
1020 add "left" [] 0xff51;
1021 add "right" [] 0xff53;
1022 add "home" [] 0xff50;
1023 add "end" [] 0xff57;
1024 add "ins" ["insert"] 0xff63;
1025 add "del" ["delete"] 0xffff;
1026 add "esc" ["escape"] 0xff1b;
1027 add "pgup" ["pageup"] 0xff55;
1028 add "pgdown" ["pagedown"] 0xff56;
1029 add "backspace" [] 0xff08;
1030 add "up" [] 0xff52;
1031 add "down" [] 0xff54;
1032 t, f;
1035 let keyname k =
1036 try Hashtbl.find xlatf k
1037 with Not_found -> Printf.sprintf "%#x" k;
1040 let namekey name =
1041 try Hashtbl.find xlatt name
1042 with Not_found ->
1043 if String.length name = 1
1044 then Char.code name.[0]
1045 else int_of_string name;