-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsender.cpp
More file actions
211 lines (177 loc) · 5.54 KB
/
sender.cpp
File metadata and controls
211 lines (177 loc) · 5.54 KB
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
#include <iostream>
#include <boost/array.hpp>
#include <boost/asio.hpp>
#include <stdio.h>
#include <xcb/xcb.h>
#include <xcb/xtest.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include "xcbutil.h"
#include "mouse.h"
using boost::asio::ip::tcp;
using namespace std;
float xscale=0, yscale=0; // Scaling values for setting the proper resolution
int mouseId; // The given mouse id (provided by the server)
bool terminated = false; // Whether or not to terminate the connection
// Perform all necessary initializations
void initialize ( ) {
xcbInit ( );
}
// Initialize the network
void initNetwork ( char * ip ) {
boost::asio::io_service io_service;
tcp::resolver resolver(io_service);
tcp::resolver::query query(ip, "daytime");
tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
tcp::resolver::iterator end;
tcp::socket socket(io_service);
boost::system::error_code error = boost::asio::error::host_not_found;
while (!terminated && error && endpoint_iterator != end)
{
socket.close();
socket.connect(*endpoint_iterator++, error);
}
if (error)
throw boost::system::system_error(error);
}
MouseEvent event;
// Send an event over the socket
void send ( tcp::socket & sock) {
cout << "Sending : " << event.type << " " << event.mouseId << " "
<< event.buttonId << " " << event.x << " " << event.y << endl;
std::vector<uint16_t> vec(sizeof(MouseEvent)/sizeof(uint16_t),0);
// Create the event and convert to network order
vec[0] = htons(event.type);
vec[1] = htons(event.mouseId);
vec[2] = htons(event.buttonId);
vec[3] = htons(event.x);
vec[4] = htons(event.y);
// Send the event
int num = sock.send ( boost::asio::buffer(vec) );
cout << "Sent " << num << " bytes" << endl;
}
// Send a mouse move event
void sendMouseMove (tcp::socket & sock, int x, int y ) {
// Create the event
event.type = MC_BUTTON_MOVE;
event.mouseId = mouseId;
event.buttonId = 1;
event.x = x * xscale;
event.y = y * yscale;
// Send the event
send ( sock );
}
// Send a mouse down event
void sendMouseDown (tcp::socket & sock, int x, int y, int detail) {
// Create the event
event.type = MC_BUTTON_DOWN;
event.mouseId = mouseId;
event.buttonId = detail;
event.x = x;
event.y = y;
// Send the event
send ( sock );
}
// Send a mouse up event
void sendMouseUp (tcp::socket & sock, int x, int y, int detail) {
// Create the event
event.type = MC_BUTTON_UP;
event.mouseId = mouseId;
event.buttonId = detail;
event.x = x;
event.y = y;
// Send the event
send ( sock );
}
// Get the resolution of the screen
void getWindowDim( tcp::socket & socket, int * dim ){
boost::system::error_code error;
vector<uint16_t> vec(3, 0);
std::size_t length = boost::asio::read(socket, boost::asio::buffer(vec), boost::asio::transfer_all(), error);
dim[0] = ntohs ( vec[0] ); // x of resolution
dim[1] = ntohs ( vec[1] ); // y of resolution
mouseId = ntohs ( vec[2] ); // id of virtual cursor associated with sender
}
// Tear down the connection
void terminate(tcp::socket & sock)
{
// Create the termination event
event.type = MC_TERMINATE;
event.mouseId = mouseId;
event.buttonId = 2;
event.x = 0;
event.y = 0;
// Send the termination event
send(sock);
}
int main(int argc, char* argv[])
{
initialize ( );
grabMouse ( );
try
{
if (argc != 2)
{
std::cerr << "Usage: client <host>" << endl;
return 1;
}
boost::asio::io_service io_service;
tcp::resolver resolver(io_service);
tcp::resolver::query query(argv[1], "daytime");
tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
tcp::resolver::iterator end;
tcp::socket socket(io_service);
boost::system::error_code error = boost::asio::error::host_not_found;
while (error && endpoint_iterator != end)
{
socket.close();
socket.connect(*endpoint_iterator++, error);
}
if (error)
throw boost::system::system_error(error);
cout << "Connected to the world of tomorrow!" << endl;
int dim[2];
getWindowDim(socket, dim);
cout << "dim: " << dim[0] << " " << dim[1] << endl;
pair<int,int> dimNative = getResolution();
xscale = dim[0] / (float) dimNative.first;
yscale = dim[1] / (float) dimNative.second;
xcb_generic_event_t * event;
// Event loop
// Wait for button event. Print mouse position on button release and print ouch on button pres
while( !terminated && socket.is_open() && (event = xcb_wait_for_event (xcbGetDisplay()))) {
switch (event->response_type & ~0x80) {
case XCB_BUTTON_PRESS:
xcb_button_press_event_t *press;
press = (xcb_button_press_event_t *) event;
sendMouseDown (socket, press->event_x, press->event_y, press->detail );
break;
case XCB_BUTTON_RELEASE:
xcb_button_press_event_t *release;
release = (xcb_button_press_event_t *) event;
sendMouseUp (socket, release->event_x, release->event_y, release->detail );
if(release->detail == 2)
{
terminate(socket);
terminated = true;
return 0;
}
break;
case XCB_MOTION_NOTIFY:
xcb_motion_notify_event_t *motion;
motion = (xcb_motion_notify_event_t *)event;
sendMouseMove (socket, motion->event_x, motion->event_y );
break;
default:
break;
}
}
}
catch (std::exception& e)
{
std::cerr << e.what() << endl;
}
return 0;
}