1
2/*
3
4Copyright 1985, 1986, 1987, 1998  The Open Group
5
6Permission to use, copy, modify, distribute, and sell this software and its
7documentation for any purpose is hereby granted without fee, provided that
8the above copyright notice appear in all copies and that both that
9copyright notice and this permission notice appear in supporting
10documentation.
11
12The above copyright notice and this permission notice shall be included
13in all copies or substantial portions of the Software.
14
15THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18IN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR
19OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21OTHER DEALINGS IN THE SOFTWARE.
22
23Except as contained in this notice, the name of The Open Group shall
24not be used in advertising or otherwise to promote the sale, use or
25other dealings in this Software without prior written authorization
26from The Open Group.
27
28*/
29
30/*
31 *	XEvToWire.c - Internal support routines for the C subroutine
32 *	interface library (Xlib) to the X Window System Protocol V11.0.
33 */
34
35#ifdef HAVE_CONFIG_H
36#include <config.h>
37#endif
38#include "Xlibint.h"
39
40/*
41 * Reformat an XEvent structure to a wire event of the right type.
42 * Return True on success.  If the type is unrecognized, return what
43 * _XUnknownNativeEvent returns (i.e., False).
44 */
45Status
46_XEventToWire(
47    register Display *dpy,
48    register XEvent *re,        /* in: from */
49    register xEvent *event)     /* out: to */
50{
51	switch (event->u.u.type = re->type) {
52	      case KeyPress:
53	      case KeyRelease:
54	        {
55			register XKeyEvent *ev = (XKeyEvent*) re;
56			event->u.keyButtonPointer.root	= ev->root;
57			event->u.keyButtonPointer.event	 = ev->window;
58			event->u.keyButtonPointer.child  = ev->subwindow;
59			event->u.keyButtonPointer.time	 = ev->time;
60			event->u.keyButtonPointer.eventX = ev->x ;
61			event->u.keyButtonPointer.eventY = ev->y ;
62			event->u.keyButtonPointer.rootX	 = ev->x_root;
63			event->u.keyButtonPointer.rootY  = ev->y_root;
64			event->u.keyButtonPointer.state  = ev->state;
65			event->u.keyButtonPointer.sameScreen = ev->same_screen;
66			event->u.u.detail = ev->keycode;
67		}
68	      	break;
69	      case ButtonPress:
70	      case ButtonRelease:
71	        {
72			register XButtonEvent *ev =  (XButtonEvent *) re;
73			event->u.keyButtonPointer.root	 = ev->root;
74			event->u.keyButtonPointer.event	 = ev->window;
75			event->u.keyButtonPointer.child	 = ev->subwindow;
76			event->u.keyButtonPointer.time	 = ev->time;
77			event->u.keyButtonPointer.eventX = ev->x;
78			event->u.keyButtonPointer.eventY = ev->y;
79			event->u.keyButtonPointer.rootX	 = ev->x_root;
80			event->u.keyButtonPointer.rootY	 = ev->y_root;
81			event->u.keyButtonPointer.state	 = ev->state;
82			event->u.keyButtonPointer.sameScreen	= ev->same_screen;
83			event->u.u.detail		= ev->button;
84		}
85	        break;
86	      case MotionNotify:
87	        {
88			register XMotionEvent *ev =   (XMotionEvent *)re;
89			event->u.keyButtonPointer.root	= ev->root;
90			event->u.keyButtonPointer.event	= ev->window;
91			event->u.keyButtonPointer.child	= ev->subwindow;
92			event->u.keyButtonPointer.time	= ev->time;
93			event->u.keyButtonPointer.eventX= ev->x;
94			event->u.keyButtonPointer.eventY= ev->y;
95			event->u.keyButtonPointer.rootX	= ev->x_root;
96			event->u.keyButtonPointer.rootY	= ev->y_root;
97			event->u.keyButtonPointer.state	= ev->state;
98			event->u.keyButtonPointer.sameScreen= ev->same_screen;
99			event->u.u.detail		= ev->is_hint;
100		}
101	        break;
102	      case EnterNotify:
103	      case LeaveNotify:
104		{
105			register XCrossingEvent *ev   = (XCrossingEvent *) re;
106			event->u.enterLeave.root	= ev->root;
107			event->u.enterLeave.event	= ev->window;
108			event->u.enterLeave.child	= ev->subwindow;
109			event->u.enterLeave.time	= ev->time;
110			event->u.enterLeave.eventX	= ev->x;
111			event->u.enterLeave.eventY	= ev->y;
112			event->u.enterLeave.rootX	= ev->x_root;
113			event->u.enterLeave.rootY	= ev->y_root;
114			event->u.enterLeave.state	= ev->state;
115			event->u.enterLeave.mode	= ev->mode;
116			event->u.enterLeave.flags	= 0;
117			if (ev->same_screen) {
118				event->u.enterLeave.flags |= ELFlagSameScreen;
119				}
120			if (ev->focus) {
121				event->u.enterLeave.flags |= ELFlagFocus;
122				}
123			event->u.u.detail		= ev->detail;
124		}
125		  break;
126	      case FocusIn:
127	      case FocusOut:
128		{
129			register XFocusChangeEvent *ev = (XFocusChangeEvent *) re;
130			event->u.focus.window	= ev->window;
131			event->u.focus.mode	= ev->mode;
132			event->u.u.detail	= ev->detail;
133		}
134		  break;
135	      case KeymapNotify:
136		{
137			register XKeymapEvent *ev = (XKeymapEvent *) re;
138			memcpy ((char *)(((xKeymapEvent *) event)->map),
139				&ev->key_vector[1],
140				sizeof (((xKeymapEvent *) event)->map));
141		}
142		break;
143	      case Expose:
144		{
145			register XExposeEvent *ev = (XExposeEvent *) re;
146			event->u.expose.window		= ev->window;
147			event->u.expose.x		= ev->x;
148			event->u.expose.y		= ev->y;
149			event->u.expose.width		= ev->width;
150			event->u.expose.height		= ev->height;
151			event->u.expose.count		= ev->count;
152		}
153		break;
154	      case GraphicsExpose:
155		{
156		    register XGraphicsExposeEvent *ev =
157			(XGraphicsExposeEvent *) re;
158		    event->u.graphicsExposure.drawable	= ev->drawable;
159		    event->u.graphicsExposure.x		= ev->x;
160		    event->u.graphicsExposure.y		= ev->y;
161		    event->u.graphicsExposure.width	= ev->width;
162		    event->u.graphicsExposure.height	= ev->height;
163		    event->u.graphicsExposure.count	= ev->count;
164		    event->u.graphicsExposure.majorEvent= ev->major_code;
165		    event->u.graphicsExposure.minorEvent= ev->minor_code;
166		}
167		break;
168	      case NoExpose:
169		{
170		    register XNoExposeEvent *ev = (XNoExposeEvent *) re;
171		    event->u.noExposure.drawable	= ev->drawable;
172		    event->u.noExposure.majorEvent	= ev->major_code;
173		    event->u.noExposure.minorEvent	= ev->minor_code;
174		}
175		break;
176	      case VisibilityNotify:
177		{
178		    register XVisibilityEvent *ev = (XVisibilityEvent *) re;
179		    event->u.visibility.window		= ev->window;
180		    event->u.visibility.state		= ev->state;
181		}
182		break;
183	      case CreateNotify:
184		{
185		    register XCreateWindowEvent *ev =
186			 (XCreateWindowEvent *) re;
187		    event->u.createNotify.window	= ev->window;
188		    event->u.createNotify.parent	= ev->parent;
189		    event->u.createNotify.x		= ev->x;
190		    event->u.createNotify.y		= ev->y;
191		    event->u.createNotify.width		= ev->width;
192		    event->u.createNotify.height	= ev->height;
193		    event->u.createNotify.borderWidth	= ev->border_width;
194		    event->u.createNotify.override	= ev->override_redirect;
195		}
196		break;
197	      case DestroyNotify:
198		{
199		    register XDestroyWindowEvent *ev =
200				(XDestroyWindowEvent *) re;
201		    event->u.destroyNotify.window	= ev->window;
202		    event->u.destroyNotify.event	= ev->event;
203		}
204		break;
205	      case UnmapNotify:
206		{
207		    register XUnmapEvent *ev = (XUnmapEvent *) re;
208		    event->u.unmapNotify.window	= ev->window;
209		    event->u.unmapNotify.event	= ev->event;
210		    event->u.unmapNotify.fromConfigure	= ev->from_configure;
211		}
212		break;
213	      case MapNotify:
214		{
215		    register XMapEvent *ev = (XMapEvent *) re;
216		    event->u.mapNotify.window	= ev->window;
217		    event->u.mapNotify.event	= ev->event;
218		    event->u.mapNotify.override	= ev->override_redirect;
219		}
220		break;
221	      case MapRequest:
222		{
223		    register XMapRequestEvent *ev = (XMapRequestEvent *) re;
224		    event->u.mapRequest.window	= ev->window;
225		    event->u.mapRequest.parent	= ev->parent;
226		}
227		break;
228	      case ReparentNotify:
229		{
230		    register XReparentEvent *ev = (XReparentEvent *) re;
231		    event->u.reparent.window	= ev->window;
232		    event->u.reparent.event	= ev->event;
233		    event->u.reparent.parent	= ev->parent;
234		    event->u.reparent.x		= ev->x;
235		    event->u.reparent.y		= ev->y;
236		    event->u.reparent.override	= ev->override_redirect;
237		}
238		break;
239	      case ConfigureNotify:
240		{
241		    register XConfigureEvent *ev = (XConfigureEvent *) re;
242		    event->u.configureNotify.window	= ev->window;
243		    event->u.configureNotify.event	= ev->event;
244		    event->u.configureNotify.aboveSibling	= ev->above;
245		    event->u.configureNotify.x		= ev->x;
246		    event->u.configureNotify.y		= ev->y;
247		    event->u.configureNotify.width	= ev->width;
248		    event->u.configureNotify.height	= ev->height;
249		    event->u.configureNotify.borderWidth= ev->border_width;
250		    event->u.configureNotify.override	= ev->override_redirect;
251		}
252		break;
253	      case ConfigureRequest:
254		{
255		    register XConfigureRequestEvent *ev =
256		        (XConfigureRequestEvent *) re;
257		    event->u.configureRequest.window	= ev->window;
258		    event->u.configureRequest.parent	= ev->parent;
259		    event->u.configureRequest.sibling   = ev->above;
260		    event->u.configureRequest.x		= ev->x;
261		    event->u.configureRequest.y		= ev->y;
262		    event->u.configureRequest.width	= ev->width;
263		    event->u.configureRequest.height	= ev->height;
264		    event->u.configureRequest.borderWidth= ev->border_width;
265		    event->u.configureRequest.valueMask= ev->value_mask;
266		    event->u.u.detail 			= ev->detail;
267		}
268		break;
269	      case GravityNotify:
270		{
271		    register XGravityEvent *ev  = (XGravityEvent *) re;
272		    event->u.gravity.window	= ev->window;
273		    event->u.gravity.event	= ev->event;
274		    event->u.gravity.x		= ev->x;
275		    event->u.gravity.y		= ev->y;
276		}
277		break;
278	      case ResizeRequest:
279		{
280		    register XResizeRequestEvent *ev =
281			(XResizeRequestEvent *) re;
282		    event->u.resizeRequest.window	= ev->window;
283		    event->u.resizeRequest.width	= ev->width;
284		    event->u.resizeRequest.height	= ev->height;
285		}
286		break;
287	      case CirculateNotify:
288		{
289		    register XCirculateEvent *ev = (XCirculateEvent *) re;
290		    event->u.circulate.window		= ev->window;
291		    event->u.circulate.event		= ev->event;
292		    event->u.circulate.place		= ev->place;
293		}
294		break;
295	      case CirculateRequest:
296		{
297		    register XCirculateRequestEvent *ev =
298		        (XCirculateRequestEvent *) re;
299		    event->u.circulate.window		= ev->window;
300		    event->u.circulate.event		= ev->parent;
301		    event->u.circulate.place		= ev->place;
302		}
303		break;
304	      case PropertyNotify:
305		{
306		    register XPropertyEvent *ev = (XPropertyEvent *) re;
307		    event->u.property.window		= ev->window;
308		    event->u.property.atom		= ev->atom;
309		    event->u.property.time		= ev->time;
310		    event->u.property.state		= ev->state;
311		}
312		break;
313	      case SelectionClear:
314		{
315		    register XSelectionClearEvent *ev =
316			 (XSelectionClearEvent *) re;
317		    event->u.selectionClear.window	= ev->window;
318		    event->u.selectionClear.atom	= ev->selection;
319		    event->u.selectionClear.time	= ev->time;
320		}
321		break;
322	      case SelectionRequest:
323		{
324		    register XSelectionRequestEvent *ev =
325		        (XSelectionRequestEvent *) re;
326		    event->u.selectionRequest.owner	= ev->owner;
327		    event->u.selectionRequest.requestor	= ev->requestor;
328		    event->u.selectionRequest.selection	= ev->selection;
329		    event->u.selectionRequest.target	= ev->target;
330		    event->u.selectionRequest.property	= ev->property;
331		    event->u.selectionRequest.time	= ev->time;
332		}
333		break;
334	      case SelectionNotify:
335		{
336		    register XSelectionEvent *ev = (XSelectionEvent *) re;
337		    event->u.selectionNotify.requestor	= ev->requestor;
338		    event->u.selectionNotify.selection	= ev->selection;
339		    event->u.selectionNotify.target	= ev->target;
340		    event->u.selectionNotify.property	= ev->property;
341		    event->u.selectionNotify.time	= ev->time;
342		}
343		break;
344	      case ColormapNotify:
345		{
346		    register XColormapEvent *ev = (XColormapEvent *) re;
347		    event->u.colormap.window		= ev->window;
348		    event->u.colormap.colormap		= ev->colormap;
349		    event->u.colormap.new		= ev->new;
350		    event->u.colormap.state		= ev->state;
351	        }
352		break;
353	      case ClientMessage:
354		{
355		   register int i;
356		   register XClientMessageEvent *ev
357		   			= (XClientMessageEvent *) re;
358		   event->u.clientMessage.window = ev->window;
359		   event->u.u.detail		 = ev->format;
360		   switch (ev->format) {
361			case 8:
362			  event->u.clientMessage.u.b.type   = ev->message_type;
363			  for (i = 0; i < 20; i++)
364			   event->u.clientMessage.u.b.bytes[i] = ev->data.b[i];
365			  break;
366			case 16:
367			  event->u.clientMessage.u.s.type   = ev->message_type;
368			  event->u.clientMessage.u.s.shorts0   = ev->data.s[0];
369			  event->u.clientMessage.u.s.shorts1   = ev->data.s[1];
370			  event->u.clientMessage.u.s.shorts2   = ev->data.s[2];
371			  event->u.clientMessage.u.s.shorts3   = ev->data.s[3];
372			  event->u.clientMessage.u.s.shorts4   = ev->data.s[4];
373			  event->u.clientMessage.u.s.shorts5   = ev->data.s[5];
374			  event->u.clientMessage.u.s.shorts6   = ev->data.s[6];
375			  event->u.clientMessage.u.s.shorts7   = ev->data.s[7];
376			  event->u.clientMessage.u.s.shorts8   = ev->data.s[8];
377			  event->u.clientMessage.u.s.shorts9   = ev->data.s[9];
378			  break;
379			case 32:
380			  event->u.clientMessage.u.l.type   = ev->message_type;
381			  event->u.clientMessage.u.l.longs0   = ev->data.l[0];
382			  event->u.clientMessage.u.l.longs1   = ev->data.l[1];
383			  event->u.clientMessage.u.l.longs2   = ev->data.l[2];
384			  event->u.clientMessage.u.l.longs3   = ev->data.l[3];
385			  event->u.clientMessage.u.l.longs4   = ev->data.l[4];
386			  break;
387			default:
388			  /* client passing bogus data, let server complain */
389			  break;
390			}
391		    }
392		break;
393	      case MappingNotify:
394		  {
395		    register XMappingEvent *ev = (XMappingEvent *) re;
396		    event->u.mappingNotify.firstKeyCode = ev->first_keycode;
397		    event->u.mappingNotify.request 	= ev->request;
398		    event->u.mappingNotify.count	= ev->count;
399		   }
400		break;
401
402	      default:
403		return(_XUnknownNativeEvent(dpy, re, event));
404	}
405	return(1);
406}
407