touch.c 31.6 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 25 26 27 28 29 30 31 32
/*
 * Copyright © 2011 Collabra Ltd.
 * Copyright © 2011 Red Hat, Inc.
 *
 * 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: Daniel Stone <daniel@fooishbar.org>
 */

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

#include "inputstr.h"
#include "scrnintstr.h"
33
#include "dixgrabs.h"
34

35 36
#include "eventstr.h"
#include "exevents.h"
37
#include "exglobals.h"
38 39 40
#include "inpututils.h"
#include "eventconvert.h"
#include "windowstr.h"
41
#include "mi.h"
42

43 44
#define TOUCH_HISTORY_SIZE 100

45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
/**
 * Some documentation about touch points:
 * The driver submits touch events with it's own (unique) touch point ID.
 * The driver may re-use those IDs, the DDX doesn't care. It just passes on
 * the data to the DIX. In the server, the driver's ID is referred to as the
 * DDX id anyway.
 *
 * On a TouchBegin, we create a DDXTouchPointInfo that contains the DDX id
 * and the client ID that this touchpoint will have. The client ID is the
 * one visible on the protocol.
 *
 * TouchUpdate and TouchEnd will only be processed if there is an active
 * touchpoint with the same DDX id.
 *
 * The DDXTouchPointInfo struct is stored dev->last.touches. When the event
 * being processed, it becomes a TouchPointInfo in dev->touch-touches which
 * contains amongst other things the sprite trace and delivery information.
 */

64 65 66 67 68 69 70 71 72 73
/**
 * Check which devices need a bigger touch event queue and grow their
 * last.touches by half it's current size.
 *
 * @param client Always the serverClient
 * @param closure Always NULL
 *
 * @return Always True. If we fail to grow we probably will topple over soon
 * anyway and re-executing this won't help.
 */
74

75
static Bool
76
TouchResizeQueue(DeviceIntPtr dev)
77
{
78 79
    DDXTouchPointInfoPtr tmp;
    size_t size;
80

81 82
    /* Grow sufficiently so we don't need to do it often */
    size = dev->last.num_touches + dev->last.num_touches / 2 + 1;
83

84 85 86
    tmp = reallocarray(dev->last.touches, size, sizeof(*dev->last.touches));
    if (tmp) {
        int j;
87

88 89 90 91 92
        dev->last.touches = tmp;
        for (j = dev->last.num_touches; j < size; j++)
            TouchInitDDXTouchPoint(dev, &dev->last.touches[j]);
        dev->last.num_touches = size;
        return TRUE;
93
    }
94
    return FALSE;
95 96
}

97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
/**
 * Given the DDX-facing ID (which is _not_ DeviceEvent::detail.touch), find the
 * associated DDXTouchPointInfoRec.
 *
 * @param dev The device to create the touch point for
 * @param ddx_id Touch id assigned by the driver/ddx
 * @param create Create the touchpoint if it cannot be found
 */
DDXTouchPointInfoPtr
TouchFindByDDXID(DeviceIntPtr dev, uint32_t ddx_id, Bool create)
{
    DDXTouchPointInfoPtr ti;
    int i;

    if (!dev->touch)
        return NULL;

114
    for (i = 0; i < dev->last.num_touches; i++) {
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
        ti = &dev->last.touches[i];
        if (ti->active && ti->ddx_id == ddx_id)
            return ti;
    }

    return create ? TouchBeginDDXTouch(dev, ddx_id) : NULL;
}

/**
 * Given a unique DDX ID for a touchpoint, create a touchpoint record and
 * return it.
 *
 * If no other touch points are active, mark new touchpoint for pointer
 * emulation.
 *
 * Returns NULL on failure (i.e. if another touch with that ID is already active,
 * allocation failure).
 */
DDXTouchPointInfoPtr
TouchBeginDDXTouch(DeviceIntPtr dev, uint32_t ddx_id)
{
    static int next_client_id = 1;
    int i;
    TouchClassPtr t = dev->touch;
    DDXTouchPointInfoPtr ti = NULL;
140
    Bool emulate_pointer;
141 142 143 144

    if (!t)
        return NULL;

145 146
    emulate_pointer = (t->mode == XIDirectTouch);

147 148 149 150 151
    /* Look for another active touchpoint with the same DDX ID. DDX
     * touchpoints must be unique. */
    if (TouchFindByDDXID(dev, ddx_id, FALSE))
        return NULL;

152 153 154 155 156 157 158
    for (;;) {
        for (i = 0; i < dev->last.num_touches; i++) {
            /* Only emulate pointer events on the first touch */
            if (dev->last.touches[i].active)
                emulate_pointer = FALSE;
            else if (!ti)           /* ti is now first non-active touch rec */
                ti = &dev->last.touches[i];
159

160 161 162 163 164 165
            if (!emulate_pointer && ti)
                break;
        }
        if (ti)
            break;
        if (!TouchResizeQueue(dev))
166 167 168
            break;
    }

169
    if (ti) {
170
        int client_id;
171

172 173 174 175 176 177 178 179 180
        ti->active = TRUE;
        ti->ddx_id = ddx_id;
        client_id = next_client_id;
        next_client_id++;
        if (next_client_id == 0)
            next_client_id = 1;
        ti->client_id = client_id;
        ti->emulate_pointer = emulate_pointer;
    }
181
    return ti;
182 183 184 185 186 187 188 189 190 191 192 193
}

void
TouchEndDDXTouch(DeviceIntPtr dev, DDXTouchPointInfoPtr ti)
{
    TouchClassPtr t = dev->touch;

    if (!t)
        return;

    ti->active = FALSE;
}
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217

void
TouchInitDDXTouchPoint(DeviceIntPtr dev, DDXTouchPointInfoPtr ddxtouch)
{
    memset(ddxtouch, 0, sizeof(*ddxtouch));
    ddxtouch->valuators = valuator_mask_new(dev->valuator->numAxes);
}

Bool
TouchInitTouchPoint(TouchClassPtr t, ValuatorClassPtr v, int index)
{
    TouchPointInfoPtr ti;

    if (index >= t->num_touches)
        return FALSE;
    ti = &t->touches[index];

    memset(ti, 0, sizeof(*ti));

    ti->valuators = valuator_mask_new(v->numAxes);
    if (!ti->valuators)
        return FALSE;

    ti->sprite.spriteTrace = calloc(32, sizeof(*ti->sprite.spriteTrace));
218
    if (!ti->sprite.spriteTrace) {
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
        valuator_mask_free(&ti->valuators);
        return FALSE;
    }
    ti->sprite.spriteTraceSize = 32;
    ti->sprite.spriteTrace[0] = screenInfo.screens[0]->root;
    ti->sprite.hot.pScreen = screenInfo.screens[0];
    ti->sprite.hotPhys.pScreen = screenInfo.screens[0];

    ti->client_id = -1;

    return TRUE;
}

void
TouchFreeTouchPoint(DeviceIntPtr device, int index)
{
    TouchPointInfoPtr ti;
236
    int i;
237 238 239 240 241

    if (!device->touch || index >= device->touch->num_touches)
        return;
    ti = &device->touch->touches[index];

242 243 244
    if (ti->active)
        TouchEndTouch(device, ti);

245 246 247
    for (i = 0; i < ti->num_listeners; i++)
        TouchRemoveListener(ti, ti->listeners[0].listener);

248 249 250 251 252 253 254 255 256 257 258
    valuator_mask_free(&ti->valuators);
    free(ti->sprite.spriteTrace);
    ti->sprite.spriteTrace = NULL;
    free(ti->listeners);
    ti->listeners = NULL;
    free(ti->history);
    ti->history = NULL;
    ti->history_size = 0;
    ti->history_elements = 0;
}

259 260 261 262 263 264 265 266 267 268 269 270 271 272
/**
 * Given a client-facing ID (e.g. DeviceEvent::detail.touch), find the
 * associated TouchPointInfoRec.
 */
TouchPointInfoPtr
TouchFindByClientID(DeviceIntPtr dev, uint32_t client_id)
{
    TouchClassPtr t = dev->touch;
    TouchPointInfoPtr ti;
    int i;

    if (!t)
        return NULL;

273
    for (i = 0; i < t->num_touches; i++) {
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
        ti = &t->touches[i];
        if (ti->active && ti->client_id == client_id)
            return ti;
    }

    return NULL;
}

/**
 * Given a unique ID for a touchpoint, create a touchpoint record in the
 * server.
 *
 * Returns NULL on failure (i.e. if another touch with that ID is already active,
 * allocation failure).
 */
TouchPointInfoPtr
TouchBeginTouch(DeviceIntPtr dev, int sourceid, uint32_t touchid,
                Bool emulate_pointer)
{
    int i;
    TouchClassPtr t = dev->touch;
    TouchPointInfoPtr ti;
    void *tmp;

    if (!t)
        return NULL;

    /* Look for another active touchpoint with the same client ID.  It's
     * technically legitimate for a touchpoint to still exist with the same
     * ID but only once the 32 bits wrap over and you've used up 4 billion
     * touch ids without lifting that one finger off once. In which case
     * you deserve a medal or something, but not error handling code. */
    if (TouchFindByClientID(dev, touchid))
        return NULL;

309 310
 try_find_touch:
    for (i = 0; i < t->num_touches; i++) {
311 312 313 314 315 316 317 318 319 320 321 322
        ti = &t->touches[i];
        if (!ti->active) {
            ti->active = TRUE;
            ti->client_id = touchid;
            ti->sourceid = sourceid;
            ti->emulate_pointer = emulate_pointer;
            return ti;
        }
    }

    /* If we get here, then we've run out of touches: enlarge dev->touch and
     * try again. */
323
    tmp = reallocarray(t->touches, t->num_touches + 1, sizeof(*ti));
324
    if (tmp) {
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
        t->touches = tmp;
        t->num_touches++;
        if (TouchInitTouchPoint(t, dev->valuator, t->num_touches - 1))
            goto try_find_touch;
    }

    return NULL;
}

/**
 * Releases a touchpoint for use: this must only be called after all events
 * related to that touchpoint have been sent and finalised.  Called from
 * ProcessTouchEvent and friends.  Not by you.
 */
void
TouchEndTouch(DeviceIntPtr dev, TouchPointInfoPtr ti)
{
342 343
    int i;

344
    if (ti->emulate_pointer) {
345 346
        GrabPtr grab;

347
        if ((grab = dev->deviceGrab.grab)) {
348 349
            if (dev->deviceGrab.fromPassiveGrab &&
                !dev->button->buttonsDown &&
350 351
                !dev->touch->buttonsDown && GrabIsPointerGrab(grab))
                (*dev->deviceGrab.DeactivateGrab) (dev);
352 353 354
        }
    }

355 356 357
    for (i = 0; i < ti->num_listeners; i++)
        TouchRemoveListener(ti, ti->listeners[0].listener);

358 359 360 361 362 363 364 365 366
    ti->active = FALSE;
    ti->pending_finish = FALSE;
    ti->sprite.spriteTraceGood = 0;
    free(ti->listeners);
    ti->listeners = NULL;
    ti->num_listeners = 0;
    ti->num_grabs = 0;
    ti->client_id = 0;

367 368
    TouchEventHistoryFree(ti);

369 370
    valuator_mask_zero(ti->valuators);
}
371

372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
/**
 * Allocate the event history for this touch pointer. Calling this on a
 * touchpoint that already has an event history does nothing but counts as
 * as success.
 *
 * @return TRUE on success, FALSE on allocation errors
 */
Bool
TouchEventHistoryAllocate(TouchPointInfoPtr ti)
{
    if (ti->history)
        return TRUE;

    ti->history = calloc(TOUCH_HISTORY_SIZE, sizeof(*ti->history));
    ti->history_elements = 0;
    if (ti->history)
        ti->history_size = TOUCH_HISTORY_SIZE;
    return ti->history != NULL;
}

void
TouchEventHistoryFree(TouchPointInfoPtr ti)
{
    free(ti->history);
    ti->history = NULL;
    ti->history_size = 0;
    ti->history_elements = 0;
}

/**
 * Store the given event on the event history (if one exists)
 * A touch event history consists of one TouchBegin and several TouchUpdate
 * events (if applicable) but no TouchEnd event.
 * If more than one TouchBegin is pushed onto the stack, the push is
 * ignored, calling this function multiple times for the TouchBegin is
 * valid.
 */
void
TouchEventHistoryPush(TouchPointInfoPtr ti, const DeviceEvent *ev)
{
    if (!ti->history)
        return;

415 416 417 418
    switch (ev->type) {
    case ET_TouchBegin:
        /* don't store the same touchbegin twice */
        if (ti->history_elements > 0)
419
            return;
420 421 422 423 424 425 426
        break;
    case ET_TouchUpdate:
        break;
    case ET_TouchEnd:
        return;                 /* no TouchEnd events in the history */
    default:
        return;
427 428 429
    }

    /* We only store real events in the history */
430
    if (ev->flags & (TOUCH_CLIENT_ID | TOUCH_REPLAYING))
431 432 433 434
        return;

    ti->history[ti->history_elements++] = *ev;
    /* FIXME: proper overflow fixes */
435
    if (ti->history_elements > ti->history_size - 1) {
436
        ti->history_elements = ti->history_size - 1;
437
        DebugF("source device %d: history size %zu overflowing for touch %u\n",
438
               ti->sourceid, ti->history_size, ti->client_id);
439 440 441 442 443 444
    }
}

void
TouchEventHistoryReplay(TouchPointInfoPtr ti, DeviceIntPtr dev, XID resource)
{
445
    int i;
446 447 448 449

    if (!ti->history)
        return;

450
    TouchDeliverDeviceClassesChangedEvent(ti, ti->history[0].time, resource);
451

452
    for (i = 0; i < ti->history_elements; i++) {
453
        DeviceEvent *ev = &ti->history[i];
454

455
        ev->flags |= TOUCH_REPLAYING;
456 457 458 459 460 461 462 463 464 465 466 467 468 469 470
        ev->resource = resource;
        /* FIXME:
           We're replaying ti->history which contains the TouchBegin +
           all TouchUpdates for ti. This needs to be passed on to the next
           listener. If that is a touch listener, everything is dandy.
           If the TouchBegin however triggers a sync passive grab, the
           TouchUpdate events must be sent to EnqueueEvent so the events end
           up in syncEvents.pending to be forwarded correctly in a
           subsequent ComputeFreeze().

           However, if we just send them to EnqueueEvent the sync'ing device
           prevents handling of touch events for ownership listeners who
           want the events right here, right now.
         */
        dev->public.processInputProc((InternalEvent*)ev, dev);
471 472
    }
}
473

474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497
void
TouchDeliverDeviceClassesChangedEvent(TouchPointInfoPtr ti, Time time,
                                      XID resource)
{
    DeviceIntPtr dev;
    int num_events = 0;
    InternalEvent dcce;

    dixLookupDevice(&dev, ti->sourceid, serverClient, DixWriteAccess);

    if (!dev)
        return;

    /* UpdateFromMaster generates at most one event */
    UpdateFromMaster(&dcce, dev, DEVCHANGE_POINTER_EVENT, &num_events);
    BUG_WARN(num_events > 1);

    if (num_events) {
        dcce.any.time = time;
        /* FIXME: This doesn't do anything */
        dev->public.processInputProc(&dcce, dev);
    }
}

498 499 500 501 502 503 504 505 506 507 508
Bool
TouchBuildDependentSpriteTrace(DeviceIntPtr dev, SpritePtr sprite)
{
    int i;
    TouchClassPtr t = dev->touch;
    WindowPtr *trace;
    SpritePtr srcsprite;

    /* All touches should have the same sprite trace, so find and reuse an
     * existing touch's sprite if possible, else use the device's sprite. */
    for (i = 0; i < t->num_touches; i++)
509 510
        if (!t->touches[i].pending_finish &&
            t->touches[i].sprite.spriteTraceGood > 0)
511 512 513 514 515 516 517 518
            break;
    if (i < t->num_touches)
        srcsprite = &t->touches[i].sprite;
    else if (dev->spriteInfo->sprite)
        srcsprite = dev->spriteInfo->sprite;
    else
        return FALSE;

519
    if (srcsprite->spriteTraceGood > sprite->spriteTraceSize) {
520 521
        trace = reallocarray(sprite->spriteTrace,
                             srcsprite->spriteTraceSize, sizeof(*trace));
522
        if (!trace) {
523 524 525 526 527 528 529
            sprite->spriteTraceGood = 0;
            return FALSE;
        }
        sprite->spriteTrace = trace;
        sprite->spriteTraceSize = srcsprite->spriteTraceGood;
    }
    memcpy(sprite->spriteTrace, srcsprite->spriteTrace,
530
           srcsprite->spriteTraceGood * sizeof(*trace));
531 532 533 534 535 536 537 538 539 540
    sprite->spriteTraceGood = srcsprite->spriteTraceGood;

    return TRUE;
}

/**
 * Ensure a window trace is present in ti->sprite, constructing one for
 * TouchBegin events.
 */
Bool
541 542
TouchBuildSprite(DeviceIntPtr sourcedev, TouchPointInfoPtr ti,
                 InternalEvent *ev)
543 544 545 546
{
    TouchClassPtr t = sourcedev->touch;
    SpritePtr sprite = &ti->sprite;

547
    if (t->mode == XIDirectTouch) {
548 549 550 551 552 553 554 555 556 557 558 559 560
        /* Focus immediately under the touchpoint in direct touch mode.
         * XXX: Do we need to handle crossing screens here? */
        sprite->spriteTrace[0] =
            sourcedev->spriteInfo->sprite->hotPhys.pScreen->root;
        XYToWindow(sprite, ev->device_event.root_x, ev->device_event.root_y);
    }
    else if (!TouchBuildDependentSpriteTrace(sourcedev, sprite))
        return FALSE;

    if (sprite->spriteTraceGood <= 0)
        return FALSE;

    /* Mark which grabs/event selections we're delivering to: max one grab per
561 562
     * window plus the bottom-most event selection, plus any active grab. */
    ti->listeners = calloc(sprite->spriteTraceGood + 2, sizeof(*ti->listeners));
563
    if (!ti->listeners) {
564 565 566 567 568 569 570
        sprite->spriteTraceGood = 0;
        return FALSE;
    }
    ti->num_listeners = 0;

    return TRUE;
}
571

572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592
/**
 * Copy the touch event into the pointer_event, switching the required
 * fields to make it a correct pointer event.
 *
 * @param event The original touch event
 * @param[in] motion_event The respective motion event
 * @param[in] button_event The respective button event (if any)
 *
 * @returns The number of converted events.
 * @retval 0 An error occured
 * @retval 1 only the motion event is valid
 * @retval 2 motion and button event are valid
 */
int
TouchConvertToPointerEvent(const InternalEvent *event,
                           InternalEvent *motion_event,
                           InternalEvent *button_event)
{
    int ptrtype;
    int nevents = 0;

593 594
    BUG_RETURN_VAL(!event, 0);
    BUG_RETURN_VAL(!motion_event, 0);
595

596 597 598 599 600 601 602 603 604 605 606 607 608 609 610
    switch (event->any.type) {
    case ET_TouchUpdate:
        nevents = 1;
        break;
    case ET_TouchBegin:
        nevents = 2;            /* motion + press */
        ptrtype = ET_ButtonPress;
        break;
    case ET_TouchEnd:
        nevents = 2;            /* motion + release */
        ptrtype = ET_ButtonRelease;
        break;
    default:
        BUG_WARN_MSG(1, "Invalid event type %d\n", event->any.type);
        return 0;
611 612 613 614 615
    }

    BUG_WARN_MSG(!(event->device_event.flags & TOUCH_POINTER_EMULATED),
                 "Non-emulating touch event\n");

616
    motion_event->device_event = event->device_event;
617 618 619 620
    motion_event->any.type = ET_Motion;
    motion_event->device_event.detail.button = 0;
    motion_event->device_event.flags = XIPointerEmulated;

621
    if (nevents > 1) {
622
        BUG_RETURN_VAL(!button_event, 0);
623
        button_event->device_event = event->device_event;
624 625 626 627 628 629 630 631
        button_event->any.type = ptrtype;
        button_event->device_event.flags = XIPointerEmulated;
        /* detail is already correct */
    }

    return nevents;
}

632 633 634 635 636 637 638 639 640
/**
 * Return the corresponding pointer emulation internal event type for the given
 * touch event or 0 if no such event type exists.
 */
int
TouchGetPointerEventType(const InternalEvent *event)
{
    int type = 0;

641 642 643 644 645 646 647 648 649 650 651 652
    switch (event->any.type) {
    case ET_TouchBegin:
        type = ET_ButtonPress;
        break;
    case ET_TouchUpdate:
        type = ET_Motion;
        break;
    case ET_TouchEnd:
        type = ET_ButtonRelease;
        break;
    default:
        break;
653 654 655 656
    }
    return type;
}

657 658 659 660 661 662 663 664 665 666
/**
 * @returns TRUE if the specified grab or selection is the current owner of
 * the touch sequence.
 */
Bool
TouchResourceIsOwner(TouchPointInfoPtr ti, XID resource)
{
    return (ti->listeners[0].listener == resource);
}

667 668 669 670
/**
 * Add the resource to this touch's listeners.
 */
void
671 672 673
TouchAddListener(TouchPointInfoPtr ti, XID resource, int resource_type,
                 enum InputLevel level, enum TouchListenerType type,
                 enum TouchListenerState state, WindowPtr window,
674
                 const GrabPtr grab)
675
{
676 677 678 679 680 681 682 683
    GrabPtr g = NULL;

    /* We need a copy of the grab, not the grab itself since that may be
     * deleted by a UngrabButton request and leaves us with a dangling
     * pointer */
    if (grab)
        g = AllocGrab(grab);

684
    ti->listeners[ti->num_listeners].listener = resource;
685
    ti->listeners[ti->num_listeners].resource_type = resource_type;
686 687 688
    ti->listeners[ti->num_listeners].level = level;
    ti->listeners[ti->num_listeners].state = state;
    ti->listeners[ti->num_listeners].type = type;
689
    ti->listeners[ti->num_listeners].window = window;
690
    ti->listeners[ti->num_listeners].grab = g;
691 692
    if (grab)
        ti->num_grabs++;
693 694 695 696 697 698 699 700 701 702 703 704 705
    ti->num_listeners++;
}

/**
 * Remove the resource from this touch's listeners.
 *
 * @return TRUE if the resource was removed, FALSE if the resource was not
 * in the list
 */
Bool
TouchRemoveListener(TouchPointInfoPtr ti, XID resource)
{
    int i;
706 707

    for (i = 0; i < ti->num_listeners; i++) {
708
        int j;
709
        TouchListener *listener = &ti->listeners[i];
710

711
        if (listener->listener != resource)
712
            continue;
713

714
        if (listener->grab) {
715
            FreeGrab(listener->grab);
716
            listener->grab = NULL;
717
            ti->num_grabs--;
718
        }
719 720 721 722 723 724

        for (j = i; j < ti->num_listeners - 1; j++)
            ti->listeners[j] = ti->listeners[j + 1];
        ti->num_listeners--;
        ti->listeners[ti->num_listeners].listener = 0;
        ti->listeners[ti->num_listeners].state = LISTENER_AWAITING_BEGIN;
725

726
        return TRUE;
727 728 729 730 731 732 733 734 735 736 737 738
    }
    return FALSE;
}

static void
TouchAddGrabListener(DeviceIntPtr dev, TouchPointInfoPtr ti,
                     InternalEvent *ev, GrabPtr grab)
{
    enum TouchListenerType type = LISTENER_GRAB;

    /* FIXME: owner_events */

739
    if (grab->grabtype == XI2) {
740 741 742 743
        if (!xi2mask_isset(grab->xi2mask, dev, XI_TouchOwnership))
            TouchEventHistoryAllocate(ti);
        if (!xi2mask_isset(grab->xi2mask, dev, XI_TouchBegin))
            type = LISTENER_POINTER_GRAB;
744 745
    }
    else if (grab->grabtype == XI || grab->grabtype == CORE) {
746 747 748 749
        TouchEventHistoryAllocate(ti);
        type = LISTENER_POINTER_GRAB;
    }

750 751
    /* grab listeners are always RT_NONE since we keep the grab pointer */
    TouchAddListener(ti, grab->resource, RT_NONE, grab->grabtype,
752
                     type, LISTENER_AWAITING_BEGIN, grab->window, grab);
753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778
}

/**
 * Add one listener if there is a grab on the given window.
 */
static void
TouchAddPassiveGrabListener(DeviceIntPtr dev, TouchPointInfoPtr ti,
                            WindowPtr win, InternalEvent *ev)
{
    GrabPtr grab;
    Bool check_core = IsMaster(dev) && ti->emulate_pointer;

    /* FIXME: make CheckPassiveGrabsOnWindow only trigger on TouchBegin */
    grab = CheckPassiveGrabsOnWindow(win, dev, ev, check_core, FALSE);
    if (!grab)
        return;

    TouchAddGrabListener(dev, ti, ev, grab);
}

static Bool
TouchAddRegularListener(DeviceIntPtr dev, TouchPointInfoPtr ti,
                        WindowPtr win, InternalEvent *ev)
{
    InputClients *iclients = NULL;
    OtherInputMasks *inputMasks = NULL;
779
    uint16_t evtype = 0;        /* may be event type or emulated event type */
780 781 782 783 784 785 786
    enum TouchListenerType type = LISTENER_REGULAR;
    int mask;

    evtype = GetXI2Type(ev->any.type);
    mask = EventIsDeliverable(dev, ev->any.type, win);
    if (!mask && !ti->emulate_pointer)
        return FALSE;
787
    else if (!mask) {           /* now try for pointer event */
788
        mask = EventIsDeliverable(dev, TouchGetPointerEventType(ev), win);
789
        if (mask) {
790 791 792 793 794 795 796 797 798
            evtype = GetXI2Type(TouchGetPointerEventType(ev));
            type = LISTENER_POINTER_REGULAR;
        }
    }
    if (!mask)
        return FALSE;

    inputMasks = wOtherInputMasks(win);

799 800
    if (mask & EVENT_XI2_MASK) {
        nt_list_for_each_entry(iclients, inputMasks->inputClients, next) {
801 802 803 804 805 806
            if (!xi2mask_isset(iclients->xi2mask, dev, evtype))
                continue;

            if (!xi2mask_isset(iclients->xi2mask, dev, XI_TouchOwnership))
                TouchEventHistoryAllocate(ti);

807
            TouchAddListener(ti, iclients->resource, RT_INPUTCLIENT, XI2,
808
                             type, LISTENER_AWAITING_BEGIN, win, NULL);
809 810 811 812
            return TRUE;
        }
    }

813
    if (mask & EVENT_XI1_MASK) {
814 815
        int xitype = GetXIType(TouchGetPointerEventType(ev));
        Mask xi_filter = event_get_filter_from_type(dev, xitype);
816 817

        nt_list_for_each_entry(iclients, inputMasks->inputClients, next) {
818 819 820 821
            if (!(iclients->mask[dev->id] & xi_filter))
                continue;

            TouchEventHistoryAllocate(ti);
822
            TouchAddListener(ti, iclients->resource, RT_INPUTCLIENT, XI,
823
                             LISTENER_POINTER_REGULAR, LISTENER_AWAITING_BEGIN,
824
                             win, NULL);
825 826 827 828
            return TRUE;
        }
    }

829
    if (mask & EVENT_CORE_MASK) {
830 831
        int coretype = GetCoreType(TouchGetPointerEventType(ev));
        Mask core_filter = event_get_filter_from_type(dev, coretype);
832
        OtherClients *oclients;
833 834

        /* window owner */
835
        if (IsMaster(dev) && (win->eventMask & core_filter)) {
836
            TouchEventHistoryAllocate(ti);
837
            TouchAddListener(ti, win->drawable.id, RT_WINDOW, CORE,
838
                             LISTENER_POINTER_REGULAR, LISTENER_AWAITING_BEGIN,
839
                             win, NULL);
840 841 842 843
            return TRUE;
        }

        /* all others */
844 845
        nt_list_for_each_entry(oclients, wOtherClients(win), next) {
            if (!(oclients->mask & core_filter))
846 847 848
                continue;

            TouchEventHistoryAllocate(ti);
849
            TouchAddListener(ti, oclients->resource, RT_OTHERCLIENT, CORE,
850
                             type, LISTENER_AWAITING_BEGIN, win, NULL);
851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867
            return TRUE;
        }
    }

    return FALSE;
}

static void
TouchAddActiveGrabListener(DeviceIntPtr dev, TouchPointInfoPtr ti,
                           InternalEvent *ev, GrabPtr grab)
{
    if (!ti->emulate_pointer &&
        (grab->grabtype == CORE || grab->grabtype == XI))
        return;

    if (!ti->emulate_pointer &&
        grab->grabtype == XI2 &&
868
        !xi2mask_isset(grab->xi2mask, dev, XI_TouchBegin))
869 870 871 872 873 874 875 876 877 878 879 880
        return;

    TouchAddGrabListener(dev, ti, ev, grab);
}

void
TouchSetupListeners(DeviceIntPtr dev, TouchPointInfoPtr ti, InternalEvent *ev)
{
    int i;
    SpritePtr sprite = &ti->sprite;
    WindowPtr win;

881
    if (dev->deviceGrab.grab && !dev->deviceGrab.fromPassiveGrab)
882 883
        TouchAddActiveGrabListener(dev, ti, ev, dev->deviceGrab.grab);

884 885 886 887 888
    /* We set up an active touch listener for existing touches, but not any
     * passive grab or regular listeners. */
    if (ev->any.type != ET_TouchBegin)
        return;

889 890
    /* First, find all grabbing clients from the root window down
     * to the deepest child window. */
891
    for (i = 0; i < sprite->spriteTraceGood; i++) {
892 893 894 895 896 897
        win = sprite->spriteTrace[i];
        TouchAddPassiveGrabListener(dev, ti, win, ev);
    }

    /* Find the first client with an applicable event selection,
     * going from deepest child window back up to the root window. */
898
    for (i = sprite->spriteTraceGood - 1; i >= 0; i--) {
899 900 901 902 903 904 905 906
        Bool delivered;

        win = sprite->spriteTrace[i];
        delivered = TouchAddRegularListener(dev, ti, win, ev);
        if (delivered)
            return;
    }
}
907 908

/**
Peter Hutterer's avatar
Peter Hutterer committed
909 910
 * Remove the touch pointer grab from the device. Called from
 * DeactivatePointerGrab()
911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926
 */
void
TouchRemovePointerGrab(DeviceIntPtr dev)
{
    TouchPointInfoPtr ti;
    GrabPtr grab;
    DeviceEvent *ev;

    if (!dev->touch)
        return;

    grab = dev->deviceGrab.grab;
    if (!grab)
        return;

    ev = dev->deviceGrab.sync.event;
927
    if (!IsTouchEvent((InternalEvent *) ev))
928 929 930 931 932
        return;

    ti = TouchFindByClientID(dev, ev->touchid);
    if (!ti)
        return;
933 934

    /* FIXME: missing a bit of code here... */
935
}
936 937 938 939 940 941 942 943 944 945 946 947 948 949 950

/* As touch grabs don't turn into active grabs with their own resources, we
 * need to walk all the touches and remove this grab from any delivery
 * lists. */
void
TouchListenerGone(XID resource)
{
    TouchPointInfoPtr ti;
    DeviceIntPtr dev;
    InternalEvent *events = InitEventList(GetMaximumEventsNum());
    int i, j, k, nev;

    if (!events)
        FatalError("TouchListenerGone: couldn't allocate events\n");

951
    for (dev = inputInfo.devices; dev; dev = dev->next) {
952 953 954
        if (!dev->touch)
            continue;

955
        for (i = 0; i < dev->touch->num_touches; i++) {
956 957 958 959
            ti = &dev->touch->touches[i];
            if (!ti->active)
                continue;

960
            for (j = 0; j < ti->num_listeners; j++) {
961
                if (CLIENT_BITS(ti->listeners[j].listener) != resource)
962 963 964
                    continue;

                nev = GetTouchOwnershipEvents(events, dev, ti, XIRejectTouch,
965
                                              ti->listeners[j].listener, 0);
966 967 968 969 970 971 972 973 974 975
                for (k = 0; k < nev; k++)
                    mieqProcessDeviceEvent(dev, events + k, NULL);

                break;
            }
        }
    }

    FreeEventList(events, GetMaximumEventsNum());
}
976

977 978 979 980 981 982 983 984
int
TouchListenerAcceptReject(DeviceIntPtr dev, TouchPointInfoPtr ti, int listener,
                          int mode)
{
    InternalEvent *events;
    int nev;
    int i;

985 986
    BUG_RETURN_VAL(listener < 0, BadMatch);
    BUG_RETURN_VAL(listener >= ti->num_listeners, BadMatch);
987

988 989 990 991 992 993 994 995 996 997
    if (listener > 0) {
        if (mode == XIRejectTouch)
            TouchRejected(dev, ti, ti->listeners[listener].listener, NULL);
        else
            ti->listeners[listener].state = LISTENER_EARLY_ACCEPT;

        return Success;
    }

    events = InitEventList(GetMaximumEventsNum());
998
    BUG_RETURN_VAL_MSG(!events, BadAlloc, "Failed to allocate touch ownership events\n");
999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011

    nev = GetTouchOwnershipEvents(events, dev, ti, mode,
                                  ti->listeners[0].listener, 0);
    BUG_WARN_MSG(nev == 0, "Failed to get touch ownership events\n");

    for (i = 0; i < nev; i++)
        mieqProcessDeviceEvent(dev, events + i, NULL);

    FreeEventList(events, GetMaximumEventsNum());

    return nev ? Success : BadMatch;
}

1012 1013
int
TouchAcceptReject(ClientPtr client, DeviceIntPtr dev, int mode,
1014
                  uint32_t touchid, Window grab_window, XID *error)
1015 1016
{
    TouchPointInfoPtr ti;
1017
    int i;
1018

1019
    if (!dev->touch) {
1020 1021 1022 1023 1024
        *error = dev->id;
        return BadDevice;
    }

    ti = TouchFindByClientID(dev, touchid);
1025
    if (!ti) {
1026 1027 1028 1029
        *error = touchid;
        return BadValue;
    }

1030
    for (i = 0; i < ti->num_listeners; i++) {
1031 1032 1033 1034 1035 1036 1037
        if (CLIENT_ID(ti->listeners[i].listener) == client->index &&
            ti->listeners[i].window->drawable.id == grab_window)
            break;
    }
    if (i == ti->num_listeners)
        return BadAccess;

1038
    return TouchListenerAcceptReject(dev, ti, i, mode);
1039
}
1040 1041 1042 1043 1044 1045 1046 1047 1048 1049

/**
 * End physically active touches for a device.
 */
void
TouchEndPhysicallyActiveTouches(DeviceIntPtr dev)
{
    InternalEvent *eventlist = InitEventList(GetMaximumEventsNum());
    int i;

1050
    input_lock();
1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063
    mieqProcessInputEvents();
    for (i = 0; i < dev->last.num_touches; i++) {
        DDXTouchPointInfoPtr ddxti = dev->last.touches + i;

        if (ddxti->active) {
            int j;
            int nevents = GetTouchEvents(eventlist, dev, ddxti->ddx_id,
                                         XI_TouchEnd, 0, NULL);

            for (j = 0; j < nevents; j++)
                mieqProcessDeviceEvent(dev, eventlist + j, NULL);
        }
    }
1064
    input_unlock();
1065 1066 1067

    FreeEventList(eventlist, GetMaximumEventsNum());
}
1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093

/**
 * Generate and deliver a TouchEnd event.
 *
 * @param dev The device to deliver the event for.
 * @param ti The touch point record to deliver the event for.
 * @param flags Internal event flags. The called does not need to provide
 *        TOUCH_CLIENT_ID and TOUCH_POINTER_EMULATED, this function will ensure
 *        they are set appropriately.
 * @param resource The client resource to deliver to, or 0 for all clients.
 */
void
TouchEmitTouchEnd(DeviceIntPtr dev, TouchPointInfoPtr ti, int flags, XID resource)
{
    InternalEvent event;

    /* We're not processing a touch end for a frozen device */
    if (dev->deviceGrab.sync.frozen)
        return;

    flags |= TOUCH_CLIENT_ID;
    if (ti->emulate_pointer)
        flags |= TOUCH_POINTER_EMULATED;
    TouchDeliverDeviceClassesChangedEvent(ti, GetTimeInMillis(), resource);
    GetDixTouchEnd(&event, dev, ti, flags);
    DeliverTouchEvents(dev, ti, &event, resource);
1094 1095
    if (ti->num_grabs == 0)
        UpdateDeviceState(dev, &event.device_event);
1096
}
1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110

void
TouchAcceptAndEnd(DeviceIntPtr dev, int touchid)
{
    TouchPointInfoPtr ti = TouchFindByClientID(dev, touchid);
    if (!ti)
        return;

    TouchListenerAcceptReject(dev, ti, 0, XIAcceptTouch);
    if (ti->pending_finish)
        TouchEmitTouchEnd(dev, ti, 0, 0);
    if (ti->num_listeners <= 1)
        TouchEndTouch(dev, ti);
}