| 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 #include "base/callback.h" | 10 #include "base/callback.h" |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 63 "marker_event", | 63 "marker_event", |
| 64 False); | 64 False); |
| 65 } | 65 } |
| 66 | 66 |
| 67 // Returns true when the event is a marker event. | 67 // Returns true when the event is a marker event. |
| 68 bool Matcher(const base::NativeEvent& event) { | 68 bool Matcher(const base::NativeEvent& event) { |
| 69 return event->xany.type == ClientMessage && | 69 return event->xany.type == ClientMessage && |
| 70 event->xclient.message_type == MarkerEventAtom(); | 70 event->xclient.message_type == MarkerEventAtom(); |
| 71 } | 71 } |
| 72 | 72 |
| 73 void RunClosureAfterEvents(const base::Closure closure) { | |
| 74 if (closure.is_null()) | |
| 75 return; | |
| 76 static XEvent* marker_event = NULL; | |
| 77 if (!marker_event) { | |
| 78 marker_event = new XEvent(); | |
| 79 marker_event->xclient.type = ClientMessage; | |
| 80 marker_event->xclient.display = NULL; | |
| 81 marker_event->xclient.window = None; | |
| 82 marker_event->xclient.format = 8; | |
| 83 } | |
| 84 marker_event->xclient.message_type = MarkerEventAtom(); | |
| 85 aura::Desktop::GetInstance()->PostNativeEvent(marker_event); | |
| 86 new EventWaiter(closure, &Matcher); | |
| 87 } | |
| 88 | |
| 89 } // namespace | 73 } // namespace |
| 90 | 74 |
| 91 namespace ui_controls { | 75 namespace ui_controls { |
| 92 | 76 |
| 93 bool SendKeyPress(gfx::NativeWindow window, | 77 bool SendKeyPress(gfx::NativeWindow window, |
| 94 ui::KeyboardCode key, | 78 ui::KeyboardCode key, |
| 95 bool control, | 79 bool control, |
| 96 bool shift, | 80 bool shift, |
| 97 bool alt, | 81 bool alt, |
| 98 bool command) { | 82 bool command) { |
| (...skipping 27 matching lines...) Expand all Loading... |
| 126 const base::Closure& closure) { | 110 const base::Closure& closure) { |
| 127 DCHECK(!command); // No command key on Aura | 111 DCHECK(!command); // No command key on Aura |
| 128 XEvent xevent = {0}; | 112 XEvent xevent = {0}; |
| 129 xevent.xkey.type = KeyPress; | 113 xevent.xkey.type = KeyPress; |
| 130 if (control) | 114 if (control) |
| 131 SetMaskAndKeycodeThenSend(&xevent, ControlMask, XK_Control_L); | 115 SetMaskAndKeycodeThenSend(&xevent, ControlMask, XK_Control_L); |
| 132 if (shift) | 116 if (shift) |
| 133 SetMaskAndKeycodeThenSend(&xevent, ShiftMask, XK_Shift_L); | 117 SetMaskAndKeycodeThenSend(&xevent, ShiftMask, XK_Shift_L); |
| 134 if (alt) | 118 if (alt) |
| 135 SetMaskAndKeycodeThenSend(&xevent, Mod1Mask, XK_Alt_L); | 119 SetMaskAndKeycodeThenSend(&xevent, Mod1Mask, XK_Alt_L); |
| 136 xevent.xkey.keycode = | 120 xevent.xkey.keycode = ui::XKeysymForWindowsKeyCode(key, shift); |
| 137 XKeysymToKeycode(base::MessagePumpX::GetDefaultXDisplay(), | |
| 138 ui::XKeysymForWindowsKeyCode(key, shift)); | |
| 139 aura::Desktop::GetInstance()->PostNativeEvent(&xevent); | 121 aura::Desktop::GetInstance()->PostNativeEvent(&xevent); |
| 140 | 122 |
| 141 // Send key release events. | 123 // Send key release events. |
| 142 xevent.xkey.type = KeyRelease; | 124 xevent.xkey.type = KeyRelease; |
| 143 aura::Desktop::GetInstance()->PostNativeEvent(&xevent); | 125 aura::Desktop::GetInstance()->PostNativeEvent(&xevent); |
| 144 if (alt) | 126 if (alt) |
| 145 SetKeycodeAndSendThenUnmask(&xevent, Mod1Mask, XK_Alt_L); | 127 SetKeycodeAndSendThenUnmask(&xevent, Mod1Mask, XK_Alt_L); |
| 146 if (shift) | 128 if (shift) |
| 147 SetKeycodeAndSendThenUnmask(&xevent, ShiftMask, XK_Shift_L); | 129 SetKeycodeAndSendThenUnmask(&xevent, ShiftMask, XK_Shift_L); |
| 148 if (control) | 130 if (control) |
| 149 SetKeycodeAndSendThenUnmask(&xevent, ControlMask, XK_Control_L); | 131 SetKeycodeAndSendThenUnmask(&xevent, ControlMask, XK_Control_L); |
| 150 DCHECK(!xevent.xkey.state); | 132 DCHECK(!xevent.xkey.state); |
| 151 RunClosureAfterEvents(closure); | 133 RunClosureAfterAllPendingUIEvents(closure); |
| 152 return true; | 134 return true; |
| 153 } | 135 } |
| 154 | 136 |
| 155 bool SendMouseMove(long x, long y) { | 137 bool SendMouseMove(long x, long y) { |
| 156 return SendMouseMoveNotifyWhenDone(x, y, base::Closure()); | 138 return SendMouseMoveNotifyWhenDone(x, y, base::Closure()); |
| 157 } | 139 } |
| 158 | 140 |
| 159 bool SendMouseMoveNotifyWhenDone(long x, long y, const base::Closure& closure) { | 141 bool SendMouseMoveNotifyWhenDone(long x, long y, const base::Closure& closure) { |
| 160 XEvent xevent = {0}; | 142 XEvent xevent = {0}; |
| 161 XMotionEvent* xmotion = &xevent.xmotion; | 143 XMotionEvent* xmotion = &xevent.xmotion; |
| 162 xmotion->type = MotionNotify; | 144 xmotion->type = MotionNotify; |
| 163 g_current_x = xmotion->x = x; | 145 g_current_x = xmotion->x = x; |
| 164 g_current_y = xmotion->y = y; | 146 g_current_y = xmotion->y = y; |
| 165 xmotion->same_screen = True; | 147 xmotion->same_screen = True; |
| 166 // Desktop will take care of other necessary fields. | 148 // Desktop will take care of other necessary fields. |
| 167 aura::Desktop::GetInstance()->PostNativeEvent(&xevent); | 149 aura::Desktop::GetInstance()->PostNativeEvent(&xevent); |
| 168 RunClosureAfterEvents(closure); | 150 RunClosureAfterAllPendingUIEvents(closure); |
| 169 return false; | 151 return false; |
| 170 } | 152 } |
| 171 | 153 |
| 172 bool SendMouseEvents(MouseButton type, int state) { | 154 bool SendMouseEvents(MouseButton type, int state) { |
| 173 return SendMouseEventsNotifyWhenDone(type, state, base::Closure()); | 155 return SendMouseEventsNotifyWhenDone(type, state, base::Closure()); |
| 174 } | 156 } |
| 175 | 157 |
| 176 bool SendMouseEventsNotifyWhenDone(MouseButton type, | 158 bool SendMouseEventsNotifyWhenDone(MouseButton type, |
| 177 int state, | 159 int state, |
| 178 const base::Closure& closure) { | 160 const base::Closure& closure) { |
| (...skipping 22 matching lines...) Expand all Loading... |
| 201 | 183 |
| 202 aura::Desktop* desktop = aura::Desktop::GetInstance(); | 184 aura::Desktop* desktop = aura::Desktop::GetInstance(); |
| 203 if (state & DOWN) { | 185 if (state & DOWN) { |
| 204 xevent.xbutton.type = ButtonPress; | 186 xevent.xbutton.type = ButtonPress; |
| 205 desktop->PostNativeEvent(&xevent); | 187 desktop->PostNativeEvent(&xevent); |
| 206 } | 188 } |
| 207 if (state & UP) { | 189 if (state & UP) { |
| 208 xevent.xbutton.type = ButtonRelease; | 190 xevent.xbutton.type = ButtonRelease; |
| 209 desktop->PostNativeEvent(&xevent); | 191 desktop->PostNativeEvent(&xevent); |
| 210 } | 192 } |
| 211 RunClosureAfterEvents(closure); | 193 RunClosureAfterAllPendingUIEvents(closure); |
| 212 return false; | 194 return false; |
| 213 } | 195 } |
| 214 | 196 |
| 215 bool SendMouseClick(MouseButton type) { | 197 bool SendMouseClick(MouseButton type) { |
| 216 return SendMouseEvents(type, UP | DOWN); | 198 return SendMouseEvents(type, UP | DOWN); |
| 217 } | 199 } |
| 218 | 200 |
| 219 void MoveMouseToCenterAndPress(views::View* view, MouseButton button, | 201 void MoveMouseToCenterAndPress(views::View* view, MouseButton button, |
| 220 int state, const base::Closure& closure) { | 202 int state, const base::Closure& closure) { |
| 221 DCHECK(view); | 203 DCHECK(view); |
| 222 DCHECK(view->GetWidget()); | 204 DCHECK(view->GetWidget()); |
| 223 gfx::Point view_center(view->width() / 2, view->height() / 2); | 205 gfx::Point view_center(view->width() / 2, view->height() / 2); |
| 224 views::View::ConvertPointToScreen(view, &view_center); | 206 views::View::ConvertPointToScreen(view, &view_center); |
| 225 SendMouseMove(view_center.x(), view_center.y()); | 207 SendMouseMove(view_center.x(), view_center.y()); |
| 226 SendMouseEventsNotifyWhenDone(button, state, closure); | 208 SendMouseEventsNotifyWhenDone(button, state, closure); |
| 227 } | 209 } |
| 228 | 210 |
| 211 void RunClosureAfterAllPendingUIEvents(const base::Closure& closure) { |
| 212 if (closure.is_null()) |
| 213 return; |
| 214 static XEvent* marker_event = NULL; |
| 215 if (!marker_event) { |
| 216 marker_event = new XEvent(); |
| 217 marker_event->xclient.type = ClientMessage; |
| 218 marker_event->xclient.display = NULL; |
| 219 marker_event->xclient.window = None; |
| 220 marker_event->xclient.format = 8; |
| 221 } |
| 222 marker_event->xclient.message_type = MarkerEventAtom(); |
| 223 aura::Desktop::GetInstance()->PostNativeEvent(marker_event); |
| 224 new EventWaiter(closure, &Matcher); |
| 225 } |
| 226 |
| 229 } // namespace ui_controls | 227 } // namespace ui_controls |
| OLD | NEW |