2 * Copyright (c) 2013, TOYOTA MOTOR CORPORATION.
4 * This program is licensed under the terms and conditions of the
5 * Apache License, version 2.0. The full text of the Apache License is at
6 * http://www.apache.org/licenses/LICENSE-2.0
10 //==========================================================================
12 * @file CicoSCWayland.cpp
14 * @brief This file implementation of CicoSCWayland class
16 //==========================================================================
24 #include <sys/ioctl.h>
30 #include <wayland-client.h>
31 #include <ilm/ilm_control.h>
33 #include "CicoSCWayland.h"
34 #include "CicoSCWlWinMgrIF.h"
35 #include "CicoSCWindowController.h"
37 #include "ico_syc_error.h"
39 //==========================================================================
40 // private static variable
41 //==========================================================================
42 CicoSCWayland* CicoSCWayland::ms_myInstance = NULL;
44 //--------------------------------------------------------------------------
46 * @brief default constructor
48 //--------------------------------------------------------------------------
49 CicoSCWayland::CicoSCWayland()
53 m_ecoreFdHandler(NULL)
56 m_wlListener.global = wlGlobalCB;
57 m_wlListener.global_remove = NULL;
59 CicoSCWayland::ms_myInstance = this;
62 //--------------------------------------------------------------------------
66 //--------------------------------------------------------------------------
67 CicoSCWayland::~CicoSCWayland()
69 CicoSCWayland::ms_myInstance = NULL;
72 //--------------------------------------------------------------------------
74 * @brief get instance of CicoSCWayland
76 * @return instance of CicoSCWayland
78 //--------------------------------------------------------------------------
80 CicoSCWayland::getInstance(void)
82 if (NULL == ms_myInstance) {
83 ms_myInstance = new CicoSCWayland();
89 //--------------------------------------------------------------------------
91 * @brief initialize wayland connection
93 * @return ICO_SYC_EOK on success, other on error
95 //--------------------------------------------------------------------------
97 CicoSCWayland::initialize(void)
99 ICO_TRA("CicoSCWayland::initialize Enter");
101 // initialize genivi ivi-shell
102 if (ilm_init() != ILM_SUCCESS) {
103 ICO_ERR("ilm_init failed.");
104 ICO_TRA("CicoSCWayland::initialize Leave(EIO)");
108 for (int i = 0; i < (5000/50); ++i) {
109 m_wlDisplay = wl_display_connect(NULL);
110 if (NULL != m_wlDisplay) {
116 if (NULL == m_wlDisplay) {
117 ICO_ERR("wl_display_connect failed.");
118 ICO_TRA("CicoSCWayland::initialize Leave(EIO)");
122 ICO_DBG("called: wl_display_get_registry(wlDisplay=0x%08x)", m_wlDisplay);
123 m_wlRegistry = wl_display_get_registry(m_wlDisplay);
124 if (NULL == m_wlRegistry) {
125 ICO_ERR("wl_display_get_registry failed.");
126 ICO_TRA("CicoSCWayland::initialize Leave(EIO)");
130 // add wayland global callbacks
131 ICO_DBG("called: wl_registry_add_listener");
132 wl_registry_add_listener(m_wlRegistry, &m_wlListener, this);
135 int ifcount = m_wlInterfaceList.size();
136 for (int i = 0; i < (500/20); ++i) {
138 map<string, CicoSCWaylandIF*>::iterator itr;
139 itr = m_wlInterfaceList.begin();
140 for (; itr != m_wlInterfaceList.end(); ++itr) {
141 if(true == itr->second->isInitialized()) {
145 if (ifcount == initCount) {
148 ICO_DBG("called: wl_display_dispatch"
149 "(wlDisplay=0x%08x)", (int)m_wlDisplay);
150 wl_display_dispatch(m_wlDisplay);
154 if (ifcount != initCount) {
155 ICO_ERR("initialize interface time up NG");
156 ICO_TRA("CicoSCWayland::initialize Leave(EIO)");
160 ICO_DBG("initialize interface all OK");
164 ICO_DBG("called: wl_display_flush(wlDisplay=0x%08x)", (int)m_wlDisplay);
165 wl_display_flush(m_wlDisplay);
167 ICO_DBG("called: wl_display_get_fd(wlDisplay=0x%08x)", (int)m_wlDisplay);
168 m_wlFd = wl_display_get_fd(m_wlDisplay);
169 ICO_DBG("CicoSCWayland::initialize: Wayland/Weston fd=%d", m_wlFd);
171 ICO_DBG("CicoSCWayland::initialize: Wayland/Weston connect OK");
173 // initialize genivi callbacks
174 CicoSCWindowController::getInstance()->initializeGeniviLMS();
176 ICO_TRA("CicoSCWayland::initialize: Leave(EOK)");
180 //--------------------------------------------------------------------------
182 * @brief flush display
184 //--------------------------------------------------------------------------
186 CicoSCWayland::flushDisplay(void)
188 if (NULL == m_wlDisplay) {
189 ICO_ERR("wlDisplay == NULL");
192 // ICO_DBG("called: wl_display_flush(0x%08x)", (int)m_wlDisplay);
193 int wlret = wl_display_flush(m_wlDisplay);
195 int error = wl_display_get_error(m_wlDisplay);
196 ICO_ERR("wayland error(%d)", error);
200 //--------------------------------------------------------------------------
202 * @brief dispatch display
204 //--------------------------------------------------------------------------
206 CicoSCWayland::dispatchDisplay(void)
208 if (NULL == m_wlDisplay) {
209 ICO_ERR("wlDisplay == NULL");
212 // ICO_DBG("call wl_display_dispatch(0x%08x)", (int)m_wlDisplay);
213 int wlret = wl_display_dispatch(m_wlDisplay);
215 int error = wl_display_get_error(m_wlDisplay);
216 ICO_ERR("wl_display_dispatch failed. error(%d)", error);
218 if (error == EINVAL) {
224 //--------------------------------------------------------------------------
226 * @brief get wayland error
228 //--------------------------------------------------------------------------
230 CicoSCWayland::getError(void)
232 if (NULL == m_wlDisplay) {
233 ICO_ERR("wlDisplay == NULL");
237 ICO_DBG("fd=%d", wl_display_get_fd(m_wlDisplay));
238 return wl_display_get_error(m_wlDisplay);
241 //--------------------------------------------------------------------------
243 * @brief get wayland file destructor
245 * @return wayland file destructor
247 //--------------------------------------------------------------------------
248 int CicoSCWayland::getWlFd(void)
253 //--------------------------------------------------------------------------
255 * @brief add wayland file destructor handler to ecore main
257 * @return ICO_SYC_EOK on success, other on error
258 * @retval ICO_SYC_EIO ecore_main_fd_handler_add failed
260 //--------------------------------------------------------------------------
262 CicoSCWayland::addEcoreMainWlFdHandler(void)
264 ICO_TRA("CicoSCWayland::addEcoreMainWlFdHandler Enter");
265 ICO_DBG("called: ecore_main_fd_handler_add(fd=%d)", m_wlFd);
266 m_ecoreFdHandler = ecore_main_fd_handler_add(m_wlFd,
272 if (NULL == m_ecoreFdHandler) {
273 ICO_ERR("ecore_main_fd_handler_add failed.");
274 ICO_TRA("CicoSCWayland::addEcoreMainWlFdHandler Leave(EIO)");
277 ecore_main_loop_iterate();
280 if (ioctl(m_wlFd, FIONREAD, &arg) < 0) {
281 ICO_WRN("ioclt(FIONREAD) failed. errno=%d:%s", errno, strerror(errno));
285 CicoSCWayland::getInstance()->dispatchDisplay();
288 ICO_TRA("CicoSCWayland::addEcoreMainWlFdHandler Leave");
292 //--------------------------------------------------------------------------
294 * @brief add wayland interface
296 * @param [in] name interface name
297 * @param [in] interface interface instance
299 //--------------------------------------------------------------------------
301 CicoSCWayland::addWaylandIF(const char *name, CicoSCWaylandIF* interface)
303 m_wlInterfaceList[name] = interface;
306 //--------------------------------------------------------------------------
308 * @brief wayland global callback
310 * @param [in] data user data(unused)
311 * @param [in] registry wayland registry
312 * @param [in] name wayland display Id(unused)
313 * @param [in] interface wayland interface name
314 * @param [in] version wayland interface version number(unused)
316 //--------------------------------------------------------------------------
318 CicoSCWayland::globalCB(void *data,
319 struct wl_registry *registry,
321 const char *interface,
324 map<string, CicoSCWaylandIF*>::iterator itr;
325 itr = m_wlInterfaceList.find(interface);
327 if (m_wlInterfaceList.end() == itr) {
328 ICO_DBG("skip interface=%s", interface);
332 CicoSCWaylandIF *wlIF = itr->second;
333 wlIF->initInterface(data, registry, name, interface, version);
336 wl_display_flush(m_wlDisplay);
339 //--------------------------------------------------------------------------
341 * @brief wayland global callback
343 * @param [in] data user data
344 * @param [in] registry wayland registry
345 * @param [in] name wayland display Id(unused)
346 * @param [in] interface wayland interface name
347 * @param [in] version wayland interface version number(unused)
349 //--------------------------------------------------------------------------
351 CicoSCWayland::wlGlobalCB(void *data,
352 struct wl_registry *registry,
354 const char *interface,
358 ICO_ERR("data == NULL");
362 static_cast<CicoSCWayland*>(data)->globalCB(data, registry, name,
366 //--------------------------------------------------------------------------
368 * @brief wayland global callback
370 * @param [in] data user data
371 * @param [in] handler ecore file destructor handler
373 * @return ECORE_CALLBACK_RENEW on retry , ECORE_CALLBACK_CANCEL on cancel
375 //--------------------------------------------------------------------------
377 CicoSCWayland::waylandFdHandler(void *data, Ecore_Fd_Handler *handler)
379 // ICO_TRA("waylandFdHandler: Enter");
381 CicoSCWayland::getInstance()->flushDisplay();
384 if (ioctl(CicoSCWayland::getInstance()->getWlFd(), FIONREAD, &arg) < 0) {
385 ICO_WRN("ioclt(FIONREAD) failed. errno=%d:%s", errno, strerror(errno));
389 // ICO_DBG("waylandFdHandler: arg(%d))", arg);
391 CicoSCWayland::getInstance()->dispatchDisplay();
395 ICO_ERR("wayland fd read error.");
396 ICO_DBG("called: ecore_main_loop_quit()");
397 ecore_main_loop_quit();
398 return ECORE_CALLBACK_CANCEL;
401 CicoSCWayland::getInstance()->flushDisplay();
402 // ICO_TRA("waylandFdHandler: Leave");
403 return ECORE_CALLBACK_RENEW ;
405 // vim:set expandtab ts=4 sw=4: