| 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 |
| 257 // The native root window. | 231 // The native root window. |
| 258 ::Window root_window_; | 232 ::Window root_window_; |
| 259 | 233 |
| 234 // The input method for the desktop. |
| 235 #if defined(HAVE_IBUS) |
| 236 ui::InputMethodIBus input_method_; |
| 237 #else |
| 238 ui::MockInputMethod input_method_; |
| 239 #endif |
| 240 |
| 260 // Current Aura cursor. | 241 // Current Aura cursor. |
| 261 gfx::NativeCursor current_cursor_; | 242 gfx::NativeCursor current_cursor_; |
| 262 | 243 |
| 263 // The bounds of |xwindow_|. | 244 // The bounds of |xwindow_|. |
| 264 gfx::Rect bounds_; | 245 gfx::Rect bounds_; |
| 265 | 246 |
| 266 DISALLOW_COPY_AND_ASSIGN(DesktopHostLinux); | 247 DISALLOW_COPY_AND_ASSIGN(DesktopHostLinux); |
| 267 }; | 248 }; |
| 268 | 249 |
| 269 DesktopHostLinux::DesktopHostLinux(const gfx::Rect& bounds) | 250 DesktopHostLinux::DesktopHostLinux(const gfx::Rect& bounds) |
| 270 : desktop_(NULL), | 251 : desktop_(NULL), |
| 271 xdisplay_(base::MessagePumpX::GetDefaultXDisplay()), | 252 xdisplay_(base::MessagePumpX::GetDefaultXDisplay()), |
| 272 xwindow_(0), | 253 xwindow_(0), |
| 273 root_window_(DefaultRootWindow(xdisplay_)), | 254 root_window_(DefaultRootWindow(xdisplay_)), |
| 255 ALLOW_THIS_IN_INITIALIZER_LIST(input_method_(this)), |
| 274 current_cursor_(aura::kCursorNull), | 256 current_cursor_(aura::kCursorNull), |
| 275 bounds_(bounds) { | 257 bounds_(bounds) { |
| 276 xwindow_ = XCreateSimpleWindow(xdisplay_, root_window_, | 258 xwindow_ = XCreateSimpleWindow(xdisplay_, root_window_, |
| 277 bounds.x(), bounds.y(), | 259 bounds.x(), bounds.y(), |
| 278 bounds.width(), bounds.height(), | 260 bounds.width(), bounds.height(), |
| 279 0, 0, 0); | 261 0, 0, 0); |
| 280 | 262 |
| 281 long event_mask = ButtonPressMask | ButtonReleaseMask | | 263 long event_mask = ButtonPressMask | ButtonReleaseMask | FocusChangeMask | |
| 282 KeyPressMask | KeyReleaseMask | | 264 KeyPressMask | KeyReleaseMask | |
| 283 ExposureMask | VisibilityChangeMask | | 265 ExposureMask | VisibilityChangeMask | |
| 284 StructureNotifyMask | PropertyChangeMask | | 266 StructureNotifyMask | PropertyChangeMask | |
| 285 PointerMotionMask; | 267 PointerMotionMask; |
| 286 XSelectInput(xdisplay_, xwindow_, event_mask); | 268 XSelectInput(xdisplay_, xwindow_, event_mask); |
| 287 XSelectInput(xdisplay_, root_window_, StructureNotifyMask); | 269 XSelectInput(xdisplay_, root_window_, StructureNotifyMask); |
| 288 XFlush(xdisplay_); | 270 XFlush(xdisplay_); |
| 289 | 271 |
| 290 // TODO(sadrul): reenable once 103981 is fixed. | 272 // TODO(sadrul): reenable once 103981 is fixed. |
| 291 #if defined(TOUCH_UI) | 273 #if defined(TOUCH_UI) |
| 292 if (base::MessagePumpForUI::HasXInput2()) | 274 if (base::MessagePumpForUI::HasXInput2()) |
| 293 ui::TouchFactory::GetInstance()->SetupXI2ForXWindow(xwindow_); | 275 ui::TouchFactory::GetInstance()->SetupXI2ForXWindow(xwindow_); |
| 294 #endif | 276 #endif |
| 277 |
| 278 input_method_.Init(xwindow_); |
| 295 } | 279 } |
| 296 | 280 |
| 297 DesktopHostLinux::~DesktopHostLinux() { | 281 DesktopHostLinux::~DesktopHostLinux() { |
| 298 XDestroyWindow(xdisplay_, xwindow_); | 282 XDestroyWindow(xdisplay_, xwindow_); |
| 299 | 283 |
| 300 // Clears XCursorCache. | 284 // Clears XCursorCache. |
| 301 ui::GetXCursor(ui::kCursorClearXCursorCache); | 285 ui::GetXCursor(ui::kCursorClearXCursorCache); |
| 302 } | 286 } |
| 303 | 287 |
| 304 base::MessagePumpDispatcher::DispatchStatus DesktopHostLinux::Dispatch( | 288 base::MessagePumpDispatcher::DispatchStatus DesktopHostLinux::Dispatch( |
| 305 XEvent* xev) { | 289 XEvent* xev) { |
| 306 DLOG(WARNING) << "DispatchEvent:" << xev->type; | 290 DLOG(WARNING) << "DispatchEvent:" << xev->type; |
| 307 | 291 |
| 308 bool handled = false; | 292 bool handled = false; |
| 309 switch (xev->type) { | 293 switch (xev->type) { |
| 310 case Expose: | 294 case Expose: |
| 311 desktop_->Draw(); | 295 desktop_->Draw(); |
| 312 handled = true; | 296 handled = true; |
| 313 break; | 297 break; |
| 314 case KeyPress: { | 298 case KeyPress: |
| 315 KeyEvent keydown_event(xev, false); | |
| 316 handled = desktop_->DispatchKeyEvent(&keydown_event); | |
| 317 if (ShouldSendCharEventForKeyboardCode(keydown_event.key_code())) { | |
| 318 KeyEvent char_event(xev, true); | |
| 319 handled |= desktop_->DispatchKeyEvent(&char_event); | |
| 320 } | |
| 321 break; | |
| 322 } | |
| 323 case KeyRelease: { | 299 case KeyRelease: { |
| 324 KeyEvent keyup_event(xev, false); | 300 input_method_.DispatchKeyEvent(xev); |
| 325 handled = desktop_->DispatchKeyEvent(&keyup_event); | 301 handled = true; |
| 326 break; | |
| 327 } | 302 } |
| 328 case ButtonPress: | 303 case ButtonPress: |
| 329 case ButtonRelease: { | 304 case ButtonRelease: { |
| 330 MouseEvent mouseev(xev); | 305 MouseEvent mouseev(xev); |
| 331 handled = desktop_->DispatchMouseEvent(&mouseev); | 306 handled = desktop_->DispatchMouseEvent(&mouseev); |
| 332 break; | 307 break; |
| 333 } | 308 } |
| 334 case ConfigureNotify: { | 309 case ConfigureNotify: { |
| 335 if (xev->xconfigure.window == root_window_) { | 310 if (xev->xconfigure.window == root_window_) { |
| 336 desktop_->OnNativeScreenResized( | 311 desktop_->OnNativeScreenResized( |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 394 break; | 369 break; |
| 395 default: | 370 default: |
| 396 NOTREACHED(); | 371 NOTREACHED(); |
| 397 } | 372 } |
| 398 | 373 |
| 399 // If we coalesced an event we need to free its cookie. | 374 // If we coalesced an event we need to free its cookie. |
| 400 if (num_coalesced > 0) | 375 if (num_coalesced > 0) |
| 401 XFreeEventData(xev->xgeneric.display, &last_event.xcookie); | 376 XFreeEventData(xev->xgeneric.display, &last_event.xcookie); |
| 402 break; | 377 break; |
| 403 } | 378 } |
| 379 case FocusIn: { |
| 380 input_method_.OnFocus(); |
| 381 break; |
| 382 } |
| 383 case FocusOut: { |
| 384 input_method_.OnBlur(); |
| 385 break; |
| 386 } |
| 404 case MapNotify: { | 387 case MapNotify: { |
| 405 // If there's no window manager running, we need to assign the X input | 388 // If there's no window manager running, we need to assign the X input |
| 406 // focus to our host window. | 389 // focus to our host window. |
| 407 if (!IsWindowManagerPresent()) | 390 if (!IsWindowManagerPresent()) |
| 408 XSetInputFocus(xdisplay_, xwindow_, RevertToNone, CurrentTime); | 391 XSetInputFocus(xdisplay_, xwindow_, RevertToNone, CurrentTime); |
| 409 handled = true; | 392 handled = true; |
| 410 break; | 393 break; |
| 411 } | 394 } |
| 412 case MotionNotify: { | 395 case MotionNotify: { |
| 413 // Discard all but the most recent motion event that targets the same | 396 // Discard all but the most recent motion event that targets the same |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 534 DLOG(WARNING) << "PostEvent:" << xevent.type << ", status=" << status; | 517 DLOG(WARNING) << "PostEvent:" << xevent.type << ", status=" << status; |
| 535 } | 518 } |
| 536 | 519 |
| 537 bool DesktopHostLinux::IsWindowManagerPresent() { | 520 bool DesktopHostLinux::IsWindowManagerPresent() { |
| 538 // Per ICCCM 2.8, "Manager Selections", window managers should take ownership | 521 // Per ICCCM 2.8, "Manager Selections", window managers should take ownership |
| 539 // of WM_Sn selections (where n is a screen number). | 522 // of WM_Sn selections (where n is a screen number). |
| 540 ::Atom wm_s0_atom = XInternAtom(xdisplay_, "WM_S0", False); | 523 ::Atom wm_s0_atom = XInternAtom(xdisplay_, "WM_S0", False); |
| 541 return XGetSelectionOwner(xdisplay_, wm_s0_atom) != None; | 524 return XGetSelectionOwner(xdisplay_, wm_s0_atom) != None; |
| 542 } | 525 } |
| 543 | 526 |
| 527 ui::InputMethod* DesktopHostLinux::GetInputMethod() { |
| 528 return &input_method_; |
| 529 } |
| 530 |
| 531 void DesktopHostLinux::DispatchKeyEventPostIME(const base::NativeEvent& event) { |
| 532 KeyEvent aura_event(event, false /* is_char */); |
| 533 desktop_->DispatchKeyEvent(&aura_event); |
| 534 // We don't send a Char event here since the input method takes care of it. |
| 535 } |
| 536 |
| 537 void DesktopHostLinux::DispatchFabricatedKeyEventPostIME( |
| 538 ui::EventType type, ui::KeyboardCode key_code, int flags) { |
| 539 // Dispatch a ui::VKEY_PROCESSKEY event etc. generated by |input_method_|. |
| 540 KeyEvent aura_event(type, key_code, flags); |
| 541 desktop_->DispatchKeyEvent(&aura_event); |
| 542 } |
| 543 |
| 544 } // namespace | 544 } // namespace |
| 545 | 545 |
| 546 // static | 546 // static |
| 547 DesktopHost* DesktopHost::Create(const gfx::Rect& bounds) { | 547 DesktopHost* DesktopHost::Create(const gfx::Rect& bounds) { |
| 548 return new DesktopHostLinux(bounds); | 548 return new DesktopHostLinux(bounds); |
| 549 } | 549 } |
| 550 | 550 |
| 551 // static | 551 // static |
| 552 gfx::Size DesktopHost::GetNativeScreenSize() { | 552 gfx::Size DesktopHost::GetNativeScreenSize() { |
| 553 ::Display* xdisplay = base::MessagePumpX::GetDefaultXDisplay(); | 553 ::Display* xdisplay = base::MessagePumpX::GetDefaultXDisplay(); |
| 554 return gfx::Size(DisplayWidth(xdisplay, 0), DisplayHeight(xdisplay, 0)); | 554 return gfx::Size(DisplayWidth(xdisplay, 0), DisplayHeight(xdisplay, 0)); |
| 555 } | 555 } |
| 556 | 556 |
| 557 } // namespace aura | 557 } // namespace aura |
| OLD | NEW |