]>
Commit | Line | Data |
---|---|---|
a02a5cfc KO |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: src/unix/uiactionx11.cpp | |
3 | // Purpose: wxUIActionSimulator implementation | |
571d991b | 4 | // Author: Kevin Ollivier, Steven Lamerton, Vadim Zeitlin |
a02a5cfc KO |
5 | // Modified by: |
6 | // Created: 2010-03-06 | |
b5b208a1 | 7 | // RCS-ID: $Id$ |
a02a5cfc | 8 | // Copyright: (c) Kevin Ollivier |
571d991b VZ |
9 | // (c) 2010 Steven Lamerton |
10 | // (c) 2010 Vadim Zeitlin | |
a02a5cfc KO |
11 | // Licence: wxWindows licence |
12 | ///////////////////////////////////////////////////////////////////////////// | |
13 | ||
9b7e0226 VZ |
14 | #include "wx/defs.h" |
15 | ||
16 | #if wxUSE_UIACTIONSIMULATOR | |
17 | ||
18 | #include "wx/uiaction.h" | |
bdbdb4d1 VZ |
19 | #include "wx/event.h" |
20 | #include "wx/evtloop.h" | |
a02a5cfc | 21 | |
beee38cb | 22 | #include <X11/Xlib.h> |
a02a5cfc | 23 | #include <X11/Xutil.h> |
571d991b VZ |
24 | |
25 | #include "wx/unix/utilsx11.h" | |
26 | ||
27 | namespace | |
28 | { | |
a02a5cfc KO |
29 | |
30 | void SendButtonEvent(int button, bool isDown) | |
31 | { | |
571d991b | 32 | int xbutton; |
a02a5cfc KO |
33 | switch (button) |
34 | { | |
35 | case wxMOUSE_BTN_LEFT: | |
36 | xbutton = 1; | |
37 | break; | |
571d991b | 38 | case wxMOUSE_BTN_MIDDLE: |
a02a5cfc KO |
39 | xbutton = 2; |
40 | break; | |
571d991b | 41 | case wxMOUSE_BTN_RIGHT: |
a02a5cfc KO |
42 | xbutton = 3; |
43 | break; | |
44 | default: | |
45 | wxFAIL_MSG("Unsupported button passed in."); | |
571d991b | 46 | return; |
a02a5cfc KO |
47 | } |
48 | ||
571d991b VZ |
49 | wxX11Display display; |
50 | wxCHECK_RET(display, "No display available!"); | |
9b7e0226 | 51 | |
571d991b | 52 | XEvent event; |
a02a5cfc | 53 | memset(&event, 0x00, sizeof(event)); |
9b7e0226 | 54 | |
571d991b | 55 | event.type = isDown ? ButtonPress : ButtonRelease; |
a02a5cfc KO |
56 | event.xbutton.button = xbutton; |
57 | event.xbutton.same_screen = True; | |
9b7e0226 | 58 | |
571d991b VZ |
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); | |
a02a5cfc | 63 | event.xbutton.subwindow = event.xbutton.window; |
9b7e0226 | 64 | |
a02a5cfc KO |
65 | while (event.xbutton.subwindow) |
66 | { | |
67 | event.xbutton.window = event.xbutton.subwindow; | |
571d991b VZ |
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); | |
a02a5cfc | 72 | } |
9b7e0226 | 73 | |
a02a5cfc | 74 | XSendEvent(display, PointerWindow, True, 0xfff, &event); |
a02a5cfc KO |
75 | } |
76 | ||
571d991b VZ |
77 | } // anonymous namespace |
78 | ||
a02a5cfc KO |
79 | bool wxUIActionSimulator::MouseDown(int button) |
80 | { | |
81 | SendButtonEvent(button, true); | |
82 | return true; | |
83 | } | |
84 | ||
85 | bool wxUIActionSimulator::MouseMove(long x, long y) | |
9b7e0226 | 86 | { |
571d991b | 87 | wxX11Display display; |
a02a5cfc | 88 | wxASSERT_MSG(display, "No display available!"); |
571d991b VZ |
89 | |
90 | Window root = display.DefaultRoot(); | |
a02a5cfc | 91 | XWarpPointer(display, None, root, 0, 0, 0, 0, x, y); |
571d991b | 92 | |
bdbdb4d1 VZ |
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 | ||
a02a5cfc KO |
102 | return true; |
103 | } | |
104 | ||
105 | bool wxUIActionSimulator::MouseUp(int button) | |
106 | { | |
107 | SendButtonEvent(button, false); | |
108 | return true; | |
109 | } | |
110 | ||
571d991b | 111 | bool wxUIActionSimulator::DoKey(int keycode, int modifiers, bool isDown) |
a02a5cfc | 112 | { |
571d991b VZ |
113 | wxX11Display display; |
114 | wxCHECK_MSG(display, false, "No display available!"); | |
9b7e0226 | 115 | |
571d991b | 116 | int mask, type; |
9b7e0226 | 117 | |
571d991b VZ |
118 | if ( isDown ) |
119 | { | |
120 | type = KeyPress; | |
a02a5cfc KO |
121 | mask = KeyPressMask; |
122 | } | |
571d991b VZ |
123 | else |
124 | { | |
125 | type = KeyRelease; | |
a02a5cfc KO |
126 | mask = KeyReleaseMask; |
127 | } | |
9b7e0226 | 128 | |
571d991b VZ |
129 | WXKeySym xkeysym = wxCharCodeWXToX(keycode); |
130 | KeyCode xkeycode = XKeysymToKeycode(display, xkeysym); | |
131 | if ( xkeycode == NoSymbol ) | |
132 | return false; | |
9b7e0226 | 133 | |
571d991b VZ |
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; | |
9b7e0226 | 164 | |
571d991b | 165 | XSendEvent(event.display, event.window, True, mask, (XEvent*) &event); |
a02a5cfc KO |
166 | |
167 | return true; | |
168 | } | |
169 | ||
9b7e0226 | 170 | #endif // wxUSE_UIACTIONSIMULATOR |