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 "base/message_loop/message_pump_x11.h"
11 #include "ui/gfx/rect.h"
12 #include "ui/gfx/x/x11_types.h"
17 class NativeViewportX11 : public NativeViewport,
18 public base::MessagePumpDispatcher {
20 NativeViewportX11(NativeViewportDelegate* delegate)
21 : delegate_(delegate) {
24 virtual ~NativeViewportX11() {
25 base::MessagePumpX11::Current()->RemoveDispatcherForRootWindow(this);
26 base::MessagePumpX11::Current()->RemoveDispatcherForWindow(window_);
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 base::MessagePumpX11::Current()->AddDispatcherForWindow(this, window_);
57 base::MessagePumpX11::Current()->AddDispatcherForRootWindow(this);
59 delegate_->OnAcceleratedWidgetAvailable(window_);
62 virtual void Show() OVERRIDE {
63 XDisplay* display = gfx::GetXDisplay();
64 XMapWindow(display, window_);
68 virtual void Hide() OVERRIDE {
69 XWithdrawWindow(gfx::GetXDisplay(), window_, 0);
72 virtual void Close() OVERRIDE {
73 // TODO(beng): perform this in response to XWindow destruction.
74 delegate_->OnDestroyed();
77 virtual gfx::Size GetSize() OVERRIDE {
78 return bounds_.size();
81 virtual void SetBounds(const gfx::Rect& bounds) OVERRIDE {
85 virtual void SetCapture() OVERRIDE {
89 virtual void ReleaseCapture() OVERRIDE {
93 // Overridden from base::MessagePumpDispatcher:
94 virtual uint32_t Dispatch(const base::NativeEvent& event) OVERRIDE {
95 switch (event->type) {
97 if (event->xclient.message_type == atom_wm_protocols_) {
98 Atom protocol = static_cast<Atom>(event->xclient.data.l[0]);
99 if (protocol == atom_wm_delete_window_)
100 delegate_->OnDestroyed();
105 return POST_DISPATCH_NONE;
108 NativeViewportDelegate* delegate_;
111 Atom atom_wm_protocols_;
112 Atom atom_wm_delete_window_;
114 DISALLOW_COPY_AND_ASSIGN(NativeViewportX11);
118 scoped_ptr<NativeViewport> NativeViewport::Create(
119 shell::Context* context,
120 NativeViewportDelegate* delegate) {
121 return scoped_ptr<NativeViewport>(new NativeViewportX11(delegate)).Pass();
124 } // namespace services