summaryrefslogtreecommitdiff
path: root/src/minicontrol-viewer.c
blob: ad7c66a71b197d9f126a1487c3c80fe20c4ab276 (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
/*
 * Copyright (c) 2013 - 2016 Samsung Electronics Co., Ltd All Rights Reserved
 *
 * 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 <Elementary.h>
#include <Ecore_Evas.h>

#include "minicontrol-internal.h"
#include "minicontrol-type.h"
#include "minicontrol-viewer.h"
#include "minicontrol-viewer-internal.h"
#include "minicontrol-log.h"
#include "minicontrol-private.h"

#define MINICTRL_PLUG_DATA_KEY "__minictrl_plug_name"

struct _minicontrol_viewer {
	minictrl_sig_handle *event_sh;
	minicontrol_viewer_event_cb callback;
	void *user_data;
};

static struct _minicontrol_viewer *g_minicontrol_viewer_h;

EXPORT_API int minicontrol_viewer_send_event(const char *minicontrol_name,
		minicontrol_viewer_event_e event, bundle *event_arg)
{
	int ret = MINICONTROL_ERROR_NONE;

	CHECK_MINICONTROL_FEATURE();

	if (minicontrol_name == NULL) {
		ERR("appid is NULL, invaild parameter");
		return MINICONTROL_ERROR_INVALID_PARAMETER;
	}

	ret = _minictrl_send_event(MINICTRL_DBUS_SIG_TO_PROVIDER,
			minicontrol_name, event, event_arg);

	return ret;
}

static void _sig_to_viewer_handler_cb(void *data, GVariant *parameters)
{
	char *minicontrol_name = NULL;
	minicontrol_event_e event;
	bundle *event_arg_bundle = NULL;
	bundle_raw *serialized_arg = NULL;
	unsigned int serialized_arg_length = 0;

	g_variant_get(parameters, "(&si&su)", &minicontrol_name, &event,
			&serialized_arg, &serialized_arg_length);

	if (serialized_arg_length != 0) {
		event_arg_bundle = bundle_decode(serialized_arg,
				serialized_arg_length);
		if (event_arg_bundle == NULL) {
			/* LCOV_EXCL_START */
			ERR("fail to deserialize arguments");
			return;
			/* LCOV_EXCL_STOP */
		}
	}

	if (g_minicontrol_viewer_h->callback) {
		g_minicontrol_viewer_h->callback(event, minicontrol_name,
				event_arg_bundle,
				g_minicontrol_viewer_h->user_data);
	}

	bundle_free(event_arg_bundle);
}


EXPORT_API int minicontrol_viewer_set_event_cb(
		minicontrol_viewer_event_cb callback, void *data)
{
	minictrl_sig_handle *event_sh;
	struct _minicontrol_viewer *minicontrol_viewer_h;

	CHECK_MINICONTROL_FEATURE();

	if (!callback) {
		ERR("MINICONTROL_ERROR_INVALID_PARAMETER");
		return MINICONTROL_ERROR_INVALID_PARAMETER;
	}

	INFO("g_minicontrol_viewer_h [%p]", g_minicontrol_viewer_h);

	if (g_minicontrol_viewer_h == NULL) {
		event_sh = _minictrl_dbus_sig_handle_attach(
				MINICTRL_DBUS_SIG_TO_VIEWER,
				_sig_to_viewer_handler_cb, NULL);
		if (!event_sh) {
			/* LCOV_EXCL_START */
			ERR("fail to _minictrl_dbus_sig_handle_attach - %s",
					MINICTRL_DBUS_SIG_TO_VIEWER);
			return MINICONTROL_ERROR_IPC_FAILURE;
			/* LCOV_EXCL_STOP */
		}

		minicontrol_viewer_h =
			malloc(sizeof(struct _minicontrol_viewer));
		if (!minicontrol_viewer_h) {
			/* LCOV_EXCL_START */
			ERR("fail to alloc minicontrol_viewer_h");
			_minictrl_dbus_sig_handle_dettach(event_sh);
			return MINICONTROL_ERROR_OUT_OF_MEMORY;
			/* LCOV_EXCL_STOP */
		}

		minicontrol_viewer_h->event_sh = event_sh;
		g_minicontrol_viewer_h = minicontrol_viewer_h;
	}

	g_minicontrol_viewer_h->callback = callback;
	g_minicontrol_viewer_h->user_data = data;
	INFO("callback[%p], data[%p]", callback, data);

	return _minictrl_viewer_req_message_send();
}

EXPORT_API int minicontrol_viewer_unset_event_cb(void)
{
	CHECK_MINICONTROL_FEATURE();

	if (!g_minicontrol_viewer_h)
		return MINICONTROL_ERROR_NONE;

	if (g_minicontrol_viewer_h->event_sh) {
		_minictrl_dbus_sig_handle_dettach(
				g_minicontrol_viewer_h->event_sh);
	}

	free(g_minicontrol_viewer_h);
	g_minicontrol_viewer_h = NULL;

	return MINICONTROL_ERROR_NONE;
}

/* LCOV_EXCL_START */
static void _minictrl_plug_server_del(Ecore_Evas *ee)
{
	char *minicontrol_name = NULL;

	minicontrol_name = ecore_evas_data_get(ee, MINICTRL_PLUG_DATA_KEY);
	if (!minicontrol_name) {
		ERR("fail to get minicontrol_name");
		return;
	}

	INFO("server - %s is deleted", minicontrol_name);

	/*
	 * To avoid retrying to free minicontrol_name again,
	 * set MINICTRL_PLUG_DATA_KEY as NULL
	 */
	ecore_evas_data_set(ee, MINICTRL_PLUG_DATA_KEY, NULL);

	/* send message to remove plug */
	_minictrl_provider_message_send(MINICONTROL_EVENT_STOP,
			minicontrol_name, 0, 0, MINICONTROL_PRIORITY_LOW, 0);
	_minictrl_provider_proc_send(MINICONTROL_DBUS_PROC_INCLUDE);
	free(minicontrol_name);
}
/* LCOV_EXCL_STOP */

static void _minictrl_plug_del(void *data, Evas *e, Evas_Object *obj,
		void *event_info)
{
	Ecore_Evas *ee = data;
	char *minicontrol_name;

	if (!ee)
		return;

	minicontrol_name = ecore_evas_data_get(ee, MINICTRL_PLUG_DATA_KEY);
	if (minicontrol_name) {
		/*
		 * Sending an event 'MINICONTROL_EVENT_REQUEST_HIDE'
		 * should be done by minicontrol viewer manually
		 */
		free(minicontrol_name);
		ecore_evas_data_set(ee, MINICTRL_PLUG_DATA_KEY, NULL);
	}
}

EXPORT_API Evas_Object *minicontrol_viewer_add(Evas_Object *parent,
		const char *minicontrol_name)
{
	Evas_Object *plug = NULL;
	Evas_Object *plug_img = NULL;
	Ecore_Evas *ee = NULL;

	CHECK_MINICONTROL_FEATURE_RET_NULL();

	if (parent == NULL || minicontrol_name == NULL) {
		ERR("invalid parameter");
		set_last_result(MINICONTROL_ERROR_INVALID_PARAMETER);
		return NULL;
	}

	plug = elm_plug_add(parent);
	if (!plug) {
		/* LCOV_EXCL_START */
		ERR("fail to create plug");
		set_last_result(MINICONTROL_ERROR_ELM_FAILURE);
		return NULL;
		/* LCOV_EXCL_STOP */
	}

	if (!elm_plug_connect(plug, minicontrol_name, 0, EINA_TRUE)) {
		/* LCOV_EXCL_START */
		ERR("Cannot connect plug[%s]", minicontrol_name);
		set_last_result(MINICONTROL_ERROR_ELM_FAILURE);
		evas_object_del(plug);
		return NULL;
		/* LCOV_EXCL_STOP */
	}

	plug_img = elm_plug_image_object_get(plug);

	ee = ecore_evas_object_ecore_evas_get(plug_img);
	ecore_evas_data_set(ee, MINICTRL_PLUG_DATA_KEY,
			strdup(minicontrol_name));
	ecore_evas_callback_delete_request_set(ee, _minictrl_plug_server_del);

	evas_object_event_callback_add(plug, EVAS_CALLBACK_DEL,
			_minictrl_plug_del, ee);

	set_last_result(MINICONTROL_ERROR_NONE);
	return plug;
}

/* LCOV_EXCL_START */
EXPORT_API Evas_Object *minicontrol_viewer_image_object_get(
		const Evas_Object *obj)
{
	CHECK_MINICONTROL_FEATURE_RET_NULL();

	return elm_plug_image_object_get(obj);
}
/* LCOV_EXCL_STOP */

/* LCOV_EXCL_START */
EXPORT_API int minicontrol_viewer_request(const char *minicontrol_name,
		minicontrol_request_e request, int value)
{
	minicontrol_viewer_event_e event = MINICONTROL_EVENT_REPORT_ANGLE;
	bundle *event_arg_bundle;
	char bundle_value_buffer[BUNDLE_BUFFER_LENGTH];

	CHECK_MINICONTROL_FEATURE();

	if (minicontrol_name == NULL) {
		ERR("appid is NULL, invaild parameter");
		return MINICONTROL_ERROR_INVALID_PARAMETER;
	}

	if (request != MINICONTROL_REQ_ROTATE_PROVIDER)
		return MINICONTROL_ERROR_INVALID_PARAMETER;

	event_arg_bundle = bundle_create();
	if (event_arg_bundle == NULL) {
		ERR("fail to create a bundle instance");
		return MINICONTROL_ERROR_OUT_OF_MEMORY;
	}

	snprintf(bundle_value_buffer, sizeof(bundle_value_buffer), "%d", value);
	bundle_add_str(event_arg_bundle, "angle", bundle_value_buffer);
	_minictrl_send_event(MINICTRL_DBUS_SIG_TO_PROVIDER, minicontrol_name,
			event, event_arg_bundle);
	bundle_free(event_arg_bundle);

	return MINICONTROL_ERROR_NONE;
}
/* LCOV_EXCL_STOP */