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

« back to all changes in this revision

Viewing changes to src/player/LibMTDevEventSource.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 "LibMTDevEventSource.h"
23
 
 
24
 
#include "TouchEvent.h"
25
 
#include "Player.h"
26
 
#include "AVGNode.h"
27
 
#include "TouchStatus.h"
28
 
 
29
 
#include "../base/Logger.h"
30
 
#include "../base/Point.h"
31
 
#include "../base/ObjectCounter.h"
32
 
#include "../base/Exception.h"
33
 
#include "../base/OSHelper.h"
34
 
 
35
 
#include <linux/input.h>
36
 
#include <fcntl.h>
37
 
#include <sys/types.h>
38
 
#include <sys/stat.h>
39
 
#include <set>
40
 
 
41
 
extern "C" {
42
 
#include <mtdev.h>
43
 
#include <mtdev-mapping.h>
44
 
}
45
 
 
46
 
using namespace std;
47
 
 
48
 
namespace avg {
49
 
 
50
 
LibMTDevEventSource::LibMTDevEventSource()
51
 
    : m_LastID(0),
52
 
      m_pMTDevice(0)
53
 
{
54
 
}
55
 
 
56
 
LibMTDevEventSource::~LibMTDevEventSource()
57
 
{
58
 
    if (m_pMTDevice) {
59
 
        mtdev_close(m_pMTDevice);
60
 
        delete m_pMTDevice;
61
 
    }
62
 
}
63
 
 
64
 
void LibMTDevEventSource::start()
65
 
66
 
    string sDevice("/dev/input/event3");
67
 
    getEnv("AVG_LINUX_MULTITOUCH_DEVICE", sDevice);
68
 
    m_DeviceFD = ::open(sDevice.c_str(), O_RDONLY | O_NONBLOCK);
69
 
    if (m_DeviceFD == -1) {
70
 
        throw Exception(AVG_ERR_MT_INIT, 
71
 
                string("Linux multitouch event source: Could not open device file '")+
72
 
                sDevice+"'. "+strerror(errno)+".");
73
 
    }
74
 
    m_pMTDevice = new mtdev;
75
 
    int err = mtdev_open(m_pMTDevice, m_DeviceFD);
76
 
    if (err == -1) {
77
 
        throw Exception(AVG_ERR_MT_INIT, 
78
 
                string("Linux multitouch event source: Could not open mtdev '")+
79
 
                sDevice+"'. "+strerror(errno)+".");
80
 
    }
81
 
    input_absinfo* pAbsInfo;
82
 
    pAbsInfo = &(m_pMTDevice->caps.abs[MTDEV_POSITION_X]);
83
 
    m_Dimensions.tl.x = pAbsInfo->minimum;
84
 
    m_Dimensions.br.x = pAbsInfo->maximum;
85
 
    pAbsInfo = &(m_pMTDevice->caps.abs[MTDEV_POSITION_Y]);
86
 
    m_Dimensions.tl.y = pAbsInfo->minimum;
87
 
    m_Dimensions.br.y = pAbsInfo->maximum;
88
 
    
89
 
    MultitouchEventSource::start();
90
 
    AVG_TRACE(Logger::CONFIG, "Linux MTDev Multitouch event source created.");
91
 
}
92
 
 
93
 
std::vector<EventPtr> LibMTDevEventSource::pollEvents()
94
 
{
95
 
    struct input_event events[64];
96
 
    int numEvents = mtdev_get(m_pMTDevice, m_DeviceFD, events, 64);
97
 
/*
98
 
    if (numEvents > 0) {
99
 
        cerr << "---- read ----" << endl;
100
 
    }
101
 
*/
102
 
    static int curSlot = 0;
103
 
 
104
 
    set<int> changedIDs;
105
 
    for (int i = 0; i < numEvents; ++i) {
106
 
        input_event event = events[i];
107
 
        if (event.type == EV_ABS && event.code == ABS_MT_SLOT) {
108
 
//            cerr << ">> slot " << event.value << endl;
109
 
            curSlot = event.value;
110
 
        } else {
111
 
            TouchData* pTouch;
112
 
            switch (event.code) {
113
 
                case ABS_MT_TRACKING_ID:
114
 
//                    cerr << ">> ABS_MT_TRACKING_ID: " << event.value << endl;
115
 
                    pTouch = &(m_Slots[curSlot]);
116
 
                    if (event.value == -1) {
117
 
                        TouchStatusPtr pTouchStatus = getTouchStatus(pTouch->id);
118
 
//                        cerr << "up " << pTouch->id << endl;
119
 
                        if (pTouchStatus) {
120
 
//                            cerr << "  --> remove" << endl;
121
 
                            TouchEventPtr pOldEvent = pTouchStatus->getLastEvent();
122
 
                            TouchEventPtr pUpEvent = 
123
 
                                    boost::dynamic_pointer_cast<TouchEvent>(
124
 
                                    pOldEvent->cloneAs(Event::CURSORUP));
125
 
                            pTouchStatus->updateEvent(pUpEvent);
126
 
                        }
127
 
                        pTouch->id = -1;
128
 
                    } else {
129
 
                        pTouch->id = event.value;
130
 
                    }
131
 
                    break;
132
 
                case ABS_MT_POSITION_X:
133
 
//                    cerr << ">> ABS_MT_POSITION_X: " << event.value << endl;
134
 
                    pTouch = &(m_Slots[curSlot]);
135
 
                    pTouch->pos.x = event.value;
136
 
                    break;
137
 
                case ABS_MT_POSITION_Y:
138
 
//                    cerr << ">> ABS_MT_POSITION_Y: " << event.value << endl;
139
 
                    pTouch = &(m_Slots[curSlot]);
140
 
                    pTouch->pos.y = event.value;
141
 
                    break;
142
 
                default:
143
 
                    break;
144
 
            }
145
 
//            cerr << ">> new id: " << curSlot << endl;
146
 
            changedIDs.insert(curSlot);
147
 
        }
148
 
    }
149
 
    for (set<int>::iterator it = changedIDs.begin(); it != changedIDs.end(); ++it) {
150
 
        map<int, TouchData>::iterator it2 = m_Slots.find(*it);
151
 
        if (it2 != m_Slots.end()) {
152
 
            const TouchData& touch = it2->second;
153
 
//            cerr << "slot: " << *it << ", id: " << touch.id << ", pos: " << touch.pos 
154
 
//                    << endl;
155
 
//            AVG_ASSERT(touch.pos.x != 0);
156
 
            if (touch.id != -1) {
157
 
                TouchStatusPtr pTouchStatus = getTouchStatus(touch.id);
158
 
                if (!pTouchStatus) {
159
 
//                    cerr << "down" << endl;
160
 
                    // Down
161
 
                    m_LastID++;
162
 
                    TouchEventPtr pEvent = createEvent(m_LastID, Event::CURSORDOWN, 
163
 
                            touch.pos); 
164
 
                    addTouchStatus((long)touch.id, pEvent);
165
 
                } else {
166
 
//                    cerr << "move" << endl;
167
 
                    // Move
168
 
                    TouchEventPtr pEvent = createEvent(0, Event::CURSORMOTION, touch.pos); 
169
 
                    pTouchStatus->updateEvent(pEvent);
170
 
                }
171
 
            }
172
 
        }
173
 
    }
174
 
    return MultitouchEventSource::pollEvents();
175
 
}
176
 
 
177
 
TouchEventPtr LibMTDevEventSource::createEvent(int id, Event::Type type, IntPoint pos)
178
 
{
179
 
    DPoint size = getWindowSize();
180
 
    DPoint normPos = DPoint(double(pos.x-m_Dimensions.tl.x)/m_Dimensions.width(),
181
 
            double(pos.y-m_Dimensions.tl.y)/m_Dimensions.height());
182
 
    IntPoint screenPos(int(normPos.x*size.x+0.5), int(normPos.y*size.y+0.5));
183
 
    return TouchEventPtr(new TouchEvent(id, type, screenPos, Event::TOUCH, DPoint(0,0), 
184
 
            0, 20, 1, DPoint(5,0), DPoint(0,5)));
185
 
}
186
 
           
187
 
}