Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / ozone / wayland / display.h
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Copyright 2013 Intel Corporation. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5
6 #ifndef OZONE_WAYLAND_DISPLAY_H_
7 #define OZONE_WAYLAND_DISPLAY_H_
8
9 #if !defined(__STDC_FORMAT_MACROS)
10 #define __STDC_FORMAT_MACROS
11 #endif
12
13 #include <wayland-client.h>
14 #include <list>
15 #include <map>
16
17 #include "base/basictypes.h"
18 #include "ozone/ui/events/window_state_change_handler.h"
19 #include "ozone/wayland/input/text-client-protocol.h"
20 #include "ui/ozone/public/surface_factory_ozone.h"
21
22 namespace ozonewayland {
23
24 class WaylandDisplayPollThread;
25 class WaylandInputDevice;
26 class WaylandScreen;
27 class WaylandShell;
28 class WaylandWindow;
29 struct wl_egl_window;
30
31 typedef std::map<unsigned, WaylandWindow*> WindowMap;
32
33 // WaylandDisplay is a wrapper around wl_display. Once we get a valid
34 // wl_display, the Wayland server will send different events to register
35 // the Wayland compositor, shell, screens, input devices, ...
36 class WaylandDisplay : public ui::WindowStateChangeHandler,
37                        public ui::SurfaceFactoryOzone {
38  public:
39   WaylandDisplay();
40   virtual ~WaylandDisplay();
41
42   // Ownership is not passed to the caller.
43   static WaylandDisplay* GetInstance() { return instance_; }
44
45   // Returns a pointer to wl_display.
46   wl_display* display() const { return display_; }
47
48   wl_registry* registry() const { return registry_; }
49
50   WaylandInputDevice* PrimaryInput() const { return primary_input_; }
51
52   // Returns a list of the registered screens.
53   const std::list<WaylandScreen*>& GetScreenList() const;
54   WaylandScreen* PrimaryScreen() const { return primary_screen_ ; }
55
56   WaylandShell* GetShell() const { return shell_; }
57
58   wl_shm* shm() const { return shm_; }
59   wl_compositor* GetCompositor() const { return compositor_; }
60   struct wl_text_input_manager* GetTextInputManager() const;
61
62   int GetDisplayFd() const { return wl_display_get_fd(display_); }
63   unsigned GetSerial() const { return serial_; }
64   void SetSerial(unsigned serial) { serial_ = serial; }
65   // Returns WaylandWindow associated with w. The ownership is not transferred
66   // to the caller.
67   WaylandWindow* GetWindow(unsigned window_handle) const;
68   gfx::AcceleratedWidget GetNativeWindow(unsigned window_handle);
69
70   // Destroys WaylandWindow whose handle is w.
71   void DestroyWindow(unsigned w);
72
73   // Does a round trip to Wayland server. This call blocks the current thread
74   // until all pending request are processed by the server.
75   void FlushDisplay();
76
77   bool InitializeHardware();
78
79   // Ozone Display implementation:
80   intptr_t GetNativeDisplay() override;
81
82   // Ownership is passed to the caller.
83   scoped_ptr<ui::SurfaceOzoneEGL> CreateEGLSurfaceForWidget(
84       gfx::AcceleratedWidget widget) override;
85
86   bool LoadEGLGLES2Bindings(
87       ui::SurfaceFactoryOzone::AddGLLibraryCallback add_gl_library,
88       ui::SurfaceFactoryOzone::SetGLGetProcAddressProcCallback
89       proc_address) override;
90   const int32* GetEGLSurfaceProperties(const int32* desired_list) override;
91
92   // WindowStateChangeHandler implementation:
93   void SetWidgetState(unsigned widget,
94                       ui::WidgetState state) override;
95   void SetWidgetTitle(unsigned w,
96                       const base::string16& title) override;
97   void SetWidgetCursor(int cursor_type) override;
98   void CreateWidget(unsigned widget,
99                     unsigned parent,
100                     unsigned x,
101                     unsigned y,
102                     ui::WidgetType type) override;
103
104   void LookAheadOutputGeometry();
105
106  private:
107   void InitializeDisplay();
108   // Creates a WaylandWindow backed by EGL Window and maps it to w. This can be
109   // useful for callers to track a particular surface. By default the type of
110   // surface(i.e. toplevel, menu) is none. One needs to explicitly call
111   // WaylandWindow::SetShellAttributes to set this. The ownership of
112   // WaylandWindow is not passed to the caller.
113   WaylandWindow* CreateAcceleratedSurface(unsigned w);
114
115   // Starts polling on display fd. This should be used when one needs to
116   // continuously read pending events coming from Wayland compositor and
117   // dispatch them. The polling is done completely on a separate thread and
118   // doesn't block the thread from which this is called.
119   void StartProcessingEvents();
120   // Stops polling on display fd.
121   void StopProcessingEvents();
122
123   void Terminate();
124   WaylandWindow* GetWidget(unsigned w) const;
125   // This handler resolves all server events used in initialization. It also
126   // handles input device registration, screen registration.
127   static void DisplayHandleGlobal(
128       void *data,
129       struct wl_registry *registry,
130       uint32_t name,
131       const char *interface,
132       uint32_t version);
133   // This handler resolves only screen registration. In general you don't want
134   // to use this but the one below.
135   static void DisplayHandleOutputOnly(
136       void *data,
137       struct wl_registry *registry,
138       uint32_t name,
139       const char *interface,
140       uint32_t version);
141
142   // WaylandDisplay manages the memory of all these pointers.
143   wl_display* display_;
144   wl_registry* registry_;
145   wl_compositor* compositor_;
146   WaylandShell* shell_;
147   wl_shm* shm_;
148   struct wl_text_input_manager* text_input_manager_;
149   WaylandScreen* primary_screen_;
150   WaylandScreen* look_ahead_screen_;
151   WaylandInputDevice* primary_input_;
152   WaylandDisplayPollThread* display_poll_thread_;
153
154   std::list<WaylandScreen*> screen_list_;
155   std::list<WaylandInputDevice*> input_list_;
156   WindowMap widget_map_;
157   unsigned serial_;
158   bool processing_events_;
159   static WaylandDisplay* instance_;
160   DISALLOW_COPY_AND_ASSIGN(WaylandDisplay);
161 };
162
163 }  // namespace ozonewayland
164
165 #endif  // OZONE_WAYLAND_DISPLAY_H_