]> git.saurik.com Git - wxWidgets.git/blob - src/unix/uiactionx11.cpp
Avoid overflowing the wake up when handling events in Unix console apps.
[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
20 #include <X11/Xlib.h>
21 #include <X11/Xutil.h>
22
23 #include "wx/unix/utilsx11.h"
24
25 namespace
26 {
27
28 void SendButtonEvent(int button, bool isDown)
29 {
30 int xbutton;
31 switch (button)
32 {
33 case wxMOUSE_BTN_LEFT:
34 xbutton = 1;
35 break;
36 case wxMOUSE_BTN_MIDDLE:
37 xbutton = 2;
38 break;
39 case wxMOUSE_BTN_RIGHT:
40 xbutton = 3;
41 break;
42 default:
43 wxFAIL_MSG("Unsupported button passed in.");
44 return;
45 }
46
47 wxX11Display display;
48 wxCHECK_RET(display, "No display available!");
49
50 XEvent event;
51 memset(&event, 0x00, sizeof(event));
52
53 event.type = isDown ? ButtonPress : ButtonRelease;
54 event.xbutton.button = xbutton;
55 event.xbutton.same_screen = True;
56
57 XQueryPointer(display, display.DefaultRoot(),
58 &event.xbutton.root, &event.xbutton.window,
59 &event.xbutton.x_root, &event.xbutton.y_root,
60 &event.xbutton.x, &event.xbutton.y, &event.xbutton.state);
61 event.xbutton.subwindow = event.xbutton.window;
62
63 while (event.xbutton.subwindow)
64 {
65 event.xbutton.window = event.xbutton.subwindow;
66 XQueryPointer(display, event.xbutton.window,
67 &event.xbutton.root, &event.xbutton.subwindow,
68 &event.xbutton.x_root, &event.xbutton.y_root,
69 &event.xbutton.x, &event.xbutton.y, &event.xbutton.state);
70 }
71
72 XSendEvent(display, PointerWindow, True, 0xfff, &event);
73 }
74
75 } // anonymous namespace
76
77 bool wxUIActionSimulator::MouseDown(int button)
78 {
79 SendButtonEvent(button, true);
80 return true;
81 }
82
83 bool wxUIActionSimulator::MouseMove(long x, long y)
84 {
85 wxX11Display display;
86 wxASSERT_MSG(display, "No display available!");
87
88 Window root = display.DefaultRoot();
89 XWarpPointer(display, None, root, 0, 0, 0, 0, x, y);
90
91 return true;
92 }
93
94 bool wxUIActionSimulator::MouseUp(int button)
95 {
96 SendButtonEvent(button, false);
97 return true;
98 }
99
100 bool wxUIActionSimulator::DoKey(int keycode, int modifiers, bool isDown)
101 {
102 wxX11Display display;
103 wxCHECK_MSG(display, false, "No display available!");
104
105 int mask, type;
106
107 if ( isDown )
108 {
109 type = KeyPress;
110 mask = KeyPressMask;
111 }
112 else
113 {
114 type = KeyRelease;
115 mask = KeyReleaseMask;
116 }
117
118 WXKeySym xkeysym = wxCharCodeWXToX(keycode);
119 KeyCode xkeycode = XKeysymToKeycode(display, xkeysym);
120 if ( xkeycode == NoSymbol )
121 return false;
122
123 Window focus;
124 int revert;
125 XGetInputFocus(display, &focus, &revert);
126 if (focus == None)
127 return false;
128
129 int mod = 0;
130
131 if (modifiers & wxMOD_SHIFT)
132 mod |= ShiftMask;
133 //Mod1 is alt in the vast majority of cases
134 if (modifiers & wxMOD_ALT)
135 mod |= Mod1Mask;
136 if (modifiers & wxMOD_CMD)
137 mod |= ControlMask;
138
139 XKeyEvent event;
140 event.display = display;
141 event.window = focus;
142 event.root = DefaultRootWindow(event.display);
143 event.subwindow = None;
144 event.time = CurrentTime;
145 event.x = 1;
146 event.y = 1;
147 event.x_root = 1;
148 event.y_root = 1;
149 event.same_screen = True;
150 event.type = type;
151 event.state = mod;
152 event.keycode = xkeycode;
153
154 XSendEvent(event.display, event.window, True, mask, (XEvent*) &event);
155
156 return true;
157 }
158
159 #endif // wxUSE_UIACTIONSIMULATOR