| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "mojo/services/native_viewport/native_viewport.h" | 5 #include "mojo/services/native_viewport/native_viewport.h" |
| 6 | 6 |
| 7 #include <X11/Xlib.h> | 7 #include <X11/Xlib.h> |
| 8 #include <X11/Xutil.h> |
| 8 | 9 |
| 9 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
| 11 #include "ui/events/event.h" |
| 10 #include "ui/events/platform/platform_event_dispatcher.h" | 12 #include "ui/events/platform/platform_event_dispatcher.h" |
| 11 #include "ui/events/platform/platform_event_source.h" | 13 #include "ui/events/platform/platform_event_source.h" |
| 14 #include "ui/events/platform/x11/x11_event_source.h" |
| 12 #include "ui/gfx/rect.h" | 15 #include "ui/gfx/rect.h" |
| 13 #include "ui/gfx/x/x11_types.h" | 16 #include "ui/gfx/x/x11_types.h" |
| 14 | 17 |
| 15 namespace mojo { | 18 namespace mojo { |
| 16 namespace services { | 19 namespace services { |
| 17 | 20 |
| 18 class NativeViewportX11 : public NativeViewport, | 21 class NativeViewportX11 : public NativeViewport, |
| 19 public ui::PlatformEventDispatcher { | 22 public ui::PlatformEventDispatcher { |
| 20 public: | 23 public: |
| 21 NativeViewportX11(NativeViewportDelegate* delegate) | 24 NativeViewportX11(NativeViewportDelegate* delegate) |
| 22 : delegate_(delegate) { | 25 : delegate_(delegate) { |
| 23 } | 26 } |
| 24 | 27 |
| 25 virtual ~NativeViewportX11() { | 28 virtual ~NativeViewportX11() { |
| 26 ui::PlatformEventSource::GetInstance()->RemovePlatformEventDispatcher(this); | 29 event_source_->RemovePlatformEventDispatcher(this); |
| 27 | 30 |
| 28 XDestroyWindow(gfx::GetXDisplay(), window_); | 31 XDestroyWindow(gfx::GetXDisplay(), window_); |
| 29 } | 32 } |
| 30 | 33 |
| 31 private: | 34 private: |
| 32 // Overridden from NativeViewport: | 35 // Overridden from NativeViewport: |
| 33 virtual void Init(const gfx::Rect& bounds) OVERRIDE { | 36 virtual void Init(const gfx::Rect& bounds) OVERRIDE { |
| 34 XDisplay* display = gfx::GetXDisplay(); | 37 XDisplay* display = gfx::GetXDisplay(); |
| 35 | 38 |
| 36 XSetWindowAttributes swa; | 39 XSetWindowAttributes swa; |
| 37 memset(&swa, 0, sizeof(swa)); | 40 memset(&swa, 0, sizeof(swa)); |
| 38 swa.override_redirect = False; | 41 swa.override_redirect = False; |
| 39 | 42 |
| 40 bounds_ = bounds; | 43 bounds_ = bounds; |
| 41 window_ = XCreateWindow( | 44 window_ = XCreateWindow( |
| 42 display, | 45 display, |
| 43 DefaultRootWindow(display), | 46 DefaultRootWindow(display), |
| 44 bounds_.x(), bounds_.y(), bounds_.width(), bounds_.height(), | 47 bounds_.x(), bounds_.y(), bounds_.width(), bounds_.height(), |
| 45 0, // border width | 48 0, // border width |
| 46 CopyFromParent, // depth | 49 CopyFromParent, // depth |
| 47 InputOutput, | 50 InputOutput, |
| 48 CopyFromParent, // visual | 51 CopyFromParent, // visual |
| 49 CWBackPixmap | CWOverrideRedirect, | 52 CWBackPixmap | CWOverrideRedirect, |
| 50 &swa); | 53 &swa); |
| 51 | 54 |
| 52 atom_wm_protocols_ = XInternAtom(display, "WM_PROTOCOLS", 1); | 55 atom_wm_protocols_ = XInternAtom(display, "WM_PROTOCOLS", 1); |
| 53 atom_wm_delete_window_ = XInternAtom(display, "WM_DELETE_WINDOW", 1); | 56 atom_wm_delete_window_ = XInternAtom(display, "WM_DELETE_WINDOW", 1); |
| 54 XSetWMProtocols(display, window_, &atom_wm_delete_window_, 1); | 57 XSetWMProtocols(display, window_, &atom_wm_delete_window_, 1); |
| 55 | 58 |
| 56 event_source_.reset(ui::PlatformEventSource::GetInstance()); | 59 event_source_ = ui::PlatformEventSource::CreateDefault(); |
| 57 if (!event_source_.get()) | 60 event_source_->AddPlatformEventDispatcher(this); |
| 58 event_source_ = ui::PlatformEventSource::CreateDefault(); | 61 |
| 59 ui::PlatformEventSource::GetInstance()->AddPlatformEventDispatcher(this); | 62 long event_mask = ButtonPressMask | ButtonReleaseMask | FocusChangeMask | |
| 63 KeyPressMask | KeyReleaseMask | EnterWindowMask | LeaveWindowMask | |
| 64 ExposureMask | VisibilityChangeMask | StructureNotifyMask | |
| 65 PropertyChangeMask | PointerMotionMask; |
| 66 XSelectInput(display, window_, event_mask); |
| 67 |
| 68 // We need a WM_CLIENT_MACHINE and WM_LOCALE_NAME value so we integrate with |
| 69 // the desktop environment. |
| 70 XSetWMProperties(display, window_, NULL, NULL, NULL, 0, NULL, NULL, NULL); |
| 71 |
| 72 // TODO(aa): Setup xinput2 events. |
| 73 // See desktop_aura/desktop_window_tree_host_x11.cc. |
| 60 | 74 |
| 61 delegate_->OnAcceleratedWidgetAvailable(window_); | 75 delegate_->OnAcceleratedWidgetAvailable(window_); |
| 62 } | 76 } |
| 63 | 77 |
| 64 virtual void Show() OVERRIDE { | 78 virtual void Show() OVERRIDE { |
| 65 XDisplay* display = gfx::GetXDisplay(); | 79 XDisplay* display = gfx::GetXDisplay(); |
| 66 XMapWindow(display, window_); | 80 XMapWindow(display, window_); |
| 81 static_cast<ui::X11EventSource*>( |
| 82 event_source_.get())->BlockUntilWindowMapped(window_); |
| 67 XFlush(display); | 83 XFlush(display); |
| 68 } | 84 } |
| 69 | 85 |
| 70 virtual void Hide() OVERRIDE { | 86 virtual void Hide() OVERRIDE { |
| 71 XWithdrawWindow(gfx::GetXDisplay(), window_, 0); | 87 XWithdrawWindow(gfx::GetXDisplay(), window_, 0); |
| 72 } | 88 } |
| 73 | 89 |
| 74 virtual void Close() OVERRIDE { | 90 virtual void Close() OVERRIDE { |
| 75 // TODO(beng): perform this in response to XWindow destruction. | 91 // TODO(beng): perform this in response to XWindow destruction. |
| 76 delegate_->OnDestroyed(); | 92 delegate_->OnDestroyed(); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 87 virtual void SetCapture() OVERRIDE { | 103 virtual void SetCapture() OVERRIDE { |
| 88 NOTIMPLEMENTED(); | 104 NOTIMPLEMENTED(); |
| 89 } | 105 } |
| 90 | 106 |
| 91 virtual void ReleaseCapture() OVERRIDE { | 107 virtual void ReleaseCapture() OVERRIDE { |
| 92 NOTIMPLEMENTED(); | 108 NOTIMPLEMENTED(); |
| 93 } | 109 } |
| 94 | 110 |
| 95 // ui::PlatformEventDispatcher: | 111 // ui::PlatformEventDispatcher: |
| 96 virtual bool CanDispatchEvent(const ui::PlatformEvent& event) OVERRIDE { | 112 virtual bool CanDispatchEvent(const ui::PlatformEvent& event) OVERRIDE { |
| 97 return event->type == ClientMessage && | 113 // TODO(aa): This is going to have to be thought through more carefully. |
| 98 event->xclient.message_type == atom_wm_protocols_; | 114 // Which events are appropriate to pass to clients? |
| 115 switch (event->type) { |
| 116 case KeyPress: |
| 117 case KeyRelease: |
| 118 case ButtonPress: |
| 119 case ButtonRelease: |
| 120 case MotionNotify: |
| 121 return true; |
| 122 case ClientMessage: |
| 123 return event->xclient.message_type != atom_wm_protocols_; |
| 124 default: |
| 125 return false; |
| 126 } |
| 99 } | 127 } |
| 100 | 128 |
| 101 virtual uint32_t DispatchEvent(const ui::PlatformEvent& event) OVERRIDE { | 129 virtual uint32_t DispatchEvent(const ui::PlatformEvent& event) OVERRIDE { |
| 102 Atom protocol = static_cast<Atom>(event->xclient.data.l[0]); | 130 if (event->type == ClientMessage) { |
| 103 if (protocol == atom_wm_delete_window_) | 131 Atom protocol = static_cast<Atom>(event->xclient.data.l[0]); |
| 104 delegate_->OnDestroyed(); | 132 if (protocol == atom_wm_delete_window_) |
| 133 delegate_->OnDestroyed(); |
| 134 } else if (event->type == KeyPress || event->type == KeyRelease) { |
| 135 ui::KeyEvent key_event(event, true); |
| 136 delegate_->OnEvent(&key_event); |
| 137 } else if (event->type == ButtonPress || event->type == ButtonRelease || |
| 138 event->type == MotionNotify) { |
| 139 ui::MouseEvent mouse_event(event); |
| 140 delegate_->OnEvent(&mouse_event); |
| 141 } |
| 105 return ui::POST_DISPATCH_NONE; | 142 return ui::POST_DISPATCH_NONE; |
| 106 } | 143 } |
| 107 | 144 |
| 108 scoped_ptr<ui::PlatformEventSource> event_source_; | 145 scoped_ptr<ui::PlatformEventSource> event_source_; |
| 109 NativeViewportDelegate* delegate_; | 146 NativeViewportDelegate* delegate_; |
| 110 gfx::Rect bounds_; | 147 gfx::Rect bounds_; |
| 111 XID window_; | 148 XID window_; |
| 112 Atom atom_wm_protocols_; | 149 Atom atom_wm_protocols_; |
| 113 Atom atom_wm_delete_window_; | 150 Atom atom_wm_delete_window_; |
| 114 | 151 |
| 115 DISALLOW_COPY_AND_ASSIGN(NativeViewportX11); | 152 DISALLOW_COPY_AND_ASSIGN(NativeViewportX11); |
| 116 }; | 153 }; |
| 117 | 154 |
| 118 // static | 155 // static |
| 119 scoped_ptr<NativeViewport> NativeViewport::Create( | 156 scoped_ptr<NativeViewport> NativeViewport::Create( |
| 120 shell::Context* context, | 157 shell::Context* context, |
| 121 NativeViewportDelegate* delegate) { | 158 NativeViewportDelegate* delegate) { |
| 122 return scoped_ptr<NativeViewport>(new NativeViewportX11(delegate)).Pass(); | 159 return scoped_ptr<NativeViewport>(new NativeViewportX11(delegate)).Pass(); |
| 123 } | 160 } |
| 124 | 161 |
| 125 } // namespace services | 162 } // namespace services |
| 126 } // namespace mojo | 163 } // namespace mojo |
| OLD | NEW |