geext.c revision 1b5d61b8
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    REQUEST(xGEReq);
142    if (stuff->ReqType >= GENumberRequests)
143        return BadRequest;
144    return (*SProcGEVector[stuff->ReqType]) (client);
145}
146
147/* Reset extension. Called on server shutdown. */
148static void
149GEResetProc(ExtensionEntry * extEntry)
150{
151    EventSwapVector[GenericEvent] = NotImplemented;
152}
153
154/*  Calls the registered event swap function for the extension.
155 *
156 *  Each extension can register a swap function to handle GenericEvents being
157 *  swapped properly. The server calls SGEGenericEvent() before the event is
158 *  written on the wire, this one calls the registered swap function to do the
159 *  work.
160 */
161static void _X_COLD
162SGEGenericEvent(xEvent *from, xEvent *to)
163{
164    xGenericEvent *gefrom = (xGenericEvent *) from;
165    xGenericEvent *geto = (xGenericEvent *) to;
166
167    if ((gefrom->extension & 0x7f) > MAXEXTENSIONS) {
168        ErrorF("GE: Invalid extension offset for event.\n");
169        return;
170    }
171
172    if (GEExtensions[EXT_MASK(gefrom->extension)].evswap)
173        GEExtensions[EXT_MASK(gefrom->extension)].evswap(gefrom, geto);
174}
175
176/* Init extension, register at server.
177 * Since other extensions may rely on XGE (XInput does already), it is a good
178 * idea to init XGE first, before any other extension.
179 */
180void
181GEExtensionInit(void)
182{
183    ExtensionEntry *extEntry;
184
185    if (!dixRegisterPrivateKey
186        (&GEClientPrivateKeyRec, PRIVATE_CLIENT, sizeof(GEClientInfoRec)))
187        FatalError("GEExtensionInit: GE private request failed.\n");
188
189    if ((extEntry = AddExtension(GE_NAME,
190                                 0, GENumberErrors,
191                                 ProcGEDispatch, SProcGEDispatch,
192                                 GEResetProc, StandardMinorOpcode)) != 0) {
193        memset(GEExtensions, 0, sizeof(GEExtensions));
194
195        EventSwapVector[GenericEvent] = (EventSwapPtr) SGEGenericEvent;
196    }
197    else {
198        FatalError("GEInit: AddExtensions failed.\n");
199    }
200
201}
202
203/************************************************************/
204/*                interface for extensions                  */
205/************************************************************/
206
207/* Register an extension with GE. The given swap function will be called each
208 * time an event is sent to a client with different byte order.
209 * @param extension The extensions major opcode
210 * @param ev_swap The event swap function.
211 * @param ev_fill Called for an event before delivery. The extension now has
212 * the chance to fill in necessary fields for the event.
213 */
214void
215GERegisterExtension(int extension,
216                    void (*ev_swap) (xGenericEvent *from, xGenericEvent *to))
217{
218    if (EXT_MASK(extension) >= MAXEXTENSIONS)
219        FatalError("GE: extension > MAXEXTENSIONS. This should not happen.\n");
220
221    /* extension opcodes are > 128, might as well save some space here */
222    GEExtensions[EXT_MASK(extension)].evswap = ev_swap;
223}
224
225/* Sets type and extension field for a generic event. This is just an
226 * auxiliary function, extensions could do it manually too.
227 */
228void
229GEInitEvent(xGenericEvent *ev, int extension)
230{
231    ev->type = GenericEvent;
232    ev->extension = extension;
233    ev->length = 0;
234}
235