summaryrefslogtreecommitdiff
path: root/record.cpp
blob: 80c38460fa210cf63e396ce06f85e56e779bc932 (plain)
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <time.h>
#include <X11/Xlib.h>
#include <X11/extensions/XInput.h>
#ifdef HAVE_XI2
#include <X11/extensions/XInput2.h>
#endif
#include <X11/XKBlib.h>

#include "global.h"
#include "rec.h"

static volatile int done = 0;

static unsigned
get_record_time()
{
	struct timespec ts;

	clock_gettime(CLOCK_MONOTONIC, &ts);
	return ts.tv_sec * 1000u + ts.tv_nsec / 1000000u;
}

static void
sig_end(int sig)
{
	done = 1;
}

namespace {
class DoRecord
{
public:
	DoRecord(Window _win, const char *fn);
	void go();
private:
	void handle_xi_mouse(XIDeviceEvent* ev);
	void handle_xi_keys(XIDeviceEvent* ev);
	void handle_xinput(XGenericEventCookie *cookie);
	void handle_ConfigureNotify(XEvent *event);
	void handle_xev(XEvent *ev);
	void record_move();

	WriteRecordsFile file;
	bool has_focus, inside;
	Window win;
	Rect rc;
};
};

DoRecord::DoRecord(Window _win, const char *fn):
	file(fn), has_focus(false), inside(false), win(_win), rc(get_win_pos(_win))
{
	/* TODO get focus and if mouse inside */

	xev_init(win);
	xinput_init(dpy, win);

	/* mark which window we selected to record */
	Record r(Record::TargetWindow, get_record_time());
	r.set_field(Record::Window, win);
	file.put(r);

	/* fake move to tell where mouse was */
	record_move();
}

void
DoRecord::go()
{
	signal(SIGINT, sig_end);
	signal(SIGTERM, sig_end);

	while (!done) {
		XEvent event;

		XNextEvent (dpy, &event);

		if (is_xinput_event(&event)) {
			XGenericEventCookie *cookie = &event.xcookie;
			if (XGetEventData(dpy, cookie)) {
				handle_xinput(cookie);
				XFreeEventData(dpy, cookie);
			}
		} else {
			handle_xev(&event);
		}
	}

	signal(SIGINT, SIG_DFL);
	signal(SIGTERM, SIG_DFL);
}

void
DoRecord::record_move()
{
	Record r(Record::WindowMove, get_record_time());
	r.set_field(Record::X, rc.x);
	r.set_field(Record::Y, rc.y);
	r.set_field(Record::Width, rc.width);
	r.set_field(Record::Height, rc.height);
	r.set_field(Record::Window, win);
	file.put(r);
}

static Record::RecordType get_record_type(int evtype)
{
	switch (evtype) {
	case XI_KeyPress:
		return Record::KeyDown;
	case XI_KeyRelease:
		return Record::KeyUp;
	case XI_ButtonPress:
		return Record::ButtonDown;
	case XI_ButtonRelease:
		return Record::ButtonUp;
	case XI_Motion:
		return Record::MouseMotion;
	}
	fprintf (stderr, "unhandled event\n");
	exit(EXIT_FAILURE);
}

void
DoRecord::handle_xi_mouse(XIDeviceEvent* ev)
{
	int mouse_x = (int) ev->root_x;
	if (mouse_x < rc.x || mouse_x - rc.x >= rc.width)
		return;

	int mouse_y = (int) ev->root_y;
	if (mouse_y < rc.y || mouse_y - rc.y >= rc.height)
		return;

	if (!inside)
		return;

	Record r(get_record_type(ev->evtype), get_record_time());
	r.set_field(Record::X, mouse_x - rc.x);
	r.set_field(Record::Y, mouse_y - rc.y);
	r.set_field(Record::State, ev->mods.effective);
	r.set_field(Record::Detail, ev->detail);
	r.set_field(Record::Window, win);
	file.put(r);

	if (verbose == 1)
		printf("Mouse moved at (%d,%d)\n", mouse_x, mouse_y);
}

void
DoRecord::handle_xi_keys(XIDeviceEvent* ev)
{
	Record r(get_record_type(ev->evtype), get_record_time());
	r.set_field(Record::X, ev->root_x - rc.x);
	r.set_field(Record::Y, ev->root_y - rc.y);
	r.set_field(Record::State, ev->mods.effective);
	r.set_field(Record::Detail, ev->detail);
	r.set_field(Record::Window, win);
	KeySym	key = XkbKeycodeToKeysym(dpy, ev->detail,
		0, ev->mods.effective & ShiftMask ? 1 : 0);
	r.set_field(Record::Keysym, key);
	file.put(r);
}

void
DoRecord::handle_xinput(XGenericEventCookie *cookie)
{
	if (verbose >= 2)
		xinput_dump(dpy, cookie);

	switch (cookie->evtype) {
	case XI_KeyPress:
	case XI_KeyRelease:
		if (has_focus) {
			xinput_dump(dpy, cookie);
			handle_xi_keys((XIDeviceEvent*) cookie->data);
		}
		break;
	case XI_ButtonPress:
	case XI_ButtonRelease:
	case XI_Motion:
		handle_xi_mouse((XIDeviceEvent*) cookie->data);
		break;
	/* don't care about these */
	case XI_RawKeyPress:
	case XI_RawKeyRelease:
	case XI_RawButtonPress:
	case XI_RawButtonRelease:
	case XI_RawTouchBegin:
	case XI_RawTouchUpdate:
	case XI_RawTouchEnd:
	case XI_RawMotion:
		break;
	default:
		if (verbose == 1)
			xinput_dump(dpy, cookie);
		break;
	}
}

void
DoRecord::handle_ConfigureNotify(XEvent *event)
{
	XConfigureEvent *e = &event->xconfigure;

	if (e->window == win) {
		rc.x = e->x;
		rc.y = e->y;
		rc.width = e->width;
		rc.height = e->height;
		record_move();
	}
}

void
DoRecord::handle_xev(XEvent *ev)
{
	if (verbose >= 2)
		xev_dump(ev);

	switch (ev->type) {
	case FocusIn:
		if (ev->xfocus.window == win)
			has_focus = 1;
		break;
	case FocusOut:
		if (ev->xfocus.window == win
		    && ev->xfocus.mode != NotifyWhileGrabbed)
			has_focus = 0;
		break;
	case EnterNotify:
		if (ev->xcrossing.window == win)
			inside = 1;
		break;
	case LeaveNotify:
		if (ev->xcrossing.window == win)
			inside = 0;
		break;
	case DestroyNotify:
		if (ev->xdestroywindow.window == win)
			done = 1;
		break;
	case ConfigureNotify:
		handle_ConfigureNotify(ev);
		break;
	/* don't care */
	case KeyPress:
	case KeyRelease:
	case ButtonPress:
	case ButtonRelease:
	case MotionNotify:
	case ColormapNotify:
		break;
	case KeymapNotify:
	case Expose:
	case GraphicsExpose:
	case NoExpose:
	case VisibilityNotify:
	case CreateNotify:	/* see if our client was created ? */
	case UnmapNotify:
	case MapNotify:
	case MapRequest:
	case ReparentNotify:
	case ConfigureRequest:
	case GravityNotify:
	case ResizeRequest:
	case CirculateNotify:
	case CirculateRequest:
	case PropertyNotify:	/* handle status change (visible/not) ? */
	case SelectionClear:
	case SelectionRequest:
	case SelectionNotify:
	case ClientMessage:
	case MappingNotify:
		if (verbose == 1)
			xev_dump(ev);
		break;
	}
}

void record(const char *fn_out, Window w)
{
	if (w == 0) {
		fprintf (stderr, "record mode require a window to be specified\n");
		exit(EXIT_FAILURE);
	}

	DoRecord action(w, fn_out);

	action.go();
}