widget/gonk/libui/InputDispatcher.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/widget/gonk/libui/InputDispatcher.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,1117 @@
     1.4 +/*
     1.5 + * Copyright (C) 2010 The Android Open Source Project
     1.6 + *
     1.7 + * Licensed under the Apache License, Version 2.0 (the "License");
     1.8 + * you may not use this file except in compliance with the License.
     1.9 + * You may obtain a copy of the License at
    1.10 + *
    1.11 + *      http://www.apache.org/licenses/LICENSE-2.0
    1.12 + *
    1.13 + * Unless required by applicable law or agreed to in writing, software
    1.14 + * distributed under the License is distributed on an "AS IS" BASIS,
    1.15 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    1.16 + * See the License for the specific language governing permissions and
    1.17 + * limitations under the License.
    1.18 + */
    1.19 +
    1.20 +#ifndef _UI_INPUT_DISPATCHER_H
    1.21 +#define _UI_INPUT_DISPATCHER_H
    1.22 +
    1.23 +#include "Input.h"
    1.24 +#include "InputTransport.h"
    1.25 +#include <utils/KeyedVector.h>
    1.26 +#include <utils/Vector.h>
    1.27 +#include <utils/threads.h>
    1.28 +#include <utils/Timers.h>
    1.29 +#include <utils/RefBase.h>
    1.30 +#include <utils/String8.h>
    1.31 +#include <utils/Looper.h>
    1.32 +#include <utils/BitSet.h>
    1.33 +#include <cutils/atomic.h>
    1.34 +
    1.35 +#include <stddef.h>
    1.36 +#include <unistd.h>
    1.37 +#include <limits.h>
    1.38 +
    1.39 +#include "InputWindow.h"
    1.40 +#include "InputApplication.h"
    1.41 +#include "InputListener.h"
    1.42 +
    1.43 +
    1.44 +namespace android {
    1.45 +
    1.46 +/*
    1.47 + * Constants used to report the outcome of input event injection.
    1.48 + */
    1.49 +enum {
    1.50 +    /* (INTERNAL USE ONLY) Specifies that injection is pending and its outcome is unknown. */
    1.51 +    INPUT_EVENT_INJECTION_PENDING = -1,
    1.52 +
    1.53 +    /* Injection succeeded. */
    1.54 +    INPUT_EVENT_INJECTION_SUCCEEDED = 0,
    1.55 +
    1.56 +    /* Injection failed because the injector did not have permission to inject
    1.57 +     * into the application with input focus. */
    1.58 +    INPUT_EVENT_INJECTION_PERMISSION_DENIED = 1,
    1.59 +
    1.60 +    /* Injection failed because there were no available input targets. */
    1.61 +    INPUT_EVENT_INJECTION_FAILED = 2,
    1.62 +
    1.63 +    /* Injection failed due to a timeout. */
    1.64 +    INPUT_EVENT_INJECTION_TIMED_OUT = 3
    1.65 +};
    1.66 +
    1.67 +/*
    1.68 + * Constants used to determine the input event injection synchronization mode.
    1.69 + */
    1.70 +enum {
    1.71 +    /* Injection is asynchronous and is assumed always to be successful. */
    1.72 +    INPUT_EVENT_INJECTION_SYNC_NONE = 0,
    1.73 +
    1.74 +    /* Waits for previous events to be dispatched so that the input dispatcher can determine
    1.75 +     * whether input event injection willbe permitted based on the current input focus.
    1.76 +     * Does not wait for the input event to finish processing. */
    1.77 +    INPUT_EVENT_INJECTION_SYNC_WAIT_FOR_RESULT = 1,
    1.78 +
    1.79 +    /* Waits for the input event to be completely processed. */
    1.80 +    INPUT_EVENT_INJECTION_SYNC_WAIT_FOR_FINISHED = 2,
    1.81 +};
    1.82 +
    1.83 +
    1.84 +/*
    1.85 + * An input target specifies how an input event is to be dispatched to a particular window
    1.86 + * including the window's input channel, control flags, a timeout, and an X / Y offset to
    1.87 + * be added to input event coordinates to compensate for the absolute position of the
    1.88 + * window area.
    1.89 + */
    1.90 +struct InputTarget {
    1.91 +    enum {
    1.92 +        /* This flag indicates that the event is being delivered to a foreground application. */
    1.93 +        FLAG_FOREGROUND = 1 << 0,
    1.94 +
    1.95 +        /* This flag indicates that the target of a MotionEvent is partly or wholly
    1.96 +         * obscured by another visible window above it.  The motion event should be
    1.97 +         * delivered with flag AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED. */
    1.98 +        FLAG_WINDOW_IS_OBSCURED = 1 << 1,
    1.99 +
   1.100 +        /* This flag indicates that a motion event is being split across multiple windows. */
   1.101 +        FLAG_SPLIT = 1 << 2,
   1.102 +
   1.103 +        /* This flag indicates that the pointer coordinates dispatched to the application
   1.104 +         * will be zeroed out to avoid revealing information to an application. This is
   1.105 +         * used in conjunction with FLAG_DISPATCH_AS_OUTSIDE to prevent apps not sharing
   1.106 +         * the same UID from watching all touches. */
   1.107 +        FLAG_ZERO_COORDS = 1 << 3,
   1.108 +
   1.109 +        /* This flag indicates that the event should be sent as is.
   1.110 +         * Should always be set unless the event is to be transmuted. */
   1.111 +        FLAG_DISPATCH_AS_IS = 1 << 8,
   1.112 +
   1.113 +        /* This flag indicates that a MotionEvent with AMOTION_EVENT_ACTION_DOWN falls outside
   1.114 +         * of the area of this target and so should instead be delivered as an
   1.115 +         * AMOTION_EVENT_ACTION_OUTSIDE to this target. */
   1.116 +        FLAG_DISPATCH_AS_OUTSIDE = 1 << 9,
   1.117 +
   1.118 +        /* This flag indicates that a hover sequence is starting in the given window.
   1.119 +         * The event is transmuted into ACTION_HOVER_ENTER. */
   1.120 +        FLAG_DISPATCH_AS_HOVER_ENTER = 1 << 10,
   1.121 +
   1.122 +        /* This flag indicates that a hover event happened outside of a window which handled
   1.123 +         * previous hover events, signifying the end of the current hover sequence for that
   1.124 +         * window.
   1.125 +         * The event is transmuted into ACTION_HOVER_ENTER. */
   1.126 +        FLAG_DISPATCH_AS_HOVER_EXIT = 1 << 11,
   1.127 +
   1.128 +        /* This flag indicates that the event should be canceled.
   1.129 +         * It is used to transmute ACTION_MOVE into ACTION_CANCEL when a touch slips
   1.130 +         * outside of a window. */
   1.131 +        FLAG_DISPATCH_AS_SLIPPERY_EXIT = 1 << 12,
   1.132 +
   1.133 +        /* This flag indicates that the event should be dispatched as an initial down.
   1.134 +         * It is used to transmute ACTION_MOVE into ACTION_DOWN when a touch slips
   1.135 +         * into a new window. */
   1.136 +        FLAG_DISPATCH_AS_SLIPPERY_ENTER = 1 << 13,
   1.137 +
   1.138 +        /* Mask for all dispatch modes. */
   1.139 +        FLAG_DISPATCH_MASK = FLAG_DISPATCH_AS_IS
   1.140 +                | FLAG_DISPATCH_AS_OUTSIDE
   1.141 +                | FLAG_DISPATCH_AS_HOVER_ENTER
   1.142 +                | FLAG_DISPATCH_AS_HOVER_EXIT
   1.143 +                | FLAG_DISPATCH_AS_SLIPPERY_EXIT
   1.144 +                | FLAG_DISPATCH_AS_SLIPPERY_ENTER,
   1.145 +    };
   1.146 +
   1.147 +    // The input channel to be targeted.
   1.148 +    sp<InputChannel> inputChannel;
   1.149 +
   1.150 +    // Flags for the input target.
   1.151 +    int32_t flags;
   1.152 +
   1.153 +    // The x and y offset to add to a MotionEvent as it is delivered.
   1.154 +    // (ignored for KeyEvents)
   1.155 +    float xOffset, yOffset;
   1.156 +
   1.157 +    // Scaling factor to apply to MotionEvent as it is delivered.
   1.158 +    // (ignored for KeyEvents)
   1.159 +    float scaleFactor;
   1.160 +
   1.161 +    // The subset of pointer ids to include in motion events dispatched to this input target
   1.162 +    // if FLAG_SPLIT is set.
   1.163 +    BitSet32 pointerIds;
   1.164 +};
   1.165 +
   1.166 +
   1.167 +/*
   1.168 + * Input dispatcher configuration.
   1.169 + *
   1.170 + * Specifies various options that modify the behavior of the input dispatcher.
   1.171 + * The values provided here are merely defaults. The actual values will come from ViewConfiguration
   1.172 + * and are passed into the dispatcher during initialization.
   1.173 + */
   1.174 +struct InputDispatcherConfiguration {
   1.175 +    // The key repeat initial timeout.
   1.176 +    nsecs_t keyRepeatTimeout;
   1.177 +
   1.178 +    // The key repeat inter-key delay.
   1.179 +    nsecs_t keyRepeatDelay;
   1.180 +
   1.181 +    InputDispatcherConfiguration() :
   1.182 +            keyRepeatTimeout(500 * 1000000LL),
   1.183 +            keyRepeatDelay(50 * 1000000LL) { }
   1.184 +};
   1.185 +
   1.186 +
   1.187 +/*
   1.188 + * Input dispatcher policy interface.
   1.189 + *
   1.190 + * The input reader policy is used by the input reader to interact with the Window Manager
   1.191 + * and other system components.
   1.192 + *
   1.193 + * The actual implementation is partially supported by callbacks into the DVM
   1.194 + * via JNI.  This interface is also mocked in the unit tests.
   1.195 + */
   1.196 +class InputDispatcherPolicyInterface : public virtual RefBase {
   1.197 +protected:
   1.198 +    InputDispatcherPolicyInterface() { }
   1.199 +    virtual ~InputDispatcherPolicyInterface() { }
   1.200 +
   1.201 +public:
   1.202 +    /* Notifies the system that a configuration change has occurred. */
   1.203 +    virtual void notifyConfigurationChanged(nsecs_t when) = 0;
   1.204 +
   1.205 +    /* Notifies the system that an application is not responding.
   1.206 +     * Returns a new timeout to continue waiting, or 0 to abort dispatch. */
   1.207 +    virtual nsecs_t notifyANR(const sp<InputApplicationHandle>& inputApplicationHandle,
   1.208 +            const sp<InputWindowHandle>& inputWindowHandle) = 0;
   1.209 +
   1.210 +    /* Notifies the system that an input channel is unrecoverably broken. */
   1.211 +    virtual void notifyInputChannelBroken(const sp<InputWindowHandle>& inputWindowHandle) = 0;
   1.212 +
   1.213 +    /* Gets the input dispatcher configuration. */
   1.214 +    virtual void getDispatcherConfiguration(InputDispatcherConfiguration* outConfig) = 0;
   1.215 +
   1.216 +    /* Returns true if automatic key repeating is enabled. */
   1.217 +    virtual bool isKeyRepeatEnabled() = 0;
   1.218 +
   1.219 +    /* Filters an input event.
   1.220 +     * Return true to dispatch the event unmodified, false to consume the event.
   1.221 +     * A filter can also transform and inject events later by passing POLICY_FLAG_FILTERED
   1.222 +     * to injectInputEvent.
   1.223 +     */
   1.224 +    virtual bool filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags) = 0;
   1.225 +
   1.226 +    /* Intercepts a key event immediately before queueing it.
   1.227 +     * The policy can use this method as an opportunity to perform power management functions
   1.228 +     * and early event preprocessing such as updating policy flags.
   1.229 +     *
   1.230 +     * This method is expected to set the POLICY_FLAG_PASS_TO_USER policy flag if the event
   1.231 +     * should be dispatched to applications.
   1.232 +     */
   1.233 +    virtual void interceptKeyBeforeQueueing(const KeyEvent* keyEvent, uint32_t& policyFlags) = 0;
   1.234 +
   1.235 +    /* Intercepts a touch, trackball or other motion event before queueing it.
   1.236 +     * The policy can use this method as an opportunity to perform power management functions
   1.237 +     * and early event preprocessing such as updating policy flags.
   1.238 +     *
   1.239 +     * This method is expected to set the POLICY_FLAG_PASS_TO_USER policy flag if the event
   1.240 +     * should be dispatched to applications.
   1.241 +     */
   1.242 +    virtual void interceptMotionBeforeQueueing(nsecs_t when, uint32_t& policyFlags) = 0;
   1.243 +
   1.244 +    /* Allows the policy a chance to intercept a key before dispatching. */
   1.245 +    virtual nsecs_t interceptKeyBeforeDispatching(const sp<InputWindowHandle>& inputWindowHandle,
   1.246 +            const KeyEvent* keyEvent, uint32_t policyFlags) = 0;
   1.247 +
   1.248 +    /* Allows the policy a chance to perform default processing for an unhandled key.
   1.249 +     * Returns an alternate keycode to redispatch as a fallback, or 0 to give up. */
   1.250 +    virtual bool dispatchUnhandledKey(const sp<InputWindowHandle>& inputWindowHandle,
   1.251 +            const KeyEvent* keyEvent, uint32_t policyFlags, KeyEvent* outFallbackKeyEvent) = 0;
   1.252 +
   1.253 +    /* Notifies the policy about switch events.
   1.254 +     */
   1.255 +    virtual void notifySwitch(nsecs_t when,
   1.256 +            uint32_t switchValues, uint32_t switchMask, uint32_t policyFlags) = 0;
   1.257 +
   1.258 +    /* Poke user activity for an event dispatched to a window. */
   1.259 +    virtual void pokeUserActivity(nsecs_t eventTime, int32_t eventType) = 0;
   1.260 +
   1.261 +    /* Checks whether a given application pid/uid has permission to inject input events
   1.262 +     * into other applications.
   1.263 +     *
   1.264 +     * This method is special in that its implementation promises to be non-reentrant and
   1.265 +     * is safe to call while holding other locks.  (Most other methods make no such guarantees!)
   1.266 +     */
   1.267 +    virtual bool checkInjectEventsPermissionNonReentrant(
   1.268 +            int32_t injectorPid, int32_t injectorUid) = 0;
   1.269 +};
   1.270 +
   1.271 +
   1.272 +/* Notifies the system about input events generated by the input reader.
   1.273 + * The dispatcher is expected to be mostly asynchronous. */
   1.274 +class InputDispatcherInterface : public virtual RefBase, public InputListenerInterface {
   1.275 +protected:
   1.276 +    InputDispatcherInterface() { }
   1.277 +    virtual ~InputDispatcherInterface() { }
   1.278 +
   1.279 +public:
   1.280 +    /* Dumps the state of the input dispatcher.
   1.281 +     *
   1.282 +     * This method may be called on any thread (usually by the input manager). */
   1.283 +    virtual void dump(String8& dump) = 0;
   1.284 +
   1.285 +    /* Called by the heatbeat to ensures that the dispatcher has not deadlocked. */
   1.286 +    virtual void monitor() = 0;
   1.287 +
   1.288 +    /* Runs a single iteration of the dispatch loop.
   1.289 +     * Nominally processes one queued event, a timeout, or a response from an input consumer.
   1.290 +     *
   1.291 +     * This method should only be called on the input dispatcher thread.
   1.292 +     */
   1.293 +    virtual void dispatchOnce() = 0;
   1.294 +
   1.295 +    /* Injects an input event and optionally waits for sync.
   1.296 +     * The synchronization mode determines whether the method blocks while waiting for
   1.297 +     * input injection to proceed.
   1.298 +     * Returns one of the INPUT_EVENT_INJECTION_XXX constants.
   1.299 +     *
   1.300 +     * This method may be called on any thread (usually by the input manager).
   1.301 +     */
   1.302 +    virtual int32_t injectInputEvent(const InputEvent* event,
   1.303 +            int32_t injectorPid, int32_t injectorUid, int32_t syncMode, int32_t timeoutMillis,
   1.304 +            uint32_t policyFlags) = 0;
   1.305 +
   1.306 +    /* Sets the list of input windows.
   1.307 +     *
   1.308 +     * This method may be called on any thread (usually by the input manager).
   1.309 +     */
   1.310 +    virtual void setInputWindows(const Vector<sp<InputWindowHandle> >& inputWindowHandles) = 0;
   1.311 +
   1.312 +    /* Sets the focused application.
   1.313 +     *
   1.314 +     * This method may be called on any thread (usually by the input manager).
   1.315 +     */
   1.316 +    virtual void setFocusedApplication(
   1.317 +            const sp<InputApplicationHandle>& inputApplicationHandle) = 0;
   1.318 +
   1.319 +    /* Sets the input dispatching mode.
   1.320 +     *
   1.321 +     * This method may be called on any thread (usually by the input manager).
   1.322 +     */
   1.323 +    virtual void setInputDispatchMode(bool enabled, bool frozen) = 0;
   1.324 +
   1.325 +    /* Sets whether input event filtering is enabled.
   1.326 +     * When enabled, incoming input events are sent to the policy's filterInputEvent
   1.327 +     * method instead of being dispatched.  The filter is expected to use
   1.328 +     * injectInputEvent to inject the events it would like to have dispatched.
   1.329 +     * It should include POLICY_FLAG_FILTERED in the policy flags during injection.
   1.330 +     */
   1.331 +    virtual void setInputFilterEnabled(bool enabled) = 0;
   1.332 +
   1.333 +    /* Transfers touch focus from the window associated with one channel to the
   1.334 +     * window associated with the other channel.
   1.335 +     *
   1.336 +     * Returns true on success.  False if the window did not actually have touch focus.
   1.337 +     */
   1.338 +    virtual bool transferTouchFocus(const sp<InputChannel>& fromChannel,
   1.339 +            const sp<InputChannel>& toChannel) = 0;
   1.340 +
   1.341 +    /* Registers or unregister input channels that may be used as targets for input events.
   1.342 +     * If monitor is true, the channel will receive a copy of all input events.
   1.343 +     *
   1.344 +     * These methods may be called on any thread (usually by the input manager).
   1.345 +     */
   1.346 +    virtual status_t registerInputChannel(const sp<InputChannel>& inputChannel,
   1.347 +            const sp<InputWindowHandle>& inputWindowHandle, bool monitor) = 0;
   1.348 +    virtual status_t unregisterInputChannel(const sp<InputChannel>& inputChannel) = 0;
   1.349 +};
   1.350 +
   1.351 +/* Dispatches events to input targets.  Some functions of the input dispatcher, such as
   1.352 + * identifying input targets, are controlled by a separate policy object.
   1.353 + *
   1.354 + * IMPORTANT INVARIANT:
   1.355 + *     Because the policy can potentially block or cause re-entrance into the input dispatcher,
   1.356 + *     the input dispatcher never calls into the policy while holding its internal locks.
   1.357 + *     The implementation is also carefully designed to recover from scenarios such as an
   1.358 + *     input channel becoming unregistered while identifying input targets or processing timeouts.
   1.359 + *
   1.360 + *     Methods marked 'Locked' must be called with the lock acquired.
   1.361 + *
   1.362 + *     Methods marked 'LockedInterruptible' must be called with the lock acquired but
   1.363 + *     may during the course of their execution release the lock, call into the policy, and
   1.364 + *     then reacquire the lock.  The caller is responsible for recovering gracefully.
   1.365 + *
   1.366 + *     A 'LockedInterruptible' method may called a 'Locked' method, but NOT vice-versa.
   1.367 + */
   1.368 +class InputDispatcher : public InputDispatcherInterface {
   1.369 +protected:
   1.370 +    virtual ~InputDispatcher();
   1.371 +
   1.372 +public:
   1.373 +    explicit InputDispatcher(const sp<InputDispatcherPolicyInterface>& policy);
   1.374 +
   1.375 +    virtual void dump(String8& dump);
   1.376 +    virtual void monitor();
   1.377 +
   1.378 +    virtual void dispatchOnce();
   1.379 +
   1.380 +    virtual void notifyConfigurationChanged(const NotifyConfigurationChangedArgs* args);
   1.381 +    virtual void notifyKey(const NotifyKeyArgs* args);
   1.382 +    virtual void notifyMotion(const NotifyMotionArgs* args);
   1.383 +    virtual void notifySwitch(const NotifySwitchArgs* args);
   1.384 +    virtual void notifyDeviceReset(const NotifyDeviceResetArgs* args);
   1.385 +
   1.386 +    virtual int32_t injectInputEvent(const InputEvent* event,
   1.387 +            int32_t injectorPid, int32_t injectorUid, int32_t syncMode, int32_t timeoutMillis,
   1.388 +            uint32_t policyFlags);
   1.389 +
   1.390 +    virtual void setInputWindows(const Vector<sp<InputWindowHandle> >& inputWindowHandles);
   1.391 +    virtual void setFocusedApplication(const sp<InputApplicationHandle>& inputApplicationHandle);
   1.392 +    virtual void setInputDispatchMode(bool enabled, bool frozen);
   1.393 +    virtual void setInputFilterEnabled(bool enabled);
   1.394 +
   1.395 +    virtual bool transferTouchFocus(const sp<InputChannel>& fromChannel,
   1.396 +            const sp<InputChannel>& toChannel);
   1.397 +
   1.398 +    virtual status_t registerInputChannel(const sp<InputChannel>& inputChannel,
   1.399 +            const sp<InputWindowHandle>& inputWindowHandle, bool monitor);
   1.400 +    virtual status_t unregisterInputChannel(const sp<InputChannel>& inputChannel);
   1.401 +
   1.402 +private:
   1.403 +    template <typename T>
   1.404 +    struct Link {
   1.405 +        T* next;
   1.406 +        T* prev;
   1.407 +
   1.408 +    protected:
   1.409 +        inline Link() : next(NULL), prev(NULL) { }
   1.410 +    };
   1.411 +
   1.412 +    struct InjectionState {
   1.413 +        mutable int32_t refCount;
   1.414 +
   1.415 +        int32_t injectorPid;
   1.416 +        int32_t injectorUid;
   1.417 +        int32_t injectionResult;  // initially INPUT_EVENT_INJECTION_PENDING
   1.418 +        bool injectionIsAsync; // set to true if injection is not waiting for the result
   1.419 +        int32_t pendingForegroundDispatches; // the number of foreground dispatches in progress
   1.420 +
   1.421 +        InjectionState(int32_t injectorPid, int32_t injectorUid);
   1.422 +        void release();
   1.423 +
   1.424 +    private:
   1.425 +        ~InjectionState();
   1.426 +    };
   1.427 +
   1.428 +    struct EventEntry : Link<EventEntry> {
   1.429 +        enum {
   1.430 +            TYPE_CONFIGURATION_CHANGED,
   1.431 +            TYPE_DEVICE_RESET,
   1.432 +            TYPE_KEY,
   1.433 +            TYPE_MOTION
   1.434 +        };
   1.435 +
   1.436 +        mutable int32_t refCount;
   1.437 +        int32_t type;
   1.438 +        nsecs_t eventTime;
   1.439 +        uint32_t policyFlags;
   1.440 +        InjectionState* injectionState;
   1.441 +
   1.442 +        bool dispatchInProgress; // initially false, set to true while dispatching
   1.443 +
   1.444 +        inline bool isInjected() const { return injectionState != NULL; }
   1.445 +
   1.446 +        void release();
   1.447 +
   1.448 +        virtual void appendDescription(String8& msg) const = 0;
   1.449 +
   1.450 +    protected:
   1.451 +        EventEntry(int32_t type, nsecs_t eventTime, uint32_t policyFlags);
   1.452 +        virtual ~EventEntry();
   1.453 +        void releaseInjectionState();
   1.454 +    };
   1.455 +
   1.456 +    struct ConfigurationChangedEntry : EventEntry {
   1.457 +        ConfigurationChangedEntry(nsecs_t eventTime);
   1.458 +        virtual void appendDescription(String8& msg) const;
   1.459 +
   1.460 +    protected:
   1.461 +        virtual ~ConfigurationChangedEntry();
   1.462 +    };
   1.463 +
   1.464 +    struct DeviceResetEntry : EventEntry {
   1.465 +        int32_t deviceId;
   1.466 +
   1.467 +        DeviceResetEntry(nsecs_t eventTime, int32_t deviceId);
   1.468 +        virtual void appendDescription(String8& msg) const;
   1.469 +
   1.470 +    protected:
   1.471 +        virtual ~DeviceResetEntry();
   1.472 +    };
   1.473 +
   1.474 +    struct KeyEntry : EventEntry {
   1.475 +        int32_t deviceId;
   1.476 +        uint32_t source;
   1.477 +        int32_t action;
   1.478 +        int32_t flags;
   1.479 +        int32_t keyCode;
   1.480 +        int32_t scanCode;
   1.481 +        int32_t metaState;
   1.482 +        int32_t repeatCount;
   1.483 +        nsecs_t downTime;
   1.484 +
   1.485 +        bool syntheticRepeat; // set to true for synthetic key repeats
   1.486 +
   1.487 +        enum InterceptKeyResult {
   1.488 +            INTERCEPT_KEY_RESULT_UNKNOWN,
   1.489 +            INTERCEPT_KEY_RESULT_SKIP,
   1.490 +            INTERCEPT_KEY_RESULT_CONTINUE,
   1.491 +            INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER,
   1.492 +        };
   1.493 +        InterceptKeyResult interceptKeyResult; // set based on the interception result
   1.494 +        nsecs_t interceptKeyWakeupTime; // used with INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER
   1.495 +
   1.496 +        KeyEntry(nsecs_t eventTime,
   1.497 +                int32_t deviceId, uint32_t source, uint32_t policyFlags, int32_t action,
   1.498 +                int32_t flags, int32_t keyCode, int32_t scanCode, int32_t metaState,
   1.499 +                int32_t repeatCount, nsecs_t downTime);
   1.500 +        virtual void appendDescription(String8& msg) const;
   1.501 +        void recycle();
   1.502 +
   1.503 +    protected:
   1.504 +        virtual ~KeyEntry();
   1.505 +    };
   1.506 +
   1.507 +    struct MotionEntry : EventEntry {
   1.508 +        nsecs_t eventTime;
   1.509 +        int32_t deviceId;
   1.510 +        uint32_t source;
   1.511 +        int32_t action;
   1.512 +        int32_t flags;
   1.513 +        int32_t metaState;
   1.514 +        int32_t buttonState;
   1.515 +        int32_t edgeFlags;
   1.516 +        float xPrecision;
   1.517 +        float yPrecision;
   1.518 +        nsecs_t downTime;
   1.519 +        int32_t displayId;
   1.520 +        uint32_t pointerCount;
   1.521 +        PointerProperties pointerProperties[MAX_POINTERS];
   1.522 +        PointerCoords pointerCoords[MAX_POINTERS];
   1.523 +
   1.524 +        MotionEntry(nsecs_t eventTime,
   1.525 +                int32_t deviceId, uint32_t source, uint32_t policyFlags,
   1.526 +                int32_t action, int32_t flags,
   1.527 +                int32_t metaState, int32_t buttonState, int32_t edgeFlags,
   1.528 +                float xPrecision, float yPrecision,
   1.529 +                nsecs_t downTime, int32_t displayId, uint32_t pointerCount,
   1.530 +                const PointerProperties* pointerProperties, const PointerCoords* pointerCoords);
   1.531 +        virtual void appendDescription(String8& msg) const;
   1.532 +
   1.533 +    protected:
   1.534 +        virtual ~MotionEntry();
   1.535 +    };
   1.536 +
   1.537 +    // Tracks the progress of dispatching a particular event to a particular connection.
   1.538 +    struct DispatchEntry : Link<DispatchEntry> {
   1.539 +        const uint32_t seq; // unique sequence number, never 0
   1.540 +
   1.541 +        EventEntry* eventEntry; // the event to dispatch
   1.542 +        int32_t targetFlags;
   1.543 +        float xOffset;
   1.544 +        float yOffset;
   1.545 +        float scaleFactor;
   1.546 +        nsecs_t deliveryTime; // time when the event was actually delivered
   1.547 +
   1.548 +        // Set to the resolved action and flags when the event is enqueued.
   1.549 +        int32_t resolvedAction;
   1.550 +        int32_t resolvedFlags;
   1.551 +
   1.552 +        DispatchEntry(EventEntry* eventEntry,
   1.553 +                int32_t targetFlags, float xOffset, float yOffset, float scaleFactor);
   1.554 +        ~DispatchEntry();
   1.555 +
   1.556 +        inline bool hasForegroundTarget() const {
   1.557 +            return targetFlags & InputTarget::FLAG_FOREGROUND;
   1.558 +        }
   1.559 +
   1.560 +        inline bool isSplit() const {
   1.561 +            return targetFlags & InputTarget::FLAG_SPLIT;
   1.562 +        }
   1.563 +
   1.564 +    private:
   1.565 +        static volatile int32_t sNextSeqAtomic;
   1.566 +
   1.567 +        static uint32_t nextSeq();
   1.568 +    };
   1.569 +
   1.570 +    // A command entry captures state and behavior for an action to be performed in the
   1.571 +    // dispatch loop after the initial processing has taken place.  It is essentially
   1.572 +    // a kind of continuation used to postpone sensitive policy interactions to a point
   1.573 +    // in the dispatch loop where it is safe to release the lock (generally after finishing
   1.574 +    // the critical parts of the dispatch cycle).
   1.575 +    //
   1.576 +    // The special thing about commands is that they can voluntarily release and reacquire
   1.577 +    // the dispatcher lock at will.  Initially when the command starts running, the
   1.578 +    // dispatcher lock is held.  However, if the command needs to call into the policy to
   1.579 +    // do some work, it can release the lock, do the work, then reacquire the lock again
   1.580 +    // before returning.
   1.581 +    //
   1.582 +    // This mechanism is a bit clunky but it helps to preserve the invariant that the dispatch
   1.583 +    // never calls into the policy while holding its lock.
   1.584 +    //
   1.585 +    // Commands are implicitly 'LockedInterruptible'.
   1.586 +    struct CommandEntry;
   1.587 +    typedef void (InputDispatcher::*Command)(CommandEntry* commandEntry);
   1.588 +
   1.589 +    class Connection;
   1.590 +    struct CommandEntry : Link<CommandEntry> {
   1.591 +        CommandEntry(Command command);
   1.592 +        ~CommandEntry();
   1.593 +
   1.594 +        Command command;
   1.595 +
   1.596 +        // parameters for the command (usage varies by command)
   1.597 +        sp<Connection> connection;
   1.598 +        nsecs_t eventTime;
   1.599 +        KeyEntry* keyEntry;
   1.600 +        sp<InputApplicationHandle> inputApplicationHandle;
   1.601 +        sp<InputWindowHandle> inputWindowHandle;
   1.602 +        int32_t userActivityEventType;
   1.603 +        uint32_t seq;
   1.604 +        bool handled;
   1.605 +    };
   1.606 +
   1.607 +    // Generic queue implementation.
   1.608 +    template <typename T>
   1.609 +    struct Queue {
   1.610 +        T* head;
   1.611 +        T* tail;
   1.612 +
   1.613 +        inline Queue() : head(NULL), tail(NULL) {
   1.614 +        }
   1.615 +
   1.616 +        inline bool isEmpty() const {
   1.617 +            return !head;
   1.618 +        }
   1.619 +
   1.620 +        inline void enqueueAtTail(T* entry) {
   1.621 +            entry->prev = tail;
   1.622 +            if (tail) {
   1.623 +                tail->next = entry;
   1.624 +            } else {
   1.625 +                head = entry;
   1.626 +            }
   1.627 +            entry->next = NULL;
   1.628 +            tail = entry;
   1.629 +        }
   1.630 +
   1.631 +        inline void enqueueAtHead(T* entry) {
   1.632 +            entry->next = head;
   1.633 +            if (head) {
   1.634 +                head->prev = entry;
   1.635 +            } else {
   1.636 +                tail = entry;
   1.637 +            }
   1.638 +            entry->prev = NULL;
   1.639 +            head = entry;
   1.640 +        }
   1.641 +
   1.642 +        inline void dequeue(T* entry) {
   1.643 +            if (entry->prev) {
   1.644 +                entry->prev->next = entry->next;
   1.645 +            } else {
   1.646 +                head = entry->next;
   1.647 +            }
   1.648 +            if (entry->next) {
   1.649 +                entry->next->prev = entry->prev;
   1.650 +            } else {
   1.651 +                tail = entry->prev;
   1.652 +            }
   1.653 +        }
   1.654 +
   1.655 +        inline T* dequeueAtHead() {
   1.656 +            T* entry = head;
   1.657 +            head = entry->next;
   1.658 +            if (head) {
   1.659 +                head->prev = NULL;
   1.660 +            } else {
   1.661 +                tail = NULL;
   1.662 +            }
   1.663 +            return entry;
   1.664 +        }
   1.665 +
   1.666 +        uint32_t count() const;
   1.667 +    };
   1.668 +
   1.669 +    /* Specifies which events are to be canceled and why. */
   1.670 +    struct CancelationOptions {
   1.671 +        enum Mode {
   1.672 +            CANCEL_ALL_EVENTS = 0,
   1.673 +            CANCEL_POINTER_EVENTS = 1,
   1.674 +            CANCEL_NON_POINTER_EVENTS = 2,
   1.675 +            CANCEL_FALLBACK_EVENTS = 3,
   1.676 +        };
   1.677 +
   1.678 +        // The criterion to use to determine which events should be canceled.
   1.679 +        Mode mode;
   1.680 +
   1.681 +        // Descriptive reason for the cancelation.
   1.682 +        const char* reason;
   1.683 +
   1.684 +        // The specific keycode of the key event to cancel, or -1 to cancel any key event.
   1.685 +        int32_t keyCode;
   1.686 +
   1.687 +        // The specific device id of events to cancel, or -1 to cancel events from any device.
   1.688 +        int32_t deviceId;
   1.689 +
   1.690 +        CancelationOptions(Mode mode, const char* reason) :
   1.691 +                mode(mode), reason(reason), keyCode(-1), deviceId(-1) { }
   1.692 +    };
   1.693 +
   1.694 +    /* Tracks dispatched key and motion event state so that cancelation events can be
   1.695 +     * synthesized when events are dropped. */
   1.696 +    class InputState {
   1.697 +    public:
   1.698 +        InputState();
   1.699 +        ~InputState();
   1.700 +
   1.701 +        // Returns true if there is no state to be canceled.
   1.702 +        bool isNeutral() const;
   1.703 +
   1.704 +        // Returns true if the specified source is known to have received a hover enter
   1.705 +        // motion event.
   1.706 +        bool isHovering(int32_t deviceId, uint32_t source, int32_t displayId) const;
   1.707 +
   1.708 +        // Records tracking information for a key event that has just been published.
   1.709 +        // Returns true if the event should be delivered, false if it is inconsistent
   1.710 +        // and should be skipped.
   1.711 +        bool trackKey(const KeyEntry* entry, int32_t action, int32_t flags);
   1.712 +
   1.713 +        // Records tracking information for a motion event that has just been published.
   1.714 +        // Returns true if the event should be delivered, false if it is inconsistent
   1.715 +        // and should be skipped.
   1.716 +        bool trackMotion(const MotionEntry* entry, int32_t action, int32_t flags);
   1.717 +
   1.718 +        // Synthesizes cancelation events for the current state and resets the tracked state.
   1.719 +        void synthesizeCancelationEvents(nsecs_t currentTime,
   1.720 +                Vector<EventEntry*>& outEvents, const CancelationOptions& options);
   1.721 +
   1.722 +        // Clears the current state.
   1.723 +        void clear();
   1.724 +
   1.725 +        // Copies pointer-related parts of the input state to another instance.
   1.726 +        void copyPointerStateTo(InputState& other) const;
   1.727 +
   1.728 +        // Gets the fallback key associated with a keycode.
   1.729 +        // Returns -1 if none.
   1.730 +        // Returns AKEYCODE_UNKNOWN if we are only dispatching the unhandled key to the policy.
   1.731 +        int32_t getFallbackKey(int32_t originalKeyCode);
   1.732 +
   1.733 +        // Sets the fallback key for a particular keycode.
   1.734 +        void setFallbackKey(int32_t originalKeyCode, int32_t fallbackKeyCode);
   1.735 +
   1.736 +        // Removes the fallback key for a particular keycode.
   1.737 +        void removeFallbackKey(int32_t originalKeyCode);
   1.738 +
   1.739 +        inline const KeyedVector<int32_t, int32_t>& getFallbackKeys() const {
   1.740 +            return mFallbackKeys;
   1.741 +        }
   1.742 +
   1.743 +    private:
   1.744 +        struct KeyMemento {
   1.745 +            int32_t deviceId;
   1.746 +            uint32_t source;
   1.747 +            int32_t keyCode;
   1.748 +            int32_t scanCode;
   1.749 +            int32_t metaState;
   1.750 +            int32_t flags;
   1.751 +            nsecs_t downTime;
   1.752 +            uint32_t policyFlags;
   1.753 +        };
   1.754 +
   1.755 +        struct MotionMemento {
   1.756 +            int32_t deviceId;
   1.757 +            uint32_t source;
   1.758 +            int32_t flags;
   1.759 +            float xPrecision;
   1.760 +            float yPrecision;
   1.761 +            nsecs_t downTime;
   1.762 +            int32_t displayId;
   1.763 +            uint32_t pointerCount;
   1.764 +            PointerProperties pointerProperties[MAX_POINTERS];
   1.765 +            PointerCoords pointerCoords[MAX_POINTERS];
   1.766 +            bool hovering;
   1.767 +            uint32_t policyFlags;
   1.768 +
   1.769 +            void setPointers(const MotionEntry* entry);
   1.770 +        };
   1.771 +
   1.772 +        Vector<KeyMemento> mKeyMementos;
   1.773 +        Vector<MotionMemento> mMotionMementos;
   1.774 +        KeyedVector<int32_t, int32_t> mFallbackKeys;
   1.775 +
   1.776 +        ssize_t findKeyMemento(const KeyEntry* entry) const;
   1.777 +        ssize_t findMotionMemento(const MotionEntry* entry, bool hovering) const;
   1.778 +
   1.779 +        void addKeyMemento(const KeyEntry* entry, int32_t flags);
   1.780 +        void addMotionMemento(const MotionEntry* entry, int32_t flags, bool hovering);
   1.781 +
   1.782 +        static bool shouldCancelKey(const KeyMemento& memento,
   1.783 +                const CancelationOptions& options);
   1.784 +        static bool shouldCancelMotion(const MotionMemento& memento,
   1.785 +                const CancelationOptions& options);
   1.786 +    };
   1.787 +
   1.788 +    /* Manages the dispatch state associated with a single input channel. */
   1.789 +    class Connection : public RefBase {
   1.790 +    protected:
   1.791 +        virtual ~Connection();
   1.792 +
   1.793 +    public:
   1.794 +        enum Status {
   1.795 +            // Everything is peachy.
   1.796 +            STATUS_NORMAL,
   1.797 +            // An unrecoverable communication error has occurred.
   1.798 +            STATUS_BROKEN,
   1.799 +            // The input channel has been unregistered.
   1.800 +            STATUS_ZOMBIE
   1.801 +        };
   1.802 +
   1.803 +        Status status;
   1.804 +        sp<InputChannel> inputChannel; // never null
   1.805 +        sp<InputWindowHandle> inputWindowHandle; // may be null
   1.806 +        bool monitor;
   1.807 +        InputPublisher inputPublisher;
   1.808 +        InputState inputState;
   1.809 +
   1.810 +        // True if the socket is full and no further events can be published until
   1.811 +        // the application consumes some of the input.
   1.812 +        bool inputPublisherBlocked;
   1.813 +
   1.814 +        // Queue of events that need to be published to the connection.
   1.815 +        Queue<DispatchEntry> outboundQueue;
   1.816 +
   1.817 +        // Queue of events that have been published to the connection but that have not
   1.818 +        // yet received a "finished" response from the application.
   1.819 +        Queue<DispatchEntry> waitQueue;
   1.820 +
   1.821 +        explicit Connection(const sp<InputChannel>& inputChannel,
   1.822 +                const sp<InputWindowHandle>& inputWindowHandle, bool monitor);
   1.823 +
   1.824 +        inline const char* getInputChannelName() const { return inputChannel->getName().string(); }
   1.825 +
   1.826 +        const char* getWindowName() const;
   1.827 +        const char* getStatusLabel() const;
   1.828 +
   1.829 +        DispatchEntry* findWaitQueueEntry(uint32_t seq);
   1.830 +    };
   1.831 +
   1.832 +    enum DropReason {
   1.833 +        DROP_REASON_NOT_DROPPED = 0,
   1.834 +        DROP_REASON_POLICY = 1,
   1.835 +        DROP_REASON_APP_SWITCH = 2,
   1.836 +        DROP_REASON_DISABLED = 3,
   1.837 +        DROP_REASON_BLOCKED = 4,
   1.838 +        DROP_REASON_STALE = 5,
   1.839 +    };
   1.840 +
   1.841 +    sp<InputDispatcherPolicyInterface> mPolicy;
   1.842 +    InputDispatcherConfiguration mConfig;
   1.843 +
   1.844 +    Mutex mLock;
   1.845 +
   1.846 +    Condition mDispatcherIsAliveCondition;
   1.847 +
   1.848 +    sp<Looper> mLooper;
   1.849 +
   1.850 +    EventEntry* mPendingEvent;
   1.851 +    Queue<EventEntry> mInboundQueue;
   1.852 +    Queue<CommandEntry> mCommandQueue;
   1.853 +
   1.854 +    void dispatchOnceInnerLocked(nsecs_t* nextWakeupTime);
   1.855 +
   1.856 +    // Enqueues an inbound event.  Returns true if mLooper->wake() should be called.
   1.857 +    bool enqueueInboundEventLocked(EventEntry* entry);
   1.858 +
   1.859 +    // Cleans up input state when dropping an inbound event.
   1.860 +    void dropInboundEventLocked(EventEntry* entry, DropReason dropReason);
   1.861 +
   1.862 +    // App switch latency optimization.
   1.863 +    bool mAppSwitchSawKeyDown;
   1.864 +    nsecs_t mAppSwitchDueTime;
   1.865 +
   1.866 +    static bool isAppSwitchKeyCode(int32_t keyCode);
   1.867 +    bool isAppSwitchKeyEventLocked(KeyEntry* keyEntry);
   1.868 +    bool isAppSwitchPendingLocked();
   1.869 +    void resetPendingAppSwitchLocked(bool handled);
   1.870 +
   1.871 +    // Stale event latency optimization.
   1.872 +    static bool isStaleEventLocked(nsecs_t currentTime, EventEntry* entry);
   1.873 +
   1.874 +    // Blocked event latency optimization.  Drops old events when the user intends
   1.875 +    // to transfer focus to a new application.
   1.876 +    EventEntry* mNextUnblockedEvent;
   1.877 +
   1.878 +    sp<InputWindowHandle> findTouchedWindowAtLocked(int32_t displayId, int32_t x, int32_t y);
   1.879 +
   1.880 +    // All registered connections mapped by channel file descriptor.
   1.881 +    KeyedVector<int, sp<Connection> > mConnectionsByFd;
   1.882 +
   1.883 +    ssize_t getConnectionIndexLocked(const sp<InputChannel>& inputChannel);
   1.884 +
   1.885 +    // Input channels that will receive a copy of all input events.
   1.886 +    Vector<sp<InputChannel> > mMonitoringChannels;
   1.887 +
   1.888 +    // Event injection and synchronization.
   1.889 +    Condition mInjectionResultAvailableCondition;
   1.890 +    bool hasInjectionPermission(int32_t injectorPid, int32_t injectorUid);
   1.891 +    void setInjectionResultLocked(EventEntry* entry, int32_t injectionResult);
   1.892 +
   1.893 +    Condition mInjectionSyncFinishedCondition;
   1.894 +    void incrementPendingForegroundDispatchesLocked(EventEntry* entry);
   1.895 +    void decrementPendingForegroundDispatchesLocked(EventEntry* entry);
   1.896 +
   1.897 +    // Key repeat tracking.
   1.898 +    struct KeyRepeatState {
   1.899 +        KeyEntry* lastKeyEntry; // or null if no repeat
   1.900 +        nsecs_t nextRepeatTime;
   1.901 +    } mKeyRepeatState;
   1.902 +
   1.903 +    void resetKeyRepeatLocked();
   1.904 +    KeyEntry* synthesizeKeyRepeatLocked(nsecs_t currentTime);
   1.905 +
   1.906 +    // Deferred command processing.
   1.907 +    bool haveCommandsLocked() const;
   1.908 +    bool runCommandsLockedInterruptible();
   1.909 +    CommandEntry* postCommandLocked(Command command);
   1.910 +
   1.911 +    // Input filter processing.
   1.912 +    bool shouldSendKeyToInputFilterLocked(const NotifyKeyArgs* args);
   1.913 +    bool shouldSendMotionToInputFilterLocked(const NotifyMotionArgs* args);
   1.914 +
   1.915 +    // Inbound event processing.
   1.916 +    void drainInboundQueueLocked();
   1.917 +    void releasePendingEventLocked();
   1.918 +    void releaseInboundEventLocked(EventEntry* entry);
   1.919 +
   1.920 +    // Dispatch state.
   1.921 +    bool mDispatchEnabled;
   1.922 +    bool mDispatchFrozen;
   1.923 +    bool mInputFilterEnabled;
   1.924 +
   1.925 +    Vector<sp<InputWindowHandle> > mWindowHandles;
   1.926 +
   1.927 +    sp<InputWindowHandle> getWindowHandleLocked(const sp<InputChannel>& inputChannel) const;
   1.928 +    bool hasWindowHandleLocked(const sp<InputWindowHandle>& windowHandle) const;
   1.929 +
   1.930 +    // Focus tracking for keys, trackball, etc.
   1.931 +    sp<InputWindowHandle> mFocusedWindowHandle;
   1.932 +
   1.933 +    // Focus tracking for touch.
   1.934 +    struct TouchedWindow {
   1.935 +        sp<InputWindowHandle> windowHandle;
   1.936 +        int32_t targetFlags;
   1.937 +        BitSet32 pointerIds;        // zero unless target flag FLAG_SPLIT is set
   1.938 +    };
   1.939 +    struct TouchState {
   1.940 +        bool down;
   1.941 +        bool split;
   1.942 +        int32_t deviceId; // id of the device that is currently down, others are rejected
   1.943 +        uint32_t source;  // source of the device that is current down, others are rejected
   1.944 +        int32_t displayId; // id to the display that currently has a touch, others are rejected
   1.945 +        Vector<TouchedWindow> windows;
   1.946 +
   1.947 +        TouchState();
   1.948 +        ~TouchState();
   1.949 +        void reset();
   1.950 +        void copyFrom(const TouchState& other);
   1.951 +        void addOrUpdateWindow(const sp<InputWindowHandle>& windowHandle,
   1.952 +                int32_t targetFlags, BitSet32 pointerIds);
   1.953 +        void removeWindow(const sp<InputWindowHandle>& windowHandle);
   1.954 +        void filterNonAsIsTouchWindows();
   1.955 +        sp<InputWindowHandle> getFirstForegroundWindowHandle() const;
   1.956 +        bool isSlippery() const;
   1.957 +    };
   1.958 +
   1.959 +    TouchState mTouchState;
   1.960 +    TouchState mTempTouchState;
   1.961 +
   1.962 +    // Focused application.
   1.963 +    sp<InputApplicationHandle> mFocusedApplicationHandle;
   1.964 +
   1.965 +    // Dispatcher state at time of last ANR.
   1.966 +    String8 mLastANRState;
   1.967 +
   1.968 +    // Dispatch inbound events.
   1.969 +    bool dispatchConfigurationChangedLocked(
   1.970 +            nsecs_t currentTime, ConfigurationChangedEntry* entry);
   1.971 +    bool dispatchDeviceResetLocked(
   1.972 +            nsecs_t currentTime, DeviceResetEntry* entry);
   1.973 +    bool dispatchKeyLocked(
   1.974 +            nsecs_t currentTime, KeyEntry* entry,
   1.975 +            DropReason* dropReason, nsecs_t* nextWakeupTime);
   1.976 +    bool dispatchMotionLocked(
   1.977 +            nsecs_t currentTime, MotionEntry* entry,
   1.978 +            DropReason* dropReason, nsecs_t* nextWakeupTime);
   1.979 +    void dispatchEventLocked(nsecs_t currentTime, EventEntry* entry,
   1.980 +            const Vector<InputTarget>& inputTargets);
   1.981 +
   1.982 +    void logOutboundKeyDetailsLocked(const char* prefix, const KeyEntry* entry);
   1.983 +    void logOutboundMotionDetailsLocked(const char* prefix, const MotionEntry* entry);
   1.984 +
   1.985 +    // Keeping track of ANR timeouts.
   1.986 +    enum InputTargetWaitCause {
   1.987 +        INPUT_TARGET_WAIT_CAUSE_NONE,
   1.988 +        INPUT_TARGET_WAIT_CAUSE_SYSTEM_NOT_READY,
   1.989 +        INPUT_TARGET_WAIT_CAUSE_APPLICATION_NOT_READY,
   1.990 +    };
   1.991 +
   1.992 +    InputTargetWaitCause mInputTargetWaitCause;
   1.993 +    nsecs_t mInputTargetWaitStartTime;
   1.994 +    nsecs_t mInputTargetWaitTimeoutTime;
   1.995 +    bool mInputTargetWaitTimeoutExpired;
   1.996 +    sp<InputApplicationHandle> mInputTargetWaitApplicationHandle;
   1.997 +
   1.998 +    // Contains the last window which received a hover event.
   1.999 +    sp<InputWindowHandle> mLastHoverWindowHandle;
  1.1000 +
  1.1001 +    // Finding targets for input events.
  1.1002 +    int32_t handleTargetsNotReadyLocked(nsecs_t currentTime, const EventEntry* entry,
  1.1003 +            const sp<InputApplicationHandle>& applicationHandle,
  1.1004 +            const sp<InputWindowHandle>& windowHandle,
  1.1005 +            nsecs_t* nextWakeupTime, const char* reason);
  1.1006 +    void resumeAfterTargetsNotReadyTimeoutLocked(nsecs_t newTimeout,
  1.1007 +            const sp<InputChannel>& inputChannel);
  1.1008 +    nsecs_t getTimeSpentWaitingForApplicationLocked(nsecs_t currentTime);
  1.1009 +    void resetANRTimeoutsLocked();
  1.1010 +
  1.1011 +    int32_t findFocusedWindowTargetsLocked(nsecs_t currentTime, const EventEntry* entry,
  1.1012 +            Vector<InputTarget>& inputTargets, nsecs_t* nextWakeupTime);
  1.1013 +    int32_t findTouchedWindowTargetsLocked(nsecs_t currentTime, const MotionEntry* entry,
  1.1014 +            Vector<InputTarget>& inputTargets, nsecs_t* nextWakeupTime,
  1.1015 +            bool* outConflictingPointerActions);
  1.1016 +
  1.1017 +    void addWindowTargetLocked(const sp<InputWindowHandle>& windowHandle,
  1.1018 +            int32_t targetFlags, BitSet32 pointerIds, Vector<InputTarget>& inputTargets);
  1.1019 +    void addMonitoringTargetsLocked(Vector<InputTarget>& inputTargets);
  1.1020 +
  1.1021 +    void pokeUserActivityLocked(const EventEntry* eventEntry);
  1.1022 +    bool checkInjectionPermission(const sp<InputWindowHandle>& windowHandle,
  1.1023 +            const InjectionState* injectionState);
  1.1024 +    bool isWindowObscuredAtPointLocked(const sp<InputWindowHandle>& windowHandle,
  1.1025 +            int32_t x, int32_t y) const;
  1.1026 +    bool isWindowReadyForMoreInputLocked(nsecs_t currentTime,
  1.1027 +            const sp<InputWindowHandle>& windowHandle, const EventEntry* eventEntry);
  1.1028 +    String8 getApplicationWindowLabelLocked(const sp<InputApplicationHandle>& applicationHandle,
  1.1029 +            const sp<InputWindowHandle>& windowHandle);
  1.1030 +
  1.1031 +    // Manage the dispatch cycle for a single connection.
  1.1032 +    // These methods are deliberately not Interruptible because doing all of the work
  1.1033 +    // with the mutex held makes it easier to ensure that connection invariants are maintained.
  1.1034 +    // If needed, the methods post commands to run later once the critical bits are done.
  1.1035 +    void prepareDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection,
  1.1036 +            EventEntry* eventEntry, const InputTarget* inputTarget);
  1.1037 +    void enqueueDispatchEntriesLocked(nsecs_t currentTime, const sp<Connection>& connection,
  1.1038 +            EventEntry* eventEntry, const InputTarget* inputTarget);
  1.1039 +    void enqueueDispatchEntryLocked(const sp<Connection>& connection,
  1.1040 +            EventEntry* eventEntry, const InputTarget* inputTarget, int32_t dispatchMode);
  1.1041 +    void startDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection);
  1.1042 +    void finishDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection,
  1.1043 +            uint32_t seq, bool handled);
  1.1044 +    void abortBrokenDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection,
  1.1045 +            bool notify);
  1.1046 +    void drainDispatchQueueLocked(Queue<DispatchEntry>* queue);
  1.1047 +    void releaseDispatchEntryLocked(DispatchEntry* dispatchEntry);
  1.1048 +    static int handleReceiveCallback(int fd, int events, void* data);
  1.1049 +
  1.1050 +    void synthesizeCancelationEventsForAllConnectionsLocked(
  1.1051 +            const CancelationOptions& options);
  1.1052 +    void synthesizeCancelationEventsForInputChannelLocked(const sp<InputChannel>& channel,
  1.1053 +            const CancelationOptions& options);
  1.1054 +    void synthesizeCancelationEventsForConnectionLocked(const sp<Connection>& connection,
  1.1055 +            const CancelationOptions& options);
  1.1056 +
  1.1057 +    // Splitting motion events across windows.
  1.1058 +    MotionEntry* splitMotionEvent(const MotionEntry* originalMotionEntry, BitSet32 pointerIds);
  1.1059 +
  1.1060 +    // Reset and drop everything the dispatcher is doing.
  1.1061 +    void resetAndDropEverythingLocked(const char* reason);
  1.1062 +
  1.1063 +    // Dump state.
  1.1064 +    void dumpDispatchStateLocked(String8& dump);
  1.1065 +    void logDispatchStateLocked();
  1.1066 +
  1.1067 +    // Registration.
  1.1068 +    void removeMonitorChannelLocked(const sp<InputChannel>& inputChannel);
  1.1069 +    status_t unregisterInputChannelLocked(const sp<InputChannel>& inputChannel, bool notify);
  1.1070 +
  1.1071 +    // Add or remove a connection to the mActiveConnections vector.
  1.1072 +    void activateConnectionLocked(Connection* connection);
  1.1073 +    void deactivateConnectionLocked(Connection* connection);
  1.1074 +
  1.1075 +    // Interesting events that we might like to log or tell the framework about.
  1.1076 +    void onDispatchCycleFinishedLocked(
  1.1077 +            nsecs_t currentTime, const sp<Connection>& connection, uint32_t seq, bool handled);
  1.1078 +    void onDispatchCycleBrokenLocked(
  1.1079 +            nsecs_t currentTime, const sp<Connection>& connection);
  1.1080 +    void onANRLocked(
  1.1081 +            nsecs_t currentTime, const sp<InputApplicationHandle>& applicationHandle,
  1.1082 +            const sp<InputWindowHandle>& windowHandle,
  1.1083 +            nsecs_t eventTime, nsecs_t waitStartTime, const char* reason);
  1.1084 +
  1.1085 +    // Outbound policy interactions.
  1.1086 +    void doNotifyConfigurationChangedInterruptible(CommandEntry* commandEntry);
  1.1087 +    void doNotifyInputChannelBrokenLockedInterruptible(CommandEntry* commandEntry);
  1.1088 +    void doNotifyANRLockedInterruptible(CommandEntry* commandEntry);
  1.1089 +    void doInterceptKeyBeforeDispatchingLockedInterruptible(CommandEntry* commandEntry);
  1.1090 +    void doDispatchCycleFinishedLockedInterruptible(CommandEntry* commandEntry);
  1.1091 +    bool afterKeyEventLockedInterruptible(const sp<Connection>& connection,
  1.1092 +            DispatchEntry* dispatchEntry, KeyEntry* keyEntry, bool handled);
  1.1093 +    bool afterMotionEventLockedInterruptible(const sp<Connection>& connection,
  1.1094 +            DispatchEntry* dispatchEntry, MotionEntry* motionEntry, bool handled);
  1.1095 +    void doPokeUserActivityLockedInterruptible(CommandEntry* commandEntry);
  1.1096 +    void initializeKeyEvent(KeyEvent* event, const KeyEntry* entry);
  1.1097 +
  1.1098 +    // Statistics gathering.
  1.1099 +    void updateDispatchStatisticsLocked(nsecs_t currentTime, const EventEntry* entry,
  1.1100 +            int32_t injectionResult, nsecs_t timeSpentWaitingForApplication);
  1.1101 +    void traceInboundQueueLengthLocked();
  1.1102 +    void traceOutboundQueueLengthLocked(const sp<Connection>& connection);
  1.1103 +    void traceWaitQueueLengthLocked(const sp<Connection>& connection);
  1.1104 +};
  1.1105 +
  1.1106 +/* Enqueues and dispatches input events, endlessly. */
  1.1107 +class InputDispatcherThread : public Thread {
  1.1108 +public:
  1.1109 +    explicit InputDispatcherThread(const sp<InputDispatcherInterface>& dispatcher);
  1.1110 +    ~InputDispatcherThread();
  1.1111 +
  1.1112 +private:
  1.1113 +    virtual bool threadLoop();
  1.1114 +
  1.1115 +    sp<InputDispatcherInterface> mDispatcher;
  1.1116 +};
  1.1117 +
  1.1118 +} // namespace android
  1.1119 +
  1.1120 +#endif // _UI_INPUT_DISPATCHER_H

mercurial