]> git.saurik.com Git - wxWidgets.git/blob - src/unix/uiactionx11.cpp
Extract PipeIOHandler class into a header and rename to wxWakeUpPipe.
[wxWidgets.git] / src / unix / uiactionx11.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/unix/uiactionx11.cpp
3 // Purpose: wxUIActionSimulator implementation
4 // Author: Kevin Ollivier, Steven Lamerton, Vadim Zeitlin
5 // Modified by:
6 // Created: 2010-03-06
7 // RCS-ID: $Id$
8 // Copyright: (c) Kevin Ollivier
9 // (c) 2010 Steven Lamerton
10 // (c) 2010 Vadim Zeitlin
11 // Licence: wxWindows licence
12 /////////////////////////////////////////////////////////////////////////////
13
14 #include "wx/defs.h"
15
16 #if wxUSE_UIACTIONSIMULATOR
17
18 #include "wx/uiaction.h"
19 #include "wx/event.h"
20 #include "wx/evtloop.h"
21
22 #include <X11/Xlib.h>
23 #include <X11/Xutil.h>
24
25 #include "wx/unix/utilsx11.h"
26
27 namespace
28 {
29
30 void SendButtonEvent(int button, bool isDown)
31 {
32 int xbutton;
33 switch (button)
34 {
35 case wxMOUSE_BTN_LEFT:
36 xbutton = 1;
37 break;
38 case wxMOUSE_BTN_MIDDLE:
39 xbutton = 2;
40 break;
41 case wxMOUSE_BTN_RIGHT:
42 xbutton = 3;
43 break;
44 default:
45 wxFAIL_MSG("Unsupported button passed in.");
46 return;
47 }
48
49 wxX11Display display;
50 wxCHECK_RET(display, "No display available!");
51
52 XEvent event;
53 memset(&event, 0x00, sizeof(event));
54
55 event.type = isDown ? ButtonPress : ButtonRelease;
56 event.xbutton.button = xbutton;
57 event.xbutton.same_screen = True;
58
59 XQueryPointer(display, display.DefaultRoot(),
60 &event.xbutton.root, &event.xbutton.window,
61 &event.xbutton.x_root, &event.xbutton.y_root,
62 &event.xbutton.x, &event.xbutton.y, &event.xbutton.state);
63 event.xbutton.subwindow = event.xbutton.window;
64
65 while (event.xbutton.subwindow)
66 {
67 event.xbutton.window = event.xbutton.subwindow;
68 XQueryPointer(display, event.xbutton.window,
69 &event.xbutton.root, &event.xbutton.subwindow,
70 &event.xbutton.x_root, &event.xbutton.y_root,
71 &event.xbutton.x, &event.xbutton.y, &event.xbutton.state);
72 }
73
74 XSendEvent(display, PointerWindow, True, 0xfff, &event);
75 }
76
77 } // anonymous namespace
78
79 bool wxUIActionSimulator::MouseDown(int button)
80 {
81 SendButtonEvent(button, true);
82 return true;
83 }
84
85 bool wxUIActionSimulator::MouseMove(long x, long y)
86 {
87 wxX11Display display;
88 wxASSERT_MSG(display, "No display available!");
89
90 Window root = display.DefaultRoot();
91 XWarpPointer(display, None, root, 0, 0, 0, 0, x, y);
92
93 // At least with wxGTK we must always process the pending events before the
94 // mouse position change really takes effect, so just do it from here
95 // instead of forcing the client code using this function to always use
96 // wxYield() which is unnecessary under the other platforms.
97 if ( wxEventLoopBase* const loop = wxEventLoop::GetActive() )
98 {
99 loop->YieldFor(wxEVT_CATEGORY_USER_INPUT);
100 }
101
102 return true;
103 }
104
105 bool wxUIActionSimulator::MouseUp(int button)
106 {
107 SendButtonEvent(button, false);
108 return true;
109 }
110
111 bool wxUIActionSimulator::DoKey(int keycode, int modifiers, bool isDown)
112 {
113 wxX11Display display;
114 wxCHECK_MSG(display, false, "No display available!");
115
116 int mask, type;
117
118 if ( isDown )
119 {
120 type = KeyPress;
121 mask = KeyPressMask;
122 }
123 else
124 {
125 type = KeyRelease;
126 mask = KeyReleaseMask;
127 }
128
129 WXKeySym xkeysym = wxCharCodeWXToX(keycode);
130 KeyCode xkeycode = XKeysymToKeycode(display, xkeysym);
131 if ( xkeycode == NoSymbol )
132 return false;
133
134 Window focus;
135 int revert;
136 XGetInputFocus(display, &focus, &revert);
137 if (focus == None)
138 return false;
139
140 int mod = 0;
141
142 if (modifiers & wxMOD_SHIFT)
143 mod |= ShiftMask;
144 //Mod1 is alt in the vast majority of cases
145 if (modifiers & wxMOD_ALT)
146 mod |= Mod1Mask;
147 if (modifiers & wxMOD_CMD)
148 mod |= ControlMask;
149
150 XKeyEvent event;
151 event.display = display;
152 event.window = focus;
153 event.root = DefaultRootWindow(event.display);
154 event.subwindow = None;
155 event.time = CurrentTime;
156 event.x = 1;
157 event.y = 1;
158 event.x_root = 1;
159 event.y_root = 1;
160 event.same_screen = True;
161 event.type = type;
162 event.state = mod;
163 event.keycode = xkeycode;
164
165 XSendEvent(event.display, event.window, True, mask, (XEvent*) &event);
166
167 return true;
168 }
169
170 #endif // wxUSE_UIACTIONSIMULATOR