summaryrefslogtreecommitdiff
path: root/src/player/AppleTrackpadInputDevice.cpp
blob: 63059ae01ba71826c92019fa078454730230c8c1 (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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
//
//  libavg - Media Playback Engine. 
//  Copyright (C) 2003-2014 Ulrich von Zadow
//
//  This library is free software; you can redistribute it and/or
//  modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation; either
//  version 2 of the License, or (at your option) any later version.
//
//  This library is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with this library; if not, write to the Free Software
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
//  Current versions can be found at www.libavg.de
//

#include "AppleTrackpadInputDevice.h"
#include "TouchEvent.h"
#include "Player.h"
#include "AVGNode.h"
#include "TouchStatus.h"

#include "../base/Logger.h"
#include "../base/ObjectCounter.h"
#include "../base/Exception.h"

#include <CoreFoundation/CoreFoundation.h>

using namespace std;

namespace avg {

AppleTrackpadInputDevice* AppleTrackpadInputDevice::s_pInstance(0);

AppleTrackpadInputDevice::AppleTrackpadInputDevice()
    : m_LastID(0)
{
    s_pInstance = this;
}

AppleTrackpadInputDevice::~AppleTrackpadInputDevice()
{
    MTDeviceStop(m_Device);
    MTUnregisterContactFrameCallback(m_Device, callback);
    MTDeviceRelease(m_Device);
    s_pInstance = 0;
}

void AppleTrackpadInputDevice::start()
{
    MultitouchInputDevice::start();
    m_Device = MTDeviceCreateDefault();
    MTRegisterContactFrameCallback(m_Device, callback);
    MTDeviceStart(m_Device, 0);
    AVG_TRACE(Logger::category::CONFIG, Logger::severity::INFO,
            "Apple Trackpad Multitouch event source created.");
}

void AppleTrackpadInputDevice::onData(int device, Finger* pFingers, int numFingers, 
        float timestamp, int frame)
{
    lock_guard lock(getMutex());
    for (int i = 0; i < numFingers; i++) {
        Finger* pFinger = &pFingers[i];
        TouchStatusPtr pTouchStatus = getTouchStatus(pFinger->identifier);
        if (!pTouchStatus) {
            m_LastID++;
            TouchEventPtr pEvent = createEvent(m_LastID, pFinger, Event::CURSOR_DOWN);
            addTouchStatus(pFinger->identifier, pEvent);
        } else {
            Event::Type eventType;
            if (pFinger->state == 7) {
                eventType = Event::CURSOR_UP;
                removeTouchStatus(pFinger->identifier);
            } else {
                eventType = Event::CURSOR_MOTION;
            }
            TouchEventPtr pEvent = createEvent(0, pFinger, eventType);
            pTouchStatus->pushEvent(pEvent);
        }
    }
}

int AppleTrackpadInputDevice::callback(int device, Finger *data, int nFingers, 
        double timestamp, int frame) 
{
    AVG_ASSERT(s_pInstance != 0);
    s_pInstance->onData(device, data, nFingers, timestamp, frame);
    return 0;
}

TouchEventPtr AppleTrackpadInputDevice::createEvent(int avgID, Finger* pFinger, 
        Event::Type eventType)
{
    glm::vec2 size = getTouchArea();
    IntPoint pos = getScreenPos(glm::vec2(pFinger->normalized.pos.x,
            1-pFinger->normalized.pos.y));
    glm::vec2 speed(pFinger->normalized.vel.x*size.x, pFinger->normalized.vel.y*size.y);
    float eccentricity = pFinger->majorAxis/pFinger->minorAxis;
    glm::vec2 majorAxis = fromPolar(pFinger->angle, pFinger->majorAxis);
    majorAxis.y = -majorAxis.y;
    glm::vec2 minorAxis = fromPolar(pFinger->angle+1.57, pFinger->minorAxis);
    minorAxis.y = -minorAxis.y;

    TouchEventPtr pEvent(new TouchEvent(avgID, eventType, pos, Event::TOUCH,
                speed, pFinger->angle, pFinger->size, eccentricity, majorAxis, 
                minorAxis)); 
    return pEvent;
}

}