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 <ico-uxf-weston-plugin/ico_window_mgr-client-protocol.h>
32 #include <ilm/ilm_control.h>
34 #include "CicoSCWayland.h"
35 #include "CicoSCWlWinMgrIF.h"
36 #include "CicoSCWindowController.h"
38 #include "ico_syc_error.h"
40 //==========================================================================
41 // private static variable
42 //==========================================================================
43 CicoSCWayland* CicoSCWayland::ms_myInstance = NULL;
45 //--------------------------------------------------------------------------
47 * @brief default constructor
49 //--------------------------------------------------------------------------
50 CicoSCWayland::CicoSCWayland()
54 m_ecoreFdHandler(NULL)
57 m_wlListener.global = wlGlobalCB;
58 m_wlListener.global_remove = NULL;
60 CicoSCWayland::ms_myInstance = this;
63 //--------------------------------------------------------------------------
67 //--------------------------------------------------------------------------
68 CicoSCWayland::~CicoSCWayland()
70 CicoSCWayland::ms_myInstance = NULL;
73 //--------------------------------------------------------------------------
75 * @brief get instance of CicoSCWayland
77 * @return instance of CicoSCWayland
79 //--------------------------------------------------------------------------
81 CicoSCWayland::getInstance(void)
83 if (NULL == ms_myInstance) {
84 ms_myInstance = new CicoSCWayland();
90 //--------------------------------------------------------------------------
92 * @brief initialize wayland connection
94 * @return ICO_SYC_EOK on success, other on error
96 //--------------------------------------------------------------------------
98 CicoSCWayland::initialize(void)
100 ICO_TRA("CicoSCWayland::initialize Enter");
102 for (int i = 0; i < (5000/50); ++i) {
103 m_wlDisplay = wl_display_connect(NULL);
104 if (NULL != m_wlDisplay) {
110 if (NULL == m_wlDisplay) {
111 ICO_ERR("wl_display_connect failed.");
112 ICO_TRA("CicoSCWayland::initialize Leave(EIO)");
116 ICO_DBG("called: wl_display_get_registry(wlDisplay=0x%08x)", m_wlDisplay);
117 m_wlRegistry = wl_display_get_registry(m_wlDisplay);
118 if (NULL == m_wlRegistry) {
119 ICO_ERR("wl_display_get_registry failed.");
120 ICO_TRA("CicoSCWayland::initialize Leave(EIO)");
124 // add wayland global callbacks
125 ICO_DBG("called: wl_registry_add_listener");
126 wl_registry_add_listener(m_wlRegistry, &m_wlListener, this);
129 int ifcount = m_wlInterfaceList.size();
130 for (int i = 0; i < (500/20); ++i) {
132 map<string, CicoSCWaylandIF*>::iterator itr;
133 itr = m_wlInterfaceList.begin();
134 for (; itr != m_wlInterfaceList.end(); ++itr) {
135 if(true == itr->second->isInitialized()) {
139 if (ifcount == initCount) {
142 ICO_DBG("called: wl_display_dispatch"
143 "(wlDisplay=0x%08x)", (int)m_wlDisplay);
144 wl_display_dispatch(m_wlDisplay);
148 if (ifcount != initCount) {
149 ICO_ERR("initialize interface time up NG");
150 ICO_TRA("CicoSCWayland::initialize Leave(EIO)");
154 ICO_DBG("initialize interface all OK");
158 ICO_DBG("called: wl_display_flush(wlDisplay=0x%08x)", (int)m_wlDisplay);
159 wl_display_flush(m_wlDisplay);
161 // initialize genivi ivi-shell
162 if (ilm_init() != ILM_SUCCESS) {
163 ICO_ERR("ilm_init failed.");
164 ICO_TRA("CicoSCWayland::initialize Leave(EIO)");
168 ICO_DBG("called: wl_display_get_fd(wlDisplay=0x%08x)", (int)m_wlDisplay);
169 m_wlFd = wl_display_get_fd(m_wlDisplay);
170 ICO_DBG("CicoSCWayland::initialize: Wayland/Weston fd=%d", m_wlFd);
172 ICO_DBG("CicoSCWayland::initialize: Wayland/Weston connect OK");
174 // initialize genivi callbacks
175 CicoSCWindowController::getInstance()->initializeGeniviLMS();
177 ICO_TRA("CicoSCWayland::initialize: Leave(EOK)");
181 //--------------------------------------------------------------------------
183 * @brief flush display
185 //--------------------------------------------------------------------------
187 CicoSCWayland::flushDisplay(void)
189 if (NULL == m_wlDisplay) {
190 ICO_ERR("wlDisplay == NULL");
193 // ICO_DBG("called: wl_display_flush(0x%08x)", (int)m_wlDisplay);
194 int wlret = wl_display_flush(m_wlDisplay);
196 int error = wl_display_get_error(m_wlDisplay);
197 ICO_ERR("wayland error(%d)", error);
201 //--------------------------------------------------------------------------
203 * @brief dispatch display
205 //--------------------------------------------------------------------------
207 CicoSCWayland::dispatchDisplay(void)
209 if (NULL == m_wlDisplay) {
210 ICO_ERR("wlDisplay == NULL");
213 // ICO_DBG("call wl_display_dispatch(0x%08x)", (int)m_wlDisplay);
214 int wlret = wl_display_dispatch(m_wlDisplay);
216 int error = wl_display_get_error(m_wlDisplay);
217 ICO_ERR("wl_display_dispatch failed. error(%d)", error);
219 if (error == EINVAL) {
225 //--------------------------------------------------------------------------
227 * @brief get wayland error
229 //--------------------------------------------------------------------------
231 CicoSCWayland::getError(void)
233 if (NULL == m_wlDisplay) {
234 ICO_ERR("wlDisplay == NULL");
238 ICO_DBG("fd=%d", wl_display_get_fd(m_wlDisplay));
239 return wl_display_get_error(m_wlDisplay);
242 //--------------------------------------------------------------------------
244 * @brief get wayland file destructor
246 * @return wayland file destructor
248 //--------------------------------------------------------------------------
249 int CicoSCWayland::getWlFd(void)
254 //--------------------------------------------------------------------------
256 * @brief add wayland file destructor handler to ecore main
258 * @return ICO_SYC_EOK on success, other on error
259 * @retval ICO_SYC_EIO ecore_main_fd_handler_add failed
261 //--------------------------------------------------------------------------
263 CicoSCWayland::addEcoreMainWlFdHandler(void)
265 ICO_TRA("CicoSCWayland::addEcoreMainWlFdHandler Enter");
266 ICO_DBG("called: ecore_main_fd_handler_add(fd=%d)", m_wlFd);
267 m_ecoreFdHandler = ecore_main_fd_handler_add(m_wlFd,
273 if (NULL == m_ecoreFdHandler) {
274 ICO_ERR("ecore_main_fd_handler_add failed.");
275 ICO_TRA("CicoSCWayland::addEcoreMainWlFdHandler Leave(EIO)");
278 ecore_main_loop_iterate();
281 if (ioctl(m_wlFd, FIONREAD, &arg) < 0) {
282 ICO_WRN("ioclt(FIONREAD) failed. errno=%d:%s", errno, strerror(errno));
286 CicoSCWayland::getInstance()->dispatchDisplay();
289 ICO_TRA("CicoSCWayland::addEcoreMainWlFdHandler Leave");
293 //--------------------------------------------------------------------------
295 * @brief add wayland interface
297 * @param [in] name interface name
298 * @param [in] interface interface instance
300 //--------------------------------------------------------------------------
302 CicoSCWayland::addWaylandIF(const char *name, CicoSCWaylandIF* interface)
304 m_wlInterfaceList[name] = interface;
307 //--------------------------------------------------------------------------
309 * @brief wayland global callback
311 * @param [in] data user data(unused)
312 * @param [in] registry wayland registry
313 * @param [in] name wayland display Id(unused)
314 * @param [in] interface wayland interface name
315 * @param [in] version wayland interface version number(unused)
317 //--------------------------------------------------------------------------
319 CicoSCWayland::globalCB(void *data,
320 struct wl_registry *registry,
322 const char *interface,
325 map<string, CicoSCWaylandIF*>::iterator itr;
326 itr = m_wlInterfaceList.find(interface);
328 if (m_wlInterfaceList.end() == itr) {
329 ICO_DBG("skip interface=%s", interface);
333 CicoSCWaylandIF *wlIF = itr->second;
334 wlIF->initInterface(data, registry, name, interface, version);
337 wl_display_flush(m_wlDisplay);
340 //--------------------------------------------------------------------------
342 * @brief wayland global callback
344 * @param [in] data user data
345 * @param [in] registry wayland registry
346 * @param [in] name wayland display Id(unused)
347 * @param [in] interface wayland interface name
348 * @param [in] version wayland interface version number(unused)
350 //--------------------------------------------------------------------------
352 CicoSCWayland::wlGlobalCB(void *data,
353 struct wl_registry *registry,
355 const char *interface,
359 ICO_ERR("data == NULL");
363 static_cast<CicoSCWayland*>(data)->globalCB(data, registry, name,
367 //--------------------------------------------------------------------------
369 * @brief wayland global callback
371 * @param [in] data user data
372 * @param [in] handler ecore file destructor handler
374 * @return ECORE_CALLBACK_RENEW on retry , ECORE_CALLBACK_CANCEL on cancel
376 //--------------------------------------------------------------------------
378 CicoSCWayland::waylandFdHandler(void *data, Ecore_Fd_Handler *handler)
380 // ICO_TRA("waylandFdHandler: Enter");
382 CicoSCWayland::getInstance()->flushDisplay();
385 if (ioctl(CicoSCWayland::getInstance()->getWlFd(), FIONREAD, &arg) < 0) {
386 ICO_WRN("ioclt(FIONREAD) failed. errno=%d:%s", errno, strerror(errno));
390 // ICO_DBG("waylandFdHandler: arg(%d))", arg);
392 CicoSCWayland::getInstance()->dispatchDisplay();
396 ICO_ERR("wayland fd read error.");
397 ICO_DBG("called: ecore_main_loop_quit()");
398 ecore_main_loop_quit();
399 return ECORE_CALLBACK_CANCEL;
402 CicoSCWayland::getInstance()->flushDisplay();
403 // ICO_TRA("waylandFdHandler: Leave");
404 return ECORE_CALLBACK_RENEW ;
406 // vim:set expandtab ts=4 sw=4: