| 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 void SetCursor(gfx::NativeCursor cursor_type) OVERRIDE; | 209 virtual void SetCursor(gfx::NativeCursor cursor_type) OVERRIDE; |
| 242 virtual gfx::Point QueryMouseLocation() OVERRIDE; | 210 virtual gfx::Point QueryMouseLocation() OVERRIDE; |
| 243 virtual void PostNativeEvent(const base::NativeEvent& event) OVERRIDE; | 211 virtual void PostNativeEvent(const base::NativeEvent& event) OVERRIDE; |
| 212 virtual ui::InputMethod* GetInputMethod() OVERRIDE; |
| 213 |
| 214 // ui::internal::InputMethodDelegate Override. |
| 215 virtual void DispatchKeyEventPostIME(const base::NativeEvent& event) OVERRIDE; |
| 216 virtual void DispatchFabricatedKeyEventPostIME( |
| 217 ui::EventType type, ui::KeyboardCode key_code, int flags) OVERRIDE; |
| 244 | 218 |
| 245 // Returns true if there's an X window manager present... in most cases. Some | 219 // Returns true if there's an X window manager present... in most cases. Some |
| 246 // window managers (notably, ion3) don't implement enough of ICCCM for us to | 220 // window managers (notably, ion3) don't implement enough of ICCCM for us to |
| 247 // detect that they're there. | 221 // detect that they're there. |
| 248 bool IsWindowManagerPresent(); | 222 bool IsWindowManagerPresent(); |
| 249 | 223 |
| 250 Desktop* desktop_; | 224 Desktop* desktop_; |
| 251 | 225 |
| 252 // The display and the native X window hosting the desktop. | 226 // The display and the native X window hosting the desktop. |
| 253 Display* xdisplay_; | 227 Display* xdisplay_; |
| 254 ::Window xwindow_; | 228 ::Window xwindow_; |
| 255 | 229 |
| 230 // The input method for the desktop. |
| 231 #if defined(HAVE_IBUS) |
| 232 ui::InputMethodIBus input_method_; |
| 233 #else |
| 234 ui::MockInputMethod input_method_; |
| 235 #endif |
| 236 |
| 256 // Current Aura cursor. | 237 // Current Aura cursor. |
| 257 gfx::NativeCursor current_cursor_; | 238 gfx::NativeCursor current_cursor_; |
| 258 | 239 |
| 259 // The size of |xwindow_|. | 240 // The size of |xwindow_|. |
| 260 gfx::Size size_; | 241 gfx::Size size_; |
| 261 | 242 |
| 262 DISALLOW_COPY_AND_ASSIGN(DesktopHostLinux); | 243 DISALLOW_COPY_AND_ASSIGN(DesktopHostLinux); |
| 263 }; | 244 }; |
| 264 | 245 |
| 265 DesktopHostLinux::DesktopHostLinux(const gfx::Rect& bounds) | 246 DesktopHostLinux::DesktopHostLinux(const gfx::Rect& bounds) |
| 266 : desktop_(NULL), | 247 : desktop_(NULL), |
| 267 xdisplay_(base::MessagePumpX::GetDefaultXDisplay()), | 248 xdisplay_(base::MessagePumpX::GetDefaultXDisplay()), |
| 268 xwindow_(0), | 249 xwindow_(0), |
| 250 ALLOW_THIS_IN_INITIALIZER_LIST(input_method_(this)), |
| 269 current_cursor_(aura::kCursorNull), | 251 current_cursor_(aura::kCursorNull), |
| 270 size_(bounds.size()) { | 252 size_(bounds.size()) { |
| 271 xwindow_ = XCreateSimpleWindow(xdisplay_, DefaultRootWindow(xdisplay_), | 253 xwindow_ = XCreateSimpleWindow(xdisplay_, DefaultRootWindow(xdisplay_), |
| 272 bounds.x(), bounds.y(), | 254 bounds.x(), bounds.y(), |
| 273 bounds.width(), bounds.height(), | 255 bounds.width(), bounds.height(), |
| 274 0, 0, 0); | 256 0, 0, 0); |
| 275 | 257 |
| 276 long event_mask = ButtonPressMask | ButtonReleaseMask | | 258 long event_mask = ButtonPressMask | ButtonReleaseMask | FocusChangeMask | |
| 277 KeyPressMask | KeyReleaseMask | | 259 KeyPressMask | KeyReleaseMask | |
| 278 ExposureMask | VisibilityChangeMask | | 260 ExposureMask | VisibilityChangeMask | |
| 279 StructureNotifyMask | PropertyChangeMask | | 261 StructureNotifyMask | PropertyChangeMask | |
| 280 PointerMotionMask; | 262 PointerMotionMask; |
| 281 XSelectInput(xdisplay_, xwindow_, event_mask); | 263 XSelectInput(xdisplay_, xwindow_, event_mask); |
| 282 XFlush(xdisplay_); | 264 XFlush(xdisplay_); |
| 283 | 265 |
| 284 // TODO(sadrul): reenable once 103981 is fixed. | 266 // TODO(sadrul): reenable once 103981 is fixed. |
| 285 #if defined(TOUCH_UI) | 267 #if defined(TOUCH_UI) |
| 286 if (base::MessagePumpForUI::HasXInput2()) | 268 if (base::MessagePumpForUI::HasXInput2()) |
| 287 ui::TouchFactory::GetInstance()->SetupXI2ForXWindow(xwindow_); | 269 ui::TouchFactory::GetInstance()->SetupXI2ForXWindow(xwindow_); |
| 288 #endif | 270 #endif |
| 271 |
| 272 input_method_.Init(xwindow_); |
| 289 } | 273 } |
| 290 | 274 |
| 291 DesktopHostLinux::~DesktopHostLinux() { | 275 DesktopHostLinux::~DesktopHostLinux() { |
| 292 XDestroyWindow(xdisplay_, xwindow_); | 276 XDestroyWindow(xdisplay_, xwindow_); |
| 293 | 277 |
| 294 // Clears XCursorCache. | 278 // Clears XCursorCache. |
| 295 ui::GetXCursor(ui::kCursorClearXCursorCache); | 279 ui::GetXCursor(ui::kCursorClearXCursorCache); |
| 296 } | 280 } |
| 297 | 281 |
| 298 base::MessagePumpDispatcher::DispatchStatus DesktopHostLinux::Dispatch( | 282 base::MessagePumpDispatcher::DispatchStatus DesktopHostLinux::Dispatch( |
| 299 XEvent* xev) { | 283 XEvent* xev) { |
| 300 bool handled = false; | 284 bool handled = false; |
| 301 switch (xev->type) { | 285 switch (xev->type) { |
| 302 case Expose: | 286 case Expose: |
| 303 desktop_->Draw(); | 287 desktop_->Draw(); |
| 304 handled = true; | 288 handled = true; |
| 305 break; | 289 break; |
| 306 case KeyPress: { | 290 case KeyPress: |
| 307 KeyEvent keydown_event(xev, false); | |
| 308 handled = desktop_->DispatchKeyEvent(&keydown_event); | |
| 309 if (ShouldSendCharEventForKeyboardCode(keydown_event.key_code())) { | |
| 310 KeyEvent char_event(xev, true); | |
| 311 handled |= desktop_->DispatchKeyEvent(&char_event); | |
| 312 } | |
| 313 break; | |
| 314 } | |
| 315 case KeyRelease: { | 291 case KeyRelease: { |
| 316 KeyEvent keyup_event(xev, false); | 292 input_method_.DispatchKeyEvent(xev); |
| 317 handled = desktop_->DispatchKeyEvent(&keyup_event); | 293 handled = true; |
| 318 break; | |
| 319 } | 294 } |
| 320 case ButtonPress: | 295 case ButtonPress: |
| 321 case ButtonRelease: { | 296 case ButtonRelease: { |
| 322 MouseEvent mouseev(xev); | 297 MouseEvent mouseev(xev); |
| 323 handled = desktop_->DispatchMouseEvent(&mouseev); | 298 handled = desktop_->DispatchMouseEvent(&mouseev); |
| 324 break; | 299 break; |
| 325 } | 300 } |
| 326 case ConfigureNotify: { | 301 case ConfigureNotify: { |
| 327 DCHECK_EQ(xdisplay_, xev->xconfigure.display); | 302 DCHECK_EQ(xdisplay_, xev->xconfigure.display); |
| 328 DCHECK_EQ(xwindow_, xev->xconfigure.window); | 303 DCHECK_EQ(xwindow_, xev->xconfigure.window); |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 379 break; | 354 break; |
| 380 default: | 355 default: |
| 381 NOTREACHED(); | 356 NOTREACHED(); |
| 382 } | 357 } |
| 383 | 358 |
| 384 // If we coalesced an event we need to free its cookie. | 359 // If we coalesced an event we need to free its cookie. |
| 385 if (num_coalesced > 0) | 360 if (num_coalesced > 0) |
| 386 XFreeEventData(xev->xgeneric.display, &last_event.xcookie); | 361 XFreeEventData(xev->xgeneric.display, &last_event.xcookie); |
| 387 break; | 362 break; |
| 388 } | 363 } |
| 364 case FocusIn: { |
| 365 input_method_.OnFocus(); |
| 366 break; |
| 367 } |
| 368 case FocusOut: { |
| 369 input_method_.OnBlur(); |
| 370 break; |
| 371 } |
| 389 case MapNotify: { | 372 case MapNotify: { |
| 390 // If there's no window manager running, we need to assign the X input | 373 // If there's no window manager running, we need to assign the X input |
| 391 // focus to our host window. | 374 // focus to our host window. |
| 392 if (!IsWindowManagerPresent()) | 375 if (!IsWindowManagerPresent()) |
| 393 XSetInputFocus(xdisplay_, xwindow_, RevertToNone, CurrentTime); | 376 XSetInputFocus(xdisplay_, xwindow_, RevertToNone, CurrentTime); |
| 394 handled = true; | 377 handled = true; |
| 395 break; | 378 break; |
| 396 } | 379 } |
| 397 case MotionNotify: { | 380 case MotionNotify: { |
| 398 // Discard all but the most recent motion event that targets the same | 381 // Discard all but the most recent motion event that targets the same |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 491 ::XPutBackEvent(xdisplay_, &xevent); | 474 ::XPutBackEvent(xdisplay_, &xevent); |
| 492 } | 475 } |
| 493 | 476 |
| 494 bool DesktopHostLinux::IsWindowManagerPresent() { | 477 bool DesktopHostLinux::IsWindowManagerPresent() { |
| 495 // Per ICCCM 2.8, "Manager Selections", window managers should take ownership | 478 // Per ICCCM 2.8, "Manager Selections", window managers should take ownership |
| 496 // of WM_Sn selections (where n is a screen number). | 479 // of WM_Sn selections (where n is a screen number). |
| 497 ::Atom wm_s0_atom = XInternAtom(xdisplay_, "WM_S0", False); | 480 ::Atom wm_s0_atom = XInternAtom(xdisplay_, "WM_S0", False); |
| 498 return XGetSelectionOwner(xdisplay_, wm_s0_atom) != None; | 481 return XGetSelectionOwner(xdisplay_, wm_s0_atom) != None; |
| 499 } | 482 } |
| 500 | 483 |
| 484 ui::InputMethod* DesktopHostLinux::GetInputMethod() { |
| 485 return &input_method_; |
| 486 } |
| 487 |
| 488 void DesktopHostLinux::DispatchKeyEventPostIME(const base::NativeEvent& event) { |
| 489 KeyEvent aura_event(event, false /* is_char */); |
| 490 desktop_->DispatchKeyEvent(&aura_event); |
| 491 // We don't send a Char event here since the input method takes care of it. |
| 492 } |
| 493 |
| 494 void DesktopHostLinux::DispatchFabricatedKeyEventPostIME( |
| 495 ui::EventType type, ui::KeyboardCode key_code, int flags) { |
| 496 // Dispatch a ui::VKEY_PROCESSKEY event etc. generated by |input_method_|. |
| 497 KeyEvent aura_event(type, key_code, flags); |
| 498 desktop_->DispatchKeyEvent(&aura_event); |
| 499 } |
| 500 |
| 501 } // namespace | 501 } // namespace |
| 502 | 502 |
| 503 // static | 503 // static |
| 504 DesktopHost* DesktopHost::Create(const gfx::Rect& bounds) { | 504 DesktopHost* DesktopHost::Create(const gfx::Rect& bounds) { |
| 505 return new DesktopHostLinux(bounds); | 505 return new DesktopHostLinux(bounds); |
| 506 } | 506 } |
| 507 | 507 |
| 508 // static | 508 // static |
| 509 gfx::Size DesktopHost::GetNativeDisplaySize() { | 509 gfx::Size DesktopHost::GetNativeDisplaySize() { |
| 510 ::Display* xdisplay = base::MessagePumpX::GetDefaultXDisplay(); | 510 ::Display* xdisplay = base::MessagePumpX::GetDefaultXDisplay(); |
| 511 return gfx::Size(DisplayWidth(xdisplay, 0), DisplayHeight(xdisplay, 0)); | 511 return gfx::Size(DisplayWidth(xdisplay, 0), DisplayHeight(xdisplay, 0)); |
| 512 } | 512 } |
| 513 | 513 |
| 514 } // namespace aura | 514 } // namespace aura |
| OLD | NEW |