summaryrefslogtreecommitdiff
path: root/client/ug-client.c
blob: 7c2ac361ac1595618f8a26235f003f8683b3558c (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
/*
 *  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 <appsvc.h>
#include <app.h>
#include <runtime_info.h>

#include "ug-client.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 rotate(enum appcore_rm m, void *data)
{
	struct appdata *ad = data;
	int r;

	if (ad == NULL || ad->win == NULL)
		return 0;

	switch (m) {
		case APPCORE_RM_PORTRAIT_NORMAL:
			ug_send_event(UG_EVENT_ROTATE_PORTRAIT);
			r = 0;
			break;
		case APPCORE_RM_PORTRAIT_REVERSE:
			r = 180;
			ug_send_event(UG_EVENT_ROTATE_PORTRAIT_UPSIDEDOWN);
			break;
		case APPCORE_RM_LANDSCAPE_NORMAL:
			ug_send_event(UG_EVENT_ROTATE_LANDSCAPE);
			r = 270;
			break;
		case APPCORE_RM_LANDSCAPE_REVERSE:
			ug_send_event(UG_EVENT_ROTATE_LANDSCAPE_UPSIDEDOWN);
			r = 90;
			break;
		default:
			r = -1;
			break;
	}

	LOGE("rotate cb / rm : %d , r : %d", m, r);

	if(r >= 0)
		elm_win_rotation_with_resize_set(ad->win, r);

	return 0;
}

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 result, void *priv)
{
	struct appdata *ad;
	int ret;

	if (!ug || !priv)
		return;
	ad = priv;

	ret = service_reply_to_launch_request(result, ad->request, SERVICE_RESULT_SUCCEEDED);
	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)
{
	if (!ug)
		return;

	ug_destroy(ug);
	elm_exit();
}

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

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

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);
		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);
	}

	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)
{
	return ug_send_event(UG_EVENT_LANG_CHANGE);
}

static int app_create(void *data)
{
	struct appdata *ad = data;
	enum appcore_rm rm;
	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;
	elm_win_indicator_mode_set(win, ELM_WIN_INDICATOR_SHOW);
	lang_changed(ad);

	if (appcore_get_rotation_state(&rm) == 0)
		rotate(rm, ad);

	appcore_set_rotation_cb(rotate, ad);
	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);

	return 0;
}

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

	LOGD("app_terminate called");

	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;
	}

	return 0;
}

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

	LOGD("app_pause called");

	ug_pause();
	if (!ad->is_transient) {
		LOGD("app_pause received. close ug service");
		elm_exit();
	}
	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);
	else
		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.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 (!val)
		return -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);
}