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();
35 XSetWindowAttributes swa;
36 memset(&swa, 0, sizeof(swa));
37 swa.override_redirect = False;
39 window_ = XCreateWindow(
41 DefaultRootWindow(display),
42 bounds_.x(), bounds_.y(), bounds_.width(), bounds_.height(),
44 CopyFromParent, // depth
46 CopyFromParent, // visual
47 CWBackPixmap | CWOverrideRedirect, &swa);
49 base::MessagePumpX11::Current()->AddDispatcherForWindow(this, window_);
50 base::MessagePumpX11::Current()->AddDispatcherForRootWindow(this);
52 delegate_->OnAcceleratedWidgetAvailable(window_);
55 virtual void Show() OVERRIDE {
56 XDisplay* display = gfx::GetXDisplay();
57 XMapWindow(display, window_);
61 virtual void Close() OVERRIDE {
62 // TODO(beng): perform this in response to XWindow destruction.
63 delegate_->OnDestroyed();
66 virtual gfx::Size GetSize() OVERRIDE {
67 return bounds_.size();
70 virtual void SetBounds(const gfx::Rect& bounds) OVERRIDE {
74 virtual void SetCapture() OVERRIDE {
78 virtual void ReleaseCapture() OVERRIDE {
82 // Overridden from base::MessagePumpDispatcher:
83 virtual bool Dispatch(const base::NativeEvent& event) OVERRIDE {
87 NativeViewportDelegate* delegate_;
91 DISALLOW_COPY_AND_ASSIGN(NativeViewportX11);
95 scoped_ptr<NativeViewport> NativeViewport::Create(
96 shell::Context* context,
97 NativeViewportDelegate* delegate) {
98 return scoped_ptr<NativeViewport>(new NativeViewportX11(delegate)).Pass();
101 } // namespace services