OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/automation/ui_controls.h" | 5 #include "chrome/browser/automation/ui_controls.h" |
6 | 6 |
7 #include <X11/keysym.h> | 7 #include <X11/keysym.h> |
8 #include <X11/Xlib.h> | 8 #include <X11/Xlib.h> |
9 | 9 |
| 10 // X macro fail. |
| 11 #if defined(RootWindow) |
| 12 #undef RootWindow |
| 13 #endif |
| 14 |
10 #include "base/callback.h" | 15 #include "base/callback.h" |
11 #include "base/logging.h" | 16 #include "base/logging.h" |
12 #include "base/message_pump_x.h" | 17 #include "base/message_pump_x.h" |
13 #include "chrome/browser/automation/ui_controls_internal.h" | 18 #include "chrome/browser/automation/ui_controls_internal.h" |
14 #include "ui/aura/desktop.h" | 19 #include "ui/aura/root_window.h" |
15 #include "ui/base/keycodes/keyboard_code_conversion_x.h" | 20 #include "ui/base/keycodes/keyboard_code_conversion_x.h" |
16 #include "ui/views/view.h" | 21 #include "ui/views/view.h" |
17 | 22 |
18 namespace { | 23 namespace { |
19 | 24 |
20 // Event waiter executes the specified closure|when a matching event | 25 // Event waiter executes the specified closure|when a matching event |
21 // is found. | 26 // is found. |
22 // TODO(oshima): Move this to base. | 27 // TODO(oshima): Move this to base. |
23 class EventWaiter : public MessageLoopForUI::Observer { | 28 class EventWaiter : public MessageLoopForUI::Observer { |
24 public: | 29 public: |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
83 DCHECK(!command); // No command key on Aura | 88 DCHECK(!command); // No command key on Aura |
84 return SendKeyPressNotifyWhenDone( | 89 return SendKeyPressNotifyWhenDone( |
85 window, key, control, shift, alt, command, base::Closure()); | 90 window, key, control, shift, alt, command, base::Closure()); |
86 } | 91 } |
87 | 92 |
88 void SetMaskAndKeycodeThenSend(XEvent* xevent, | 93 void SetMaskAndKeycodeThenSend(XEvent* xevent, |
89 unsigned int mask, | 94 unsigned int mask, |
90 unsigned int keycode) { | 95 unsigned int keycode) { |
91 xevent->xkey.state |= mask; | 96 xevent->xkey.state |= mask; |
92 xevent->xkey.keycode = keycode; | 97 xevent->xkey.keycode = keycode; |
93 aura::Desktop::GetInstance()->PostNativeEvent(xevent); | 98 aura::RootWindow::GetInstance()->PostNativeEvent(xevent); |
94 } | 99 } |
95 | 100 |
96 void SetKeycodeAndSendThenUnmask(XEvent* xevent, | 101 void SetKeycodeAndSendThenUnmask(XEvent* xevent, |
97 unsigned int mask, | 102 unsigned int mask, |
98 unsigned int keycode) { | 103 unsigned int keycode) { |
99 xevent->xkey.keycode = keycode; | 104 xevent->xkey.keycode = keycode; |
100 aura::Desktop::GetInstance()->PostNativeEvent(xevent); | 105 aura::RootWindow::GetInstance()->PostNativeEvent(xevent); |
101 xevent->xkey.state ^= mask; | 106 xevent->xkey.state ^= mask; |
102 } | 107 } |
103 | 108 |
104 bool SendKeyPressNotifyWhenDone(gfx::NativeWindow window, | 109 bool SendKeyPressNotifyWhenDone(gfx::NativeWindow window, |
105 ui::KeyboardCode key, | 110 ui::KeyboardCode key, |
106 bool control, | 111 bool control, |
107 bool shift, | 112 bool shift, |
108 bool alt, | 113 bool alt, |
109 bool command, | 114 bool command, |
110 const base::Closure& closure) { | 115 const base::Closure& closure) { |
111 DCHECK(!command); // No command key on Aura | 116 DCHECK(!command); // No command key on Aura |
112 XEvent xevent = {0}; | 117 XEvent xevent = {0}; |
113 xevent.xkey.type = KeyPress; | 118 xevent.xkey.type = KeyPress; |
114 if (control) | 119 if (control) |
115 SetMaskAndKeycodeThenSend(&xevent, ControlMask, XK_Control_L); | 120 SetMaskAndKeycodeThenSend(&xevent, ControlMask, XK_Control_L); |
116 if (shift) | 121 if (shift) |
117 SetMaskAndKeycodeThenSend(&xevent, ShiftMask, XK_Shift_L); | 122 SetMaskAndKeycodeThenSend(&xevent, ShiftMask, XK_Shift_L); |
118 if (alt) | 123 if (alt) |
119 SetMaskAndKeycodeThenSend(&xevent, Mod1Mask, XK_Alt_L); | 124 SetMaskAndKeycodeThenSend(&xevent, Mod1Mask, XK_Alt_L); |
120 xevent.xkey.keycode = ui::XKeysymForWindowsKeyCode(key, shift); | 125 xevent.xkey.keycode = ui::XKeysymForWindowsKeyCode(key, shift); |
121 aura::Desktop::GetInstance()->PostNativeEvent(&xevent); | 126 aura::RootWindow::GetInstance()->PostNativeEvent(&xevent); |
122 | 127 |
123 // Send key release events. | 128 // Send key release events. |
124 xevent.xkey.type = KeyRelease; | 129 xevent.xkey.type = KeyRelease; |
125 aura::Desktop::GetInstance()->PostNativeEvent(&xevent); | 130 aura::RootWindow::GetInstance()->PostNativeEvent(&xevent); |
126 if (alt) | 131 if (alt) |
127 SetKeycodeAndSendThenUnmask(&xevent, Mod1Mask, XK_Alt_L); | 132 SetKeycodeAndSendThenUnmask(&xevent, Mod1Mask, XK_Alt_L); |
128 if (shift) | 133 if (shift) |
129 SetKeycodeAndSendThenUnmask(&xevent, ShiftMask, XK_Shift_L); | 134 SetKeycodeAndSendThenUnmask(&xevent, ShiftMask, XK_Shift_L); |
130 if (control) | 135 if (control) |
131 SetKeycodeAndSendThenUnmask(&xevent, ControlMask, XK_Control_L); | 136 SetKeycodeAndSendThenUnmask(&xevent, ControlMask, XK_Control_L); |
132 DCHECK(!xevent.xkey.state); | 137 DCHECK(!xevent.xkey.state); |
133 RunClosureAfterAllPendingUIEvents(closure); | 138 RunClosureAfterAllPendingUIEvents(closure); |
134 return true; | 139 return true; |
135 } | 140 } |
136 | 141 |
137 bool SendMouseMove(long x, long y) { | 142 bool SendMouseMove(long x, long y) { |
138 return SendMouseMoveNotifyWhenDone(x, y, base::Closure()); | 143 return SendMouseMoveNotifyWhenDone(x, y, base::Closure()); |
139 } | 144 } |
140 | 145 |
141 bool SendMouseMoveNotifyWhenDone(long x, long y, const base::Closure& closure) { | 146 bool SendMouseMoveNotifyWhenDone(long x, long y, const base::Closure& closure) { |
142 XEvent xevent = {0}; | 147 XEvent xevent = {0}; |
143 XMotionEvent* xmotion = &xevent.xmotion; | 148 XMotionEvent* xmotion = &xevent.xmotion; |
144 xmotion->type = MotionNotify; | 149 xmotion->type = MotionNotify; |
145 g_current_x = xmotion->x = x; | 150 g_current_x = xmotion->x = x; |
146 g_current_y = xmotion->y = y; | 151 g_current_y = xmotion->y = y; |
147 xmotion->same_screen = True; | 152 xmotion->same_screen = True; |
148 // Desktop will take care of other necessary fields. | 153 // RootWindow will take care of other necessary fields. |
149 aura::Desktop::GetInstance()->PostNativeEvent(&xevent); | 154 aura::RootWindow::GetInstance()->PostNativeEvent(&xevent); |
150 RunClosureAfterAllPendingUIEvents(closure); | 155 RunClosureAfterAllPendingUIEvents(closure); |
151 return false; | 156 return false; |
152 } | 157 } |
153 | 158 |
154 bool SendMouseEvents(MouseButton type, int state) { | 159 bool SendMouseEvents(MouseButton type, int state) { |
155 return SendMouseEventsNotifyWhenDone(type, state, base::Closure()); | 160 return SendMouseEventsNotifyWhenDone(type, state, base::Closure()); |
156 } | 161 } |
157 | 162 |
158 bool SendMouseEventsNotifyWhenDone(MouseButton type, | 163 bool SendMouseEventsNotifyWhenDone(MouseButton type, |
159 int state, | 164 int state, |
(...skipping 12 matching lines...) Expand all Loading... |
172 break; | 177 break; |
173 case MIDDLE: | 178 case MIDDLE: |
174 xbutton->button = Button2; | 179 xbutton->button = Button2; |
175 xbutton->state = Button2Mask; | 180 xbutton->state = Button2Mask; |
176 break; | 181 break; |
177 case RIGHT: | 182 case RIGHT: |
178 xbutton->button = Button3; | 183 xbutton->button = Button3; |
179 xbutton->state = Button3Mask; | 184 xbutton->state = Button3Mask; |
180 break; | 185 break; |
181 } | 186 } |
182 // Desktop will take care of other necessary fields. | 187 // RootWindow will take care of other necessary fields. |
183 | 188 |
184 aura::Desktop* desktop = aura::Desktop::GetInstance(); | 189 aura::RootWindow* root_window = aura::RootWindow::GetInstance(); |
185 if (state & DOWN) { | 190 if (state & DOWN) { |
186 xevent.xbutton.type = ButtonPress; | 191 xevent.xbutton.type = ButtonPress; |
187 desktop->PostNativeEvent(&xevent); | 192 root_window->PostNativeEvent(&xevent); |
188 } | 193 } |
189 if (state & UP) { | 194 if (state & UP) { |
190 xevent.xbutton.type = ButtonRelease; | 195 xevent.xbutton.type = ButtonRelease; |
191 desktop->PostNativeEvent(&xevent); | 196 root_window->PostNativeEvent(&xevent); |
192 } | 197 } |
193 RunClosureAfterAllPendingUIEvents(closure); | 198 RunClosureAfterAllPendingUIEvents(closure); |
194 return false; | 199 return false; |
195 } | 200 } |
196 | 201 |
197 bool SendMouseClick(MouseButton type) { | 202 bool SendMouseClick(MouseButton type) { |
198 return SendMouseEvents(type, UP | DOWN); | 203 return SendMouseEvents(type, UP | DOWN); |
199 } | 204 } |
200 | 205 |
201 void MoveMouseToCenterAndPress(views::View* view, MouseButton button, | 206 void MoveMouseToCenterAndPress(views::View* view, MouseButton button, |
(...skipping 11 matching lines...) Expand all Loading... |
213 return; | 218 return; |
214 static XEvent* marker_event = NULL; | 219 static XEvent* marker_event = NULL; |
215 if (!marker_event) { | 220 if (!marker_event) { |
216 marker_event = new XEvent(); | 221 marker_event = new XEvent(); |
217 marker_event->xclient.type = ClientMessage; | 222 marker_event->xclient.type = ClientMessage; |
218 marker_event->xclient.display = NULL; | 223 marker_event->xclient.display = NULL; |
219 marker_event->xclient.window = None; | 224 marker_event->xclient.window = None; |
220 marker_event->xclient.format = 8; | 225 marker_event->xclient.format = 8; |
221 } | 226 } |
222 marker_event->xclient.message_type = MarkerEventAtom(); | 227 marker_event->xclient.message_type = MarkerEventAtom(); |
223 aura::Desktop::GetInstance()->PostNativeEvent(marker_event); | 228 aura::RootWindow::GetInstance()->PostNativeEvent(marker_event); |
224 new EventWaiter(closure, &Matcher); | 229 new EventWaiter(closure, &Matcher); |
225 } | 230 } |
226 | 231 |
227 } // namespace ui_controls | 232 } // namespace ui_controls |
OLD | NEW |