fix exception handling
[profile/tv/apps/native/videoplayer.git] / src / control.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <Elementary.h>
18 #include <Eina.h>
19 #include <dbg.h>
20 #include <AppCommon.h>
21 #include <ViewMgr.h>
22 #include <BaseView.h>
23 #include <InputHandler.h>
24 #include "define.h"
25 #include "i18n.h"
26 #include "control.h"
27
28 #define MAX_CONTROL_BTNS 6
29
30 struct _event_cb {
31         void (*func)(void *data, const char *ev);
32         void *data;
33 };
34
35 struct SControlData {
36         Evas_Object *base;
37         Evas_Object *focused;
38         Evas_Object *btn[MAX_CONTROL_BTNS];
39         int btns;
40         struct _event_cb cb;
41 };
42
43 void CVideoController::sm_CbEvent(void *dt, Evas_Object *obj,
44                 const char *emission, const char *source)
45 {
46         ASSERT(dt);
47
48         struct SControlData *data;
49
50         data = (SControlData *)dt;
51         data->cb.func(data->cb.data, source);
52 }
53
54 void CVideoController::m_SetFocusDirection(Evas_Object **btn, int count)
55 {
56         Evas_Object *obj;
57         int i;
58
59         for (i = 0; i < count; i++) {
60                 if (i == count - 1)
61                         obj = btn[0];
62                 else
63                         obj = btn[i + 1];
64
65                 elm_object_focus_next_object_set(btn[i], obj, ELM_FOCUS_RIGHT);
66
67                 if (i == 0)
68                         obj = btn[count - 1];
69                 else
70                         obj = btn[i - 1];
71
72                 elm_object_focus_next_object_set(btn[i], obj, ELM_FOCUS_LEFT);
73         }
74 }
75
76 Evas_Object *CVideoController::m_AddButton(const char *style)
77 {
78         Evas_Object *btn;
79
80         btn = elm_button_add(m->base);
81         if (!btn)
82                 return NULL;
83
84         elm_object_style_set(btn, style);
85         evas_object_show(btn);
86
87         return btn;
88 }
89
90 int CVideoController::m_AddControls(const char **btns, int count)
91 {
92         int i;
93         char buf[32];
94
95         for (i = 0; i < count; i++) {
96                 snprintf(buf, sizeof(buf), "video_btn_%s", btns[i]);
97                 m->btn[i] = m_AddButton(buf);
98                 if (!m->btn[i])
99                         return -1;
100
101                 snprintf(buf, sizeof(buf), "part_btn%d", i);
102                 elm_object_part_content_set(m->base,
103                                 buf, m->btn[i]);
104
105                 Connect(m->btn[i], -1, TYPE_MOUSE_MOVE | TYPE_CLICKED);
106
107                 elm_object_signal_callback_add(m->btn[i],
108                                 SIG_BTN_ANI_FIN, btns[i],
109                                 sm_CbEvent, m);
110
111                 elm_object_focus_next_object_set(m->btn[i],
112                                 m->btn[i], ELM_FOCUS_DOWN);
113
114                 m->btns++;
115         }
116
117         return 0;
118 }
119
120 bool CVideoController::Create(Evas_Object *base, const char **btns, int count)
121 {
122         ASSERT(!m);
123
124         int r;
125
126         if (!base || !btns)
127                 return false;
128
129         m = new SControlData;
130         if (!m) {
131                 _ERR("allocation failed");
132                 return false;
133         }
134
135         m->base = base;
136         m->btns = 0;
137
138         r = m_AddControls(btns, count);
139         if (r < 0)
140                 return false;
141
142         m_SetFocusDirection(m->btn, m->btns);
143
144         return true;
145 }
146
147 void CVideoController::Destroy(void)
148 {
149         ASSERT(m);
150
151         delete m;
152         m = NULL;
153 }
154
155 void CVideoController::AddHandler(void (*func)(void *, const char *),
156                 void *param)
157 {
158         ASSERT(m);
159
160         if (!func)
161                 return;
162
163         m->cb.func = func;
164         m->cb.data = param;
165 }
166
167 void CVideoController::Focus(int id, Eina_Bool foc)
168 {
169         ASSERT(m);
170
171         elm_object_focus_set(m->btn[id], foc);
172 }
173
174 void CVideoController::Signal(int id, const char *emission, const char *source)
175 {
176         ASSERT(m);
177
178         elm_object_signal_emit(m->btn[id], emission, source);
179 }
180
181 void CVideoController::Hide(void)
182 {
183         ASSERT(m);
184
185         int i;
186
187         for (i = 0; i < m->btns; i++) {
188                 evas_object_freeze_events_set(m->btn[i], EINA_TRUE);
189                 elm_object_focus_allow_set(m->btn[i], EINA_FALSE);
190         }
191 }
192
193 void CVideoController::Show(void)
194 {
195         ASSERT(m);
196
197         int i;
198
199         for (i = 0; i < m->btns; i++) {
200                 evas_object_freeze_events_set(m->btn[i], EINA_FALSE);
201                 elm_object_focus_allow_set(m->btn[i], EINA_TRUE);
202         }
203 }
204
205 void CVideoController::OnMouseMove(int id, Evas *e, Evas_Object *obj,
206                         Evas_Event_Mouse_Move *ev)
207 {
208         ASSERT(obj);
209
210         if (!elm_object_focus_get(obj))
211                 elm_object_focus_set(obj, EINA_TRUE);
212 }
213
214 void CVideoController::OnMouseClicked(int id, Evas_Object *obj)
215 {
216         ASSERT(obj);
217
218         elm_object_signal_emit(obj, SIG_BTN_ANI_START, "");
219 }