geext.c revision 6747b715
1/*
2 * Copyright 2007-2008 Peter Hutterer
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 *
23 * Author: Peter Hutterer, University of South Australia, NICTA
24 */
25
26#ifdef HAVE_DIX_CONFIG_H
27#include <dix-config.h>
28#endif
29#include "windowstr.h"
30#include <X11/extensions/ge.h>
31#include "registry.h"
32
33#include "geint.h"
34#include "geext.h"
35#include "protocol-versions.h"
36
37#define rClient(obj) (clients[CLIENT_ID((obj)->resource)])
38
39DevPrivateKeyRec GEClientPrivateKeyRec;
40
41int RT_GECLIENT  = 0;
42
43GEExtension GEExtensions[MAXEXTENSIONS];
44
45/* Major available requests */
46static const int version_requests[] = {
47    X_GEQueryVersion,	/* before client sends QueryVersion */
48    X_GEQueryVersion,	/* must be set to last request in version 1 */
49};
50
51/* Forward declarations */
52static void SGEGenericEvent(xEvent* from, xEvent* to);
53
54#define NUM_VERSION_REQUESTS	(sizeof (version_requests) / sizeof (version_requests[0]))
55
56/************************************************************/
57/*                request handlers                          */
58/************************************************************/
59
60static int
61ProcGEQueryVersion(ClientPtr client)
62{
63    int n;
64    GEClientInfoPtr pGEClient = GEGetClient(client);
65    xGEQueryVersionReply rep;
66    REQUEST(xGEQueryVersionReq);
67
68    REQUEST_SIZE_MATCH(xGEQueryVersionReq);
69
70    rep.repType = X_Reply;
71    rep.RepType = X_GEQueryVersion;
72    rep.length = 0;
73    rep.sequenceNumber = client->sequence;
74
75    /* return the supported version by the server */
76    rep.majorVersion = SERVER_GE_MAJOR_VERSION;
77    rep.minorVersion = SERVER_GE_MINOR_VERSION;
78
79    /* Remember version the client requested */
80    pGEClient->major_version = stuff->majorVersion;
81    pGEClient->minor_version = stuff->minorVersion;
82
83    if (client->swapped)
84    {
85	swaps(&rep.sequenceNumber, n);
86        swapl(&rep.length, n);
87        swaps(&rep.majorVersion, n);
88        swaps(&rep.minorVersion, n);
89    }
90
91    WriteToClient(client, sizeof(xGEQueryVersionReply), (char*)&rep);
92    return Success;
93}
94
95int (*ProcGEVector[GENumberRequests])(ClientPtr) = {
96    /* Version 1.0 */
97    ProcGEQueryVersion
98};
99
100/************************************************************/
101/*                swapped request handlers                  */
102/************************************************************/
103static int
104SProcGEQueryVersion(ClientPtr client)
105{
106    int n;
107    REQUEST(xGEQueryVersionReq);
108
109    swaps(&stuff->length, n);
110    REQUEST_SIZE_MATCH(xGEQueryVersionReq);
111    swaps(&stuff->majorVersion, n);
112    swaps(&stuff->minorVersion, n);
113    return(*ProcGEVector[stuff->ReqType])(client);
114}
115
116int (*SProcGEVector[GENumberRequests])(ClientPtr) = {
117    /* Version 1.0 */
118    SProcGEQueryVersion
119};
120
121
122/************************************************************/
123/*                callbacks                                 */
124/************************************************************/
125
126/* dispatch requests */
127static int
128ProcGEDispatch(ClientPtr client)
129{
130    GEClientInfoPtr pGEClient = GEGetClient(client);
131    REQUEST(xGEReq);
132
133    if (pGEClient->major_version >= NUM_VERSION_REQUESTS)
134        return BadRequest;
135    if (stuff->ReqType > version_requests[pGEClient->major_version])
136        return BadRequest;
137
138    return (ProcGEVector[stuff->ReqType])(client);
139}
140
141/* dispatch swapped requests */
142static int
143SProcGEDispatch(ClientPtr client)
144{
145    REQUEST(xGEReq);
146    if (stuff->ReqType >= GENumberRequests)
147        return BadRequest;
148    return (*SProcGEVector[stuff->ReqType])(client);
149}
150
151/**
152 * Called when a new client inits a connection to the X server.
153 *
154 * We alloc a simple struct to store the client's major/minor version. Can be
155 * used in the furture for versioning support.
156 */
157static void
158GEClientCallback(CallbackListPtr *list,
159                 pointer closure,
160                 pointer data)
161{
162    NewClientInfoRec	*clientinfo = (NewClientInfoRec *) data;
163    ClientPtr		pClient = clientinfo->client;
164    GEClientInfoPtr     pGEClient = GEGetClient(pClient);
165
166    pGEClient->major_version = 0;
167    pGEClient->minor_version = 0;
168}
169
170/* Reset extension. Called on server shutdown. */
171static void
172GEResetProc(ExtensionEntry *extEntry)
173{
174    DeleteCallback(&ClientStateCallback, GEClientCallback, 0);
175    EventSwapVector[GenericEvent] = NotImplemented;
176}
177
178/*  Calls the registered event swap function for the extension.
179 *
180 *  Each extension can register a swap function to handle GenericEvents being
181 *  swapped properly. The server calls SGEGenericEvent() before the event is
182 *  written on the wire, this one calls the registered swap function to do the
183 *  work.
184 */
185static void
186SGEGenericEvent(xEvent* from, xEvent* to)
187{
188    xGenericEvent* gefrom = (xGenericEvent*)from;
189    xGenericEvent* geto = (xGenericEvent*)to;
190
191    if (gefrom->extension > MAXEXTENSIONS)
192    {
193        ErrorF("GE: Invalid extension offset for event.\n");
194        return;
195    }
196
197    if (GEExtensions[gefrom->extension & 0x7F].evswap)
198        GEExtensions[gefrom->extension & 0x7F].evswap(gefrom, geto);
199}
200
201/* Init extension, register at server.
202 * Since other extensions may rely on XGE (XInput does already), it is a good
203 * idea to init XGE first, before any other extension.
204 */
205void
206GEExtensionInit(void)
207{
208    ExtensionEntry *extEntry;
209
210    if (!dixRegisterPrivateKey(&GEClientPrivateKeyRec, PRIVATE_CLIENT, sizeof(GEClientInfoRec)))
211        FatalError("GEExtensionInit: GE private request failed.\n");
212
213    if(!AddCallback(&ClientStateCallback, GEClientCallback, 0))
214    {
215        FatalError("GEExtensionInit: register client callback failed.\n");
216    }
217
218    if((extEntry = AddExtension(GE_NAME,
219                        0, GENumberErrors,
220                        ProcGEDispatch, SProcGEDispatch,
221                        GEResetProc, StandardMinorOpcode)) != 0)
222    {
223        memset(GEExtensions, 0, sizeof(GEExtensions));
224
225        EventSwapVector[GenericEvent] = (EventSwapPtr) SGEGenericEvent;
226    } else {
227        FatalError("GEInit: AddExtensions failed.\n");
228    }
229
230}
231
232/************************************************************/
233/*                interface for extensions                  */
234/************************************************************/
235
236/* Register an extension with GE. The given swap function will be called each
237 * time an event is sent to a client with different byte order.
238 * @param extension The extensions major opcode
239 * @param ev_swap The event swap function.
240 * @param ev_fill Called for an event before delivery. The extension now has
241 * the chance to fill in necessary fields for the event.
242 */
243void
244GERegisterExtension(int extension,
245                    void (*ev_swap)(xGenericEvent* from, xGenericEvent* to))
246{
247    if ((extension & 0x7F) >=  MAXEXTENSIONS)
248        FatalError("GE: extension > MAXEXTENSIONS. This should not happen.\n");
249
250    /* extension opcodes are > 128, might as well save some space here */
251    GEExtensions[extension & 0x7f].evswap = ev_swap;
252}
253
254
255/* Sets type and extension field for a generic event. This is just an
256 * auxiliary function, extensions could do it manually too.
257 */
258void
259GEInitEvent(xGenericEvent* ev, int extension)
260{
261    ev->type = GenericEvent;
262    ev->extension = extension;
263    ev->length = 0;
264}
265
266