summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFrediano Ziglio <fziglio@redhat.com>2015-08-04 02:54:31 +0100
committerFrediano Ziglio <fziglio@redhat.com>2015-08-04 02:54:31 +0100
commit2b525c78c02b6a5c2d7afa5aaa6f930606adcdbc (patch)
tree397f28d6750d3bf0059cde22556ad2aae1f60fdc
parente8e04507d1fd3c34af9c3066671eb6bdddb43247 (diff)
fix sending keys/mouse
use relative positions also
-rw-r--r--play.cpp50
-rw-r--r--record.cpp19
2 files changed, 52 insertions, 17 deletions
diff --git a/play.cpp b/play.cpp
index 8c3ce44..e9fbd10 100644
--- a/play.cpp
+++ b/play.cpp
@@ -14,6 +14,27 @@
static FILE *f_in = NULL;
+static int x, y, width, height;
+
+static void
+get_win_pos(Window win)
+{
+ int win_x, win_y;
+ Window child;
+ XWindowAttributes attr;
+ XGetWindowAttributes(dpy, win, &attr);
+
+ if (!XTranslateCoordinates(dpy, win, DefaultRootWindow(dpy), 0, 0, &win_x, &win_y, &child)) {
+ fprintf(stderr, "Error XTranslateCoordinates\n");
+ exit(EXIT_FAILURE);
+ }
+ x = win_x;
+ y = win_y;
+ width = attr.width;
+ height = attr.height;
+}
+
+
namespace {
class Record {
public:
@@ -71,6 +92,8 @@ void play(const char *fn, Window win)
exit(EXIT_FAILURE);
}
+ get_win_pos(win);
+
unsigned prev_time = 0;
while (1) {
Record rec;
@@ -92,15 +115,19 @@ void play(const char *fn, Window win)
switch (rec.get_type()) {
case XI_KeyPress:
+ ev.type = KeyPress;
+ goto keyboard;
case XI_KeyRelease:
- ev.type = rec.get_type() == XI_KeyPress ? KeyPress : KeyRelease;
+ ev.type = KeyRelease;
+ keyboard:
ev.xkey.x = rec.get_field(1);
- ev.xkey.x_root = ev.xkey.x;
+ ev.xkey.x_root = ev.xkey.x + x;
ev.xkey.y = rec.get_field(2);
- ev.xkey.y_root = ev.xkey.y;
+ ev.xkey.y_root = ev.xkey.y + y;
ev.xkey.state = rec.get_field(3);
ev.xkey.keycode = rec.get_field(4);
ev.xkey.same_screen = True;
+// XSendEvent(dpy, win, True, KeyPressMask, &ev);
XSendEvent(dpy, win, False, 0, &ev);
XFlush(dpy);
break;
@@ -112,14 +139,25 @@ void play(const char *fn, Window win)
goto mouse;
case XI_Motion:
ev.type = MotionNotify;
- mouse:
ev.xkey.x = rec.get_field(1);
- ev.xkey.x_root = ev.xkey.x;
+ ev.xkey.x_root = ev.xkey.x + x;
ev.xkey.y = rec.get_field(2);
- ev.xkey.y_root = ev.xkey.y;
+ ev.xkey.y_root = ev.xkey.y + y;
ev.xkey.state = rec.get_field(3);
rec.get_field(4);
+// XSendEvent(dpy, win, False, 0, &ev);
+ XWarpPointer(dpy, win, win, 0,0,0,0, ev.xkey.x, ev.xkey.y);
+ XFlush(dpy);
+ break;
+ mouse:
+ ev.xkey.x = rec.get_field(1);
+ ev.xkey.x_root = ev.xkey.x + x;
+ ev.xkey.y = rec.get_field(2);
+ ev.xkey.y_root = ev.xkey.y + y;
+ ev.xkey.state = rec.get_field(3);
+ ev.xbutton.button = rec.get_field(4);
XSendEvent(dpy, win, False, 0, &ev);
+// XSendEvent(dpy, win, True, ButtonPressMask, &ev);
XFlush(dpy);
break;
default:
diff --git a/record.cpp b/record.cpp
index 8cc2fd2..e1945a4 100644
--- a/record.cpp
+++ b/record.cpp
@@ -70,7 +70,7 @@ get_win_pos(void)
XWindowAttributes attr;
XGetWindowAttributes(dpy, win, &attr);
- if (!XTranslateCoordinates(dpy, win, DefaultRootWindow(dpy), attr.x, attr.y, &win_x, &win_y, &child)) {
+ if (!XTranslateCoordinates(dpy, win, DefaultRootWindow(dpy), 0, 0, &win_x, &win_y, &child)) {
fprintf(stderr, "Error XTranslateCoordinates\n");
exit(EXIT_FAILURE);
}
@@ -94,15 +94,11 @@ handle_xi_mouse(XIDeviceEvent* ev)
if (!inside)
return;
- unsigned mask = 0;
- for (unsigned n = 0; n < ev->buttons.mask_len && n < 4; ++n)
- mask |= ev->buttons.mask[n] << (n*8u);
-
Record r(ev->evtype);
- r.add_field(1, mouse_x);
- r.add_field(2, mouse_y);
+ r.add_field(1, mouse_x - x);
+ r.add_field(2, mouse_y - y);
r.add_field(3, ev->mods.effective);
- r.add_field(4, mask);
+ r.add_field(4, ev->detail);
if (verbose == 1)
printf("Mouse moved at (%d,%d)\n", mouse_x, mouse_y);
@@ -112,8 +108,8 @@ static void
handle_xi_keys(XIDeviceEvent* ev)
{
Record r(ev->evtype);
- r.add_field(1, ev->root_x);
- r.add_field(2, ev->root_y);
+ r.add_field(1, ev->root_x - x);
+ r.add_field(2, ev->root_y - y);
r.add_field(3, ev->mods.effective);
r.add_field(4, ev->detail);
}
@@ -179,7 +175,8 @@ handle_xev(XEvent *ev)
has_focus = 1;
break;
case FocusOut:
- if (ev->xfocus.window == win)
+ if (ev->xfocus.window == win
+ && ev->xfocus.mode != NotifyWhileGrabbed)
has_focus = 0;
break;
case EnterNotify: