~ubuntu-branches/ubuntu/trusty/libavg/trusty-proposed

« back to all changes in this revision

Viewing changes to src/player/EventStream.cpp

  • Committer: Package Import Robot
  • Author(s): OXullo Intersecans
  • Date: 2011-12-06 22:44:56 UTC
  • mfrom: (1.1.6)
  • Revision ID: package-import@ubuntu.com-20111206224456-qc7250z3ya1vi8s9
Tags: 1.7.0-0ubuntu1
* New upstream release (LP: #899183)
* Remove patches 0002-libav-0.7.patch, 0003-fglrx-segfault-on-startup.patch
  now merged to upstream
* Remove unnecessary .la files
* Update debian/watch file
* Fix debian/copyright dep-5 compliancy
* Update standards to version 3.9.2
* Add man pages for avg_checktouch, avg_checkvsync, avg_showsvg
* Minor debian/rules enhancement
* Add librsvg2-dev, libgdk-pixbuf2.0-dev to Build-Depends
* Proper transition to dh_python2

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
//
2
 
//  libavg - Media Playback Engine. 
3
 
//  Copyright (C) 2003-2008 Ulrich von Zadow
4
 
//
5
 
//  This library is free software; you can redistribute it and/or
6
 
//  modify it under the terms of the GNU Lesser General Public
7
 
//  License as published by the Free Software Foundation; either
8
 
//  version 2 of the License, or (at your option) any later version.
9
 
//
10
 
//  This library is distributed in the hope that it will be useful,
11
 
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
 
//  Lesser General Public License for more details.
14
 
//
15
 
//  You should have received a copy of the GNU Lesser General Public
16
 
//  License along with this library; if not, write to the Free Software
17
 
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
 
//
19
 
//  Current versions can be found at www.libavg.de
20
 
//
21
 
 
22
 
#include "EventStream.h"
23
 
#include "TouchEvent.h"
24
 
 
25
 
#include "../base/Logger.h"
26
 
#include "../base/ObjectCounter.h"
27
 
#include "../base/Exception.h"
28
 
 
29
 
#include <math.h>
30
 
 
31
 
const int MAXMISSINGFRAMES=1;
32
 
 
33
 
using namespace std;
34
 
 
35
 
namespace avg {
36
 
    
37
 
    int EventStream::s_LastLabel = 0;
38
 
 
39
 
    EventStream::EventStream(BlobPtr pFirstBlob, long long time)
40
 
        : m_Time(time)
41
 
    {
42
 
        ObjectCounter::get()->incRef(&typeid(*this));
43
 
        m_ID = ++s_LastLabel;
44
 
        m_pBlob = pFirstBlob;
45
 
        m_Pos = m_pBlob->getCenter();
46
 
        m_OldPos = m_Pos;
47
 
        m_FirstPos = m_Pos;
48
 
        m_State = DOWN_PENDING;
49
 
        m_Stale = false;
50
 
        m_OldTime = 0;
51
 
        m_VanishCounter = 0;
52
 
    }
53
 
 
54
 
    EventStream::~EventStream()
55
 
    {
56
 
        ObjectCounter::get()->decRef(&typeid(*this));
57
 
    }
58
 
 
59
 
    void EventStream::blobChanged(BlobPtr pNewBlob, long long time, bool bEventOnMove)
60
 
    {
61
 
        AVG_ASSERT(m_pBlob);
62
 
        AVG_ASSERT(pNewBlob);
63
 
        m_VanishCounter = 0;
64
 
        DPoint c = pNewBlob->getCenter();
65
 
        bool bPosChanged;
66
 
        if (bEventOnMove) {
67
 
            bPosChanged = (calcDist(c, m_Pos) > 1);
68
 
        } else {
69
 
            bPosChanged = true;
70
 
        }
71
 
        switch (m_State) {
72
 
            case DOWN_PENDING:
73
 
                //finger touch has not been polled yet. update position
74
 
                break;
75
 
            case VANISHED:
76
 
                m_State = MOTION_PENDING;
77
 
                break;
78
 
            case DOWN_DELIVERED:
79
 
                //fingerdown delivered, change to motion states
80
 
                if (bPosChanged)
81
 
                    m_State = MOTION_PENDING;
82
 
                else
83
 
                    m_State = MOTION_DELIVERED;
84
 
                break;
85
 
            case MOTION_PENDING:
86
 
                break;
87
 
            case MOTION_DELIVERED:
88
 
                if (bPosChanged) {
89
 
                    m_State = MOTION_PENDING;
90
 
                }
91
 
                break;
92
 
            default:
93
 
                //pass
94
 
                break;
95
 
        }
96
 
        if (bPosChanged) {
97
 
            m_OldTime = m_Time;
98
 
            m_Time = time;
99
 
            m_OldPos = m_Pos;
100
 
            m_Pos = c;
101
 
        }
102
 
        m_pBlob = pNewBlob;
103
 
        m_Stale = false;
104
 
    };
105
 
        
106
 
    void EventStream::blobGone()
107
 
    {
108
 
        switch (m_State) {
109
 
            case DOWN_PENDING:
110
 
                m_State = UP_DELIVERED;
111
 
                break;
112
 
            case UP_PENDING:
113
 
            case UP_DELIVERED:
114
 
                break;
115
 
            default:
116
 
                m_State = VANISHED;
117
 
                m_VanishCounter++;
118
 
                if (m_VanishCounter >= MAXMISSINGFRAMES) {
119
 
                    m_State = UP_PENDING;
120
 
                }
121
 
                break;
122
 
        }
123
 
    }
124
 
 
125
 
    EventPtr EventStream::pollevent(DeDistortPtr pDeDistort, const DRect& displayROI, 
126
 
            CursorEvent::Source source, bool bEventOnMove)
127
 
    {
128
 
        AVG_ASSERT(m_pBlob);
129
 
        DPoint BlobOffset = pDeDistort->getActiveBlobArea(displayROI).tl;
130
 
        DPoint pt = m_pBlob->getCenter()+BlobOffset;
131
 
        DPoint screenpos = pDeDistort->transformBlobToScreen(pt);
132
 
        IntPoint pos(int(screenpos.x+0.5), int(screenpos.y+0.5)); 
133
 
        DPoint oldPos = pDeDistort->transformBlobToScreen(m_OldPos + BlobOffset);
134
 
        DPoint newPos = pDeDistort->transformBlobToScreen(m_Pos + BlobOffset);
135
 
        DPoint speed = getSpeed(oldPos, newPos);
136
 
        DPoint firstDoubleScreenPos = pDeDistort->transformBlobToScreen(
137
 
                m_FirstPos+BlobOffset);
138
 
        IntPoint firstScreenPos(int(firstDoubleScreenPos.x+0.5), 
139
 
                int(firstDoubleScreenPos.y+0.5));
140
 
        switch (m_State) {
141
 
            case DOWN_PENDING:
142
 
                m_State = DOWN_DELIVERED;
143
 
                return EventPtr(new TouchEvent(m_ID, Event::CURSORDOWN,
144
 
                        m_pBlob, pos, source, speed, firstScreenPos));
145
 
            case MOTION_PENDING:
146
 
                m_State = MOTION_DELIVERED;
147
 
                return EventPtr(new TouchEvent(m_ID, Event::CURSORMOTION,
148
 
                        m_pBlob, pos, source, speed, firstScreenPos));
149
 
            case UP_PENDING:
150
 
                m_State = UP_DELIVERED;
151
 
                return EventPtr(new TouchEvent(m_ID, Event::CURSORUP,
152
 
                        m_pBlob, pos, source, speed, firstScreenPos));
153
 
            case DOWN_DELIVERED:
154
 
            case MOTION_DELIVERED:
155
 
                if (!bEventOnMove) {
156
 
                    return EventPtr(new TouchEvent(m_ID, Event::CURSORMOTION,
157
 
                            m_pBlob, pos, source, speed, firstScreenPos));
158
 
                } else {
159
 
                    return EventPtr();
160
 
                }
161
 
            case UP_DELIVERED:
162
 
            default:
163
 
                //return no event
164
 
                return EventPtr();
165
 
        }
166
 
    };
167
 
 
168
 
    bool EventStream::isGone()
169
 
    {
170
 
        return (m_State == UP_DELIVERED);
171
 
    }
172
 
 
173
 
    void EventStream::setStale() {
174
 
        m_Stale = true;
175
 
    }
176
 
 
177
 
    bool EventStream::isStale() {
178
 
        return m_Stale;
179
 
    }
180
 
    
181
 
    string EventStream::stateToString(StreamState State) 
182
 
    {
183
 
        switch(State) {
184
 
            case DOWN_PENDING:
185
 
                return "DOWN_PENDING";
186
 
            case DOWN_DELIVERED:
187
 
                return "DOWN_DELIVERED";
188
 
            case MOTION_PENDING:
189
 
                return "MOTION_PENDING";
190
 
            case MOTION_DELIVERED:
191
 
                return "MOTION_DELIVERED";
192
 
            case VANISHED:
193
 
                return "VANISHED";
194
 
            case UP_PENDING:
195
 
                return "UP_PENDING";
196
 
            case UP_DELIVERED:
197
 
                return "UP_DELIVERED";
198
 
            default:
199
 
                return "Broken state";
200
 
        }
201
 
    }
202
 
 
203
 
    void EventStream::dump() 
204
 
    {
205
 
        cerr << "  " << m_ID << ": " << stateToString(m_State) << ", stale: " 
206
 
                << m_Stale << endl;
207
 
        if (m_State == VANISHED) {
208
 
            cerr << "    VanishCounter: " << m_VanishCounter << endl;
209
 
        }
210
 
    }
211
 
 
212
 
    DPoint EventStream::getSpeed (const DPoint& oldPos, const DPoint& newPos)
213
 
    {
214
 
        if (m_OldTime==0 || m_Time == m_OldTime) {
215
 
            return DPoint(0,0);
216
 
        } else {
217
 
            double timeDiff = double(m_OldTime-m_Time);
218
 
            return (oldPos-newPos)/timeDiff;
219
 
        }
220
 
    }
221
 
 
222
 
}