summaryrefslogtreecommitdiffstats
path: root/remoting/android/java/src/org/chromium/chromoting/TrackpadInputStrategy.java
blob: edc9bafc93f13d4b2e7cc343b541232be456f8f0 (plain)
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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
79
80
81
82
83
84
85
86
87
88
89
90
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package org.chromium.chromoting;

import org.chromium.chromoting.jni.JniInterface;

/**
 * Defines a set of behavior and methods to simulate trackpad behavior when responding to
 * local input event data.  This class is also responsible for forwarding input event data
 * to the remote host for injection there.
 */
public class TrackpadInputStrategy implements InputStrategyInterface {
    private final RenderData mRenderData;
    private final DesktopViewInterface mViewer;

    public TrackpadInputStrategy(DesktopViewInterface viewer, RenderData renderData) {
        mViewer = viewer;
        mRenderData = renderData;
    }

    @Override
    public void injectRemoteMoveEvent(int x, int y) {
        injectRemoteButtonEvent(x, y, TouchInputHandlerInterface.BUTTON_UNDEFINED, false);
    }

    @Override
    public void injectRemoteButtonEvent(int button, boolean pressed) {
        int x;
        int y;
        synchronized (mRenderData) {
            x = mRenderData.cursorPosition.x;
            y = mRenderData.cursorPosition.y;
        }

        injectRemoteButtonEvent(x, y, button, pressed);
    }

    @Override
    public void injectRemoteScrollEvent(int deltaX, int deltaY) {
        JniInterface.sendMouseWheelEvent(deltaX, deltaY);
    }

    @Override
    public DesktopView.InputFeedbackType getShortPressFeedbackType() {
        return DesktopView.InputFeedbackType.NONE;
    }

    @Override
    public DesktopView.InputFeedbackType getLongPressFeedbackType() {
        return DesktopView.InputFeedbackType.SMALL_ANIMATION;
    }

    @Override
    public boolean centerCursorInView() {
        return true;
    }

    @Override
    public boolean invertCursorMovement() {
        return true;
    }

    private void injectRemoteButtonEvent(int x, int y, int button, boolean pressed) {
        boolean cursorMoved = false;
        synchronized (mRenderData) {
            // Test if the cursor actually moved, which requires repainting the cursor. This
            // requires that |mRenderData.cursorPosition| was not assigned to beforehand.
            if (x != mRenderData.cursorPosition.x) {
                mRenderData.cursorPosition.x = x;
                cursorMoved = true;
            }
            if (y != mRenderData.cursorPosition.y) {
                mRenderData.cursorPosition.y = y;
                cursorMoved = true;
            }
        }

        if (button == TouchInputHandlerInterface.BUTTON_UNDEFINED && !cursorMoved) {
            // No need to inject anything or repaint.
            return;
        }

        JniInterface.sendMouseEvent(x, y, button, pressed);
        if (cursorMoved) {
            mViewer.transformationChanged();
        }
    }
}