1 /***************************************************************************
3 * Copyright 2010, 2011 BMW Car IT GmbH
4 * Copyright (C) 2011 DENSO CORPORATION and Robert Bosch Car Multimedia Gmbh
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
20 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
21 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
22 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
23 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
24 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
25 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
26 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
28 ****************************************************************************/
30 #ifndef _WAYLANDBASEWINDOWSYSTEM_H_
31 #define _WAYLANDBASEWINDOWSYSTEM_H_
32 #include "WindowSystems/BaseWindowSystem.h"
33 #include "GraphicSystems/BaseGraphicSystem.h"
35 #include <wayland-server.h>
36 #include <wayland-client.h>
37 #include "PlatformSurfaces/WaylandPlatformSurface.h"
39 #include "ScreenShotType.h"
41 #include "InputManager.h"
42 #include "WindowSystems/WaylandInputEvent.h"
43 #include "Rectangle.h"
47 typedef enum waylandWindowSystemStates
53 } WaylandWindowSystemStates;
55 typedef struct _wlevent
60 enum wl_pointer_button_state buttonState;
62 enum wl_keyboard_key_state keyState;
71 struct native_surface;
73 class WaylandBaseWindowSystem : public BaseWindowSystem
76 WaylandBaseWindowSystem(const char* displayname, int width, int height, Scene* pScene, InputManager* pInputManager);
77 virtual ~WaylandBaseWindowSystem();
78 bool init(BaseGraphicSystem<void*, void*>* sys);
79 bool start(int maxIterationDurationInMS);
81 void signalRedrawEvent();
82 void wakeUpRendererThread();
83 void setSystemState(WaylandWindowSystemStates state); // TODO:don't check state
84 WaylandWindowSystemStates getSystemState();
85 struct wl_display* getNativeDisplayHandle();
86 virtual void allocatePlatformSurface(Surface *surface);
87 virtual void deallocatePlatformSurface(Surface *surface);
88 void doScreenShot(std::string fileName, const uint screen_id);
89 void doScreenShotOfLayer(std::string fileName, const uint id);
90 void doScreenShotOfSurface(std::string fileName, const uint id, const uint layer_id);
91 int getWindowWidth() const;
92 int getWindowHeight() const;
93 virtual void finishFrame();
94 virtual void scheduleRepaint(void *data);
97 struct wl_display* m_wlDisplay;
98 struct wl_display* m_wlDisplayClient;
99 struct wl_registry* m_wlRegistryClient;
100 struct wl_compositor* m_wlCompositorClient;
101 struct wl_surface* m_wlSurfaceClient;
102 pthread_t renderThread;
103 pthread_mutex_t run_lock;
104 BaseGraphicSystem<void*, void*>* graphicSystem;
105 virtual void RedrawAllLayers(bool clear, bool swap);
106 virtual void renderHWLayer(Layer* layer);
107 virtual bool initCompositor();
108 struct wl_shm* m_wlShm;
109 struct wl_global* m_serverInfoGlobal;
111 struct wl_global* m_wlCompositorGlobal;
112 virtual bool createNativeContext() = 0;
113 virtual bool initGraphicSystem() = 0;
116 ScreenShotType m_takeScreenshot;
117 const char* m_displayname;
119 WaylandWindowSystemStates m_systemState;
120 uint m_manageConnectionId;
121 std::string m_screenShotFile;
122 uint m_screenShotScreenID;
123 uint m_screenShotSurfaceID;
124 uint m_screenShotLayerID;
129 bool m_bRepaintNeeded;
130 bool m_bRepaintScheduled;
131 bool m_bUseFrameTimer;
132 struct wl_event_source* m_finishFrameTimer;
134 struct wl_list m_listFrameCallback;
135 struct wl_list m_nativeSurfaceList;
137 int m_maxIterationDurationInMS;
139 void createServerinfo(WaylandBaseWindowSystem* windowSystem);
140 struct native_surface* createNativeSurface();
141 void postReleaseBuffer(struct wl_buffer *buffer);
142 virtual void attachBufferToNativeSurface(struct wl_buffer* buffer, struct wl_surface* surface);
143 void repaint(int msecs);
147 void shutdownCompositor();
148 Surface* getSurfaceFromNativeSurface(struct native_surface* nativeSurface);
149 struct native_surface* getNativeSurfaceFromSurface(Surface* surface);
150 virtual void checkForNewSurfaceNativeContent();
152 void calculateSurfaceFps(Surface *currentSurface, float time);
156 static void bindCompositor(struct wl_client* client, void* data, uint32_t version, uint32_t id);
157 static int signalEventOnTerm(int signal_number, void *data);
158 static void destroyListenerSurfaceBuffer(struct wl_listener* listener, void *data);
159 static void destroyListenerSurfacePendingBuffer(struct wl_listener* listener, void *data);
160 static void idleEventRepaint(void *data);
161 bool createWaylandClient();
162 void releaseWaylandClient();
164 WaylandInputEvent* m_inputEvent;
165 virtual bool createInputEvent();
168 static void serverinfoIFCreateConnection(struct wl_client *client, struct wl_resource *resource);
169 static void bindServerinfo(struct wl_client *client, void *data, uint32_t version, uint32_t id);
170 static uint32_t getTime(void);
171 static void destroySurfaceCallback(struct wl_resource* resource);
172 static void destroyFrameCallback(struct wl_resource *resource);
173 static void* eventLoopCallback(void* ptr);
174 static void registryHandleGlobalClient(void* data,
175 struct wl_registry* registry,
177 const char* interface,
179 static void surfaceListenerFrame(void* data, struct wl_callback* callback, uint32_t time);
180 static int finishFrameHandler(void *data);
182 // wl_surface interface
183 static void surfaceIFDestroy(struct wl_client *client,
184 struct wl_resource *resource);
185 static void surfaceIFAttach(struct wl_client *client,
186 struct wl_resource *resource,
187 struct wl_resource *buffer_resource,
190 static void surfaceIFDamage(struct wl_client *client,
191 struct wl_resource *resource,
196 static void surfaceIFFrame(struct wl_client *client,
197 struct wl_resource *resource,
199 static void surfaceIFCommit(struct wl_client *client,
200 struct wl_resource *resource);
202 // wl_compositor interface
203 static void compositorIFCreateSurface(struct wl_client *client, struct wl_resource* resource, uint32_t id);
205 struct wl_list m_connectionList;
208 void manageWLInputEvent(const InputDevice type, const InputEventState state, const WLEvent *wlEvent);
211 inline void WaylandBaseWindowSystem::setSystemState(WaylandWindowSystemStates state)
213 m_systemState = state;
216 inline WaylandWindowSystemStates WaylandBaseWindowSystem::getSystemState()
218 return m_systemState;
221 inline struct wl_display* WaylandBaseWindowSystem::getNativeDisplayHandle()
226 inline bool WaylandBaseWindowSystem::createInputEvent()
231 inline int WaylandBaseWindowSystem::getWindowWidth() const
236 inline int WaylandBaseWindowSystem::getWindowHeight() const
242 struct native_surface
244 struct wl_surface* surface;
245 WaylandBaseWindowSystem* windowSystem;
246 struct wl_buffer* buffer;
249 uint32_t visual; // TODO confirm:it's necessary or not
250 struct wl_listener buffer_destroy_listener;
253 /* All the pending state, that wl_surface.commit will apply. */
256 /* wl_surface.attach */
258 struct wl_buffer *buffer;
259 struct wl_listener buffer_destroy_listener;
263 /* wl_surface.damage */
267 /* wl_surface.frame */
268 struct wl_list frame_callback_list;
272 struct native_frame_callback
274 struct wl_resource* resource;
278 struct native_process;
279 typedef void (*cleanupFuncForNativeProcess)(struct native_process* process, int status);
281 struct native_process
284 cleanupFuncForNativeProcess cleanup;
289 #endif /* _WAYLANDBASEWINDOWSYSTEM_H_ */