Upstream version 5.34.104.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
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     base::MessagePumpX11::Current()->AddDispatcherForWindow(this, window_);
57     base::MessagePumpX11::Current()->AddDispatcherForRootWindow(this);
58
59     delegate_->OnAcceleratedWidgetAvailable(window_);
60   }
61
62   virtual void Show() OVERRIDE {
63     XDisplay* display = gfx::GetXDisplay();
64     XMapWindow(display, window_);
65     XFlush(display);
66   }
67
68   virtual void Hide() OVERRIDE {
69     XWithdrawWindow(gfx::GetXDisplay(), window_, 0);
70   }
71
72   virtual void Close() OVERRIDE {
73     // TODO(beng): perform this in response to XWindow destruction.
74     delegate_->OnDestroyed();
75   }
76
77   virtual gfx::Size GetSize() OVERRIDE {
78     return bounds_.size();
79   }
80
81   virtual void SetBounds(const gfx::Rect& bounds) OVERRIDE {
82     NOTIMPLEMENTED();
83   }
84
85   virtual void SetCapture() OVERRIDE {
86     NOTIMPLEMENTED();
87   }
88
89   virtual void ReleaseCapture() OVERRIDE {
90     NOTIMPLEMENTED();
91   }
92
93   // Overridden from base::MessagePumpDispatcher:
94   virtual uint32_t Dispatch(const base::NativeEvent& event) OVERRIDE {
95     switch (event->type) {
96       case ClientMessage: {
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();
101         }
102         break;
103       }
104     }
105     return POST_DISPATCH_NONE;
106   }
107
108   NativeViewportDelegate* delegate_;
109   gfx::Rect bounds_;
110   XID window_;
111   Atom atom_wm_protocols_;
112   Atom atom_wm_delete_window_;
113
114   DISALLOW_COPY_AND_ASSIGN(NativeViewportX11);
115 };
116
117 // static
118 scoped_ptr<NativeViewport> NativeViewport::Create(
119     shell::Context* context,
120     NativeViewportDelegate* delegate) {
121   return scoped_ptr<NativeViewport>(new NativeViewportX11(delegate)).Pass();
122 }
123
124 }  // namespace services
125 }  // namespace mojo