439035aeb768751c435dc57aabeb5e6cce66752b
[llpp.git] / wsi.ml
blob439035aeb768751c435dc57aabeb5e6cce66752b
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
66 ; mutable levl3mask : int
67 ; mutable levl5mask : int
69 and fs =
70 | NoFs
71 | Fs of (int * int * int * int)
74 let state =
75 { mink = max_int
76 ; maxk = min_int
77 ; keymap = [||]
78 ; fifo = Queue.create ()
79 ; seq = 0
80 ; protoatom = -1
81 ; deleatom = -1
82 ; idbase = -1
83 ; fullscreen = (fun _ -> ())
84 ; setwmname = (fun _ -> ())
85 ; sock = Unix.stdin
86 ; t = onot
87 ; x = -1
88 ; y = -1
89 ; w = -1
90 ; h = -1
91 ; fs = NoFs
92 ; stringatom = 31
93 ; curcurs = CURSOR_INHERIT
94 ; capslmask = 0
95 ; levl3mask = 0
96 ; levl5mask = 0
100 let w8 s pos i = String.set s pos (Char.chr (i land 0xff));;
102 let w16 s pos i =
103 w8 s pos i;
104 w8 s (pos+1) (i lsr 8);
107 let w32 s pos i =
108 w16 s pos i;
109 w16 s (pos+2) (i lsr 16);
112 let r16 s pos =
113 let rb pos1 = Char.code (String.get s (pos + pos1)) in
114 (rb 0) lor ((rb 1) lsl 8)
117 let r16s s pos =
118 let i = r16 s pos in
119 i - ((i land 0x8000) lsl 1);
122 let r8 s pos = Char.code (String.get s pos);;
124 let r32 s pos =
125 let rb pos1 = Char.code (String.get s (pos + pos1)) in
126 let l = (rb 0) lor ((rb 1) lsl 8)
127 and u = (rb 2) lor ((rb 3) lsl 8) in
128 (u lsl 16) lor l
131 let exntos = function
132 | Unix.Unix_error (e, s, a) -> Printf.sprintf "%s(%s):%s(%d)"
133 s a (Unix.error_message e) (Obj.magic e)
134 | exn -> Printexc.to_string exn;
137 let error fmt = Printf.kprintf failwith fmt;;
139 let readstr sock n =
140 let s = String.create n in
141 let rec loop pos n =
142 let m = Unix.read sock s pos n in
143 if m = 0
144 then state.t#quit;
145 if n != m
146 then (
147 ignore (Unix.select [sock] [] [] 0.01);
148 loop (pos + m) (n - m)
151 loop 0 n;
155 let sendstr1 s pos len sock =
156 vlog "%d => %S" state.seq s;
157 state.seq <- state.seq + 1;
158 let n = Unix.send sock s pos len [] in
159 if n != len
160 then error "send %d returned %d" len n;
163 let updkmap sock resp =
164 let syms = r8 resp 1 in
165 let len = r32 resp 4 in
166 let data =
167 if len > 0
168 then readstr sock (4*len)
169 else ""
171 let m = len / syms in
172 state.keymap <- Array.make_matrix
173 (state.maxk - state.mink) syms 0xffffff;
174 let rec loop i = if i = m then () else
175 let k = i*4*syms in
176 let rec loop2 k l = if l = syms then () else
177 let v = r32 data k in
178 state.keymap.(i).(l) <- v;
179 loop2 (k+4) (l+1)
181 loop2 k 0;
182 loop (i+1);
184 loop 0;
187 let updmodmap sock resp =
188 let n = r8 resp 1 in
189 let len = r16 resp 4 in
190 let data =
191 if len > 0
192 then readstr sock (len*4)
193 else ""
195 let modmap = Array.make_matrix 8 n 0xffffff in
196 let rec loop l = if l = 8 then () else
197 let p = l*n in
198 let rec loop1 m = if m = n then () else
199 let p = p+m in
200 let code = r8 data p in
201 modmap.(l).(m) <- code;
202 if l = 1
203 then (
204 let ki = code - state.mink in
205 if ki >= 0
206 then
207 let a = state.keymap.(ki) in
208 let rec capsloop i = if i = Array.length a || i > 3 then () else
209 let s = a.(i) in
210 if s = 0xffe5
211 then state.capslmask <- 2
212 else capsloop (i+1)
214 capsloop 0;
216 else (
217 if l > 3
218 then (
219 let ki = code - state.mink in
220 if ki >= 0
221 then
222 let a = state.keymap.(ki) in
223 let rec lloop i = if i = Array.length a || i > 3 then () else
224 let s = a.(i) in
225 match s with
226 | 0xfe03 -> state.levl3mask <- 1 lsl l
227 | 0xfe11 -> state.levl5mask <- 1 lsl l
228 | _ -> lloop (i+1)
230 lloop 0;
233 loop1 (m+1)
235 loop1 0;
236 loop (l+1)
238 loop 0;
241 let sendwithrep sock s f =
242 Queue.push f state.fifo;
243 sendstr1 s 0 (String.length s) sock;
246 let padcatl ss =
247 let b = Buffer.create 16 in
248 List.iter (Buffer.add_string b) ss;
249 let bl = Buffer.length b in
250 let pl = bl land 3 in
251 if pl != 0
252 then (
253 let pad = "123" in
254 Buffer.add_substring b pad 0 (4 - pl);
256 Buffer.contents b;
259 let padcat s1 s2 = padcatl [s1; s2];;
261 let internreq name onlyifexists =
262 let s = "\016\000\000\000\000\000\000\000" in
263 let s = padcat s name in
264 if onlyifexists then w8 s 1 1;
265 w16 s 2 (String.length s / 4);
266 w16 s 4 (String.length name);
270 let sendintern sock s onlyifexists f =
271 let s = internreq s onlyifexists in
272 sendwithrep sock s f;
275 let createwindowreq wid parent x y w h bw mask =
276 let s = "\001\000\009\000wwwwppppxxyywwhhbwccvvvvmmmmeeee" in
277 w32 s 4 wid;
278 w32 s 8 parent;
279 w16 s 12 x;
280 w16 s 14 y;
281 w16 s 16 w;
282 w16 s 18 h;
283 w16 s 20 bw;
284 w16 s 22 1;
285 w32 s 24 0;
286 w32 s 28 0x800; (* eventmask *)
287 w32 s 32 mask;
291 let getgeometryreq wid =
292 let s = "\014u\002\000dddd" in
293 w32 s 4 wid;
297 let mapreq wid =
298 let s = "\008u\002\000wwww" in
299 w32 s 4 wid;
303 let getkeymapreq first count =
304 let s = "\101u\002\000fcuu" in
305 w8 s 4 first;
306 w8 s 5 count;
310 let changepropreq wid prop typ format props =
311 let s = "\018\000llwwwwppppttttfuuuLLLL" in
312 let s = padcat s props in
313 w16 s 2 (String.length s / 4);
314 w32 s 4 wid;
315 w32 s 8 prop;
316 w32 s 12 typ;
317 w8 s 16 format;
318 let ful = String.length props / (match format with
319 | 8 -> 1
320 | 16 -> 2
321 | 32 -> 4
322 | n -> error "no idea what %d means" n)
324 w32 s 20 ful;
328 let openfontreq fid name =
329 let s = "\045ullffffnnuu" in
330 let s = padcat s name in
331 w16 s 2 (String.length s / 4);
332 w32 s 4 fid;
333 w16 s 8 (String.length name);
337 let createglyphcursorreq fid cid cindex =
338 let s = "\094u\008\000ccccffffffffssmmrrggbbRRGGBB" in
339 w32 s 4 cid;
340 w32 s 8 fid;
341 w32 s 12 fid;
342 w16 s 16 cindex;
343 w16 s 18 (cindex+1);
344 w16 s 20 0;
345 w16 s 22 0;
346 w16 s 24 0;
347 w16 s 26 0xffff;
348 w16 s 28 0xffff;
349 w16 s 30 0xffff;
353 let changewindowattributesreq wid mask attrs =
354 let s = "\002ullwwwwmmmm" in
355 let s = padcat s attrs in
356 w16 s 2 (String.length s / 4);
357 w32 s 4 wid;
358 w32 s 8 mask;
362 let configurewindowreq wid mask values =
363 let s = "\012ullwwwwmmuu" in
364 let s = padcat s values in
365 w16 s 2 (String.length s / 4);
366 w32 s 4 wid;
367 w16 s 8 mask;
371 let s32 n =
372 let s = "1234" in
373 w32 s 0 n;
377 let clientmessage format seq wid typ data =
378 let s = "\033fsswwwwtttt" in
379 let s = padcat s data in
380 w8 s 1 format;
381 w16 s 2 seq;
382 w32 s 4 wid;
383 w32 s 8 typ;
387 let sendeventreq propagate destwid mask data =
388 let s = "\025p\011\000wwwwmmmm" in
389 let s = padcat s data in
390 w8 s 1 propagate;
391 w32 s 4 destwid;
392 w32 s 8 mask;
396 let getmodifiermappingreq () =
397 let s = "\119u\001\000" in
401 let getkeysym code mask =
402 let shift = (mask land 1) lxor ((mask land state.capslmask) lsr 1) in
403 let index =
404 let l3 = (mask land state.levl3mask) != 0 in
405 let l4 = (mask land state.levl5mask) != 0 in
406 shift +
407 if l3 then (if l4 then 8 else 4) else (if l4 then 6 else 0)
409 let keysym = state.keymap.(code-state.mink).(index) in
410 if index land 1 = 1 && keysym = 0
411 then state.keymap.(code-state.mink).(index - 1)
412 else keysym
415 let readresp sock =
416 let resp = readstr sock 32 in
417 let opcode = r8 resp 0 in
418 match opcode land lnot 0x80 with
419 | 0 -> (* error *)
420 let s = resp in
421 let code = r8 s 1
422 and serial = r16 s 2
423 and resid = r32 resp 4
424 and min = r16 s 8
425 and maj = r8 s 10 in
426 error "code=%d serial=%d resid=%#x min=%d maj=%d\n%S"
427 code serial resid min maj resp;
429 | 1 -> (* response *)
430 let rep = Queue.pop state.fifo in
431 rep resp;
433 | 2 -> (* key press *)
434 if Array.length state.keymap > 0
435 then
436 let code = r8 resp 1 in
437 let mask = r16 resp 28 in
438 let keysym = getkeysym code mask in
439 vlog "keysym = %x %c mask %#x code %d"
440 keysym (Char.unsafe_chr keysym) mask code;
441 state.t#key keysym mask;
443 | 3 -> (* key release *)
444 if Array.length state.keymap > 0
445 then
446 let code = r8 resp 1 in
447 let mask = r16 resp 28 in
448 let keysym = getkeysym code mask in
449 vlog "release keysym = %x %c mask %#x code %#d"
450 keysym (Char.unsafe_chr keysym) mask code
452 | 4 -> (* buttonpress *)
453 let n = r8 resp 1
454 and x = r16s resp 24
455 and y = r16s resp 26
456 and m = r16 resp 28 in
457 state.t#mouse n true x y m;
458 vlog "press %d" n
460 | 5 -> (* buttonrelease *)
461 let n = r8 resp 1
462 and x = r16s resp 24
463 and y = r16s resp 26
464 and m = r16 resp 28 in
465 state.t#mouse n false x y m;
466 vlog "release %d %d %d" n x y
468 | 6 -> (* motion *)
469 let x = r16s resp 24 in
470 let y = r16s resp 26 in
471 let m = r16 resp 28 in
472 if m land 0x1f00 = 0
473 then state.t#pmotion x y
474 else state.t#motion x y;
475 vlog "move %dx%d => %d" x y m
477 | 7 -> (* enter *)
478 let x = r16s resp 24
479 and y = r16s resp 26 in
480 state.t#enter x y;
481 vlog "enter %d %d" x y
483 | 8 -> (* leave *)
484 state.t#leave
486 | 18 -> vlog "unmap";
488 | 19 -> (* map *)
489 vlog "map";
491 | 12 -> (* exposure *)
492 vlog "exposure";
493 state.t#expose
495 | 15 -> (* visibility *)
496 let vis = r8 resp 8 in
497 if vis != 2 then state.t#expose;
498 vlog "visibility %d" vis;
500 | 34 -> (* mapping *)
501 state.keymap <- [||];
502 let s = getkeymapreq state.mink (state.maxk-state.mink-1) in
503 sendwithrep sock s (updkmap sock);
504 state.capslmask <- 0;
505 state.levl3mask <- 0;
506 state.levl5mask <- 0;
507 let s = getmodifiermappingreq () in
508 sendwithrep sock s (updmodmap sock);
511 | 33 -> (* clientmessage *)
512 let atom = r32 resp 8 in
513 if atom = state.protoatom
514 then (
515 let atom = r32 resp 12 in
516 if atom = state.deleatom
517 then state.t#quit;
519 vlog "atom %#x" atom
521 | 21 -> (* reparent *)
522 vlog "reparent"
524 | 22 -> (* configure *)
525 let x = r16s resp 16
526 and y = r16s resp 18
527 and w = r16 resp 20
528 and h = r16 resp 22 in
529 vlog "configure cur [%d %d %d %d] conf [%d %d %d %d]"
530 state.x state.y state.w state.h
531 x y w h
533 glxsync ();
534 if w != state.w || h != state.h
535 then (
536 state.t#reshape w h;
538 state.w <- w;
539 state.h <- h;
540 state.x <- x;
541 state.y <- y;
542 state.t#expose
544 | n ->
545 dolog "event %d %S" n resp
548 let readresp sock =
549 let rec loop () =
550 readresp sock;
551 if hasdata sock then loop ();
553 loop ();
556 let sendstr s ?(pos=0) ?(len=String.length s) sock =
557 sendstr1 s pos len sock;
558 if hasdata sock then readresp sock;
561 let reshape w h =
562 if state.fs = NoFs
563 then
564 let s = "wwuuhhuu" in
565 w32 s 0 w;
566 w32 s 4 h;
567 let s = configurewindowreq state.idbase 0x000c s in
568 sendstr s state.sock;
569 else state.fullscreen state.idbase
572 let syncsendwithrep sock secstowait s f =
573 let completed = ref false in
574 sendwithrep sock s (fun resp -> f resp; completed := true);
575 let now = Unix.gettimeofday in
576 let deadline = now () +. secstowait in
577 let rec readtillcompletion () =
578 let r, _, _ = Unix.select [sock] [] [] (deadline -. now ()) in
579 match r with
580 | [] -> error "didn't get X response in %f seconds, aborting" secstowait
581 | _ ->
582 readresp sock;
583 if not !completed
584 then readtillcompletion ()
586 readtillcompletion ();
589 let syncsendintern sock secstowait s onlyifexists f =
590 let s = internreq s onlyifexists in
591 syncsendwithrep sock secstowait s f;
594 let setup sock screennum w h =
595 let s = readstr sock 2 in
596 let n = String.length s in
597 if n != 2
598 then error "failed to read X connection setup response n=%d" n;
599 match s.[0] with
600 | '\000' ->
601 let reasonlen = r8 s 1 in
602 let s = readstr sock 6 in
603 let maj = r16 s 0
604 and min = r16 s 2
605 and add = r16 s 4 in
606 let len = add*4 in
607 let data = readstr sock len in
608 let reason = String.sub data 0 reasonlen in
609 error "X connection failed maj=%d min=%d reason=%S"
610 maj min reason
612 | '\002' -> failwith "X connection setup failed: authentication required";
614 | '\001' ->
615 let s = readstr sock 38 in
616 let maj = r16 s 0
617 and min = r16 s 2
618 and add = r16 s 4
619 and idbase = r32 s 10
620 and idmask = r32 s 14
621 and vlen = r16 s 22
622 and screens = r8 s 26
623 and formats = r8 s 27
624 and minkk = r8 s 32
625 and maxkk = r8 s 33 in
626 let data = readstr sock (4*add-32) in
627 let vendor = String.sub data 0 vlen in
628 let pos = ((vlen+3) land lnot 3) + formats*8 in
630 if screennum >= screens
631 then error "invalid screen %d, max %d" screennum (screens-1);
633 let pos =
634 let s = data in
635 let rec findscreen n pos = if n = screennum then pos else
636 let pos =
637 let ndepths = r8 s (pos+39) in
638 let rec skipdepths n pos = if n = ndepths then pos else
639 let pos =
640 let nvisiuals = r16 s (pos+2) in
641 pos + nvisiuals*24 + 8
643 skipdepths (n+1) pos
645 skipdepths n (pos+40)
647 findscreen (n+1) pos
649 findscreen 0 pos
651 let root = r32 data pos in
652 let rootw = r16 data (pos+20)
653 and rooth = r16 data (pos+22) in
654 state.mink <- minkk;
655 state.maxk <- maxkk;
656 state.idbase <- idbase;
657 vlog "vendor = %S, maj=%d min=%d" vendor maj min;
658 vlog "screens = %d formats = %d" screens formats;
659 vlog "minkk = %d maxkk = %d" minkk maxkk;
660 vlog "idbase = %#x idmask = %#x" idbase idmask;
661 vlog "root=%#x %dx%d" root rootw rooth;
663 let mask = 0
664 + 0x00000001 (* KeyPress *)
665 (* + 0x00000002 *) (* KeyRelease *)
666 + 0x00000004 (* ButtonPress *)
667 + 0x00000008 (* ButtonRelease *)
668 + 0x00000010 (* EnterWindow *)
669 + 0x00000020 (* LeaveWindow *)
670 + 0x00000040 (* PointerMotion *)
671 (* + 0x00000080 *) (* PointerMotionHint *)
672 (* + 0x00000100 *) (* Button1Motion *)
673 (* + 0x00000200 *) (* Button2Motion *)
674 (* + 0x00000400 *) (* Button3Motion *)
675 (* + 0x00000800 *) (* Button4Motion *)
676 (* + 0x00001000 *) (* Button5Motion *)
677 + 0x00002000 (* ButtonMotion *)
678 (* + 0x00004000 *) (* KeymapState *)
679 + 0x00008000 (* Exposure *)
680 + 0x00010000 (* VisibilityChange *)
681 + 0x00020000 (* StructureNotify *)
682 (* + 0x00040000 *) (* ResizeRedirect *)
683 (* + 0x00080000 *) (* SubstructureNotify *)
684 (* + 0x00100000 *) (* SubstructureRedirect *)
685 (* + 0x00200000 *) (* FocusChange *)
686 (* + 0x00400000 *) (* PropertyChange *)
687 (* + 0x00800000 *) (* ColormapChange *)
688 (* + 0x01000000 *) (* OwnerGrabButton *)
690 let wid = state.idbase in
691 let s = createwindowreq wid root 0 0 w h 0 mask in
692 sendstr s sock;
694 sendintern sock "WM_PROTOCOLS" false (fun resp ->
695 state.protoatom <- r32 resp 8;
696 sendintern sock "WM_DELETE_WINDOW" false (fun resp ->
697 state.deleatom <- r32 resp 8;
698 let s = s32 state.deleatom in
699 let s = changepropreq wid state.protoatom 4 32 s in
700 sendstr s sock;
704 syncsendintern sock 2.0 "WM_CLASS" false (fun resp ->
705 let atom = r32 resp 8 in
706 let llpp = "llpp\000llpp\000" in
707 let s = changepropreq wid atom 31 8 llpp in
708 sendstr s sock;
711 let s = mapreq wid in
712 sendstr s sock;
714 let s = getkeymapreq state.mink (state.maxk-state.mink) in
715 sendwithrep sock s (updkmap sock);
717 let s = getmodifiermappingreq () in
718 sendwithrep sock s (updmodmap sock);
720 let s = openfontreq (wid+1) "cursor" in
721 sendstr s sock;
723 Array.iteri (fun i glyphindex ->
724 let s = createglyphcursorreq (wid+1) (wid+2+i) glyphindex in
725 sendstr s sock;
726 ) [|34;48;50;58;128;152|];
728 sendintern sock "UTF8_STRING" true (fun resp ->
729 let atom = r32 resp 8 in
730 if atom != 0
731 then state.stringatom <- atom;
734 let setwmname s =
735 let s = changepropreq state.idbase 39 state.stringatom 8 s in
736 sendstr s state.sock;
738 state.setwmname <- setwmname;
739 sendintern sock "_NET_WM_NAME" true (fun resp ->
740 let atom = r32 resp 8 in
741 if atom != 0
742 then state.setwmname <- (fun s ->
743 setwmname s;
744 let s = changepropreq state.idbase atom state.stringatom 8 s in
745 sendstr s state.sock;
749 state.fullscreen <- (fun wid ->
750 let s = "xxuuyyuuwwuuhhuu" in
751 match state.fs with
752 | NoFs ->
753 w32 s 0 0;
754 w32 s 4 0;
755 w32 s 8 rootw;
756 w32 s 12 rooth;
757 let s = configurewindowreq wid 0x000f s in
758 sendstr s state.sock;
759 state.fs <- Fs (state.x, state.y, state.w, state.h);
761 | Fs (x, y, w, h) ->
762 w32 s 0 x;
763 w32 s 4 y;
764 w32 s 8 w;
765 w32 s 12 h;
766 let s = configurewindowreq wid 0x000f s in
767 sendstr s state.sock;
768 state.fs <- NoFs;
771 sendintern sock "_NET_WM_STATE" true (fun resp ->
772 let nwmsatom = r32 resp 8 in
773 if nwmsatom != 0
774 then
775 sendintern sock "_NET_WM_STATE_FULLSCREEN" true (fun resp ->
776 let fsatom = r32 resp 8 in
777 if fsatom != 0
778 then
779 state.fullscreen <-
780 (fun wid ->
781 let data = String.make 20 '\000' in
782 let fs, f =
783 match state.fs with
784 | NoFs -> Fs (-1, -1, -1, -1), 1
785 | Fs _ -> NoFs, 0
787 w32 data 0 f;
788 w32 data 4 fsatom;
790 let cm = clientmessage 32 0 wid nwmsatom data in
791 let s = sendeventreq 0 root 0x180000 cm in
792 sendstr s sock;
793 state.fs <- fs;
797 let s = getgeometryreq wid in
798 syncsendwithrep sock 2.0 s (fun resp ->
799 glx wid;
800 let w = r16 resp 16
801 and h = r16 resp 18 in
802 state.w <- w;
803 state.h <- h;
806 | c ->
807 error "unknown conection setup response %d" (Char.code c)
810 let getauth haddr dnum =
811 let haddr =
812 if haddr = "localhost" || String.length haddr = 0
813 then
814 try Unix.gethostname ()
815 with exn ->
816 dolog "failed to resolve `%S': %s" haddr (exntos exn);
817 haddr
818 else haddr
820 let path =
821 try Sys.getenv "XAUTHORITY"
822 with Not_found ->
823 try Filename.concat (Sys.getenv "HOME") ".Xauthority"
824 with Not_found -> ""
826 let readauth ic =
827 let input_string ic len =
828 let s = String.create len in
829 really_input ic s 0 len;
832 let r16 s =
833 let rb pos = Char.code (String.get s pos) in
834 (rb 1) lor ((rb 0) lsl 8)
836 let rec find () =
837 let rs () =
838 let s = input_string ic 2 in
839 let n = r16 s in
840 input_string ic n
842 let family = input_string ic 2 in
843 let addr = rs () in
844 let nums = rs () in
845 let optnum =
846 try Some (int_of_string nums)
847 with exn ->
848 dolog
849 "display number(%S) is not an integer (corrupt %S?): %s"
850 nums path (exntos exn);
851 None
853 let name = rs () in
854 let data = rs () in
856 vlog "family %S addr %S(%S) num %S(%d) name %S data %S"
857 family addr haddr nums dnum name data;
858 match optnum with
859 | Some num when addr = haddr && num = dnum ->
860 name, data
861 | _ -> find ()
863 let name, data =
864 try find ()
865 with
866 | End_of_file -> "", ""
867 | exn ->
868 dolog "exception while reading X authority data (%S): %s"
869 path (exntos exn);
870 "", ""
872 close_in ic;
873 name, data;
875 let opt =
877 if String.length path = 0
878 then None
879 else Some (open_in_bin path)
880 with exn ->
881 if Sys.file_exists path
882 then
883 dolog "failed to open X authority file `%S' : %s"
884 path (exntos exn);
885 None
887 match opt with
888 | None -> "", ""
889 | Some ic -> readauth ic
892 let init t w h osx =
893 let d =
894 try Sys.getenv "DISPLAY"
895 with exn ->
896 error "could not get DISPLAY evironment variable: %s"
897 (exntos exn)
899 let getnum w b e =
900 if b = e
901 then error "invalid DISPLAY(%s) %S" w d
902 else
903 let s = String.sub d b (e - b) in
904 try int_of_string s
905 with exn ->
906 error "invalid DISPLAY %S can not parse %s(%S): %s"
907 d w s (exntos exn)
909 let rec phost pos =
910 if pos = String.length d
911 then error "invalid DISPLAY %S no display number specified" d
912 else (
913 if d.[pos] = ':'
914 then
915 let rec pdispnum pos1 =
916 if pos1 = String.length d
917 then getnum "display number" (pos+1) pos1, 0
918 else
919 match d.[pos1] with
920 | '.' ->
921 let dispnum = getnum "display number" (pos+1) pos1 in
922 let rec pscreennum pos2 =
923 if pos2 = String.length d
924 then getnum "screen number" (pos1+1) pos2
925 else
926 match d.[pos2] with
927 | '0' .. '9' -> pscreennum (pos2+1)
928 | _ ->
929 error "invalid DISPLAY %S, cannot parse screen number" d
931 dispnum, pscreennum (pos1+1)
932 | '0' .. '9' -> pdispnum (pos1+1)
933 | _ ->
934 error "invalid DISPLAY %S, cannot parse display number" d
936 String.sub d 0 pos, pdispnum (pos+1)
937 else phost (pos+1)
940 let host, (dispnum, screennum) = phost 0 in
941 let aname, adata = getauth host dispnum in
942 let fd =
943 let fd, addr =
944 if osx || String.length host = 0 || host = "unix"
945 then
946 let addr =
947 if osx
948 then Unix.ADDR_UNIX d
949 else Unix.ADDR_UNIX ("/tmp/.X11-unix/X" ^ string_of_int dispnum)
951 let fd = Unix.socket Unix.PF_UNIX Unix.SOCK_STREAM 0 in
952 fd, addr
953 else
954 let h =
955 try Unix.gethostbyname host
956 with exn ->
957 error "cannot resolve %S: %s" host (exntos exn)
959 let addr = h.Unix.h_addr_list.(0) in
960 let port = 6000 + dispnum in
961 let fd = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
962 fd, (Unix.ADDR_INET (addr, port))
964 try Unix.connect fd addr; fd
965 with exn ->
966 error "failed to connect to X: %s" (exntos exn)
968 cloexec fd;
969 let s = "luMMmmnndduu" in
970 let s = padcat s aname in
971 let s = padcat s adata in
972 w16 s 2 11;
973 w16 s 4 0;
974 w16 s 6 (String.length aname);
975 w16 s 8 (String.length adata);
976 sendstr1 s 0 (String.length s) fd;
977 state.sock <- fd;
978 setup fd screennum w h;
979 state.t <- t;
980 fd, state.w, state.h;
983 let settitle s =
984 state.setwmname s;
987 let setcursor cursor =
988 if cursor != state.curcurs
989 then
990 let n =
991 match cursor with
992 | CURSOR_INHERIT -> -1
993 | CURSOR_INFO -> 3
994 | CURSOR_CYCLE -> 2
995 | CURSOR_CROSSHAIR -> 0
996 | CURSOR_TEXT -> 5
998 let s = s32 (if n = -1 then 0 else state.idbase+2+n) in
999 let s = changewindowattributesreq state.idbase (*cursor*)0x4000 s in
1000 sendstr s state.sock;
1001 state.curcurs <- cursor;
1004 let fullscreen () =
1005 state.fullscreen state.idbase;
1008 let metamask = 0x40;;
1009 let altmask = 8;;
1010 let shiftmask = 1;;
1011 let ctrlmask = 4;;
1013 let withalt mask = mask land altmask != 0;;
1014 let withctrl mask = mask land ctrlmask != 0;;
1015 let withshift mask = mask land shiftmask != 0;;
1016 let withmeta mask = mask land metamask != 0;;
1017 let withnone mask = mask land (altmask + ctrlmask + shiftmask + metamask) = 0;;
1019 let xlatt, xlatf =
1020 let t = Hashtbl.create 20
1021 and f = Hashtbl.create 20 in
1022 let add n nl k =
1023 List.iter (fun s -> Hashtbl.add t s k) (n::nl);
1024 Hashtbl.add f k n
1026 let addc c =
1027 let s = String.create 1 in
1028 s.[0] <- c;
1029 add s [] (Char.code c)
1031 let addcr a b =
1032 let an = Char.code a and bn = Char.code b in
1033 for i = an to bn do addc (Char.chr i) done;
1035 addcr '0' '9';
1036 addcr 'a' 'z';
1037 addcr 'A' 'Z';
1038 String.iter addc "`~!@#$%^&*()-_=+\\|[{]};:,./<>?";
1039 for i = 0 to 29 do add ("f" ^ string_of_int (i+1)) [] (0xffbe + i) done;
1040 add "space" [] 0x20;
1041 add "ret" ["return"; "enter"] 0xff0d;
1042 add "tab" [] 0xff09;
1043 add "left" [] 0xff51;
1044 add "right" [] 0xff53;
1045 add "home" [] 0xff50;
1046 add "end" [] 0xff57;
1047 add "ins" ["insert"] 0xff63;
1048 add "del" ["delete"] 0xffff;
1049 add "esc" ["escape"] 0xff1b;
1050 add "pgup" ["pageup"] 0xff55;
1051 add "pgdown" ["pagedown"] 0xff56;
1052 add "backspace" [] 0xff08;
1053 add "up" [] 0xff52;
1054 add "down" [] 0xff54;
1055 t, f;
1058 let keyname k =
1059 try Hashtbl.find xlatf k
1060 with Not_found -> Printf.sprintf "%#x" k;
1063 let namekey name =
1064 try Hashtbl.find xlatt name
1065 with Not_found ->
1066 if String.length name = 1
1067 then Char.code name.[0]
1068 else int_of_string name;