]>
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" | |
a02a5cfc | 19 | |
a02a5cfc | 20 | #include <X11/Xutil.h> |
571d991b VZ |
21 | |
22 | #include "wx/unix/utilsx11.h" | |
23 | ||
24 | namespace | |
25 | { | |
a02a5cfc KO |
26 | |
27 | void SendButtonEvent(int button, bool isDown) | |
28 | { | |
571d991b | 29 | int xbutton; |
a02a5cfc KO |
30 | switch (button) |
31 | { | |
32 | case wxMOUSE_BTN_LEFT: | |
33 | xbutton = 1; | |
34 | break; | |
571d991b | 35 | case wxMOUSE_BTN_MIDDLE: |
a02a5cfc KO |
36 | xbutton = 2; |
37 | break; | |
571d991b | 38 | case wxMOUSE_BTN_RIGHT: |
a02a5cfc KO |
39 | xbutton = 3; |
40 | break; | |
41 | default: | |
42 | wxFAIL_MSG("Unsupported button passed in."); | |
571d991b | 43 | return; |
a02a5cfc KO |
44 | } |
45 | ||
571d991b VZ |
46 | wxX11Display display; |
47 | wxCHECK_RET(display, "No display available!"); | |
9b7e0226 | 48 | |
571d991b | 49 | XEvent event; |
a02a5cfc | 50 | memset(&event, 0x00, sizeof(event)); |
9b7e0226 | 51 | |
571d991b | 52 | event.type = isDown ? ButtonPress : ButtonRelease; |
a02a5cfc KO |
53 | event.xbutton.button = xbutton; |
54 | event.xbutton.same_screen = True; | |
9b7e0226 | 55 | |
571d991b VZ |
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); | |
a02a5cfc | 60 | event.xbutton.subwindow = event.xbutton.window; |
9b7e0226 | 61 | |
a02a5cfc KO |
62 | while (event.xbutton.subwindow) |
63 | { | |
64 | event.xbutton.window = event.xbutton.subwindow; | |
571d991b VZ |
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); | |
a02a5cfc | 69 | } |
9b7e0226 | 70 | |
a02a5cfc | 71 | XSendEvent(display, PointerWindow, True, 0xfff, &event); |
a02a5cfc KO |
72 | } |
73 | ||
571d991b VZ |
74 | } // anonymous namespace |
75 | ||
a02a5cfc KO |
76 | bool wxUIActionSimulator::MouseDown(int button) |
77 | { | |
78 | SendButtonEvent(button, true); | |
79 | return true; | |
80 | } | |
81 | ||
82 | bool wxUIActionSimulator::MouseMove(long x, long y) | |
9b7e0226 | 83 | { |
571d991b | 84 | wxX11Display display; |
a02a5cfc | 85 | wxASSERT_MSG(display, "No display available!"); |
571d991b VZ |
86 | |
87 | Window root = display.DefaultRoot(); | |
a02a5cfc | 88 | XWarpPointer(display, None, root, 0, 0, 0, 0, x, y); |
571d991b | 89 | |
a02a5cfc KO |
90 | return true; |
91 | } | |
92 | ||
93 | bool wxUIActionSimulator::MouseUp(int button) | |
94 | { | |
95 | SendButtonEvent(button, false); | |
96 | return true; | |
97 | } | |
98 | ||
571d991b | 99 | bool wxUIActionSimulator::DoKey(int keycode, int modifiers, bool isDown) |
a02a5cfc | 100 | { |
571d991b VZ |
101 | wxX11Display display; |
102 | wxCHECK_MSG(display, false, "No display available!"); | |
9b7e0226 | 103 | |
571d991b | 104 | int mask, type; |
9b7e0226 | 105 | |
571d991b VZ |
106 | if ( isDown ) |
107 | { | |
108 | type = KeyPress; | |
a02a5cfc KO |
109 | mask = KeyPressMask; |
110 | } | |
571d991b VZ |
111 | else |
112 | { | |
113 | type = KeyRelease; | |
a02a5cfc KO |
114 | mask = KeyReleaseMask; |
115 | } | |
9b7e0226 | 116 | |
571d991b VZ |
117 | WXKeySym xkeysym = wxCharCodeWXToX(keycode); |
118 | KeyCode xkeycode = XKeysymToKeycode(display, xkeysym); | |
119 | if ( xkeycode == NoSymbol ) | |
120 | return false; | |
9b7e0226 | 121 | |
571d991b VZ |
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; | |
9b7e0226 | 152 | |
571d991b | 153 | XSendEvent(event.display, event.window, True, mask, (XEvent*) &event); |
a02a5cfc KO |
154 | |
155 | return true; | |
156 | } | |
157 | ||
9b7e0226 | 158 | #endif // wxUSE_UIACTIONSIMULATOR |