2 * Copyright (c) 2013-2015 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include <Elementary.h>
18 #include <Ecore_Evas.h>
20 #include "minicontrol-internal.h"
21 #include "minicontrol-type.h"
22 #include "minicontrol-viewer.h"
23 #include "minicontrol-viewer-internal.h"
24 #include "minicontrol-log.h"
26 #define MINICTRL_PLUG_DATA_KEY "__minictrl_plug_name"
28 struct _minicontrol_viewer {
29 minictrl_sig_handle *event_sh;
30 minicontrol_viewer_event_cb callback;
34 static struct _minicontrol_viewer *g_minicontrol_viewer_h = NULL;
36 EXPORT_API int minicontrol_viewer_send_event(const char *minicontrol_name, minicontrol_viewer_event_e event, bundle *event_arg)
38 int ret = MINICONTROL_ERROR_NONE;
40 if (minicontrol_name == NULL) {
41 ERR("appid is NULL, invaild parameter");
42 return MINICONTROL_ERROR_INVALID_PARAMETER;
45 ret = _minictrl_send_event(MINICTRL_DBUS_SIG_TO_PROVIDER, minicontrol_name, event, event_arg);
51 static void _sig_to_viewer_handler_cb(void *data, DBusMessage *msg)
54 char *minicontrol_name = NULL;
55 minicontrol_event_e event;
57 bundle *event_arg_bundle = NULL;
58 bundle_raw *serialized_arg = NULL;
59 unsigned int serialized_arg_length = 0;
61 dbus_error_init(&err); /* Does not allocate any memory. the error only needs to be freed if it is set at some point. */
63 dbus_ret = dbus_message_get_args(msg, &err,
64 DBUS_TYPE_STRING, &minicontrol_name,
65 DBUS_TYPE_INT32, &event,
66 DBUS_TYPE_STRING, &serialized_arg,
67 DBUS_TYPE_UINT32, &serialized_arg_length,
71 ERR("fail to get args : %s", err.message);
72 dbus_error_free(&err);
76 if (serialized_arg_length != 0) {
77 event_arg_bundle = bundle_decode(serialized_arg, serialized_arg_length);
79 if (event_arg_bundle == NULL) {
80 ERR("fail to deserialize arguments");
85 if (g_minicontrol_viewer_h->callback)
86 g_minicontrol_viewer_h->callback(event, minicontrol_name, event_arg_bundle, g_minicontrol_viewer_h->user_data);
88 bundle_free(event_arg_bundle);
89 dbus_error_free(&err);
93 EXPORT_API int minicontrol_viewer_set_event_cb(minicontrol_viewer_event_cb callback, void *data)
96 ERR("MINICONTROL_ERROR_INVALID_PARAMETER");
97 return MINICONTROL_ERROR_INVALID_PARAMETER;
100 INFO("g_minicontrol_viewer_h [%p]", g_minicontrol_viewer_h);
102 if (g_minicontrol_viewer_h == NULL) {
103 minictrl_sig_handle *event_sh;
104 struct _minicontrol_viewer *minicontrol_viewer_h;
106 event_sh = _minictrl_dbus_sig_handle_attach(MINICTRL_DBUS_SIG_TO_VIEWER, _sig_to_viewer_handler_cb, NULL);
108 ERR("fail to _minictrl_dbus_sig_handle_attach - %s", MINICTRL_DBUS_SIG_TO_VIEWER);
109 return MINICONTROL_ERROR_IPC_FAILURE;
112 minicontrol_viewer_h = malloc(sizeof(struct _minicontrol_viewer));
113 if (!minicontrol_viewer_h) {
114 ERR("fail to alloc minicontrol_viewer_h");
115 _minictrl_dbus_sig_handle_dettach(event_sh);
116 return MINICONTROL_ERROR_OUT_OF_MEMORY;
119 minicontrol_viewer_h->event_sh = event_sh;
120 g_minicontrol_viewer_h = minicontrol_viewer_h;
123 g_minicontrol_viewer_h->callback = callback;
124 g_minicontrol_viewer_h->user_data = data;
125 INFO("callback[%p], data[%p]", callback, data);
127 return _minictrl_viewer_req_message_send();
130 EXPORT_API int minicontrol_viewer_unset_event_cb(void)
132 if (!g_minicontrol_viewer_h)
133 return MINICONTROL_ERROR_NONE;
135 if (g_minicontrol_viewer_h->event_sh)
136 _minictrl_dbus_sig_handle_dettach(g_minicontrol_viewer_h->event_sh);
138 free(g_minicontrol_viewer_h);
139 g_minicontrol_viewer_h = NULL;
141 return MINICONTROL_ERROR_NONE;
144 static void _minictrl_plug_server_del(Ecore_Evas *ee)
146 char *minicontrol_name = NULL;
148 minicontrol_name = ecore_evas_data_get(ee, MINICTRL_PLUG_DATA_KEY);
149 if (!minicontrol_name) {
150 ERR("fail to get minicontrol_name");
154 INFO("server - %s is deleted", minicontrol_name);
156 /* To avoid retrying to free minicontrol_name again, set MINICTRL_PLUG_DATA_KEY as NULL */
157 ecore_evas_data_set(ee, MINICTRL_PLUG_DATA_KEY, NULL);
159 /* send message to remove plug */
160 _minictrl_provider_message_send(MINICONTROL_EVENT_STOP, minicontrol_name, 0, 0, MINICONTROL_PRIORITY_LOW);
161 _minictrl_provider_proc_send(MINICONTROL_DBUS_PROC_INCLUDE);
162 free(minicontrol_name);
165 static void _minictrl_plug_del(void *data, Evas *e, Evas_Object *obj, void *event_info)
167 Ecore_Evas *ee = data;
168 char *minicontrol_name = NULL;
173 minicontrol_name = ecore_evas_data_get(ee, MINICTRL_PLUG_DATA_KEY);
175 if (minicontrol_name) {
176 /* Sending an event 'MINICONTROL_EVENT_REQUEST_HIDE' should be done by minicontrol viewer manually */
177 free(minicontrol_name);
178 ecore_evas_data_set(ee, MINICTRL_PLUG_DATA_KEY, NULL);
182 EXPORT_API Evas_Object *minicontrol_viewer_add(Evas_Object *parent, const char *minicontrol_name)
184 Evas_Object *plug = NULL;
185 Evas_Object *plug_img = NULL;
186 Ecore_Evas *ee = NULL;
188 if (parent == NULL || minicontrol_name == NULL) {
189 ERR("invalid parameter");
190 set_last_result(MINICONTROL_ERROR_INVALID_PARAMETER);
194 plug = elm_plug_add(parent);
197 ERR("fail to create plug");
198 set_last_result(MINICONTROL_ERROR_ELM_FAILURE);
202 if (!elm_plug_connect(plug, minicontrol_name, 0, EINA_TRUE)) {
203 ERR("Cannot connect plug[%s]", minicontrol_name);
204 set_last_result(MINICONTROL_ERROR_ELM_FAILURE);
205 evas_object_del(plug);
209 plug_img = elm_plug_image_object_get(plug);
211 ee = ecore_evas_object_ecore_evas_get(plug_img);
212 ecore_evas_data_set(ee, MINICTRL_PLUG_DATA_KEY, strdup(minicontrol_name));
213 ecore_evas_callback_delete_request_set(ee, _minictrl_plug_server_del);
215 evas_object_event_callback_add(plug, EVAS_CALLBACK_DEL, _minictrl_plug_del, ee);
220 EXPORT_API Evas_Object *minicontrol_viewer_image_object_get(const Evas_Object *obj)
222 return elm_plug_image_object_get(obj);
225 EXPORT_API int minicontrol_viewer_request(const char *minicontrol_name, minicontrol_request_e request, int value)
227 int ret = MINICONTROL_ERROR_NONE;
228 minicontrol_viewer_event_e event = 0;
229 bundle *event_arg_bundle = NULL;
230 char bundle_value_buffer[BUNDLE_BUFFER_LENGTH] = { 0, };
232 if (minicontrol_name == NULL) {
233 ERR("appid is NULL, invaild parameter");
234 return MINICONTROL_ERROR_INVALID_PARAMETER;
238 case MINICONTROL_REQ_ROTATE_PROVIDER: {
239 event = MINICONTROL_EVENT_REPORT_ANGLE;
240 event_arg_bundle = bundle_create();
242 if (event_arg_bundle == NULL) {
243 ERR("fail to create a bundle instance");
244 ret = MINICONTROL_ERROR_OUT_OF_MEMORY;
248 snprintf(bundle_value_buffer, BUNDLE_BUFFER_LENGTH, "%d", value);
250 bundle_add_str(event_arg_bundle, "angle", bundle_value_buffer);
253 case MINICONTROL_REQ_NONE:
254 case MINICONTROL_REQ_HIDE_VIEWER:
255 case MINICONTROL_REQ_FREEZE_SCROLL_VIEWER:
256 case MINICONTROL_REQ_UNFREEZE_SCROLL_VIEWER:
257 case MINICONTROL_REQ_REPORT_VIEWER_ANGLE:
259 ret = MINICONTROL_ERROR_INVALID_PARAMETER;
263 _minictrl_send_event(MINICTRL_DBUS_SIG_TO_PROVIDER, minicontrol_name, event, event_arg_bundle);
266 if (event_arg_bundle)
267 bundle_free(event_arg_bundle);