chiark / gitweb /
New option: movres.ignore_transience.
[e16] / eesh / comms.c
1 /*
2  * Copyright (C) 2000-2007 Carsten Haitzler, Geoff Harrison and various contributors
3  * Copyright (C) 2004-2008 Kim Woelders
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy
6  * of this software and associated documentation files (the "Software"), to
7  * deal in the Software without restriction, including without limitation the
8  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
9  * sell copies of the Software, and to permit persons to whom the Software is
10  * furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies of the Software, its documentation and marketing & publicity
14  * materials, and acknowledgment shall be given in the documentation, materials
15  * and software packages that this Software was used.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20  * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
21  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */
24 #include "E.h"
25
26 static Window       root_win;
27 static Window       my_win;
28
29 void
30 CommsInit(void)
31 {
32    char               *str;
33
34    str = getenv("ENL_WM_ROOT");
35    root_win = (str) ? strtoul(str, NULL, 0) : DefaultRootWindow(disp);
36 }
37
38 Window
39 CommsSetup(Window win __UNUSED__)
40 {
41    XSetWindowAttributes attr;
42
43    attr.override_redirect = False;
44    my_win = XCreateWindow(disp, root_win, -100, -100, 5, 5, 0, 0, InputOnly,
45                           DefaultVisual(disp, DefaultScreen(disp)),
46                           CWOverrideRedirect, &attr);
47
48    return my_win;
49 }
50
51 Window
52 CommsFindCommsWindow(void)
53 {
54    unsigned char      *s;
55    Atom                a, ar;
56    unsigned long       num, after;
57    int                 format;
58    Window              rt, comms_win;
59    int                 dint;
60    unsigned int        duint;
61
62    comms_win = None;
63
64    a = XInternAtom(disp, "ENLIGHTENMENT_COMMS", True);
65    if (a == None)
66       return None;
67
68    s = NULL;
69    XGetWindowProperty(disp, root_win, a, 0, 14, False, AnyPropertyType,
70                       &ar, &format, &num, &after, &s);
71    if (!s)
72       return None;
73
74    sscanf((char *)s, "%*s %lx", &comms_win);
75    XFree(s);
76    if (comms_win == None)
77       return None;
78
79    if (!XGetGeometry(disp, comms_win, &rt, &dint, &dint,
80                      &duint, &duint, &duint, &duint))
81       return None;
82
83    s = NULL;
84    XGetWindowProperty(disp, comms_win, a, 0, 14, False,
85                       AnyPropertyType, &ar, &format, &num, &after, &s);
86    if (!s)
87       return None;
88    XFree(s);
89
90    XSelectInput(disp, comms_win, StructureNotifyMask | SubstructureNotifyMask);
91
92    return comms_win;
93 }
94
95 void
96 CommsSend(Client * c, const char *s)
97 {
98    char                ss[20];
99    int                 i, j, k, len;
100    XEvent              ev;
101    Atom                a;
102
103    if ((!s) || (!c) || (c->win == None))
104       return;
105
106    a = XInternAtom(disp, "ENL_MSG", True);
107
108    ev.xclient.type = ClientMessage;
109    ev.xclient.serial = 0;
110    ev.xclient.send_event = True;
111    ev.xclient.window = c->win;
112    ev.xclient.message_type = a;
113    ev.xclient.format = 8;
114
115    len = strlen(s);
116    for (i = 0; i < len + 1; i += 12)
117      {
118         sprintf(ss, "%8x", (int)my_win);
119         for (j = 0; j < 12; j++)
120           {
121              ss[8 + j] = s[i + j];
122              if (!s[i + j])
123                 j = 12;
124           }
125         for (k = 0; k < 20; k++)
126            ev.xclient.data.b[k] = ss[k];
127         XSendEvent(disp, c->win, False, 0, &ev);
128      }
129 }
130
131 char               *
132 CommsGet(Client * c, XEvent * ev)
133 {
134    char                s[13], s2[9], *msg;
135    unsigned int        i;
136    Window              win;
137
138    if ((!ev) || (!c))
139       return NULL;
140    if (ev->type != ClientMessage)
141       return NULL;
142
143    s[12] = 0;
144    s2[8] = 0;
145    msg = NULL;
146
147    for (i = 0; i < 8; i++)
148       s2[i] = ev->xclient.data.b[i];
149    for (i = 0; i < 12; i++)
150       s[i] = ev->xclient.data.b[i + 8];
151
152    sscanf(s2, "%lx", &win);
153
154    /* append text to end of msg */
155    i = (c->msg) ? strlen(c->msg) : 0;
156    c->msg = EREALLOC(char, c->msg, i + strlen(s) + 1);
157
158    if (!c->msg)
159       return NULL;
160    strcpy(c->msg + i, s);
161
162    if (strlen(s) < 12)
163      {
164         msg = c->msg;
165         c->msg = NULL;
166      }
167
168    return msg;
169 }
170
171 Client             *
172 ClientCreate(Window win)
173 {
174    Client             *c;
175
176    c = EMALLOC(Client, 1);
177    if (!c)
178       return NULL;
179
180    c->win = win;
181    c->msg = NULL;
182
183    return c;
184 }
185
186 void
187 ClientDestroy(Client * c)
188 {
189    if (!c)
190       return;
191
192    Efree(c->msg);
193    Efree(c);
194 }