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 "ui/aura/desktop_host.h" | 5 #include "ui/aura/desktop_host.h" |
6 | 6 |
7 #include <X11/cursorfont.h> | 7 #include <X11/cursorfont.h> |
8 #include <X11/Xlib.h> | 8 #include <X11/Xlib.h> |
9 | 9 |
10 // Get rid of a macro from Xlib.h that conflicts with Aura's RootWindow class. | 10 // Get rid of a macro from Xlib.h that conflicts with Aura's RootWindow class. |
11 #undef RootWindow | 11 #undef RootWindow |
12 | 12 |
13 #include <algorithm> | 13 #include <algorithm> |
14 | 14 |
| 15 #include "base/event_types.h" |
15 #include "base/message_loop.h" | 16 #include "base/message_loop.h" |
16 #include "base/message_pump_x.h" | 17 #include "base/message_pump_x.h" |
17 #include "ui/aura/cursor.h" | 18 #include "ui/aura/cursor.h" |
18 #include "ui/aura/desktop.h" | 19 #include "ui/aura/desktop.h" |
19 #include "ui/aura/event.h" | 20 #include "ui/aura/event.h" |
| 21 #include "ui/base/ime/input_method_delegate.h" |
20 #include "ui/base/keycodes/keyboard_codes.h" | 22 #include "ui/base/keycodes/keyboard_codes.h" |
21 #include "ui/base/touch/touch_factory.h" | 23 #include "ui/base/touch/touch_factory.h" |
22 #include "ui/base/x/x11_util.h" | 24 #include "ui/base/x/x11_util.h" |
23 #include "ui/gfx/compositor/layer.h" | 25 #include "ui/gfx/compositor/layer.h" |
24 | 26 |
25 #include <X11/cursorfont.h> | 27 #include <X11/cursorfont.h> |
26 #include <X11/extensions/XInput2.h> | 28 #include <X11/extensions/XInput2.h> |
27 #include <X11/Xlib.h> | 29 #include <X11/Xlib.h> |
28 | 30 |
| 31 #if defined(HAVE_IBUS) |
| 32 #include "ui/base/ime/input_method_ibus.h" |
| 33 #else |
| 34 #include "ui/base/ime/mock_input_method.h" |
| 35 #endif |
| 36 |
29 using std::max; | 37 using std::max; |
30 using std::min; | 38 using std::min; |
31 | 39 |
32 namespace aura { | 40 namespace aura { |
33 | 41 |
34 namespace { | 42 namespace { |
35 | 43 |
36 // Returns X font cursor shape from an Aura cursor. | 44 // Returns X font cursor shape from an Aura cursor. |
37 int CursorShapeFromNative(gfx::NativeCursor native_cursor) { | 45 int CursorShapeFromNative(gfx::NativeCursor native_cursor) { |
38 switch (native_cursor) { | 46 switch (native_cursor) { |
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
174 } else { | 182 } else { |
175 // This isn't an event we want so free its cookie data. | 183 // This isn't an event we want so free its cookie data. |
176 XFreeEventData(display, &next_event.xcookie); | 184 XFreeEventData(display, &next_event.xcookie); |
177 } | 185 } |
178 } | 186 } |
179 break; | 187 break; |
180 } | 188 } |
181 return num_coalesed; | 189 return num_coalesed; |
182 } | 190 } |
183 | 191 |
184 // We emulate Windows' WM_KEYDOWN and WM_CHAR messages. WM_CHAR events are only | 192 class DesktopHostLinux : public DesktopHost, |
185 // generated for certain keys; see | 193 public ui::internal::InputMethodDelegate { |
186 // http://msdn.microsoft.com/en-us/library/windows/desktop/ms646268.aspx. | |
187 bool ShouldSendCharEventForKeyboardCode(ui::KeyboardCode keycode) { | |
188 if ((keycode >= ui::VKEY_0 && keycode <= ui::VKEY_9) || | |
189 (keycode >= ui::VKEY_A && keycode <= ui::VKEY_Z) || | |
190 (keycode >= ui::VKEY_NUMPAD0 && keycode <= ui::VKEY_NUMPAD9)) { | |
191 return true; | |
192 } | |
193 | |
194 switch (keycode) { | |
195 case ui::VKEY_BACK: | |
196 case ui::VKEY_RETURN: | |
197 case ui::VKEY_ESCAPE: | |
198 case ui::VKEY_SPACE: | |
199 case ui::VKEY_TAB: | |
200 // In addition to the keys listed at MSDN, we include other | |
201 // graphic-character and numpad keys. | |
202 case ui::VKEY_MULTIPLY: | |
203 case ui::VKEY_ADD: | |
204 case ui::VKEY_SUBTRACT: | |
205 case ui::VKEY_DECIMAL: | |
206 case ui::VKEY_DIVIDE: | |
207 case ui::VKEY_OEM_1: | |
208 case ui::VKEY_OEM_2: | |
209 case ui::VKEY_OEM_3: | |
210 case ui::VKEY_OEM_4: | |
211 case ui::VKEY_OEM_5: | |
212 case ui::VKEY_OEM_6: | |
213 case ui::VKEY_OEM_7: | |
214 case ui::VKEY_OEM_102: | |
215 case ui::VKEY_OEM_PLUS: | |
216 case ui::VKEY_OEM_COMMA: | |
217 case ui::VKEY_OEM_MINUS: | |
218 case ui::VKEY_OEM_PERIOD: | |
219 return true; | |
220 default: | |
221 return false; | |
222 } | |
223 } | |
224 | |
225 class DesktopHostLinux : public DesktopHost { | |
226 public: | 194 public: |
227 explicit DesktopHostLinux(const gfx::Rect& bounds); | 195 explicit DesktopHostLinux(const gfx::Rect& bounds); |
228 virtual ~DesktopHostLinux(); | 196 virtual ~DesktopHostLinux(); |
229 | 197 |
230 private: | 198 private: |
231 // base::MessageLoop::Dispatcher Override. | 199 // base::MessageLoop::Dispatcher Override. |
232 virtual DispatchStatus Dispatch(XEvent* xev) OVERRIDE; | 200 virtual DispatchStatus Dispatch(XEvent* xev) OVERRIDE; |
233 | 201 |
234 // DesktopHost Overrides. | 202 // DesktopHost Overrides. |
235 virtual void SetDesktop(Desktop* desktop) OVERRIDE; | 203 virtual void SetDesktop(Desktop* desktop) OVERRIDE; |
236 virtual gfx::AcceleratedWidget GetAcceleratedWidget() OVERRIDE; | 204 virtual gfx::AcceleratedWidget GetAcceleratedWidget() OVERRIDE; |
237 virtual void Show() OVERRIDE; | 205 virtual void Show() OVERRIDE; |
238 virtual void ToggleFullScreen() OVERRIDE; | 206 virtual void ToggleFullScreen() OVERRIDE; |
239 virtual gfx::Size GetSize() const OVERRIDE; | 207 virtual gfx::Size GetSize() const OVERRIDE; |
240 virtual void SetSize(const gfx::Size& size) OVERRIDE; | 208 virtual void SetSize(const gfx::Size& size) OVERRIDE; |
241 virtual gfx::Point GetLocationOnNativeScreen() const OVERRIDE; | 209 virtual gfx::Point GetLocationOnNativeScreen() const OVERRIDE; |
242 virtual void SetCursor(gfx::NativeCursor cursor_type) OVERRIDE; | 210 virtual void SetCursor(gfx::NativeCursor cursor_type) OVERRIDE; |
243 virtual gfx::Point QueryMouseLocation() OVERRIDE; | 211 virtual gfx::Point QueryMouseLocation() OVERRIDE; |
244 virtual void PostNativeEvent(const base::NativeEvent& event) OVERRIDE; | 212 virtual void PostNativeEvent(const base::NativeEvent& event) OVERRIDE; |
| 213 virtual ui::InputMethod* GetInputMethod() OVERRIDE; |
| 214 |
| 215 // ui::internal::InputMethodDelegate Override. |
| 216 virtual void DispatchKeyEventPostIME(const base::NativeEvent& event) OVERRIDE; |
| 217 virtual void DispatchFabricatedKeyEventPostIME( |
| 218 ui::EventType type, ui::KeyboardCode key_code, int flags) OVERRIDE; |
245 | 219 |
246 // Returns true if there's an X window manager present... in most cases. Some | 220 // Returns true if there's an X window manager present... in most cases. Some |
247 // window managers (notably, ion3) don't implement enough of ICCCM for us to | 221 // window managers (notably, ion3) don't implement enough of ICCCM for us to |
248 // detect that they're there. | 222 // detect that they're there. |
249 bool IsWindowManagerPresent(); | 223 bool IsWindowManagerPresent(); |
250 | 224 |
251 Desktop* desktop_; | 225 Desktop* desktop_; |
252 | 226 |
253 // The display and the native X window hosting the desktop. | 227 // The display and the native X window hosting the desktop. |
254 Display* xdisplay_; | 228 Display* xdisplay_; |
255 ::Window xwindow_; | 229 ::Window xwindow_; |
256 | 230 |
| 231 // The input method for the desktop. |
| 232 #if defined(HAVE_IBUS) |
| 233 ui::InputMethodIBus input_method_; |
| 234 #else |
| 235 ui::MockInputMethod input_method_; |
| 236 #endif |
| 237 |
257 // Current Aura cursor. | 238 // Current Aura cursor. |
258 gfx::NativeCursor current_cursor_; | 239 gfx::NativeCursor current_cursor_; |
259 | 240 |
260 // The bounds of |xwindow_|. | 241 // The bounds of |xwindow_|. |
261 gfx::Rect bounds_; | 242 gfx::Rect bounds_; |
262 | 243 |
263 DISALLOW_COPY_AND_ASSIGN(DesktopHostLinux); | 244 DISALLOW_COPY_AND_ASSIGN(DesktopHostLinux); |
264 }; | 245 }; |
265 | 246 |
266 DesktopHostLinux::DesktopHostLinux(const gfx::Rect& bounds) | 247 DesktopHostLinux::DesktopHostLinux(const gfx::Rect& bounds) |
267 : desktop_(NULL), | 248 : desktop_(NULL), |
268 xdisplay_(base::MessagePumpX::GetDefaultXDisplay()), | 249 xdisplay_(base::MessagePumpX::GetDefaultXDisplay()), |
269 xwindow_(0), | 250 xwindow_(0), |
| 251 ALLOW_THIS_IN_INITIALIZER_LIST(input_method_(this)), |
270 current_cursor_(aura::kCursorNull), | 252 current_cursor_(aura::kCursorNull), |
271 bounds_(bounds) { | 253 bounds_(bounds) { |
272 xwindow_ = XCreateSimpleWindow(xdisplay_, DefaultRootWindow(xdisplay_), | 254 xwindow_ = XCreateSimpleWindow(xdisplay_, DefaultRootWindow(xdisplay_), |
273 bounds.x(), bounds.y(), | 255 bounds.x(), bounds.y(), |
274 bounds.width(), bounds.height(), | 256 bounds.width(), bounds.height(), |
275 0, 0, 0); | 257 0, 0, 0); |
276 | 258 |
277 long event_mask = ButtonPressMask | ButtonReleaseMask | | 259 long event_mask = ButtonPressMask | ButtonReleaseMask | FocusChangeMask | |
278 KeyPressMask | KeyReleaseMask | | 260 KeyPressMask | KeyReleaseMask | |
279 ExposureMask | VisibilityChangeMask | | 261 ExposureMask | VisibilityChangeMask | |
280 StructureNotifyMask | PropertyChangeMask | | 262 StructureNotifyMask | PropertyChangeMask | |
281 PointerMotionMask; | 263 PointerMotionMask; |
282 XSelectInput(xdisplay_, xwindow_, event_mask); | 264 XSelectInput(xdisplay_, xwindow_, event_mask); |
283 XFlush(xdisplay_); | 265 XFlush(xdisplay_); |
284 | 266 |
285 // TODO(sadrul): reenable once 103981 is fixed. | 267 // TODO(sadrul): reenable once 103981 is fixed. |
286 #if defined(TOUCH_UI) | 268 #if defined(TOUCH_UI) |
287 if (base::MessagePumpForUI::HasXInput2()) | 269 if (base::MessagePumpForUI::HasXInput2()) |
288 ui::TouchFactory::GetInstance()->SetupXI2ForXWindow(xwindow_); | 270 ui::TouchFactory::GetInstance()->SetupXI2ForXWindow(xwindow_); |
289 #endif | 271 #endif |
| 272 |
| 273 input_method_.Init(xwindow_); |
290 } | 274 } |
291 | 275 |
292 DesktopHostLinux::~DesktopHostLinux() { | 276 DesktopHostLinux::~DesktopHostLinux() { |
293 XDestroyWindow(xdisplay_, xwindow_); | 277 XDestroyWindow(xdisplay_, xwindow_); |
294 | 278 |
295 // Clears XCursorCache. | 279 // Clears XCursorCache. |
296 ui::GetXCursor(ui::kCursorClearXCursorCache); | 280 ui::GetXCursor(ui::kCursorClearXCursorCache); |
297 } | 281 } |
298 | 282 |
299 base::MessagePumpDispatcher::DispatchStatus DesktopHostLinux::Dispatch( | 283 base::MessagePumpDispatcher::DispatchStatus DesktopHostLinux::Dispatch( |
300 XEvent* xev) { | 284 XEvent* xev) { |
301 bool handled = false; | 285 bool handled = false; |
302 switch (xev->type) { | 286 switch (xev->type) { |
303 case Expose: | 287 case Expose: |
304 desktop_->Draw(); | 288 desktop_->Draw(); |
305 handled = true; | 289 handled = true; |
306 break; | 290 break; |
307 case KeyPress: { | 291 case KeyPress: |
308 KeyEvent keydown_event(xev, false); | |
309 handled = desktop_->DispatchKeyEvent(&keydown_event); | |
310 if (ShouldSendCharEventForKeyboardCode(keydown_event.key_code())) { | |
311 KeyEvent char_event(xev, true); | |
312 handled |= desktop_->DispatchKeyEvent(&char_event); | |
313 } | |
314 break; | |
315 } | |
316 case KeyRelease: { | 292 case KeyRelease: { |
317 KeyEvent keyup_event(xev, false); | 293 input_method_.DispatchKeyEvent(xev); |
318 handled = desktop_->DispatchKeyEvent(&keyup_event); | 294 handled = true; |
319 break; | |
320 } | 295 } |
321 case ButtonPress: | 296 case ButtonPress: |
322 case ButtonRelease: { | 297 case ButtonRelease: { |
323 MouseEvent mouseev(xev); | 298 MouseEvent mouseev(xev); |
324 handled = desktop_->DispatchMouseEvent(&mouseev); | 299 handled = desktop_->DispatchMouseEvent(&mouseev); |
325 break; | 300 break; |
326 } | 301 } |
327 case ConfigureNotify: { | 302 case ConfigureNotify: { |
328 DCHECK_EQ(xdisplay_, xev->xconfigure.display); | 303 DCHECK_EQ(xdisplay_, xev->xconfigure.display); |
329 DCHECK_EQ(xwindow_, xev->xconfigure.window); | 304 DCHECK_EQ(xwindow_, xev->xconfigure.window); |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
381 break; | 356 break; |
382 default: | 357 default: |
383 NOTREACHED(); | 358 NOTREACHED(); |
384 } | 359 } |
385 | 360 |
386 // If we coalesced an event we need to free its cookie. | 361 // If we coalesced an event we need to free its cookie. |
387 if (num_coalesced > 0) | 362 if (num_coalesced > 0) |
388 XFreeEventData(xev->xgeneric.display, &last_event.xcookie); | 363 XFreeEventData(xev->xgeneric.display, &last_event.xcookie); |
389 break; | 364 break; |
390 } | 365 } |
| 366 case FocusIn: { |
| 367 input_method_.OnFocus(); |
| 368 break; |
| 369 } |
| 370 case FocusOut: { |
| 371 input_method_.OnBlur(); |
| 372 break; |
| 373 } |
391 case MapNotify: { | 374 case MapNotify: { |
392 // If there's no window manager running, we need to assign the X input | 375 // If there's no window manager running, we need to assign the X input |
393 // focus to our host window. | 376 // focus to our host window. |
394 if (!IsWindowManagerPresent()) | 377 if (!IsWindowManagerPresent()) |
395 XSetInputFocus(xdisplay_, xwindow_, RevertToNone, CurrentTime); | 378 XSetInputFocus(xdisplay_, xwindow_, RevertToNone, CurrentTime); |
396 handled = true; | 379 handled = true; |
397 break; | 380 break; |
398 } | 381 } |
399 case MotionNotify: { | 382 case MotionNotify: { |
400 // Discard all but the most recent motion event that targets the same | 383 // Discard all but the most recent motion event that targets the same |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
520 XSendEvent(xdisplay_, xwindow_, False, 0, &xevent); | 503 XSendEvent(xdisplay_, xwindow_, False, 0, &xevent); |
521 } | 504 } |
522 | 505 |
523 bool DesktopHostLinux::IsWindowManagerPresent() { | 506 bool DesktopHostLinux::IsWindowManagerPresent() { |
524 // Per ICCCM 2.8, "Manager Selections", window managers should take ownership | 507 // Per ICCCM 2.8, "Manager Selections", window managers should take ownership |
525 // of WM_Sn selections (where n is a screen number). | 508 // of WM_Sn selections (where n is a screen number). |
526 ::Atom wm_s0_atom = XInternAtom(xdisplay_, "WM_S0", False); | 509 ::Atom wm_s0_atom = XInternAtom(xdisplay_, "WM_S0", False); |
527 return XGetSelectionOwner(xdisplay_, wm_s0_atom) != None; | 510 return XGetSelectionOwner(xdisplay_, wm_s0_atom) != None; |
528 } | 511 } |
529 | 512 |
| 513 ui::InputMethod* DesktopHostLinux::GetInputMethod() { |
| 514 return &input_method_; |
| 515 } |
| 516 |
| 517 void DesktopHostLinux::DispatchKeyEventPostIME(const base::NativeEvent& event) { |
| 518 KeyEvent aura_event(event, false /* is_char */); |
| 519 desktop_->DispatchKeyEvent(&aura_event); |
| 520 // We don't send a Char event here since the input method takes care of it. |
| 521 } |
| 522 |
| 523 void DesktopHostLinux::DispatchFabricatedKeyEventPostIME( |
| 524 ui::EventType type, ui::KeyboardCode key_code, int flags) { |
| 525 // Dispatch a ui::VKEY_PROCESSKEY event etc. generated by |input_method_|. |
| 526 KeyEvent aura_event(type, key_code, flags); |
| 527 desktop_->DispatchKeyEvent(&aura_event); |
| 528 } |
| 529 |
530 } // namespace | 530 } // namespace |
531 | 531 |
532 // static | 532 // static |
533 DesktopHost* DesktopHost::Create(const gfx::Rect& bounds) { | 533 DesktopHost* DesktopHost::Create(const gfx::Rect& bounds) { |
534 return new DesktopHostLinux(bounds); | 534 return new DesktopHostLinux(bounds); |
535 } | 535 } |
536 | 536 |
537 // static | 537 // static |
538 gfx::Size DesktopHost::GetNativeDisplaySize() { | 538 gfx::Size DesktopHost::GetNativeDisplaySize() { |
539 ::Display* xdisplay = base::MessagePumpX::GetDefaultXDisplay(); | 539 ::Display* xdisplay = base::MessagePumpX::GetDefaultXDisplay(); |
540 return gfx::Size(DisplayWidth(xdisplay, 0), DisplayHeight(xdisplay, 0)); | 540 return gfx::Size(DisplayWidth(xdisplay, 0), DisplayHeight(xdisplay, 0)); |
541 } | 541 } |
542 | 542 |
543 } // namespace aura | 543 } // namespace aura |
OLD | NEW |