summaryrefslogtreecommitdiff
path: root/src/main.c
blob: 9cdc317ab60da21341a1c7893a89963987a2f6ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/*
 * Copyright (c) 2015 Samsung Electronics Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the License);
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an AS IS BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <app.h>
#include <Elementary.h>
#include <viewmgr.h>
#include <inputmgr.h>
#include <app_debug.h>

#include "define.h"
#include "tv.h"
#include "util.h"
#include "view.h"

#define KEY_MAX 256

SET_TAG(PACKAGE)

struct _appdata {
	const char *name;
	Evas_Object *win;

	int is_signal;
};

struct key_map {
	const char *view;
	const char *key[KEY_MAX];
};

static struct key_map g_kmap[] = {
	{
		VIEW_CHANNELINFO,
		{
			KEY_ENTER, KEY_ENTER_REMOTE,
			KEY_CHANNELUP, KEY_CHANNELUP_REMOTE,
			KEY_CHANNELDOWN, KEY_CHANNELDOWN_REMOTE
		}
	},
	{
		VIEW_CHANNELNUMBER,
		{
			KEY_0, KEY_1, KEY_2, KEY_3,
			KEY_4, KEY_5, KEY_6, KEY_7,
			KEY_8, KEY_9, KEY_MINUS
		}
	},
};

static void _key_down_cb(int id, void *data, Evas *e, Evas_Object *obj,
		Evas_Event_Key_Down *ev)
{
	size_t i, j;

	for (i = 0; i < sizeof(g_kmap) / sizeof(*g_kmap); i++) {
		j = 0;
		while (g_kmap[i].key[j]) {
			if (!strcmp(ev->keyname, g_kmap[i].key[j])) {
				viewmgr_show_view(g_kmap[i].view);
				viewmgr_update_view(g_kmap[i].view,
						UPDATE_TYPE_INPUT_KEY_DOWN, ev);
				return;
			}
			j++;
		}
	}
}

static void _key_up_cb(int id, void *data, Evas *e, Evas_Object *obj,
		Evas_Event_Key_Up *ev)
{
	size_t i, j;

	for (i = 0; i < sizeof(g_kmap) / sizeof(*g_kmap); i++) {
		j = 0;
		while (g_kmap[i].key[j]) {
			if (!strcmp(ev->keyname, g_kmap[i].key[j])) {
				viewmgr_show_view(g_kmap[i].view);
				viewmgr_update_view(g_kmap[i].view,
						UPDATE_TYPE_INPUT_KEY_UP, ev);
				return;
			}
			j++;
		}
	}
}

static input_handler key_handler = {
	.key_down = _key_down_cb,
	.key_up = _key_up_cb,
};

static Evas_Object *_add_win(const char *name)
{
	Evas_Object *win, *trans;

	win = elm_win_add(NULL, name, ELM_WIN_BASIC);
	if (!win) {
		_ERR("elm_win_add failed");
		return NULL;
	}
	elm_win_alpha_set(win, EINA_FALSE);
	elm_win_focus_highlight_enabled_set(win, EINA_FALSE);
	elm_win_focus_highlight_style_set(win, "invisible");

	evas_object_show(win);

	trans = evas_object_rectangle_add(evas_object_evas_get(win));
	if (!trans) {
		_ERR("Create transparent layer failed");
		evas_object_del(win);
		return NULL;
	}

	/* for transparent layer */
	evas_object_color_set(trans, 0, 0, 0, 0);
	evas_object_render_op_set(trans, EVAS_RENDER_COPY);
	elm_win_resize_object_add(win, trans);
	evas_object_size_hint_weight_set(trans,
			EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
	evas_object_show(trans);

	return win;
}

int _set_tv_overlay(Evas_Object *win)
{
	int r;
	Ecore_Wl_Window *wl_win;

	wl_win = elm_win_wl_window_get(win);

	r = tv_overlay_set(wl_win);

	return r;
}

static void _tv_signal_cb(void *data, int is_signal)
{
	struct _appdata *ad;

	if (!data) {
		_ERR("failed to get data");
		return;
	}

	ad = data;

	if (ad->is_signal == is_signal)
		return;

	ad->is_signal = is_signal;
}

static void _pause(void *data)
{
}

static void _resume(void *data)
{
	struct _appdata *ad;
	int r;

	if (!data) {
		_ERR("failed to get data");
		return;
	}

	ad = data;

	r = tv_resume();
	if (r < 0) {
		_ERR("Resume tv service failed");
		return;
	} else if (r > 0) {
		r = tv_channel_tune();
		if (r < 0) {
			_ERR("Tune channel failed");
			return;
		}
	}

	r = _set_tv_overlay(ad->win);
	if (r < 0) {
		_ERR("Set overlay failed");
		return;
	}
}

static bool _create(void *data)
{
	struct _appdata *ad;
	Evas_Object *win;
	int r;

	if (!data) {
		_ERR("failed to get data");
		return false;
	}

	ad = data;

	elm_theme_overlay_add(NULL, THEMEFILE);

	win = _add_win(ad->name);
	if (!win) {
		_ERR("failed to create win object");
		return false;
	}

	if (!viewmgr_create(win)) {
		_ERR("failed to initialize viewmgr");
		evas_object_del(win);
		return false;
	}

	viewmgr_add_view(view_channelinfo_get_vclass(), NULL);
	viewmgr_add_view(view_channelnumber_get_vclass(), NULL);

	r = tv_create();
	if (r < 0) {
		_ERR("Create TV failed");
		evas_object_del(win);
		return false;
	}

	r = _set_tv_overlay(ad->win);
	if (r < 0) {
		_ERR("Set overlay failed");
		evas_object_del(win);
		return false;
	}

	ad->win = win;

	tv_signal_cb_set(_tv_signal_cb, ad);
	inputmgr_add_callback(ad->win, 0, &key_handler, NULL);
	return true;
}

static void _terminate(void *data)
{
	struct _appdata *ad;

	if (!data) {
		_ERR("failed to get data");
		return;
	}

	ad = data;

	if (ad->win) {
		tv_destroy();

		inputmgr_remove_callback(ad->win, &key_handler);

		viewmgr_remove_view(VIEW_CHANNELINFO);
		viewmgr_remove_view(VIEW_CHANNELNUMBER);
		viewmgr_destroy();

		evas_object_del(ad->win);
		ad->win = NULL;
	}
}

static void _control(app_control_h control, void *data)
{
	char *svcid;
	int r;

	if (!data) {
		_ERR("failed to get data");
		return;
	}

	r = app_control_get_extra_data(control, KEY_SVCID, &svcid);
	if (r == SERVICE_ERROR_NONE) {
		tv_channel_tune_with_service_id(atoll(svcid));
		free(svcid);
	} else {
		tv_channel_tune();
	}

	viewmgr_show_view(VIEW_CHANNELINFO);
}

int main(int argc, char *argv[])
{
	struct _appdata ad;
	ui_app_lifecycle_callback_s cbs = {
		.create = _create,
		.terminate = _terminate,
		.pause = _pause,
		.resume = _resume,
		.app_control = _control,
	};

	memset(&ad, 0x00, sizeof(ad));
	ad.name = PACKAGE;

	return ui_app_main(argc, argv, &cbs, &ad);
}