2 * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include <AppCommon.h>
19 #include "source_mgr.h"
24 #define DEFAULT_USB_NAME "USB Dev"
25 #define DEFAULT_EXT_NAME "External"
26 #define USB_CONN_TYPE "USB"
27 #define TV_CONN_TYPE "TV ANTENNA"
30 CSourceMgr *CSourceMgr::instance = NULL;
34 Eina_List *plugged_list;
35 Eina_List *nearby_conn_list;
36 Eina_List *nearby_disc_list;
39 TUpdateUI cbUpdateSourceList;
48 nearby_conn_list = NULL;
49 nearby_disc_list = NULL;
50 memset(&cb, 0, sizeof(cb));
55 void CSourceMgr::t_FreeAllSource(void)
60 EINA_LIST_FREE(m->plugged_list, obj) {
61 si = (CSourceInfo *)obj;
76 void CSourceMgr::t_Free(CSourceInfo *si)
82 void CSourceMgr::t_UpdateUI(void *data, enum update_ui update_type)
84 if (m->cb.cbUpdateSourceList)
85 m->cb.cbUpdateSourceList(m->cb.data, data, update_type);
88 CSourceInfo *CSourceMgr::m_AllocUsbSI(void *data)
90 CUsbListener::SUsbHostDeviceInfo *devInfo;
94 devInfo = (CUsbListener::SUsbHostDeviceInfo *)data;
102 si->SetId(devInfo->vendorId);
103 si->SetType(CONN_TYPE_USB);
104 name = devInfo->product;
105 si->SetName(name ? name : DEFAULT_USB_NAME);
106 si->SetTypeName(USB_CONN_TYPE);
111 void CSourceMgr::sm_CbUsbPlug(void *cookie, int status, void *data)
113 CSourceMgr *root = (CSourceMgr *)cookie;
116 root->m_OnUsbPlug(status, data);
119 void CSourceMgr::m_OnUsbPlug(int status, void *data)
121 CUsbListener::SUsbHostDeviceInfo *devInfo;
124 Eina_List *list, *list_next;
126 devInfo = (CUsbListener::SUsbHostDeviceInfo *)data;
128 if (status == CUsbListener::USB_HOST_DEV_CONNECTED) {
129 si = m_AllocUsbSI(devInfo);
133 m->plugged_list = eina_list_append(m->plugged_list, si);
134 t_UpdateUI(si, SRC_PLUG_ADD_ONE);
136 EINA_LIST_FOREACH_SAFE(m->plugged_list, list, list_next, obj) {
137 si = (CSourceInfo *)obj;
138 if (si->Type() == CONN_TYPE_USB &&
139 si->Id() == devInfo->vendorId) {
140 m->plugged_list = eina_list_remove(
141 m->plugged_list, si);
143 t_UpdateUI(si, SRC_PLUG_DEL_ONE);
151 void CSourceMgr::sm_CbExternalPlug(int is_plugged, void *cbdata, void *data)
153 CSourceMgr *root = (CSourceMgr*)cbdata;
155 root->m_OnExternalPlug(is_plugged, cbdata);
159 void CSourceMgr::m_OnExternalPlug(int is_plugged, void *data)
163 enum ext_type *ext_type;
164 Eina_List *list, *list_next;
166 ext_type = (enum ext_type *)data;
169 si = m_AllocSI(*ext_type);
173 m->plugged_list = eina_list_append(m->plugged_list, si);
174 t_UpdateUI(si, SRC_PLUG_ADD_ONE);
177 EINA_LIST_FOREACH_SAFE(m->plugged_list, list, list_next, obj) {
178 si = (CSourceInfo *)obj;
179 if (si->Type() == *ext_type) {
180 m->plugged_list = eina_list_remove(
181 m->plugged_list, si);
183 t_UpdateUI(si, SRC_PLUG_DEL_ONE);
192 CSourceInfo *CSourceMgr::m_AllocSI(int type)
197 si = (CSourceInfo *)calloc(1, sizeof(*si));
199 _ERR("calloc failed");
203 name = CExternal::Name((ext_type)type);
204 si->SetName(name ? name : DEFAULT_EXT_NAME);
205 if (type == CONN_TYPE_TV)
206 si->SetTypeName(TV_CONN_TYPE);
208 si->SetType((ext_type)type);
214 bool CSourceMgr::t_Create(TUpdateUI cb, void *data)
218 CUsb::SCallback usbCb;
219 CExternal::SCallback extCb;
223 _ERR("Create src_mgr failed.");
229 _ERR("Create usb failed.");
233 usbCb.cb = sm_CbUsbPlug;
236 if (!m->usb->Create(&usbCb)) {
237 _ERR("Init usb failed.");
243 extCb.proc_cb = sm_CbExternalPlug;
246 if (!m->ext.Create(&extCb)) {
247 _ERR("Init external failed.");
256 m->cb.cbUpdateSourceList = cb;
263 void CSourceMgr::t_Destroy(void)
277 bool CSourceMgr::Initialize(TUpdateUI cb, void *data)
281 instance = new CSourceMgr;
284 if (!instance->t_Create(cb, data)) {
293 void CSourceMgr::Finalize(void)
297 instance->t_Destroy();
303 CSourceMgr *CSourceMgr::GetInstance(void)
309 void CSourceMgr::Refresh(void)
314 t_UpdateUI(NULL, SRC_UPDATE_ALL);
316 m->usb->GetConnected();
317 m->ext.GetConnected();
321 Eina_List *CSourceMgr::PluggedList(void)
325 return m->plugged_list;
329 Eina_List *CSourceMgr::NearbyConnList(void)
333 return m->nearby_conn_list;
337 Eina_List *CSourceMgr::NearbyDiscList(void)
341 return m->nearby_disc_list;
345 CSourceInfo *CSourceMgr::SourceInfoByData(void *data)
353 EINA_LIST_FOREACH(m->plugged_list, list, obj) {
354 si = (CSourceInfo *)obj;
355 if (si && si->Data() == data)
359 EINA_LIST_FOREACH(m->nearby_conn_list, list, obj) {
360 si = (CSourceInfo *)obj;
361 if (si && si->Data() == data)
365 EINA_LIST_FOREACH(m->nearby_disc_list, list, obj) {
366 si = (CSourceInfo *)obj;
367 if (si && si->Data() == data)