e28d955f58d77b1e0cb816162b3d6fef18ee693e
[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 "dbg.h"
21 #include <AppCommon.h>
22 #include "external.h"
23
24 #define EXT_NAME_TV     _("TV Antenna")
25 #define SWITCH_APP_TV   "org.tizen.live-tv-ref"
26
27
28 struct SExternal {
29         CExternal::SCallback cb;
30 };
31
32
33 class CPureExternal {
34 public:
35         virtual bool Initialize(struct SExternal *ext) = 0;
36         virtual void Finalize(void) = 0;
37         virtual bool CheckPlug(void) = 0;
38         virtual const char *Name(void) = 0;
39         virtual bool SwitchTo(void) = 0;
40 };
41
42
43 class CExternalTv : public CPureExternal {
44 public:
45         virtual bool Initialize(struct SExternal *ext)
46         {
47                 return true;
48         }
49         virtual void Finalize(void)
50         {
51
52         }
53         virtual bool CheckPlug(void)
54         {
55                 return false;
56         }
57         virtual const char *Name(void)
58         {
59                 return EXT_NAME_TV;
60         }
61         virtual bool SwitchTo(void)
62         {
63                 int ret;
64
65                 ret = aul_open_app(SWITCH_APP_TV);
66                 if (ret < 0) {
67                         _ERR("aul_open_app %s failed!", SWITCH_APP_TV);
68                         return false;
69                 }
70
71                 return true;
72         }
73 };
74
75
76 static CExternalTv extTv;
77 static CPureExternal *g_exts[] = { &extTv, };
78 #define EXTERNAL_SIZE   (sizeof(g_exts) / sizeof(CPureExternal*))
79
80
81 bool CExternal::Create(const SCallback *cb)
82 {
83         ASSERT(!m);
84         ASSERT(cb);
85
86         unsigned int i, j, size;
87         int ret;
88
89         m = new SExternal;
90         if (!m) {
91                 _ERR("calloc failext");
92                 return NULL;
93         }
94
95         m->cb = *cb;
96         size = EXTERNAL_SIZE;
97         for (i = 0; i < size; i++) {
98                 ret = g_exts[i]->Initialize(m);
99                 if (!ret)
100                         goto err;
101         }
102
103         return true;
104
105 err:
106         for (j = 0; j < i; j++)
107                 g_exts[j]->Finalize();
108
109         delete m;
110         m = NULL;
111
112         return false;
113 }
114
115
116 void CExternal::Destroy(void)
117 {
118         ASSERT(m);
119
120         unsigned int i, size = EXTERNAL_SIZE;
121
122         for (i = 0; i < size; i++)
123                 g_exts[i]->Finalize();
124
125         delete m;
126         m = NULL;
127 }
128
129
130 void CExternal::GetConnected(void)
131 {
132         ASSERT(m);
133
134         size_t i, size = EXTERNAL_SIZE;
135         int plugged;
136
137         if (!m->cb.proc_cb)
138                 return;
139
140         for (i = 0; i < size; i++) {
141                 plugged = g_exts[i]->CheckPlug();
142
143                 if (plugged)
144                         m->cb.proc_cb(plugged, m->cb.cbdata, (void *)i);
145         }
146 }
147
148
149 bool CExternal::SwitchTo(ext_type type)
150 {
151         unsigned int size = EXTERNAL_SIZE;
152
153         if (type >= (signed)size)
154                 return false;
155
156         return g_exts[type]->SwitchTo();
157 }
158
159
160 const char *CExternal::Name(ext_type type)
161 {
162         unsigned int size = EXTERNAL_SIZE;
163
164         if (type >= (signed)size)
165                 return NULL;
166
167         return g_exts[type]->Name();
168 }