summaryrefslogtreecommitdiff
path: root/client/ug-client.c
blob: 18adf885be1b7cc1bd9900030898962529695246 (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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
/*
 *  UI Gadget
 *
 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
 *
 * Contact: Jayoun Lee <airjany@samsung.com>
 *
 * 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 <stdio.h>
#include <appcore-efl.h>
#include <ui-gadget.h>
#include <Ecore_X.h>
#include <dlog.h>
#include <aul.h>
#include <app.h>
#include <vconf.h>

#include "ug-client.h"

#include <Ecore_X.h>
#include <utilX.h>

#ifdef LOG_TAG
#undef LOG_TAG
#endif

#define LOG_TAG "UI_GADGET_CLIENT"

static void prt_usage(const char *cmd)
{
	fprintf(stderr, "Usage: %s [-f] [-F] -n <UG NAME> [-d <Arguments>]\n",
		cmd);
	fprintf(stderr, "   Options:\n");
	fprintf(stderr, "            -d argument\n");
	fprintf(stderr, "            -F Fullview mode (Default)\n");
	fprintf(stderr, "            -f Frameview mode\n");
	fprintf(stderr, "   Example:\n");
	fprintf(stderr,
		"            %s -F -n helloUG-efl -d \"name,John Doe\" -d \"age,30\"\n",
		cmd);

}

static void win_del(void *data, Evas_Object *obj, void *event)
{
	elm_exit();
}

static void main_quit_cb(void *data, Evas_Object *obj,
			 const char *emission, const char *source)
{
	elm_exit();
}

static int ug_send_rotate_event(int angle)
{
	int ret = -1;
	LOGD("ug_send_rotate_event angle : %d", angle);
	switch(angle) {
		case 0 :
			ret = ug_send_event(UG_EVENT_ROTATE_PORTRAIT);
			break;
		case 90 :
			ret = ug_send_event(UG_EVENT_ROTATE_LANDSCAPE_UPSIDEDOWN);
			break;
		case 180 :
			ret = ug_send_event(UG_EVENT_ROTATE_PORTRAIT_UPSIDEDOWN);
			break;
		case 270 :
			ret = ug_send_event(UG_EVENT_ROTATE_LANDSCAPE);
			break;
		default :
			LOGW("wrong angle(%d) for send rotate event",angle);
			break;
	}

	return ret;
}

static void rotate(void *data, Evas_Object *obj, void *event)
{
	int changed_angle = 0;
	struct appdata *ad = data;

	changed_angle = elm_win_rotation_get((const Evas_Object *)obj);
	if(changed_angle == -1) {
		LOGE("elm_win_rotation_get error");
		return;
	}

	LOGD("rotate call back : changed angle(%d) / current angle(%d)",
		changed_angle, ad->rotate);

	if(ad->rotate != changed_angle) {
		ug_send_rotate_event(changed_angle);
	}

	ad->rotate = changed_angle;

	return;
}

void _ug_client_layout_cb(ui_gadget_h ug, enum ug_mode mode, void *priv)
{
	struct appdata *ad;
	Evas_Object *base;

	if (!ug || !priv)
		return;

	ad = priv;

	base = ug_get_layout(ug);
	if (!base) {
		LOGE("base layout is null");
		return;
	}

	switch (mode) {
	case UG_MODE_FULLVIEW:
		evas_object_size_hint_weight_set(base, EVAS_HINT_EXPAND,
						 EVAS_HINT_EXPAND);
		ug_disable_effect(ug);
		elm_object_content_set(ad->ly_main, base);
		evas_object_show(base);
		break;
	case UG_MODE_FRAMEVIEW:
		elm_object_part_content_set(ad->ly_main, "content", base);
		break;
	default:
		break;
	}
}

void _ug_client_result_cb(ui_gadget_h ug, service_h reply, void *priv)
{
	struct appdata *ad = NULL;
	int ret;
	char* value = NULL;
	int result;

	if (!ug || !priv)
		return;

	ret = service_get_extra_data (reply, UG_SERVICE_DATA_RESULT, &value);
	if((ret == SERVICE_ERROR_NONE) && (value)) {
		result = atoi(value);
		LOGD("reply result is %d", result);
	} else {
		LOGW("get reply result error(%d) . result will be SERVICE_RESULT_SUCCEEDED", ret);
		result = SERVICE_RESULT_SUCCEEDED;
	}

	ad = priv;
	if (!ad) {
		LOGE("appdata is null");
		return;
	}

	ret = service_reply_to_launch_request(reply, ad->request, (service_result_e)result);
	if (ret != SERVICE_ERROR_NONE)
		LOGE("service_reply_to_launch_request failed, %d", ret);
}

void _ug_client_destroy_cb(ui_gadget_h ug, void *priv)
{
	struct appdata *ad = NULL;

	if (!ug)
		return;

	ad = priv;
	if (!ad) {
		LOGE("appdata is null. win lower is fail");
	} else {
		LOGD("window lower");
		elm_win_lower(ad->win);
	}

	elm_exit();
}

void _ug_client_end_cb(ui_gadget_h ug, void *priv)
{
	if (!ug)
		return;

	LOGD("_ug_client_end_cb invoked");

	elm_exit();
}

static void profile_changed_cb(void *data, Evas_Object * obj, void *event)
{
	const char *profile = elm_config_profile_get();

	LOGE("!!! profile_changed_cb(%s) !!!", profile);

	if (strcmp(profile, "desktop") == 0)
		elm_win_indicator_mode_set(obj, ELM_WIN_INDICATOR_HIDE);
}

static Evas_Object *create_win(const char *name)
{
	Evas_Object *eo;
	int w, h;

	eo = elm_win_add(NULL, name, ELM_WIN_BASIC);
	if (eo) {
		elm_win_title_set(eo, name);
		elm_win_conformant_set(eo, EINA_TRUE);
		evas_object_smart_callback_add(eo, "delete,request",
					       win_del, NULL);
		/* disable destktop mode
		evas_object_smart_callback_add(eo, "profile,changed", profile_changed_cb, NULL); */
		ecore_x_window_size_get(ecore_x_window_root_first_get(),
					&w, &h);
		evas_object_resize(eo, w, h);

		elm_win_indicator_mode_set(eo,ELM_WIN_INDICATOR_SHOW);
	}

	return eo;
}

static Evas_Object *_ug_client_load_edj(Evas_Object *parent, const char *file,
			     const char *group)
{
	Evas_Object *eo;
	int r;
	eo = elm_layout_add(parent);
	if (eo) {
		r = elm_layout_file_set(eo, file, group);
		if (!r) {
			evas_object_del(eo);
			return NULL;
		}
		evas_object_size_hint_weight_set(eo,
						 EVAS_HINT_EXPAND,
						 EVAS_HINT_EXPAND);
	}
	return eo;
}

static int low_memory(void *data)
{
	return ug_send_event(UG_EVENT_LOW_MEMORY);
}

static int low_battery(void *data)
{
	return ug_send_event(UG_EVENT_LOW_BATTERY);
}

static int lang_changed(void *data)
{
	char* lang = NULL;

	lang = vconf_get_str(VCONFKEY_LANGSET);
	if(lang) {
		LOGD("lang : %s", lang);
		elm_language_set((const char*)lang);
		free(lang);
	} else {
		LOGW("language get error");
	}

	return ug_send_event(UG_EVENT_LANG_CHANGE);
}

static int region_changed(void *data)
{
	return ug_send_event(UG_EVENT_REGION_CHANGE);
}

static void _home_screen_top_cb(keynode_t* node, void *data)
{
	struct appdata *ad = data;

	if (!node) {
		LOGE("home screen top cb node value is null");
		return;
	}

	if ((node->value.i == VCONFKEY_IDLE_SCREEN_TOP_TRUE) && (!ad->is_transient)) {
		LOGW("home key pressed. window is not transient. ug client will be terminated");
		elm_exit();
	}
}

static int app_create(void *data)
{
	struct appdata *ad = data;
	Evas_Object *win;
	Evas_Object *ly;
	Evas_Object *conform;
	Evas_Object *bg;

	/* create window */
	win = create_win(PACKAGE);
	if (win == NULL)
		return -1;
	ad->win = win;
	UG_INIT_EFL(ad->win, UG_OPT_INDICATOR_ENABLE);

	bg = elm_bg_add(win);
	evas_object_size_hint_weight_set(bg, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
	elm_win_resize_object_add(win, bg);
	evas_object_show(bg);

	conform = elm_conformant_add(win);
	evas_object_size_hint_weight_set(conform, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
	ad->conform = conform;

	/* load edje */
	ly = _ug_client_load_edj(conform, EDJ_FILE, GRP_MAIN);
	if (ly == NULL)
		return -1;
	elm_win_resize_object_add(win, conform);

	evas_object_show(conform);
	elm_object_content_set(conform, ly);
	edje_object_signal_callback_add(elm_layout_edje_get(ly),
					"EXIT", "*", main_quit_cb, NULL);
	ad->ly_main = ly;
	lang_changed(ad);

	/* rotate notice */
	int angle = -1;
	angle = elm_win_rotation_get((const Evas_Object *)win);
	LOGE("rotate : %d", angle);
	if(angle != -1) {
		ug_send_rotate_event(angle);
		ad->rotate = angle;
	} else {
		LOGE("elm win rotation get error");
	}
	if(elm_win_wm_rotation_supported_get(win)) {
		int rots[4] = { 0, 90, 180, 270 };
		elm_win_wm_rotation_available_rotations_set(win, (const int*)&rots, 4);
	} else {
		LOGW("wm rotation supported get error");
	}
	evas_object_smart_callback_add(win, "wm,rotation,changed", rotate, data);

	appcore_set_event_callback(APPCORE_EVENT_LOW_MEMORY, low_memory, ad);
	appcore_set_event_callback(APPCORE_EVENT_LOW_BATTERY, low_battery, ad);
	appcore_set_event_callback(APPCORE_EVENT_LANG_CHANGE, lang_changed, ad);
	appcore_set_event_callback(APPCORE_EVENT_REGION_CHANGE, region_changed, ad);

	return 0;
}

static int app_terminate(void *data)
{
	struct appdata *ad = data;

	LOGD("app_terminate called");

	evas_object_smart_callback_del(ad->win, "wm,rotation,changed", rotate);

	ug_destroy_all();

	if (ad->ly_main) {
		evas_object_del(ad->ly_main);
		ad->ly_main = NULL;
	}

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

	service_destroy(ad->request);

	if (ad->name) {
		free(ad->name);
	}

	LOGD("app_terminate end");

	return 0;
}

static int app_pause(void *data)
{
	struct appdata *ad = data;

	LOGD("app_pause called");

	ug_pause();

#if ENABLE_TRANSIENT_SUB_MODE
	if (!ad->is_transient) {
		LOGD("app_pause received. close ug service");
		elm_exit();
	}
#endif

	return 0;
}

static int app_resume(void *data)
{
	ug_resume();
	return 0;
}

static int svc_cb(void *data)
{
	LOGD("svc_cb called");
	return 0;
}

static int app_reset(bundle *b, void *data)
{
	struct appdata *ad = data;
	struct ug_cbs cbs = { 0, };
	service_h service;
	enum ug_mode mode = UG_MODE_FULLVIEW;
	int ret;
	Ecore_X_Window id2 = elm_win_xwindow_get(ad->win);

	ret = appsvc_request_transient_app(b, id2, svc_cb, "svc test");
	if (ret) {
		LOGD("fail to request transient app: return value(%d)", ret);
		if(vconf_notify_key_changed(VCONFKEY_IDLE_SCREEN_TOP, _home_screen_top_cb, ad) != 0) {
			LOGW("home screen vconf key changed cb error");
		}
	} else {
		/* check home screen raise */
		ad->is_transient = 1;
	}

	if (ad->win) {
		elm_win_activate(ad->win);
		evas_object_show(ad->win);
	}

	if (ad->data)	/* ug-launcher */
		service_create_event(ad->data, &service);
	else
		service_create_event(b, &service);

	if(service) {
		service_clone(&ad->request, service);
		service_destroy(service);
	}

	cbs.layout_cb = _ug_client_layout_cb;
	cbs.destroy_cb = _ug_client_destroy_cb;
	cbs.result_cb = _ug_client_result_cb;
	cbs.end_cb = _ug_client_end_cb;
	cbs.priv = ad;

	mode = ad->is_frameview ? UG_MODE_FRAMEVIEW : UG_MODE_FULLVIEW;

	ad->ug = ug_create(NULL, ad->name, mode, ad->request, &cbs);
	if (ad->ug == NULL) {
		LOGE("ug_create fail: %s", ad->name);
		elm_exit();
	}

	return 0;
}

static int update_argument(const char *optarg, struct appdata *ad)
{
	const char *key;
	const char *val;
	key = strtok((char *)optarg, ",");
	if (!key)
		return -1;

	val = optarg + strlen(key) + 1;

	if (!ad->data)
		ad->data = bundle_create();
	if (!ad->data)
		return -1;
	bundle_add(ad->data, key, val);
	return 0;
}

int main(int argc, char *argv[])
{
	int opt;
	struct appdata ad;
	struct appcore_ops ops = {
		.create = app_create,
		.terminate = app_terminate,
		.pause = app_pause,
		.resume = app_resume,
		.reset = app_reset,
	};
	int cmdlen = 0;

	memset(&ad, 0x0, sizeof(struct appdata));
	ops.data = &ad;

	cmdlen = strlen(argv[0]);
	if (strncmp(argv[0], "ug-launcher", cmdlen) == 0
		|| strncmp(argv[0], "/usr/bin/ug-launcher", cmdlen) == 0) {
		while ((opt = getopt(argc, argv, "n:d:")) != -1) {
			switch (opt) {
			case 'n':
				if (optarg)
					ad.name = strdup(optarg);
				break;
			case 'f':
				ad.is_frameview = 1;
				break;
			case 'F':
				ad.is_frameview = 0;
				break;
			case 'd':
				if (update_argument(optarg, &ad)) {
					if (ad.data)
						bundle_free(ad.data);
					prt_usage(argv[0]);
					return -1;
				}
				break;
			default:
				prt_usage(argv[0]);
				return -1;
			}
		}

		if (!ad.name) {
			prt_usage(argv[0]);
			return -1;
		}
		argc = 1; // remove appsvc bundle
	} else {	/* ug-client */
		char *name = NULL;
		name = strrchr(argv[0], '/');
		if (name == NULL)
			return -1;
		/* .../bin/{name} */
		ad.name = strdup(&name[1]);
	}
	return appcore_efl_main(PACKAGE, &argc, &argv, &ops);
}