geext.c 8.07 KB
Newer Older
1
/*
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 * Copyright 2007-2008 Peter Hutterer
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 *
23
 * Author: Peter Hutterer, University of South Australia, NICTA
24
 */
25 26 27 28 29 30 31 32 33

#ifdef HAVE_DIX_CONFIG_H
#include <dix-config.h>
#endif
#include "windowstr.h"
#include <X11/extensions/ge.h>

#include "geint.h"
#include "geext.h"
34
#include "protocol-versions.h"
35
#include "extinit.h"
36

37
DevPrivateKeyRec GEClientPrivateKeyRec;
38

39
GEExtension GEExtensions[MAXEXTENSIONS];
40 41 42

/* Major available requests */
static const int version_requests[] = {
43 44
    X_GEQueryVersion,           /* before client sends QueryVersion */
    X_GEQueryVersion,           /* must be set to last request in version 1 */
45 46 47
};

/* Forward declarations */
48
static void SGEGenericEvent(xEvent *from, xEvent *to);
49 50

#define NUM_VERSION_REQUESTS	(sizeof (version_requests) / sizeof (version_requests[0]))
51
#define EXT_MASK(ext) ((ext) & 0x7F)
52 53 54 55 56

/************************************************************/
/*                request handlers                          */
/************************************************************/

57 58
static int
ProcGEQueryVersion(ClientPtr client)
59 60 61
{
    GEClientInfoPtr pGEClient = GEGetClient(client);
    xGEQueryVersionReply rep;
62

63 64 65 66
    REQUEST(xGEQueryVersionReq);

    REQUEST_SIZE_MATCH(xGEQueryVersionReq);

67 68 69 70 71 72 73 74 75 76
    rep = (xGEQueryVersionReply) {
        .repType = X_Reply,
        .RepType = X_GEQueryVersion,
        .sequenceNumber = client->sequence,
        .length = 0,

        /* return the supported version by the server */
        .majorVersion = SERVER_GE_MAJOR_VERSION,
        .minorVersion = SERVER_GE_MINOR_VERSION
    };
77

78 79 80
    /* Remember version the client requested */
    pGEClient->major_version = stuff->majorVersion;
    pGEClient->minor_version = stuff->minorVersion;
81

82 83 84 85 86
    if (client->swapped) {
        swaps(&rep.sequenceNumber);
        swapl(&rep.length);
        swaps(&rep.majorVersion);
        swaps(&rep.minorVersion);
87 88
    }

89
    WriteToClient(client, sizeof(xGEQueryVersionReply), &rep);
90
    return Success;
91 92
}

93
static int (*ProcGEVector[GENumberRequests]) (ClientPtr) = {
94
    /* Version 1.0 */
95 96
    ProcGEQueryVersion,
};
97 98 99 100 101 102 103 104 105

/************************************************************/
/*                swapped request handlers                  */
/************************************************************/
static int
SProcGEQueryVersion(ClientPtr client)
{
    REQUEST(xGEQueryVersionReq);

106
    swaps(&stuff->length);
107
    REQUEST_SIZE_MATCH(xGEQueryVersionReq);
108 109
    swaps(&stuff->majorVersion);
    swaps(&stuff->minorVersion);
110
    return (*ProcGEVector[stuff->ReqType]) (client);
111 112
}

113
static int (*SProcGEVector[GENumberRequests]) (ClientPtr) = {
114
    /* Version 1.0 */
115 116
    SProcGEQueryVersion
};
117 118 119 120 121 122

/************************************************************/
/*                callbacks                                 */
/************************************************************/

/* dispatch requests */
123
static int
124 125 126
ProcGEDispatch(ClientPtr client)
{
    GEClientInfoPtr pGEClient = GEGetClient(client);
127

128 129 130 131 132 133 134
    REQUEST(xGEReq);

    if (pGEClient->major_version >= NUM_VERSION_REQUESTS)
        return BadRequest;
    if (stuff->ReqType > version_requests[pGEClient->major_version])
        return BadRequest;

135
    return (ProcGEVector[stuff->ReqType]) (client);
136 137 138 139 140 141 142 143 144
}

/* dispatch swapped requests */
static int
SProcGEDispatch(ClientPtr client)
{
    REQUEST(xGEReq);
    if (stuff->ReqType >= GENumberRequests)
        return BadRequest;
145
    return (*SProcGEVector[stuff->ReqType]) (client);
146 147
}

148 149 150 151 152 153
/**
 * Called when a new client inits a connection to the X server.
 *
 * We alloc a simple struct to store the client's major/minor version. Can be
 * used in the furture for versioning support.
 */
154
static void
155
GEClientCallback(CallbackListPtr *list, void *closure, void *data)
156
{
157 158 159
    NewClientInfoRec *clientinfo = (NewClientInfoRec *) data;
    ClientPtr pClient = clientinfo->client;
    GEClientInfoPtr pGEClient = GEGetClient(pClient);
160 161 162 163 164

    pGEClient->major_version = 0;
    pGEClient->minor_version = 0;
}

165
/* Reset extension. Called on server shutdown. */
166
static void
167
GEResetProc(ExtensionEntry * extEntry)
168 169 170 171 172
{
    DeleteCallback(&ClientStateCallback, GEClientCallback, 0);
    EventSwapVector[GenericEvent] = NotImplemented;
}

173 174 175 176 177 178 179
/*  Calls the registered event swap function for the extension.
 *
 *  Each extension can register a swap function to handle GenericEvents being
 *  swapped properly. The server calls SGEGenericEvent() before the event is
 *  written on the wire, this one calls the registered swap function to do the
 *  work.
 */
180
static void
181
SGEGenericEvent(xEvent *from, xEvent *to)
182
{
183 184
    xGenericEvent *gefrom = (xGenericEvent *) from;
    xGenericEvent *geto = (xGenericEvent *) to;
185

186
    if ((gefrom->extension & 0x7f) > MAXEXTENSIONS) {
187 188 189 190
        ErrorF("GE: Invalid extension offset for event.\n");
        return;
    }

191 192
    if (GEExtensions[EXT_MASK(gefrom->extension)].evswap)
        GEExtensions[EXT_MASK(gefrom->extension)].evswap(gefrom, geto);
193 194
}

195 196 197 198
/* Init extension, register at server.
 * Since other extensions may rely on XGE (XInput does already), it is a good
 * idea to init XGE first, before any other extension.
 */
199
void
200 201 202 203
GEExtensionInit(void)
{
    ExtensionEntry *extEntry;

204 205
    if (!dixRegisterPrivateKey
        (&GEClientPrivateKeyRec, PRIVATE_CLIENT, sizeof(GEClientInfoRec)))
206 207
        FatalError("GEExtensionInit: GE private request failed.\n");

208
    if (!AddCallback(&ClientStateCallback, GEClientCallback, 0)) {
209 210 211
        FatalError("GEExtensionInit: register client callback failed.\n");
    }

212 213 214 215
    if ((extEntry = AddExtension(GE_NAME,
                                 0, GENumberErrors,
                                 ProcGEDispatch, SProcGEDispatch,
                                 GEResetProc, StandardMinorOpcode)) != 0) {
216 217
        memset(GEExtensions, 0, sizeof(GEExtensions));

218
        EventSwapVector[GenericEvent] = (EventSwapPtr) SGEGenericEvent;
219 220
    }
    else {
221 222 223 224 225 226 227 228 229
        FatalError("GEInit: AddExtensions failed.\n");
    }

}

/************************************************************/
/*                interface for extensions                  */
/************************************************************/

230 231
/* Register an extension with GE. The given swap function will be called each
 * time an event is sent to a client with different byte order.
232 233
 * @param extension The extensions major opcode
 * @param ev_swap The event swap function.
234 235
 * @param ev_fill Called for an event before delivery. The extension now has
 * the chance to fill in necessary fields for the event.
236
 */
237
void
238
GERegisterExtension(int extension,
239
                    void (*ev_swap) (xGenericEvent *from, xGenericEvent *to))
240
{
241
    if (EXT_MASK(extension) >= MAXEXTENSIONS)
242 243 244
        FatalError("GE: extension > MAXEXTENSIONS. This should not happen.\n");

    /* extension opcodes are > 128, might as well save some space here */
245
    GEExtensions[EXT_MASK(extension)].evswap = ev_swap;
246 247 248
}

/* Sets type and extension field for a generic event. This is just an
249 250
 * auxiliary function, extensions could do it manually too.
 */
251
void
252
GEInitEvent(xGenericEvent *ev, int extension)
253 254 255 256 257
{
    ev->type = GenericEvent;
    ev->extension = extension;
    ev->length = 0;
}