geext.c revision 706f2543
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
32#include "geint.h"
33#include "geext.h"
34#include "protocol-versions.h"
35
36DevPrivateKeyRec GEClientPrivateKeyRec;
37
38int RT_GECLIENT  = 0;
39
40GEExtension GEExtensions[MAXEXTENSIONS];
41
42/* Major available requests */
43static const int version_requests[] = {
44    X_GEQueryVersion,	/* before client sends QueryVersion */
45    X_GEQueryVersion,	/* must be set to last request in version 1 */
46};
47
48/* Forward declarations */
49static void SGEGenericEvent(xEvent* from, xEvent* to);
50
51#define NUM_VERSION_REQUESTS	(sizeof (version_requests) / sizeof (version_requests[0]))
52
53/************************************************************/
54/*                request handlers                          */
55/************************************************************/
56
57static int
58ProcGEQueryVersion(ClientPtr client)
59{
60    int n;
61    GEClientInfoPtr pGEClient = GEGetClient(client);
62    xGEQueryVersionReply rep;
63    REQUEST(xGEQueryVersionReq);
64
65    REQUEST_SIZE_MATCH(xGEQueryVersionReq);
66
67    rep.repType = X_Reply;
68    rep.RepType = X_GEQueryVersion;
69    rep.length = 0;
70    rep.sequenceNumber = client->sequence;
71
72    /* return the supported version by the server */
73    rep.majorVersion = SERVER_GE_MAJOR_VERSION;
74    rep.minorVersion = SERVER_GE_MINOR_VERSION;
75
76    /* Remember version the client requested */
77    pGEClient->major_version = stuff->majorVersion;
78    pGEClient->minor_version = stuff->minorVersion;
79
80    if (client->swapped)
81    {
82	swaps(&rep.sequenceNumber, n);
83        swapl(&rep.length, n);
84        swaps(&rep.majorVersion, n);
85        swaps(&rep.minorVersion, n);
86    }
87
88    WriteToClient(client, sizeof(xGEQueryVersionReply), (char*)&rep);
89    return Success;
90}
91
92int (*ProcGEVector[GENumberRequests])(ClientPtr) = {
93    /* Version 1.0 */
94    ProcGEQueryVersion
95};
96
97/************************************************************/
98/*                swapped request handlers                  */
99/************************************************************/
100static int
101SProcGEQueryVersion(ClientPtr client)
102{
103    int n;
104    REQUEST(xGEQueryVersionReq);
105
106    swaps(&stuff->length, n);
107    REQUEST_SIZE_MATCH(xGEQueryVersionReq);
108    swaps(&stuff->majorVersion, n);
109    swaps(&stuff->minorVersion, n);
110    return(*ProcGEVector[stuff->ReqType])(client);
111}
112
113int (*SProcGEVector[GENumberRequests])(ClientPtr) = {
114    /* Version 1.0 */
115    SProcGEQueryVersion
116};
117
118
119/************************************************************/
120/*                callbacks                                 */
121/************************************************************/
122
123/* dispatch requests */
124static int
125ProcGEDispatch(ClientPtr client)
126{
127    GEClientInfoPtr pGEClient = GEGetClient(client);
128    REQUEST(xGEReq);
129
130    if (pGEClient->major_version >= NUM_VERSION_REQUESTS)
131        return BadRequest;
132    if (stuff->ReqType > version_requests[pGEClient->major_version])
133        return BadRequest;
134
135    return (ProcGEVector[stuff->ReqType])(client);
136}
137
138/* dispatch swapped requests */
139static int
140SProcGEDispatch(ClientPtr client)
141{
142    REQUEST(xGEReq);
143    if (stuff->ReqType >= GENumberRequests)
144        return BadRequest;
145    return (*SProcGEVector[stuff->ReqType])(client);
146}
147
148/**
149 * Called when a new client inits a connection to the X server.
150 *
151 * We alloc a simple struct to store the client's major/minor version. Can be
152 * used in the furture for versioning support.
153 */
154static void
155GEClientCallback(CallbackListPtr *list,
156                 pointer closure,
157                 pointer data)
158{
159    NewClientInfoRec	*clientinfo = (NewClientInfoRec *) data;
160    ClientPtr		pClient = clientinfo->client;
161    GEClientInfoPtr     pGEClient = GEGetClient(pClient);
162
163    pGEClient->major_version = 0;
164    pGEClient->minor_version = 0;
165}
166
167/* Reset extension. Called on server shutdown. */
168static void
169GEResetProc(ExtensionEntry *extEntry)
170{
171    DeleteCallback(&ClientStateCallback, GEClientCallback, 0);
172    EventSwapVector[GenericEvent] = NotImplemented;
173}
174
175/*  Calls the registered event swap function for the extension.
176 *
177 *  Each extension can register a swap function to handle GenericEvents being
178 *  swapped properly. The server calls SGEGenericEvent() before the event is
179 *  written on the wire, this one calls the registered swap function to do the
180 *  work.
181 */
182static void
183SGEGenericEvent(xEvent* from, xEvent* to)
184{
185    xGenericEvent* gefrom = (xGenericEvent*)from;
186    xGenericEvent* geto = (xGenericEvent*)to;
187
188    if ((gefrom->extension & 0x7f) > MAXEXTENSIONS)
189    {
190        ErrorF("GE: Invalid extension offset for event.\n");
191        return;
192    }
193
194    if (GEExtensions[gefrom->extension & 0x7F].evswap)
195        GEExtensions[gefrom->extension & 0x7F].evswap(gefrom, geto);
196}
197
198/* Init extension, register at server.
199 * Since other extensions may rely on XGE (XInput does already), it is a good
200 * idea to init XGE first, before any other extension.
201 */
202void
203GEExtensionInit(void)
204{
205    ExtensionEntry *extEntry;
206
207    if (!dixRegisterPrivateKey(&GEClientPrivateKeyRec, PRIVATE_CLIENT, sizeof(GEClientInfoRec)))
208        FatalError("GEExtensionInit: GE private request failed.\n");
209
210    if(!AddCallback(&ClientStateCallback, GEClientCallback, 0))
211    {
212        FatalError("GEExtensionInit: register client callback failed.\n");
213    }
214
215    if((extEntry = AddExtension(GE_NAME,
216                        0, GENumberErrors,
217                        ProcGEDispatch, SProcGEDispatch,
218                        GEResetProc, StandardMinorOpcode)) != 0)
219    {
220        memset(GEExtensions, 0, sizeof(GEExtensions));
221
222        EventSwapVector[GenericEvent] = (EventSwapPtr) SGEGenericEvent;
223    } else {
224        FatalError("GEInit: AddExtensions failed.\n");
225    }
226
227}
228
229/************************************************************/
230/*                interface for extensions                  */
231/************************************************************/
232
233/* Register an extension with GE. The given swap function will be called each
234 * time an event is sent to a client with different byte order.
235 * @param extension The extensions major opcode
236 * @param ev_swap The event swap function.
237 * @param ev_fill Called for an event before delivery. The extension now has
238 * the chance to fill in necessary fields for the event.
239 */
240void
241GERegisterExtension(int extension,
242                    void (*ev_swap)(xGenericEvent* from, xGenericEvent* to))
243{
244    if ((extension & 0x7F) >=  MAXEXTENSIONS)
245        FatalError("GE: extension > MAXEXTENSIONS. This should not happen.\n");
246
247    /* extension opcodes are > 128, might as well save some space here */
248    GEExtensions[extension & 0x7f].evswap = ev_swap;
249}
250
251
252/* Sets type and extension field for a generic event. This is just an
253 * auxiliary function, extensions could do it manually too.
254 */
255void
256GEInitEvent(xGenericEvent* ev, int extension)
257{
258    ev->type = GenericEvent;
259    ev->extension = extension;
260    ev->length = 0;
261}
262
263