Upstream version 5.34.92.0
[platform/framework/web/crosswalk.git] / src / mojo / services / native_viewport / native_viewport_x11.cc
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.
4
5 #include "mojo/services/native_viewport/native_viewport.h"
6
7 #include <X11/Xlib.h>
8
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"
13
14 namespace mojo {
15 namespace services {
16
17 class NativeViewportX11 : public NativeViewport,
18                           public base::MessagePumpDispatcher {
19  public:
20   NativeViewportX11(NativeViewportDelegate* delegate)
21       : delegate_(delegate) {
22   }
23
24   virtual ~NativeViewportX11() {
25     base::MessagePumpX11::Current()->RemoveDispatcherForRootWindow(this);
26     base::MessagePumpX11::Current()->RemoveDispatcherForWindow(window_);
27
28     XDestroyWindow(gfx::GetXDisplay(), window_);
29   }
30
31  private:
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;
38     bounds_ = bounds;
39     window_ = XCreateWindow(
40         display,
41         DefaultRootWindow(display),
42         bounds_.x(), bounds_.y(), bounds_.width(), bounds_.height(),
43         0,  // border width
44         CopyFromParent,  // depth
45         InputOutput,
46         CopyFromParent,  // visual
47         CWBackPixmap | CWOverrideRedirect, &swa);
48
49     base::MessagePumpX11::Current()->AddDispatcherForWindow(this, window_);
50     base::MessagePumpX11::Current()->AddDispatcherForRootWindow(this);
51
52     delegate_->OnAcceleratedWidgetAvailable(window_);
53   }
54
55   virtual void Show() OVERRIDE {
56     XDisplay* display = gfx::GetXDisplay();
57     XMapWindow(display, window_);
58     XFlush(display);
59   }
60
61   virtual void Close() OVERRIDE {
62     // TODO(beng): perform this in response to XWindow destruction.
63     delegate_->OnDestroyed();
64   }
65
66   virtual gfx::Size GetSize() OVERRIDE {
67     return bounds_.size();
68   }
69
70   virtual void SetBounds(const gfx::Rect& bounds) OVERRIDE {
71     NOTIMPLEMENTED();
72   }
73
74   virtual void SetCapture() OVERRIDE {
75     NOTIMPLEMENTED();
76   }
77
78   virtual void ReleaseCapture() OVERRIDE {
79     NOTIMPLEMENTED();
80   }
81
82   // Overridden from base::MessagePumpDispatcher:
83   virtual bool Dispatch(const base::NativeEvent& event) OVERRIDE {
84     return true;
85   }
86
87   NativeViewportDelegate* delegate_;
88   gfx::Rect bounds_;
89   XID window_;
90
91   DISALLOW_COPY_AND_ASSIGN(NativeViewportX11);
92 };
93
94 // static
95 scoped_ptr<NativeViewport> NativeViewport::Create(
96     shell::Context* context,
97     NativeViewportDelegate* delegate) {
98   return scoped_ptr<NativeViewport>(new NativeViewportX11(delegate)).Pass();
99 }
100
101 }  // namespace services
102 }  // namespace mojo