Open the display only once in wxUIActionSimulatorX11Impl
Instead of opening connection to X display in every method, do it once in ctor, as we can store the display across multiple calls after the recent refactoring. This does require allocating a separate "impl" object for each public object, but should still be much less wasteful than opening and closing the display connections all the time.
This commit is contained in:
@@ -33,14 +33,7 @@ namespace
|
|||||||
class wxUIActionSimulatorX11Impl : public wxUIActionSimulatorImpl
|
class wxUIActionSimulatorX11Impl : public wxUIActionSimulatorImpl
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
// Returns a pointer to the global simulator object: as it's stateless, we
|
wxUIActionSimulatorX11Impl() { }
|
||||||
// can reuse the same one without having to allocate it on the heap all the
|
|
||||||
// time.
|
|
||||||
static wxUIActionSimulatorX11Impl* Get()
|
|
||||||
{
|
|
||||||
static wxUIActionSimulatorX11Impl s_impl;
|
|
||||||
return &s_impl;
|
|
||||||
}
|
|
||||||
|
|
||||||
virtual bool MouseMove(long x, long y) wxOVERRIDE;
|
virtual bool MouseMove(long x, long y) wxOVERRIDE;
|
||||||
virtual bool MouseDown(int button = wxMOUSE_BTN_LEFT) wxOVERRIDE;
|
virtual bool MouseDown(int button = wxMOUSE_BTN_LEFT) wxOVERRIDE;
|
||||||
@@ -49,17 +42,19 @@ public:
|
|||||||
virtual bool DoKey(int keycode, int modifiers, bool isDown) wxOVERRIDE;
|
virtual bool DoKey(int keycode, int modifiers, bool isDown) wxOVERRIDE;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// This class has no public ctors, use Get() instead.
|
|
||||||
wxUIActionSimulatorX11Impl() { }
|
|
||||||
|
|
||||||
// Common implementation of Mouse{Down,Up}()
|
// Common implementation of Mouse{Down,Up}()
|
||||||
static void SendButtonEvent(int button, bool isDown);
|
bool SendButtonEvent(int button, bool isDown);
|
||||||
|
|
||||||
|
wxX11Display m_display;
|
||||||
|
|
||||||
wxDECLARE_NO_COPY_CLASS(wxUIActionSimulatorX11Impl);
|
wxDECLARE_NO_COPY_CLASS(wxUIActionSimulatorX11Impl);
|
||||||
};
|
};
|
||||||
|
|
||||||
void wxUIActionSimulatorX11Impl::SendButtonEvent(int button, bool isDown)
|
bool wxUIActionSimulatorX11Impl::SendButtonEvent(int button, bool isDown)
|
||||||
{
|
{
|
||||||
|
if ( !m_display )
|
||||||
|
return false;
|
||||||
|
|
||||||
int xbutton;
|
int xbutton;
|
||||||
switch (button)
|
switch (button)
|
||||||
{
|
{
|
||||||
@@ -74,14 +69,11 @@ void wxUIActionSimulatorX11Impl::SendButtonEvent(int button, bool isDown)
|
|||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
wxFAIL_MSG("Unsupported button passed in.");
|
wxFAIL_MSG("Unsupported button passed in.");
|
||||||
return;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
wxX11Display display;
|
|
||||||
wxCHECK_RET(display, "No display available!");
|
|
||||||
|
|
||||||
#if wxUSE_XTEST
|
#if wxUSE_XTEST
|
||||||
XTestFakeButtonEvent(display, xbutton, isDown, 0);
|
XTestFakeButtonEvent(m_display, xbutton, isDown, 0);
|
||||||
|
|
||||||
#else // !wxUSE_XTEST
|
#else // !wxUSE_XTEST
|
||||||
XEvent event;
|
XEvent event;
|
||||||
@@ -91,7 +83,7 @@ void wxUIActionSimulatorX11Impl::SendButtonEvent(int button, bool isDown)
|
|||||||
event.xbutton.button = xbutton;
|
event.xbutton.button = xbutton;
|
||||||
event.xbutton.same_screen = True;
|
event.xbutton.same_screen = True;
|
||||||
|
|
||||||
XQueryPointer(display, display.DefaultRoot(),
|
XQueryPointer(m_display, m_display.DefaultRoot(),
|
||||||
&event.xbutton.root, &event.xbutton.window,
|
&event.xbutton.root, &event.xbutton.window,
|
||||||
&event.xbutton.x_root, &event.xbutton.y_root,
|
&event.xbutton.x_root, &event.xbutton.y_root,
|
||||||
&event.xbutton.x, &event.xbutton.y, &event.xbutton.state);
|
&event.xbutton.x, &event.xbutton.y, &event.xbutton.state);
|
||||||
@@ -100,35 +92,36 @@ void wxUIActionSimulatorX11Impl::SendButtonEvent(int button, bool isDown)
|
|||||||
while (event.xbutton.subwindow)
|
while (event.xbutton.subwindow)
|
||||||
{
|
{
|
||||||
event.xbutton.window = event.xbutton.subwindow;
|
event.xbutton.window = event.xbutton.subwindow;
|
||||||
XQueryPointer(display, event.xbutton.window,
|
XQueryPointer(m_display, event.xbutton.window,
|
||||||
&event.xbutton.root, &event.xbutton.subwindow,
|
&event.xbutton.root, &event.xbutton.subwindow,
|
||||||
&event.xbutton.x_root, &event.xbutton.y_root,
|
&event.xbutton.x_root, &event.xbutton.y_root,
|
||||||
&event.xbutton.x, &event.xbutton.y, &event.xbutton.state);
|
&event.xbutton.x, &event.xbutton.y, &event.xbutton.state);
|
||||||
}
|
}
|
||||||
|
|
||||||
XSendEvent(display, PointerWindow, True, 0xfff, &event);
|
XSendEvent(m_display, PointerWindow, True, 0xfff, &event);
|
||||||
#endif // !wxUSE_XTEST
|
#endif // !wxUSE_XTEST
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
} // anonymous namespace
|
} // anonymous namespace
|
||||||
|
|
||||||
bool wxUIActionSimulatorX11Impl::MouseDown(int button)
|
bool wxUIActionSimulatorX11Impl::MouseDown(int button)
|
||||||
{
|
{
|
||||||
SendButtonEvent(button, true);
|
return SendButtonEvent(button, true);
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool wxUIActionSimulatorX11Impl::MouseMove(long x, long y)
|
bool wxUIActionSimulatorX11Impl::MouseMove(long x, long y)
|
||||||
{
|
{
|
||||||
wxX11Display display;
|
if ( !m_display )
|
||||||
wxASSERT_MSG(display, "No display available!");
|
return false;
|
||||||
|
|
||||||
#if wxUSE_XTEST
|
#if wxUSE_XTEST
|
||||||
XTestFakeMotionEvent(display, -1, x, y, 0);
|
XTestFakeMotionEvent(m_display, -1, x, y, 0);
|
||||||
|
|
||||||
#else // !wxUSE_XTEST
|
#else // !wxUSE_XTEST
|
||||||
Window root = display.DefaultRoot();
|
Window root = m_display.DefaultRoot();
|
||||||
XWarpPointer(display, None, root, 0, 0, 0, 0, x, y);
|
XWarpPointer(m_display, None, root, 0, 0, 0, 0, x, y);
|
||||||
#endif // !wxUSE_XTEST
|
#endif // !wxUSE_XTEST
|
||||||
|
|
||||||
// At least with wxGTK we must always process the pending events before the
|
// At least with wxGTK we must always process the pending events before the
|
||||||
@@ -145,14 +138,13 @@ bool wxUIActionSimulatorX11Impl::MouseMove(long x, long y)
|
|||||||
|
|
||||||
bool wxUIActionSimulatorX11Impl::MouseUp(int button)
|
bool wxUIActionSimulatorX11Impl::MouseUp(int button)
|
||||||
{
|
{
|
||||||
SendButtonEvent(button, false);
|
return SendButtonEvent(button, false);
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool wxUIActionSimulatorX11Impl::DoKey(int keycode, int modifiers, bool isDown)
|
bool wxUIActionSimulatorX11Impl::DoKey(int keycode, int modifiers, bool isDown)
|
||||||
{
|
{
|
||||||
wxX11Display display;
|
if ( !m_display )
|
||||||
wxCHECK_MSG(display, false, "No display available!");
|
return false;
|
||||||
|
|
||||||
int mask, type;
|
int mask, type;
|
||||||
|
|
||||||
@@ -168,7 +160,7 @@ bool wxUIActionSimulatorX11Impl::DoKey(int keycode, int modifiers, bool isDown)
|
|||||||
}
|
}
|
||||||
|
|
||||||
WXKeySym xkeysym = wxCharCodeWXToX(keycode);
|
WXKeySym xkeysym = wxCharCodeWXToX(keycode);
|
||||||
KeyCode xkeycode = XKeysymToKeycode(display, xkeysym);
|
KeyCode xkeycode = XKeysymToKeycode(m_display, xkeysym);
|
||||||
if ( xkeycode == NoSymbol )
|
if ( xkeycode == NoSymbol )
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
@@ -176,13 +168,13 @@ bool wxUIActionSimulatorX11Impl::DoKey(int keycode, int modifiers, bool isDown)
|
|||||||
wxUnusedVar(modifiers);
|
wxUnusedVar(modifiers);
|
||||||
wxUnusedVar(mask);
|
wxUnusedVar(mask);
|
||||||
wxUnusedVar(type);
|
wxUnusedVar(type);
|
||||||
XTestFakeKeyEvent(display, xkeycode, isDown, 0);
|
XTestFakeKeyEvent(m_display, xkeycode, isDown, 0);
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
#else // !wxUSE_XTEST
|
#else // !wxUSE_XTEST
|
||||||
Window focus;
|
Window focus;
|
||||||
int revert;
|
int revert;
|
||||||
XGetInputFocus(display, &focus, &revert);
|
XGetInputFocus(m_display, &focus, &revert);
|
||||||
if (focus == None)
|
if (focus == None)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
@@ -197,7 +189,7 @@ bool wxUIActionSimulatorX11Impl::DoKey(int keycode, int modifiers, bool isDown)
|
|||||||
mod |= ControlMask;
|
mod |= ControlMask;
|
||||||
|
|
||||||
XKeyEvent event;
|
XKeyEvent event;
|
||||||
event.display = display;
|
event.display = m_display;
|
||||||
event.window = focus;
|
event.window = focus;
|
||||||
event.root = DefaultRootWindow(event.display);
|
event.root = DefaultRootWindow(event.display);
|
||||||
event.subwindow = None;
|
event.subwindow = None;
|
||||||
@@ -218,14 +210,13 @@ bool wxUIActionSimulatorX11Impl::DoKey(int keycode, int modifiers, bool isDown)
|
|||||||
}
|
}
|
||||||
|
|
||||||
wxUIActionSimulator::wxUIActionSimulator()
|
wxUIActionSimulator::wxUIActionSimulator()
|
||||||
: m_impl(wxUIActionSimulatorX11Impl::Get())
|
: m_impl(new wxUIActionSimulatorX11Impl())
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
wxUIActionSimulator::~wxUIActionSimulator()
|
wxUIActionSimulator::~wxUIActionSimulator()
|
||||||
{
|
{
|
||||||
// We can use a static wxUIActionSimulatorX11Impl object because it's
|
delete m_impl;
|
||||||
// stateless, so no need to delete it.
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif // wxUSE_UIACTIONSIMULATOR
|
#endif // wxUSE_UIACTIONSIMULATOR
|
||||||
|
Reference in New Issue
Block a user