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/ui/gfx/ozone_display.h"
20 #include "ozone/wayland/input/text-client-protocol.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 gfx::OzoneDisplay {
39 enum RegistrationType {
40 RegisterAsNeeded, // Handles all the required registrations.
41 RegisterOutputOnly // Only screen registration.
44 explicit WaylandDisplay(RegistrationType type = RegisterAsNeeded);
45 virtual ~WaylandDisplay();
47 // Ownership is not passed to the caller.
48 static WaylandDisplay* GetInstance() { return instance_; }
50 // Returns a pointer to wl_display.
51 wl_display* display() const { return display_; }
53 wl_registry* registry() const { return registry_; }
55 WaylandInputDevice* PrimaryInput() const { return primary_input_; }
57 // Returns a list of the registered screens.
58 const std::list<WaylandScreen*>& GetScreenList() const;
59 WaylandScreen* PrimaryScreen() const { return primary_screen_ ; }
61 WaylandShell* GetShell() const { return shell_; }
63 wl_shm* shm() const { return shm_; }
64 wl_compositor* GetCompositor() const { return compositor_; }
65 struct wl_text_input_manager* GetTextInputManager() const;
67 int GetDisplayFd() const { return wl_display_get_fd(display_); }
68 unsigned GetSerial() const { return serial_; }
69 void SetSerial(unsigned serial) { serial_ = serial; }
70 // Returns WaylandWindow associated with w. The ownership is not transferred
72 WaylandWindow* GetWindow(unsigned window_handle) const;
73 gfx::AcceleratedWidget GetNativeWindow(unsigned window_handle);
75 // Destroys WaylandWindow whose handle is w.
76 void DestroyWindow(unsigned w);
78 // Does a round trip to Wayland server. This call blocks the current thread
79 // until all pending request are processed by the server.
83 // Ozone Display implementation:
84 virtual gfx::SurfaceFactoryOzone::HardwareState InitializeHardware() OVERRIDE;
85 virtual void ShutdownHardware() OVERRIDE;
86 virtual intptr_t GetNativeDisplay() OVERRIDE;
88 virtual gfx::AcceleratedWidget GetAcceleratedWidget() OVERRIDE;
89 virtual void LookAheadOutputGeometry() OVERRIDE;
91 // Ownership is passed to the caller.
92 virtual scoped_ptr<gfx::SurfaceOzoneEGL> CreateEGLSurfaceForWidget(
93 gfx::AcceleratedWidget widget) OVERRIDE;
95 virtual bool LoadEGLGLES2Bindings(
96 gfx::SurfaceFactoryOzone::AddGLLibraryCallback add_gl_library,
97 gfx::SurfaceFactoryOzone::SetGLGetProcAddressProcCallback
98 proc_address) OVERRIDE;
99 virtual const int32* GetEGLSurfaceProperties(
100 const int32* desired_list) OVERRIDE;
102 // WindowStateChangeHandler implementation:
103 virtual void SetWidgetState(unsigned widget,
104 ui::WidgetState state,
106 unsigned height = 0) OVERRIDE;
107 virtual void SetWidgetTitle(unsigned w,
108 const base::string16& title) OVERRIDE;
109 virtual void SetWidgetCursor(int cursor_type) OVERRIDE;
110 virtual void SetWidgetAttributes(unsigned widget,
114 ui::WidgetType type) OVERRIDE;
117 void InitializeDisplay(RegistrationType type);
118 // Creates a WaylandWindow backed by EGL Window and maps it to w. This can be
119 // useful for callers to track a particular surface. By default the type of
120 // surface(i.e. toplevel, menu) is none. One needs to explicitly call
121 // WaylandWindow::SetShellAttributes to set this. The ownership of
122 // WaylandWindow is not passed to the caller.
123 WaylandWindow* CreateAcceleratedSurface(unsigned w);
125 // Starts polling on display fd. This should be used when one needs to
126 // continuously read pending events coming from Wayland compositor and
127 // dispatch them. The polling is done completely on a separate thread and
128 // doesn't block the thread from which this is called.
129 void StartProcessingEvents();
130 // Stops polling on display fd.
131 void StopProcessingEvents();
134 WaylandWindow* GetWidget(unsigned w) const;
135 // This handler resolves all server events used in initialization. It also
136 // handles input device registration, screen registration.
137 static void DisplayHandleGlobal(
139 struct wl_registry *registry,
141 const char *interface,
143 // This handler resolves only screen registration. In general you don't want
144 // to use this but the one below.
145 static void DisplayHandleOutputOnly(
147 struct wl_registry *registry,
149 const char *interface,
152 // WaylandDisplay manages the memory of all these pointers.
153 wl_display* display_;
154 wl_registry* registry_;
155 wl_compositor* compositor_;
156 WaylandShell* shell_;
158 struct wl_text_input_manager* text_input_manager_;
159 WaylandScreen* primary_screen_;
160 WaylandInputDevice* primary_input_;
161 WaylandDisplayPollThread* display_poll_thread_;
163 std::list<WaylandScreen*> screen_list_;
164 std::list<WaylandInputDevice*> input_list_;
165 WindowMap widget_map_;
167 bool processing_events_;
168 static WaylandDisplay* instance_;
169 DISALLOW_COPY_AND_ASSIGN(WaylandDisplay);
172 } // namespace ozonewayland
174 #endif // OZONE_WAYLAND_DISPLAY_H_