geext.c 13.2 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

#ifdef HAVE_DIX_CONFIG_H
#include <dix-config.h>
#endif
#include "windowstr.h"
#include <X11/extensions/ge.h>
Julien Cristau's avatar
Julien Cristau committed
31
#include "registry.h"
32 33 34 35

#include "geint.h"
#include "geext.h"

36 37 38 39
/* Currently supported XGE version */
#define SERVER_GE_MAJOR 1
#define SERVER_GE_MINOR 0

40
#define rClient(obj) (clients[CLIENT_ID((obj)->resource)])
41

42 43
int GEEventBase;
int GEErrorBase;
44 45
static int GEClientPrivateKeyIndex;
DevPrivateKey GEClientPrivateKey = &GEClientPrivateKeyIndex;
46 47
int GEEventType; /* The opcode for all GenericEvents will have. */

48 49
int RT_GECLIENT  = 0;

50

51
GEExtension GEExtensions[MAXEXTENSIONS];
52 53 54 55 56 57 58 59 60

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

/* Forward declarations */
static void SGEGenericEvent(xEvent* from, xEvent* to);
61
static void GERecalculateWinMask(WindowPtr pWin);
62 63 64 65 66 67 68

#define NUM_VERSION_REQUESTS	(sizeof (version_requests) / sizeof (version_requests[0]))

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

69 70
static int
ProcGEQueryVersion(ClientPtr client)
71 72 73 74 75 76 77 78 79 80 81 82 83
{
    int n;
    GEClientInfoPtr pGEClient = GEGetClient(client);
    xGEQueryVersionReply rep;
    REQUEST(xGEQueryVersionReq);

    REQUEST_SIZE_MATCH(xGEQueryVersionReq);

    rep.repType = X_Reply;
    rep.RepType = X_GEQueryVersion;
    rep.length = 0;
    rep.sequenceNumber = client->sequence;

84 85 86
    /* return the supported version by the server */
    rep.majorVersion = SERVER_GE_MAJOR;
    rep.minorVersion = SERVER_GE_MINOR;
87

88 89 90
    /* Remember version the client requested */
    pGEClient->major_version = stuff->majorVersion;
    pGEClient->minor_version = stuff->minorVersion;
91 92 93

    if (client->swapped)
    {
94 95
	swaps(&rep.sequenceNumber, n);
        swapl(&rep.length, n);
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
        swaps(&rep.majorVersion, n);
        swaps(&rep.minorVersion, n);
    }

    WriteToClient(client, sizeof(xGEQueryVersionReply), (char*)&rep);
    return(client->noClientException);
}

int (*ProcGEVector[GENumberRequests])(ClientPtr) = {
    /* Version 1.0 */
    ProcGEQueryVersion
};

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

    swaps(&stuff->length, n);
    REQUEST_SIZE_MATCH(xGEQueryVersionReq);
    swaps(&stuff->majorVersion, n);
    swaps(&stuff->minorVersion, n);
    return(*ProcGEVector[stuff->ReqType])(client);
}

int (*SProcGEVector[GENumberRequests])(ClientPtr) = {
    /* Version 1.0 */
    SProcGEQueryVersion
};


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

/* dispatch requests */
136
static int
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
ProcGEDispatch(ClientPtr client)
{
    GEClientInfoPtr pGEClient = GEGetClient(client);
    REQUEST(xGEReq);

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

    return (ProcGEVector[stuff->ReqType])(client);
}

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

160 161 162 163 164 165
/**
 * 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.
 */
166 167 168 169
static void
GEClientCallback(CallbackListPtr *list,
                 pointer closure,
                 pointer data)
170 171 172 173 174
{
    NewClientInfoRec	*clientinfo = (NewClientInfoRec *) data;
    ClientPtr		pClient = clientinfo->client;
    GEClientInfoPtr     pGEClient = GEGetClient(pClient);

175 176 177 178 179 180
    if (pGEClient == NULL)
    {
        pGEClient = xcalloc(1, sizeof(GEClientInfoRec));
        dixSetPrivate(&pClient->devPrivates, GEClientPrivateKey, pGEClient);
    }

181 182 183 184
    pGEClient->major_version = 0;
    pGEClient->minor_version = 0;
}

185
/* Reset extension. Called on server shutdown. */
186 187 188 189 190 191 192 193 194 195 196
static void
GEResetProc(ExtensionEntry *extEntry)
{
    DeleteCallback(&ClientStateCallback, GEClientCallback, 0);
    EventSwapVector[GenericEvent] = NotImplemented;

    GEEventBase = 0;
    GEErrorBase = 0;
    GEEventType = 0;
}

197 198 199 200 201 202 203
/*  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.
 */
204
static void
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
SGEGenericEvent(xEvent* from, xEvent* to)
{
    xGenericEvent* gefrom = (xGenericEvent*)from;
    xGenericEvent* geto = (xGenericEvent*)to;

    if (gefrom->extension > MAXEXTENSIONS)
    {
        ErrorF("GE: Invalid extension offset for event.\n");
        return;
    }

    if (GEExtensions[gefrom->extension & 0x7F].evswap)
        GEExtensions[gefrom->extension & 0x7F].evswap(gefrom, geto);
}

220 221 222 223
/**
 * Resource callback, invoked when the client disconnects and the associated
 * GE masks must be destroyed.
 */
Julien Cristau's avatar
Julien Cristau committed
224
static int
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
GEClientGone(WindowPtr pWin, XID id)
{
    GenericClientMasksPtr gclmask;
    GenericMaskPtr        gmask, prev = NULL;

    if (!pWin || !pWin->optional)
        return Success;

    gclmask = pWin->optional->geMasks;
    for (gmask = gclmask->geClients; gmask; gmask = gmask->next)
    {
        if (gmask->resource == id)
        {
            if (prev)
            {
                prev->next = gmask->next;
                xfree(gmask);
            } else {
                gclmask->geClients = NULL;
                CheckWindowOptionalNeed(pWin);
                GERecalculateWinMask(pWin);
                xfree(gmask);
            }
            return Success;
        }
        prev = gmask;
    }

    FatalError("Client not a GE client");
    return BadImplementation;
}

257 258 259 260
/* 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.
 */
261
void
262 263 264 265 266 267 268 269 270
GEExtensionInit(void)
{
    ExtensionEntry *extEntry;

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

271 272 273
    if((extEntry = AddExtension(GE_NAME,
                        GENumberEvents, GENumberErrors,
                        ProcGEDispatch, SProcGEDispatch,
274 275 276 277 278 279
                        GEResetProc, StandardMinorOpcode)) != 0)
    {
        GEEventBase = extEntry->eventBase;
        GEErrorBase = extEntry->errorBase;
        GEEventType = GEEventBase;

280 281 282
        RT_GECLIENT = CreateNewResourceType((DeleteType)GEClientGone);
        RegisterResourceName(RT_GECLIENT, "GECLIENT");

283 284
        memset(GEExtensions, 0, sizeof(GEExtensions));

285
        EventSwapVector[GenericEvent] = (EventSwapPtr) SGEGenericEvent;
286 287 288 289 290 291 292 293 294 295
    } else {
        FatalError("GEInit: AddExtensions failed.\n");
    }

}

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

296 297
/* 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.
298 299
 * @param extension The extensions major opcode
 * @param ev_swap The event swap function.
300 301
 * @param ev_fill Called for an event before delivery. The extension now has
 * the chance to fill in necessary fields for the event.
302
 */
303 304 305 306 307
void
GERegisterExtension(int extension,
                    void (*ev_swap)(xGenericEvent* from, xGenericEvent* to),
                    void (*ev_fill)(xGenericEvent* ev, DeviceIntPtr pDev,
                                    WindowPtr pWin, GrabPtr pGrab))
308 309 310 311 312 313
{
    if ((extension & 0x7F) >=  MAXEXTENSIONS)
        FatalError("GE: extension > MAXEXTENSIONS. This should not happen.\n");

    /* extension opcodes are > 128, might as well save some space here */
    GEExtensions[extension & 0x7f].evswap = ev_swap;
314
    GEExtensions[extension & 0x7f].evfill = ev_fill;
315 316 317 318
}


/* Sets type and extension field for a generic event. This is just an
319 320
 * auxiliary function, extensions could do it manually too.
 */
321 322
void
GEInitEvent(xGenericEvent* ev, int extension)
323 324 325 326 327 328 329
{
    ev->type = GenericEvent;
    ev->extension = extension;
    ev->length = 0;
}

/* Recalculates the summary mask for the window. */
330
static void
331 332 333
GERecalculateWinMask(WindowPtr pWin)
{
    int i;
334 335
    GenericMaskPtr it;
    GenericClientMasksPtr evmasks;
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358

    if (!pWin->optional)
        return;

    evmasks = pWin->optional->geMasks;

    for (i = 0; i < MAXEXTENSIONS; i++)
    {
        evmasks->eventMasks[i] = 0;
    }

    it = pWin->optional->geMasks->geClients;
    while(it)
    {
        for (i = 0; i < MAXEXTENSIONS; i++)
        {
            evmasks->eventMasks[i] |= it->eventMask[i];
        }
        it = it->next;
    }
}

/* Set generic event mask for given window. */
359 360 361
void
GEWindowSetMask(ClientPtr pClient, DeviceIntPtr pDev,
                WindowPtr pWin, int extension, Mask mask)
362
{
363
    GenericMaskPtr cli;
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380

    extension = (extension & 0x7F);

    if (extension > MAXEXTENSIONS)
    {
        ErrorF("Invalid extension number.\n");
        return;
    }

    if (!pWin->optional && !MakeWindowOptional(pWin))
    {
        ErrorF("GE: Could not make window optional.\n");
        return;
    }

    if (mask)
    {
381
        GenericClientMasksPtr evmasks = pWin->optional->geMasks;
382 383 384 385 386

        /* check for existing client */
        cli = evmasks->geClients;
        while(cli)
        {
387
            if (rClient(cli) == pClient && cli->dev == pDev)
388 389 390 391 392
                break;
            cli = cli->next;
        }
        if (!cli)
        {
393
            /* new client and/or new device */
394
            cli  = (GenericMaskPtr)xcalloc(1, sizeof(GenericMaskRec));
395 396 397 398 399 400
            if (!cli)
            {
                ErrorF("GE: Insufficient memory to alloc client.\n");
                return;
            }
            cli->next = evmasks->geClients;
401
            cli->resource = FakeClientID(pClient->index);
402
            cli->dev = pDev;
403
            evmasks->geClients = cli;
404
            AddResource(cli->resource, RT_GECLIENT, (pointer)pWin);
405 406 407 408 409 410
        }
        cli->eventMask[extension] = mask;
    } else
    {
        /* remove client. */
        cli = pWin->optional->geMasks->geClients;
411
        if (rClient(cli) == pClient && cli->dev == pDev)
412 413 414
        {
            pWin->optional->geMasks->geClients = cli->next;
            xfree(cli);
415 416
        } else
        {
417
            GenericMaskPtr prev = cli;
418 419 420 421
            cli = cli->next;

            while(cli)
            {
422
                if (rClient(cli) == pClient && cli->dev == pDev)
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438
                {
                    prev->next = cli->next;
                    xfree(cli);
                    break;
                }
                prev = cli;
                cli = cli->next;
            }
        }
        if (!cli)
            return;
    }

    GERecalculateWinMask(pWin);
}

439 440 441 442 443 444 445
/**
 * Return TRUE if the mask for the given device is set.
 * @param pWin Window the event may be delivered to.
 * @param pDev Device the device originating the event. May be NULL.
 * @param extension Extension ID
 * @param mask Event mask
 */
446 447 448
BOOL
GEDeviceMaskIsSet(WindowPtr pWin, DeviceIntPtr pDev,
                  int extension, Mask mask)
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
{
    GenericMaskPtr gemask;

    if (!pWin->optional || !pWin->optional->geMasks)
        return FALSE;

    extension &= 0x7F;

    if (!pWin->optional->geMasks->eventMasks[extension] & mask)
        return FALSE;


    gemask = pWin->optional->geMasks->geClients;

    while(gemask)
    {
465
        if ((!gemask->dev || gemask->dev == pDev) &&
466 467 468 469 470 471 472 473
                (gemask->eventMask[extension] & mask))
            return TRUE;

        gemask = gemask->next;
    }

    return FALSE;
}
474