forked from lucasw/vimjay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
input.cpp
227 lines (183 loc) · 5.76 KB
/
input.cpp
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/*
Copyright 2012 Lucas Walter
This file is part of Vimjay.
Vimjay is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Vimjay 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with Vimjay. If not, see <http://www.gnu.org/licenses/>.
*/
#include "input.h"
#include <iostream>
#include <sstream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <typeinfo>
#include <cxxabi.h> // non portable
#include <deque>
//#include <pair>
#include <boost/lexical_cast.hpp>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <glog/logging.h>
#include <gflags/gflags.h>
#include "input.h"
#include "nodes.h"
#include "misc_nodes.h"
#include "signals.h"
#include "filter.h"
#include "generate.h"
#include "screencap.h"
#include "utility.h"
//using namespace cv;
using namespace std;
namespace bm {
//DEFINE_int32(width, 640, "");
//DEFINE_int32(height, 480, "");
//DEFINE_string(mouse, "/dev/input/mouse0", "/dev/input/mouseN or /dev/input/eventN");
/*
* To work with Kinect the user must install OpenNI library and PrimeSensorModule for OpenNI and
* configure OpenCV with WITH_OPENNI flag is ON (using CMake).
TBD have a mode that takes a webcam, uses brightness as depth, and thresholds it for the valid map
*/
#if 0
int main( int argc, char* argv[] )
{
// google::ParseCommandLineFlags(&argc, &argv, true);
google::InitGoogleLogging(argv[0]);
google::LogToStderr();
google::ParseCommandLineFlags(&argc, &argv, false);
#endif
#if 0
int fd;
// cat /proc/bus/input/devices - TBD how to find just the mouses?
// - TBD how to find just the mouses?
// TBD can't get touchpad to work, don't even see it when catting /dev/input/mouseN
if ((fd = open(FLAGS_mouse.c_str(), O_RDONLY)) < 0) {
LOG(ERROR) << "couldn't open mouse " << fd;
exit(0);
}
struct input_event ev;
bool rv = true;
while(rv) {
read(fd, &ev, sizeof(struct input_event));
VLOG(1) << "value 0x" << std::hex << ev.value
<< ", type 0x" << std::hex << ev.type
<< ", code 0x" << std::hex << ev.code;
if (ev.type == EV_REL) {
if (ev.value != 0) {
// mouse move left
if (ev.code == ABS_X) LOG(INFO)<< "dx " << ev.value;
// mouse move right
if (ev.code == ABS_Y) LOG(INFO)<< "dy " << ev.value;
// wheel
if (ev.code == REL_WHEEL) LOG(INFO)<< "wheel " << ev.value;
}
}
if (ev.type == EV_MSC) {
// 0x90001 - 3
LOG(INFO) << "Button value 0x" << std::hex << ev.value
<< ", code " << ev.code;
}
}
#endif
#if 0
int width = FLAGS_width;
int height = FLAGS_height;
Display *display;
Window win;
int opcode;
bm::setupX(display, win, width, height, opcode);
//bm::setWindowDecorations(display, win,false);
cv::Mat tmp;
// BGR
tmp = cv::Mat(height, width, CV_8UC4, cv::Scalar(255,100,50));
bm::matToScreen(tmp, display, win);
//cv::imshow("temp", tmp);
//cv::waitKey(10);
/* Event loop */
while(1) {
bm::matToScreen(tmp, display, win);
usleep(10000);
#endif
Mouse::Mouse(const std::string name) :
Node(name),
display(NULL)
{
setSignal("0_x", 0);
//event_thread = boost::thread(&Mouse::runThread, this);
}
Mouse::~Mouse()
{
//run_thread = false;
//event_thread.join();
}
#if 0
bool Mouse::update()
{
if (!Node::update()) return false;
return true;
}
#endif
bool Mouse::draw(cv::Point2f ui_offset)
{
Node::draw(ui_offset);
//run_thread = true;
//while (run_thread)
//bool getMouse(Display* display, int& deviceid, int& button, int& x, int& y, bool );
{
if (!display) return false;// continue;
//VLOG(1) << "mouse";
XEvent ev;
/* Get next event; blocks until an event occurs */
while(XPending(display)) {
XNextEvent(display, &ev);
if (ev.xcookie.type == GenericEvent &&
ev.xcookie.extension == opcode &&
XGetEventData(display, &ev.xcookie))
//if (XCheckWindowEvent(display, win, PointerMotionMask | ButtonPressMask | ButtonReleaseMask, &ev))
{
VLOG(1) <<" event found";
XIDeviceEvent* evData = (XIDeviceEvent*)(ev.xcookie.data);
int deviceid = evData->deviceid;
switch(ev.xcookie.evtype)
{
case XI_Motion:
//LOG(INFO) << "motion";
setSignal(boost::lexical_cast<string>(deviceid) + "_x", evData->event_x);
setSignal(boost::lexical_cast<string>(deviceid) + "_y", evData->event_y);
VLOG(1) << deviceid << " " << evData->event_x << " " << evData->event_y;
break;
case XI_ButtonPress:
VLOG(1) << deviceid << " button: " << evData->detail;
setSignal(boost::lexical_cast<string>(deviceid) + "_" +
boost::lexical_cast<string>(evData->detail), 1);
break;
case XI_ButtonRelease:
VLOG(1) << deviceid << " unclick";
setSignal(boost::lexical_cast<string>(deviceid) + "_" +
boost::lexical_cast<string>(evData->detail), 0);
break;
#if 0
case XI_KeyPress:
LOG(INFO) << "key down";
break;
case XI_KeyRelease:
printf("key up\n");
break;
#endif
} // switch
} // correct event
XFreeEventData(display, &ev.xcookie);
//usleep(1000);
} // while
}
return true;
}
} // bm