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