chdevhier.c 10.4 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 23 24
 * 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.
 *
 * Author: Peter Hutterer, UniSA, NICTA
 */
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51

/***********************************************************************
 *
 * Request change in the device hierarchy.
 *
 */


#define	 NEED_EVENTS
#define	 NEED_REPLIES
#ifdef HAVE_DIX_CONFIG_H
#include <dix-config.h>
#endif

#include <X11/X.h>	/* for inputstr.h    */
#include <X11/Xproto.h>	/* Request macro     */
#include "inputstr.h"	/* DeviceIntPtr      */
#include "windowstr.h"	/* window structure  */
#include "scrnintstr.h"	/* screen structure  */
#include <X11/extensions/XI.h>
#include <X11/extensions/XInput.h>
#include <X11/extensions/XIproto.h>
#include <X11/extensions/geproto.h>
#include "extnsionst.h"
#include "exevents.h"
#include "exglobals.h"
#include "geext.h"
52
#include "xace.h"
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78

#include "chdevhier.h"


/***********************************************************************
 *
 * This procedure allows a client to change the device hierarchy through
 * adding new master devices, removing them, etc.
 *
 */

int SProcXChangeDeviceHierarchy(ClientPtr client)
{
    char n;

    REQUEST(xChangeDeviceHierarchyReq);
    swaps(&stuff->length, n);
    return (ProcXChangeDeviceHierarchy(client));
}

#define SWAPIF(cmd) if (client->swapped) { cmd; }

int
ProcXChangeDeviceHierarchy(ClientPtr client)
{
    DeviceIntPtr ptr, keybd;
79
    DeviceIntRec dummyDev;
80 81 82
    xAnyHierarchyChangeInfo *any;
    int required_len = sizeof(xChangeDeviceHierarchyReq);
    char n;
83
    int rc;
84
    int nchanges = 0;
85
    deviceHierarchyChangedEvent ev;
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105

    REQUEST(xChangeDeviceHierarchyReq);
    REQUEST_AT_LEAST_SIZE(xChangeDeviceHierarchyReq);

    any = (xAnyHierarchyChangeInfo*)&stuff[1];
    while(stuff->num_changes--)
    {
        SWAPIF(swapl(&any->type, n));
        SWAPIF(swaps(&any->length, n));

        required_len += any->length;
        if ((stuff->length * 4) < required_len)
            return BadLength;

        switch(any->type)
        {
            case CH_CreateMasterDevice:
                {
                    xCreateMasterInfo* c = (xCreateMasterInfo*)any;
                    char* name;
106

107 108 109 110
                    SWAPIF(swaps(&c->namelen, n));
                    name = xcalloc(c->namelen + 1, sizeof(char));
                    strncpy(name, (char*)&c[1], c->namelen);

111

112 113
                    rc = AllocMasterDevice(client, name, &ptr, &keybd);
                    if (rc != Success)
114 115
                    {
                        xfree(name);
116
                        goto unwind;
117 118 119 120 121 122 123 124 125 126 127 128 129 130
                    }

                    if (!c->sendCore)
                        ptr->coreEvents = keybd->coreEvents =  FALSE;

                    ActivateDevice(ptr);
                    ActivateDevice(keybd);

                    if (c->enable)
                    {
                        EnableDevice(ptr);
                        EnableDevice(keybd);
                    }
                    xfree(name);
131
                    nchanges++;
132 133 134 135 136 137
                }
                break;
            case CH_RemoveMasterDevice:
                {
                    xRemoveMasterInfo* r = (xRemoveMasterInfo*)any;

138
                    if (r->returnMode != AttachToMaster &&
139 140 141
                            r->returnMode != Floating)
                        return BadValue;

142
                    rc = dixLookupDevice(&ptr, r->deviceid, client,
143
                                         DixDestroyAccess);
144
                    if (rc != Success)
145
                        goto unwind;
146 147 148 149

                    if (!ptr->isMaster)
                    {
                        client->errorValue = r->deviceid;
150 151
                        rc = BadDevice;
                        goto unwind;
152
                    }
153 154 155 156

                    /* XXX: For now, don't allow removal of VCP, VCK */
                    if (ptr == inputInfo.pointer ||
                            ptr == inputInfo.keyboard)
157 158 159 160
                    {
                        rc = BadDevice;
                        goto unwind;
                    }
161 162 163

                    /* disable keyboards first */
                    if (IsPointerDevice(ptr))
164 165 166 167 168 169
                    {
                        rc = dixLookupDevice(&keybd,
                                             ptr->spriteInfo->paired->id,
                                             client,
                                             DixDestroyAccess);
                        if (rc != Success)
170
                            goto unwind;
171
                    }
172 173 174
                    else
                    {
                        keybd = ptr;
175 176 177 178 179
                        rc = dixLookupDevice(&ptr,
                                             keybd->spriteInfo->paired->id,
                                             client,
                                             DixDestroyAccess);
                        if (rc != Success)
180
                            goto unwind;
181 182
                    }

183

184 185 186 187
                    /* Disabling sends the devices floating, reattach them if
                     * desired. */
                    if (r->returnMode == AttachToMaster)
                    {
188 189
                        DeviceIntPtr attached,
                                     newptr,
190 191
                                     newkeybd;

192 193 194
                        rc = dixLookupDevice(&newptr, r->returnPointer,
                                             client, DixWriteAccess);
                        if (rc != Success)
195
                            goto unwind;
196 197 198 199

                        if (!newptr->isMaster)
                        {
                            client->errorValue = r->returnPointer;
200 201
                            rc = BadDevice;
                            goto unwind;
202 203 204 205 206
                        }

                        rc = dixLookupDevice(&newkeybd, r->returnKeyboard,
                                             client, DixWriteAccess);
                        if (rc != Success)
207
                            goto unwind;
208 209 210 211

                        if (!newkeybd->isMaster)
                        {
                            client->errorValue = r->returnKeyboard;
212 213
                            rc = BadDevice;
                            goto unwind;
214
                        }
215

216 217
                        for (attached = inputInfo.devices;
                                attached;
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
                                attached = attached->next)
                        {
                            if (!attached->isMaster) {
                                if (attached->u.master == ptr)
                                    AttachDevice(client, attached, newptr);
                                if (attached->u.master == keybd)
                                    AttachDevice(client, attached, newkeybd);
                            }
                        }
                    }

                    /* can't disable until we removed pairing */
                    keybd->spriteInfo->paired = NULL;
                    ptr->spriteInfo->paired = NULL;
                    DisableDevice(keybd);
                    DisableDevice(ptr);

                    RemoveDevice(keybd);
                    RemoveDevice(ptr);
237
                    nchanges++;
238 239 240 241 242 243
                }
                break;
            case CH_ChangeAttachment:
                {
                    xChangeAttachmentInfo* c = (xChangeAttachmentInfo*)any;

244 245 246
                    rc = dixLookupDevice(&ptr, c->deviceid, client,
                                          DixWriteAccess);
                    if (rc != Success)
247
                       goto unwind;
248 249 250 251

                    if (ptr->isMaster)
                    {
                        client->errorValue = c->deviceid;
252 253
                        rc = BadDevice;
                        goto unwind;
254
                    }
255 256 257 258 259

                    if (c->changeMode == Floating)
                        AttachDevice(client, ptr, NULL);
                    else
                    {
260 261 262 263
                        DeviceIntPtr newmaster;
                        rc = dixLookupDevice(&newmaster, c->newMaster,
                                             client, DixWriteAccess);
                        if (rc != Success)
264
                            goto unwind;
265 266 267
                        if (!newmaster->isMaster)
                        {
                            client->errorValue = c->newMaster;
268 269
                            rc = BadDevice;
                            goto unwind;
270
                        }
271

272 273
                        if (!((IsPointerDevice(newmaster) &&
                                    IsPointerDevice(ptr)) ||
274
                                (IsKeyboardDevice(newmaster) &&
275
                                 IsKeyboardDevice(ptr))))
276 277 278 279
                        {
                            rc = BadDevice;
                            goto unwind;
                        }
280 281
                        AttachDevice(client, ptr, newmaster);
                    }
282
                    nchanges++;
283 284 285 286 287 288 289
                }
                break;
        }

        any = (xAnyHierarchyChangeInfo*)((char*)any + any->length);
    }

290
unwind:
291

292 293 294 295 296 297 298 299 300 301 302 303 304
    if (nchanges > 0) /* even if an error occured, we need to send an event if
                       we changed anything in the hierarchy. */
    {
        ev.type = GenericEvent;
        ev.extension = IReqCode;
        ev.length = 0;
        ev.evtype = XI_DeviceHierarchyChangedNotify;
        ev.time = GetTimeInMillis();

        SendEventToAllWindows(&dummyDev, XI_DeviceHierarchyChangedMask,
                (xEvent*)&ev, 1);
    }
    return rc;
305 306
}