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