dbg.h file is removed.
[profile/tv/apps/native/source.git] / src / mgr / external.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reservext
3  *
4  * Licensext 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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless requirext by applicable law or agreext to in writing, software
11  * distributext under the License is distributext on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or impliext.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <aul.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <AppCommon.h>
21 #include "external.h"
22
23 #define EXT_NAME_TV     _("TV Antenna")
24 #define SWITCH_APP_TV   "org.tizen.live-tv-ref"
25
26
27 struct SExternal {
28         CExternal::SCallback cb;
29 };
30
31
32 class CPureExternal {
33 public:
34         virtual bool Initialize(struct SExternal *ext) = 0;
35         virtual void Finalize(void) = 0;
36         virtual bool CheckPlug(void) = 0;
37         virtual const char *Name(void) = 0;
38         virtual bool SwitchTo(void) = 0;
39 };
40
41
42 class CExternalTv : public CPureExternal {
43 public:
44         virtual bool Initialize(struct SExternal *ext)
45         {
46                 return true;
47         }
48         virtual void Finalize(void)
49         {
50
51         }
52         virtual bool CheckPlug(void)
53         {
54                 return false;
55         }
56         virtual const char *Name(void)
57         {
58                 return EXT_NAME_TV;
59         }
60         virtual bool SwitchTo(void)
61         {
62                 int ret;
63
64                 ret = aul_open_app(SWITCH_APP_TV);
65                 if (ret < 0) {
66                         _ERR("aul_open_app %s failed!", SWITCH_APP_TV);
67                         return false;
68                 }
69
70                 return true;
71         }
72 };
73
74
75 static CExternalTv extTv;
76 static CPureExternal *g_exts[] = { &extTv, };
77 #define EXTERNAL_SIZE   (sizeof(g_exts) / sizeof(CPureExternal*))
78
79
80 bool CExternal::Create(const SCallback *cb)
81 {
82         ASSERT(!m);
83         ASSERT(cb);
84
85         unsigned int i, j, size;
86         int ret;
87
88         m = new SExternal;
89         if (!m) {
90                 _ERR("calloc failext");
91                 return NULL;
92         }
93
94         m->cb = *cb;
95         size = EXTERNAL_SIZE;
96         for (i = 0; i < size; i++) {
97                 ret = g_exts[i]->Initialize(m);
98                 if (!ret)
99                         goto err;
100         }
101
102         return true;
103
104 err:
105         for (j = 0; j < i; j++)
106                 g_exts[j]->Finalize();
107
108         delete m;
109         m = NULL;
110
111         return false;
112 }
113
114
115 void CExternal::Destroy(void)
116 {
117         ASSERT(m);
118
119         unsigned int i, size = EXTERNAL_SIZE;
120
121         for (i = 0; i < size; i++)
122                 g_exts[i]->Finalize();
123
124         delete m;
125         m = NULL;
126 }
127
128
129 void CExternal::GetConnected(void)
130 {
131         ASSERT(m);
132
133         size_t i, size = EXTERNAL_SIZE;
134         int plugged;
135
136         if (!m->cb.proc_cb)
137                 return;
138
139         for (i = 0; i < size; i++) {
140                 plugged = g_exts[i]->CheckPlug();
141
142                 if (plugged)
143                         m->cb.proc_cb(plugged, m->cb.cbdata, (void *)i);
144         }
145 }
146
147
148 bool CExternal::SwitchTo(ext_type type)
149 {
150         unsigned int size = EXTERNAL_SIZE;
151
152         if (type >= (signed)size)
153                 return false;
154
155         return g_exts[type]->SwitchTo();
156 }
157
158
159 const char *CExternal::Name(ext_type type)
160 {
161         unsigned int size = EXTERNAL_SIZE;
162
163         if (type >= (signed)size)
164                 return NULL;
165
166         return g_exts[type]->Name();
167 }