Upstream version 7.36.149.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 "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"
14
15 namespace mojo {
16 namespace services {
17
18 class NativeViewportX11 : public NativeViewport,
19                           public ui::PlatformEventDispatcher {
20  public:
21   NativeViewportX11(NativeViewportDelegate* delegate)
22       : delegate_(delegate) {
23   }
24
25   virtual ~NativeViewportX11() {
26     ui::PlatformEventSource::GetInstance()->RemovePlatformEventDispatcher(this);
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
36     XSetWindowAttributes swa;
37     memset(&swa, 0, sizeof(swa));
38     swa.override_redirect = False;
39
40     bounds_ = bounds;
41     window_ = XCreateWindow(
42         display,
43         DefaultRootWindow(display),
44         bounds_.x(), bounds_.y(), bounds_.width(), bounds_.height(),
45         0,  // border width
46         CopyFromParent,  // depth
47         InputOutput,
48         CopyFromParent,  // visual
49         CWBackPixmap | CWOverrideRedirect,
50         &swa);
51
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);
55
56     event_source_.reset(ui::PlatformEventSource::GetInstance());
57     if (!event_source_.get())
58       event_source_ = ui::PlatformEventSource::CreateDefault();
59     ui::PlatformEventSource::GetInstance()->AddPlatformEventDispatcher(this);
60
61     delegate_->OnAcceleratedWidgetAvailable(window_);
62   }
63
64   virtual void Show() OVERRIDE {
65     XDisplay* display = gfx::GetXDisplay();
66     XMapWindow(display, window_);
67     XFlush(display);
68   }
69
70   virtual void Hide() OVERRIDE {
71     XWithdrawWindow(gfx::GetXDisplay(), window_, 0);
72   }
73
74   virtual void Close() OVERRIDE {
75     // TODO(beng): perform this in response to XWindow destruction.
76     delegate_->OnDestroyed();
77   }
78
79   virtual gfx::Size GetSize() OVERRIDE {
80     return bounds_.size();
81   }
82
83   virtual void SetBounds(const gfx::Rect& bounds) OVERRIDE {
84     NOTIMPLEMENTED();
85   }
86
87   virtual void SetCapture() OVERRIDE {
88     NOTIMPLEMENTED();
89   }
90
91   virtual void ReleaseCapture() OVERRIDE {
92     NOTIMPLEMENTED();
93   }
94
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_;
99   }
100
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;
106   }
107
108   scoped_ptr<ui::PlatformEventSource> event_source_;
109   NativeViewportDelegate* delegate_;
110   gfx::Rect bounds_;
111   XID window_;
112   Atom atom_wm_protocols_;
113   Atom atom_wm_delete_window_;
114
115   DISALLOW_COPY_AND_ASSIGN(NativeViewportX11);
116 };
117
118 // static
119 scoped_ptr<NativeViewport> NativeViewport::Create(
120     shell::Context* context,
121     NativeViewportDelegate* delegate) {
122   return scoped_ptr<NativeViewport>(new NativeViewportX11(delegate)).Pass();
123 }
124
125 }  // namespace services
126 }  // namespace mojo