summaryrefslogtreecommitdiff
path: root/home/src/clock_inf_minictrl.c
blob: 714f00a78ba88a4290f41ed01f646452307af59a (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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/*
 * Samsung API
 * Copyright (c) 2009-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/license/
 *
 * 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 <Elementary.h>
#include <Evas.h>
#include <vconf.h>
#include <bundle.h>
#include <efl_assist.h>
#include <minicontrol-viewer.h>
#include <minicontrol-monitor.h>
#include <minicontrol-handler.h>
#include <dlog.h>

#include "conf.h"
#include "layout.h"
#include "log.h"
#include "util.h"
#include "main.h"
#include "page_info.h"
#include "scroller_info.h"
#include "scroller.h"
#include "page.h"
#include "key.h"
#include "minictrl.h"
#include "dbus.h"
#include "clock_service.h"

static struct info {
	Ecore_Timer *waiting_timer;
} s_info = {
	.waiting_timer = NULL,
};

#define MINICTRL_PRIVATE_KEY_STATE "mc_pr_k_s"
#define MINICTRL_STATE_PAUSE 0xDEAD0040
#define MINICTRL_STATE_RESUME 0xCAFE0080

static Evas_Object *_scroller_get(void)
{
	Evas_Object *win = main_get_info()->win;
	Evas_Object *layout = NULL;
	Evas_Object *scroller = NULL;

	if (win != NULL) {
		layout = evas_object_data_get(win, DATA_KEY_LAYOUT);
		if (layout != NULL) {
			scroller = elm_object_part_content_get(layout, "scroller");
		}
	}

	return scroller;
}

static void _size_set(Evas_Object *minictrl_obj, int width, int height)
{
	evas_object_resize(minictrl_obj, width, height);
	evas_object_size_hint_min_set(minictrl_obj, width, height);
	evas_object_size_hint_max_set(minictrl_obj, width, height);
}

static void _pause_cb(void *data, Evas_Object *obj, void *event_info)
{
	Evas_Object *minictrl = data;
	ret_if(minictrl == NULL);

	int state = (int)evas_object_data_get(minictrl, MINICTRL_PRIVATE_KEY_STATE);
	if (state == MINICTRL_STATE_PAUSE) {
		return ;
	}

	if (minicontrol_message_send(minictrl, MINICTRL_EVENT_APP_PAUSE) == 1) {
		_D("minictrl %p is paused", minictrl);
		evas_object_data_set(minictrl, MINICTRL_PRIVATE_KEY_STATE, (void*)MINICTRL_STATE_PAUSE);
	} else {
		_E("failed to pause %p", minictrl);
	}
}

static void _resume_cb(void *data, Evas_Object *obj, void *event_info)
{
	Evas_Object *minictrl = data;
	ret_if(minictrl == NULL);

	int state = (int)evas_object_data_get(minictrl, MINICTRL_PRIVATE_KEY_STATE);
	if (state == MINICTRL_STATE_RESUME) {
		return ;
	}

	if (minicontrol_message_send(minictrl, MINICTRL_EVENT_APP_RESUME) == 1) {
		_D("minictrl %p is resumed", minictrl);
		evas_object_data_set(minictrl, MINICTRL_PRIVATE_KEY_STATE, (void*)MINICTRL_STATE_RESUME);
	} else {
		_E("failed to resume %p", minictrl);
	}
}

static int _prepare(clock_h clock)
{
	int pid = 0;

	clock_util_provider_launch(clock->pkgname, &pid, clock->configure);
	_W("pid:%d", pid);

	if (pid >= 0) {
		clock->pid = pid;
		return CLOCK_RET_OK;
	}

	return CLOCK_RET_FAIL;
}

static int _config(clock_h clock, int configure)
{
	int pid = 0;

	clock_util_provider_launch(clock->pkgname, &pid, configure);
	if (pid >= 0) {
		if (clock->pid != pid) {
			_E("pid is changed, %d %d", clock->pid, pid);
		}
		clock->pid = pid;
		return CLOCK_RET_OK;
	}

	return CLOCK_RET_FAIL;
}

static int _create(clock_h clock)
{
	Evas_Object *page = NULL;
	Evas_Object *obj = NULL;
	Evas_Object *scroller = _scroller_get();

	if (clock->view) {
		Evas_Object *mini_obj = clock_view_get_item(clock->view);
		if (mini_obj) {
			_size_set(mini_obj, clock->w, clock->h);
			evas_object_show(mini_obj);
		}
		return CLOCK_RET_OK;
	}

	obj = minicontrol_viewer_add(scroller, clock->view_id);
	if (obj != NULL) {
		_size_set(obj, clock->w, clock->h);
		evas_object_show(obj);

		page = clock_view_add(scroller, obj);
		if (page != NULL) {
			clock->view = (void *)page;

			evas_object_smart_callback_add(page, CLOCK_SMART_SIGNAL_PAUSE, _pause_cb, obj);
			evas_object_smart_callback_add(page, CLOCK_SMART_SIGNAL_RESUME, _resume_cb, obj);

			return CLOCK_RET_OK;
		}
	} else {
		_E("Failed to add viewer - %s", clock->view_id);
	}

	return CLOCK_RET_FAIL;
}

static int _attached_cb(clock_h clock)
{
	Evas_Object *page = clock->view;

	int visibility = 1;
	if (clock_service_event_state_get(CLOCK_EVENT_DEVICE_LCD) == CLOCK_EVENT_DEVICE_LCD_OFF) {
		visibility = 0;
	}
	if (clock_service_event_state_get(CLOCK_EVENT_APP) == CLOCK_EVENT_APP_PAUSE) {
		visibility = 0;
	}

	if (visibility == 1) {
		evas_object_smart_callback_call(page, CLOCK_SMART_SIGNAL_RESUME, page_get_item(page));
	} else {
		if (clock->configure != CLOCK_CONF_CLOCK_CONFIGURATION) {
			evas_object_smart_callback_call(page, CLOCK_SMART_SIGNAL_PAUSE, page_get_item(page));
		}
	}

	return CLOCK_RET_OK;
}

static int _destroy(clock_h clock)
{
	Evas_Object *page = clock->view;

	if (page != NULL) {
		evas_object_smart_callback_del(page, CLOCK_SMART_SIGNAL_PAUSE, _pause_cb);
		evas_object_smart_callback_del(page, CLOCK_SMART_SIGNAL_RESUME, _resume_cb);
		page_destroy(page);
		clock->view = NULL;
	}

	clock_util_terminate_clock_by_pid(clock->pid);

	return CLOCK_RET_OK;
}

clock_inf_s clock_inf_minictrl = {
	.async = 1,
	.use_dead_monitor = 1,
	.prepare = _prepare,
	.config = _config,
	.create = _create,
	.attached_cb = _attached_cb,
	.destroy = _destroy,
};

static Eina_Bool _view_ready_idler_cb(void *data)
{
	_D("");

	if (s_info.waiting_timer != NULL) {
		s_info.waiting_timer = NULL;
	}

	clock_h clock = data;
	retv_if(clock == NULL, ECORE_CALLBACK_CANCEL);

	clock_service_event_handler(clock, CLOCK_EVENT_VIEW_READY);

	_D("");

	return ECORE_CALLBACK_CANCEL;
}

HAPI void clock_inf_minictrl_event_hooker(int action, int pid, const char *minictrl_id, int is_realized, int width, int height)
{
	clock_h clock = NULL;
	ret_if(minictrl_id == NULL);

	switch (action) {
		case MINICONTROL_ACTION_START:
			clock = clock_manager_clock_get(CLOCK_CANDIDATE);
			if (clock != NULL) {
				if (clock->pid == pid) {
					_D("candidate clock:%s is now ready", clock->pkgname);

					if (clock->view_id == NULL) {
						clock->view_id = strdup(minictrl_id);
					}
					clock->w = width;
					clock->h = height;

					//ecore_idler_add(_view_ready_idler_cb, clock);
					if (s_info.waiting_timer != NULL) {
						ecore_timer_del(s_info.waiting_timer);
						s_info.waiting_timer = NULL;
					}
					if (is_realized == 1) {
						_W("clock is already realized");
						clock_service_event_handler(clock, CLOCK_EVENT_VIEW_READY);
					} else {
						_W("not realized, waiting 1.0 sec");
						/*
						 * To load minicontrol ASAP
						 */
						 if (clock->app_type != CLOCK_APP_TYPE_WEBAPP) {
						 	_create(clock);
						 }
						s_info.waiting_timer = ecore_timer_add(1.0f, _view_ready_idler_cb, clock);
					}
				} else {
					_E("the clock isn't what we want:%d %s", pid, minictrl_id);
				}
			} else {
				_E("[Exceptional, no candiate clock");
			}
			break;
		case MINICONTROL_ACTION_REALIZE:
			clock = clock_manager_clock_get(CLOCK_CANDIDATE);
			if (clock != NULL) {
				if (clock->pid == pid) {
					_D("candidate clock:%s is now realized", clock->pkgname);

					if (clock->view_id == NULL) {
						clock->view_id = strdup(minictrl_id);
					}
					clock->w = width;
					clock->h = height;

					if (s_info.waiting_timer != NULL) {
						ecore_timer_del(s_info.waiting_timer);
						s_info.waiting_timer = NULL;
					}
					clock_service_event_handler(clock, CLOCK_EVENT_VIEW_READY);
				} else {
					_E("the clock isn't what we want:%d %s", pid, minictrl_id);
				}
			} else {
				_E("[Exceptional, no candiate clock");
			}
			break;
		case MINICONTROL_ACTION_RESIZE:
			clock = clock_manager_clock_get(CLOCK_ATTACHED);
			if (clock != NULL) {
				if (clock->view_id == NULL) {
					/**
					 * THIS CASE MUST HAS NOT TO BE HAPPEND.
					 * But for the safety, I just handled this too.
					 */
					_E("Attached clock: has no view ID(%s)", clock->pkgname);
					if (clock->pid == pid) {
						_D("But, PID is matched: %d", clock->pid);
						clock->view_id = strdup(minictrl_id);
						if (!clock->view_id) {
							_E("strdup: %s", strerror(errno));
						} else {
							clock->w = width;
							clock->h = height;

							clock_service_event_handler(clock, CLOCK_EVENT_VIEW_RESIZED);
						}
					}
				} else if (strcmp(clock->view_id, minictrl_id) == 0) {
					_D("Attached clock: %s is updated", clock->pkgname);

					clock->w = width;
					clock->h = height;

					clock_service_event_handler(clock, CLOCK_EVENT_VIEW_RESIZED);
				}
			} else {
				_W("clock: %s isn't attached yet", minictrl_id);
			}
			clock = clock_manager_clock_get(CLOCK_CANDIDATE);
			if (clock != NULL) {
				if (clock->view_id == NULL) {
					if (clock->pid == pid) {
						_D("Clock has no view ID, but PID is matched: %d %s", clock->pid, clock->pkgname);
						clock->view_id = strdup(minictrl_id);
						if (!clock->view_id) {
							_E("strdup: %s", strerror(errno));
						} else {
							clock->w = width;
							clock->h = height;
						}
					} else {
						_D("Candidate PID is not matched? %d", clock->pid);
					}
				} else if (strcmp(clock->view_id, minictrl_id) == 0) {
					_D("candidate clock:%s is updated", clock->pkgname);
					clock->w = width;
					clock->h = height;
				}
			}
			break;
		case MINICONTROL_ACTION_STOP:
			break;
		case MINICONTROL_ACTION_REQUEST:
			if (width == MINICONTROL_REQ_FREEZE_SCROLL_VIEWER) {
				clock_service_event_handler(NULL, CLOCK_EVENT_SCROLLER_FREEZE_ON);
			} else if (width == MINICONTROL_REQ_UNFREEZE_SCROLL_VIEWER) {
				clock_service_event_handler(NULL, CLOCK_EVENT_SCROLLER_FREEZE_OFF);
			} else {
				_W("Not a case");
			}
			break;
		default:
			break;
	}
}