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