~ubuntu-branches/ubuntu/saucy/gnash/saucy-proposed

« back to all changes in this revision

Viewing changes to libdevice/events/UinputDevice.cpp

  • Committer: Package Import Robot
  • Author(s): Micah Gersten
  • Date: 2012-03-04 03:19:06 UTC
  • mfrom: (1.1.18) (3.1.24 sid)
  • Revision ID: package-import@ubuntu.com-20120304031906-p6q5rnb0xhgpof7o
Tags: 0.8.10-3ubuntu1
* Merge from Debian testing (FFe: LP: #940876), remaining changes:
  - Use mozilla-flashplugin as the alternative for now
  - Change xulrunner-dev build dep to firefox-dev
* Drop the plugin API porting patch, this has been fixed upstream
  - drop debian/patches*
* Drop the following change as we want Adobe's player to take priority
  if it's installed
  - Set alternative priority to 50 so that it matches Adobe Flash's priority

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// 
 
2
//   Copyright (C) 2010, 2011, 2012 Free Software Foundation, Inc
 
3
// 
 
4
// This program is free software; you can redistribute it and/or modify
 
5
// it under the terms of the GNU General Public License as published by
 
6
// the Free Software Foundation; either version 3 of the License, or
 
7
// (at your option) any later version.
 
8
// 
 
9
// This program is distributed in the hope that it will be useful,
 
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
// GNU General Public License for more details.
 
13
//
 
14
// You should have received a copy of the GNU General Public License
 
15
// along with this program; if not, write to the Free Software
 
16
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
17
//
 
18
 
 
19
#ifdef HAVE_CONFIG_H
 
20
#include "gnashconfig.h"
 
21
#endif
 
22
 
 
23
#include <sys/types.h>
 
24
#include <sys/stat.h>
 
25
#include <fcntl.h>
 
26
#include <errno.h>
 
27
#include <boost/shared_array.hpp>
 
28
 
 
29
#include "GnashSleep.h"
 
30
#include "log.h"
 
31
#include "InputDevice.h"
 
32
 
 
33
namespace gnash {
 
34
 
 
35
UinputDevice::UinputDevice()
 
36
    : _fd(-1)
 
37
{
 
38
    // GNASH_REPORT_FUNCTION;
 
39
}
 
40
 
 
41
UinputDevice::~UinputDevice()
 
42
{
 
43
    // GNASH_REPORT_FUNCTION;
 
44
    if (_fd) {
 
45
        if (ioctl(_fd, UI_DEV_DESTROY, 0) < 0) {
 
46
            log_error(_("ioctl(UI_DEV_DESTROY)"));
 
47
        }
 
48
    }
 
49
}
 
50
 
 
51
bool
 
52
UinputDevice::scanForDevice()
 
53
{
 
54
    // GNASH_REPORT_FUNCTION;
 
55
 
 
56
    struct stat st;
 
57
 
 
58
    // Look for these files for mouse input
 
59
    struct mouse_types {
 
60
        InputDevice::devicetype_e type;
 
61
        const char *filespec;
 
62
    };    
 
63
 
 
64
    // The Uinput device is found in one of these two locations.
 
65
    const char *mice[] = {
 
66
        "/dev/input/event4",
 
67
        "/dev/uinput",
 
68
        "/dev/input/uinput",
 
69
        0
 
70
    };
 
71
 
 
72
    int i = 0;
 
73
    while (mice[i]) {
 
74
        if (stat(mice[i], &st) == 0) {
 
75
            // Then see if we can open it, this is a write only device
 
76
            if ((_fd = open(mice[i], O_WRONLY)) < 0) {
 
77
                log_error(_("You don't have the proper permissions to open %s"),
 
78
                          mice[i]);
 
79
                i++;
 
80
                continue;
 
81
            }
 
82
            log_debug(_("Found a User mode input device at %s"), mice[i]);
 
83
            return true;
 
84
            
 
85
        }     // stat()      
 
86
        i++;
 
87
    }         // while()
 
88
    
 
89
    return false;
 
90
}
 
91
 
 
92
bool
 
93
UinputDevice::init()
 
94
{
 
95
    // GNASH_REPORT_FUNCTION;
 
96
 
 
97
    if (_fd < 0) {
 
98
        log_error(_("User Mode Input device not initialized yet!"));
 
99
        return false;
 
100
    }
 
101
    
 
102
    if (ioctl(_fd, UI_SET_EVBIT, EV_KEY) < 0) {
 
103
        log_error(_("ioctl(UI_SET_EVBIT, EV_KEY)"));
 
104
        // return false;
 
105
    }
 
106
 
 
107
#if 0 // USE_RELATIVE_POINTER
 
108
    if (ioctl(_fd, UI_SET_EVBIT, EV_REL) < 0) {
 
109
        log_error(_("ioctl(UI_SET_EVBIT, EV_REL)"));
 
110
        // return false;
 
111
    }
 
112
    if (ioctl(_fd, UI_SET_RELBIT, REL_X) < 0) {
 
113
        log_error(_("ioctl(UI_SET_RELBIT, REL_X)"));
 
114
        // return false;
 
115
    }
 
116
    if (ioctl(_fd, UI_SET_RELBIT, REL_Y) < 0) {
 
117
        log_error(_("ioctl( UI_SET_RELBIT, REL_Y)"));
 
118
        // return false;
 
119
    }
 
120
#else
 
121
#if 1 // USE_ABSOLUTE_POINTER
 
122
    struct uinput_user_dev uidev;
 
123
 
 
124
    memset(&uidev, 0, sizeof(uidev));
 
125
    snprintf(uidev.name, UINPUT_MAX_NAME_SIZE, "uinput");
 
126
    uidev.id.bustype = BUS_USB;
 
127
    // uidev.id.vendor  = 0x1;
 
128
    // uidev.id.product = 0x1;
 
129
    // uidev.id.version = 1;
 
130
 
 
131
    uidev.absmin[ABS_X] = 0;
 
132
    uidev.absmax[ABS_X] = 1024; //_screen_width;
 
133
    uidev.absfuzz[ABS_X] = 0;
 
134
    uidev.absflat[ABS_X] = 0;
 
135
    uidev.absmin[ABS_Y] = 0;
 
136
    uidev.absmax[ABS_Y] = 768; //_screen_height;
 
137
    uidev.absfuzz[ABS_Y] = 0;
 
138
    uidev.absflat[ABS_Y ] = 0;
 
139
    uidev.absmax[ABS_PRESSURE]=400;
 
140
 
 
141
    if (::write(_fd, (char *)&uidev, sizeof(uidev)) < 0) {
 
142
        log_error(_("write uidev"));
 
143
        // return false;
 
144
    }
 
145
#endif
 
146
    if (ioctl(_fd, UI_SET_EVBIT, EV_ABS) < 0) {
 
147
        log_error(_("ioctl(UI_SET_EVBIT, EV_ABS): %s"), strerror(errno));
 
148
        // return false;
 
149
    }
 
150
    if (ioctl(_fd, UI_SET_ABSBIT,ABS_X) < 0) {
 
151
        log_error(_("ioctl(UI_SET_ABSBIT,ABS_X): %s"), strerror(errno));
 
152
        // return false;
 
153
    }
 
154
    if (ioctl(_fd, UI_SET_ABSBIT, ABS_Y) < 0) {
 
155
        log_error(_("ioctl(UI_SET_ABSBIT, ABS_Y): %s"), strerror(errno));
 
156
        // return false;
 
157
    }
 
158
#endif
 
159
    
 
160
    if (ioctl(_fd, UI_SET_KEYBIT, BTN_LEFT) < 0) {
 
161
        log_error(_("ioctl(UI_SET_KEYBIT, BTN_LEFT)): %s"), strerror(errno));
 
162
        // return false;
 
163
    }
 
164
    if (ioctl(_fd, UI_SET_KEYBIT, BTN_RIGHT) < 0) {
 
165
        log_error(_("ioctl(UI_SET_KEYBIT, BTN_RIGHT): %s"), strerror(errno));
 
166
        // return false;
 
167
    }
 
168
    if (ioctl(_fd, UI_SET_EVBIT, ABS_PRESSURE) < 0) {
 
169
        log_error(_("ioctl(UI_SET_EVBIT, ABS_PRESSURE): %s"), strerror(errno));
 
170
        // return false;
 
171
    }
 
172
    // if (ioctl(_fd, UI_SET_EVBIT, ABS_TOUCH) < 0) {
 
173
    //     log_error("ioctl(UI_SET_EVBIT, ABS_TOUCH)");
 
174
    //     return false;
 
175
    // }
 
176
    if (ioctl(_fd, UI_SET_KEYBIT, BTN_MOUSE) < 0) {
 
177
        log_error(_("ioctl(UI_SET_KEYBIT, BTN_MOUSE): %s"), strerror(errno));
 
178
        // return false;
 
179
    }
 
180
 
 
181
    if (ioctl(_fd, UI_DEV_CREATE, 0) < 0) {
 
182
        log_error(_("ioctl(UI_DEV_CREATED) failed!"),  strerror(errno));
 
183
        // return false;
 
184
    }
 
185
    
 
186
    return true;
 
187
}
 
188
 
 
189
// Move the mouse cursor to a specified location
 
190
bool
 
191
UinputDevice::moveTo(int x, int y)
 
192
{
 
193
    // GNASH_REPORT_FUNCTION;
 
194
 
 
195
    struct input_event ev;
 
196
    
 
197
    memset(&ev, 0, sizeof(ev));
 
198
    gettimeofday(&ev.time, NULL);
 
199
    ev.type = EV_ABS;
 
200
    ev.code = ABS_X;
 
201
    ev.value = x;
 
202
    if (::write(_fd, &ev, sizeof(struct input_event)) < 0) {
 
203
        log_error("write ABS_X");
 
204
        return false;
 
205
    }
 
206
    
 
207
    ev.code = SYN_REPORT;
 
208
    ev.type = EV_SYN;
 
209
    if (::write(_fd, &ev, sizeof(struct input_event)) < 0) {
 
210
        log_error("write SYN");
 
211
        return false;
 
212
    }
 
213
 
 
214
    ev.type = EV_ABS;
 
215
    ev.code = ABS_Y;
 
216
    ev.value = y;
 
217
    if (::write(_fd, &ev, sizeof(struct input_event)) < 0) {
 
218
        log_error("write ABS_Y");
 
219
        return false;
 
220
    }
 
221
 
 
222
    ev.code = SYN_REPORT;
 
223
    ev.type = EV_SYN;
 
224
    if (::write(_fd, &ev, sizeof(struct input_event)) < 0) {
 
225
        log_error("write SYN");
 
226
        return false;
 
227
    }
 
228
 
 
229
    return true;
 
230
}   
 
231
 
 
232
// end of namespace
 
233
}
 
234
 
 
235
// local Variables:
 
236
// mode: C++
 
237
// indent-tabs-mode: nil
 
238
// End: