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
3 // found in the LICENSE file.
5 #include "mojo/services/native_viewport/native_viewport.h"
9 #include "base/message_loop/message_loop.h"
10 #include "ui/events/platform/platform_event_dispatcher.h"
11 #include "ui/events/platform/platform_event_source.h"
12 #include "ui/gfx/rect.h"
13 #include "ui/gfx/x/x11_types.h"
18 class NativeViewportX11 : public NativeViewport,
19 public ui::PlatformEventDispatcher {
21 NativeViewportX11(NativeViewportDelegate* delegate)
22 : delegate_(delegate) {
25 virtual ~NativeViewportX11() {
26 ui::PlatformEventSource::GetInstance()->RemovePlatformEventDispatcher(this);
28 XDestroyWindow(gfx::GetXDisplay(), window_);
32 // Overridden from NativeViewport:
33 virtual void Init(const gfx::Rect& bounds) OVERRIDE {
34 XDisplay* display = gfx::GetXDisplay();
36 XSetWindowAttributes swa;
37 memset(&swa, 0, sizeof(swa));
38 swa.override_redirect = False;
41 window_ = XCreateWindow(
43 DefaultRootWindow(display),
44 bounds_.x(), bounds_.y(), bounds_.width(), bounds_.height(),
46 CopyFromParent, // depth
48 CopyFromParent, // visual
49 CWBackPixmap | CWOverrideRedirect,
52 atom_wm_protocols_ = XInternAtom(display, "WM_PROTOCOLS", 1);
53 atom_wm_delete_window_ = XInternAtom(display, "WM_DELETE_WINDOW", 1);
54 XSetWMProtocols(display, window_, &atom_wm_delete_window_, 1);
56 event_source_.reset(ui::PlatformEventSource::GetInstance());
57 if (!event_source_.get())
58 event_source_ = ui::PlatformEventSource::CreateDefault();
59 ui::PlatformEventSource::GetInstance()->AddPlatformEventDispatcher(this);
61 delegate_->OnAcceleratedWidgetAvailable(window_);
64 virtual void Show() OVERRIDE {
65 XDisplay* display = gfx::GetXDisplay();
66 XMapWindow(display, window_);
70 virtual void Hide() OVERRIDE {
71 XWithdrawWindow(gfx::GetXDisplay(), window_, 0);
74 virtual void Close() OVERRIDE {
75 // TODO(beng): perform this in response to XWindow destruction.
76 delegate_->OnDestroyed();
79 virtual gfx::Size GetSize() OVERRIDE {
80 return bounds_.size();
83 virtual void SetBounds(const gfx::Rect& bounds) OVERRIDE {
87 virtual void SetCapture() OVERRIDE {
91 virtual void ReleaseCapture() OVERRIDE {
95 // ui::PlatformEventDispatcher:
96 virtual bool CanDispatchEvent(const ui::PlatformEvent& event) OVERRIDE {
97 return event->type == ClientMessage &&
98 event->xclient.message_type == atom_wm_protocols_;
101 virtual uint32_t DispatchEvent(const ui::PlatformEvent& event) OVERRIDE {
102 Atom protocol = static_cast<Atom>(event->xclient.data.l[0]);
103 if (protocol == atom_wm_delete_window_)
104 delegate_->OnDestroyed();
105 return ui::POST_DISPATCH_NONE;
108 scoped_ptr<ui::PlatformEventSource> event_source_;
109 NativeViewportDelegate* delegate_;
112 Atom atom_wm_protocols_;
113 Atom atom_wm_delete_window_;
115 DISALLOW_COPY_AND_ASSIGN(NativeViewportX11);
119 scoped_ptr<NativeViewport> NativeViewport::Create(
120 shell::Context* context,
121 NativeViewportDelegate* delegate) {
122 return scoped_ptr<NativeViewport>(new NativeViewportX11(delegate)).Pass();
125 } // namespace services