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.
6 #ifndef OZONE_WAYLAND_DISPLAY_H_
7 #define OZONE_WAYLAND_DISPLAY_H_
9 #if !defined(__STDC_FORMAT_MACROS)
10 #define __STDC_FORMAT_MACROS
13 #include <wayland-client.h>
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"
22 namespace ozonewayland {
24 class WaylandDisplayPollThread;
25 class WaylandInputDevice;
31 typedef std::map<unsigned, WaylandWindow*> WindowMap;
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 {
40 virtual ~WaylandDisplay();
42 // Ownership is not passed to the caller.
43 static WaylandDisplay* GetInstance() { return instance_; }
45 // Returns a pointer to wl_display.
46 wl_display* display() const { return display_; }
48 wl_registry* registry() const { return registry_; }
50 WaylandInputDevice* PrimaryInput() const { return primary_input_; }
52 // Returns a list of the registered screens.
53 const std::list<WaylandScreen*>& GetScreenList() const;
54 WaylandScreen* PrimaryScreen() const { return primary_screen_ ; }
56 WaylandShell* GetShell() const { return shell_; }
58 wl_shm* shm() const { return shm_; }
59 wl_compositor* GetCompositor() const { return compositor_; }
60 struct wl_text_input_manager* GetTextInputManager() const;
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
67 WaylandWindow* GetWindow(unsigned window_handle) const;
68 gfx::AcceleratedWidget GetNativeWindow(unsigned window_handle);
70 // Destroys WaylandWindow whose handle is w.
71 void DestroyWindow(unsigned w);
73 // Does a round trip to Wayland server. This call blocks the current thread
74 // until all pending request are processed by the server.
77 bool InitializeHardware();
79 // Ozone Display implementation:
80 intptr_t GetNativeDisplay() override;
82 // Ownership is passed to the caller.
83 scoped_ptr<ui::SurfaceOzoneEGL> CreateEGLSurfaceForWidget(
84 gfx::AcceleratedWidget widget) override;
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;
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,
102 ui::WidgetType type) override;
104 void LookAheadOutputGeometry();
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);
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();
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(
129 struct wl_registry *registry,
131 const char *interface,
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(
137 struct wl_registry *registry,
139 const char *interface,
142 // WaylandDisplay manages the memory of all these pointers.
143 wl_display* display_;
144 wl_registry* registry_;
145 wl_compositor* compositor_;
146 WaylandShell* shell_;
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_;
154 std::list<WaylandScreen*> screen_list_;
155 std::list<WaylandInputDevice*> input_list_;
156 WindowMap widget_map_;
158 bool processing_events_;
159 static WaylandDisplay* instance_;
160 DISALLOW_COPY_AND_ASSIGN(WaylandDisplay);
163 } // namespace ozonewayland
165 #endif // OZONE_WAYLAND_DISPLAY_H_