2 * Copyright 2012 Samsung Electronics Co., Ltd
4 * Licensed under the Flora License, Version 1.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.tizenopensource.org/license
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>
19 #include "minicontrol-error.h"
20 #include "minicontrol-type.h"
21 #include "minicontrol-internal.h"
22 #include "minicontrol-provider.h"
23 #include "minicontrol-log.h"
25 #define MINICTRL_PRIORITY_SUFFIX_TOP "__minicontrol_top"
26 #define MINICTRL_PRIORITY_SUFFIX_LOW "__minicontrol_low"
27 #define MINICTRL_DATA_KEY "__minictrl_data_internal"
30 MINICTRL_STATE_READY =0,
31 MINICTRL_STATE_RUNNING,
34 struct _provider_data {
37 minicontrol_priority_e priority;
39 minictrl_sig_handle *sh;
42 static void __provider_data_free(struct _provider_data *pd)
49 _minictrl_dbus_sig_handle_dettach(pd->sh);
55 static int __str_has_suffix(const char *str, const char *suffix)
66 str_len = strlen (str);
67 suffix_len = strlen (suffix);
69 if (str_len < suffix_len)
72 return strcmp(str + str_len - suffix_len, suffix);
75 static void _running_req_cb(void *data, DBusMessage *msg)
77 struct _provider_data *pd;
85 if (pd->state == MINICTRL_STATE_RUNNING) {
88 evas_object_geometry_get(pd->obj, NULL, NULL, &w, &h);
89 _minictrl_provider_message_send(MINICTRL_DBUS_SIG_START,
90 pd->name, w, h, pd->priority);
94 static int minicontrol_win_start(Evas_Object *mincontrol)
96 struct _provider_data *pd;
97 int ret = MINICONTROL_ERROR_NONE;
100 ERR("mincontrol is NULL, invaild parameter");
101 return MINICONTROL_ERROR_INVALID_PARAMETER;
104 pd = evas_object_data_get(mincontrol, MINICTRL_DATA_KEY);
106 ERR("pd is NULL, invaild parameter");
107 return MINICONTROL_ERROR_INVALID_PARAMETER;
111 ERR("pd name is NULL, invaild parameter");
112 return MINICONTROL_ERROR_INVALID_PARAMETER;
115 if (pd->state != MINICTRL_STATE_RUNNING) {
118 pd->state = MINICTRL_STATE_RUNNING;
120 evas_object_geometry_get(mincontrol, NULL, NULL, &w, &h);
121 ret = _minictrl_provider_message_send(MINICTRL_DBUS_SIG_START,
122 pd->name, w, h, pd->priority);
128 static int minicontrol_win_stop(Evas_Object *mincontrol)
130 struct _provider_data *pd;
131 int ret = MINICONTROL_ERROR_NONE;
134 ERR("mincontrol is NULL, invaild parameter");
135 return MINICONTROL_ERROR_INVALID_PARAMETER;
138 pd = evas_object_data_get(mincontrol, MINICTRL_DATA_KEY);
140 ERR("pd is NULL, invaild parameter");
141 return MINICONTROL_ERROR_INVALID_PARAMETER;
145 ERR("pd name is NULL, invaild parameter");
146 return MINICONTROL_ERROR_INVALID_PARAMETER;
148 if (pd->state != MINICTRL_STATE_READY) {
149 pd->state = MINICTRL_STATE_READY;
150 ret = _minictrl_provider_message_send(MINICTRL_DBUS_SIG_STOP,
151 pd->name, 0, 0, pd->priority);
157 static void _minictrl_win_del(void *data, Evas *e,
158 Evas_Object *obj, void *event_info)
160 struct _provider_data *pd = NULL;
162 minicontrol_win_stop(obj);
164 pd = evas_object_data_get(obj, MINICTRL_DATA_KEY);
165 __provider_data_free(pd);
167 evas_object_data_set(obj, MINICTRL_DATA_KEY, NULL);
170 static void _minictrl_win_hide(void *data, Evas *e,
171 Evas_Object *obj, void *event_info)
173 minicontrol_win_stop(obj);
176 static void _minictrl_win_show(void *data, Evas *e,
177 Evas_Object *obj, void *event_info)
179 minicontrol_win_start(obj);
182 static void _minictrl_win_resize(void *data, Evas *e,
183 Evas_Object *obj, void *event_info)
185 struct _provider_data *pd;
188 ERR("data is NULL, invaild parameter");
193 if (pd->state == MINICTRL_STATE_RUNNING) {
197 evas_object_geometry_get(obj, NULL, NULL, &w, &h);
198 _minictrl_provider_message_send(MINICTRL_DBUS_SIG_RESIZE,
199 pd->name, w, h, pd->priority);
203 static char *_minictrl_create_name(const char *name)
207 char time_buf[20]; /* 18 chars to represent time */
212 ERR("name is NULL, invaild parameter");
217 now_tm = localtime(&now);
218 strftime(time_buf, sizeof(time_buf), "%y-%m-%d-%H:%M:%S", now_tm);
220 size = snprintf(NULL, 0, "[%s]-[%s]", name, time_buf) + 1;
221 buf = (char *)malloc(sizeof(char) * size);
223 ERR("fail to alloc buf");
227 snprintf(buf, size, "[%s]-[%s]", name, time_buf);
232 static minicontrol_priority_e _minictrl_get_priroty_by_name(const char *name)
234 minicontrol_priority_e priority = MINICONTROL_PRIORITY_MIDDLE;
236 if (!__str_has_suffix(name, MINICTRL_PRIORITY_SUFFIX_TOP))
237 priority = MINICONTROL_PRIORITY_TOP;
238 else if (!__str_has_suffix(name, MINICTRL_PRIORITY_SUFFIX_LOW))
239 priority = MINICONTROL_PRIORITY_LOW;
244 EXPORT_API Evas_Object *minicontrol_win_add(const char *name)
246 Evas_Object *win = NULL;
247 char *name_inter = NULL;
248 struct _provider_data *pd;
253 win = elm_win_add(NULL, "minicontrol", ELM_WIN_SOCKET_IMAGE);
257 name_inter = _minictrl_create_name(name);
260 ERR("Fail to create name_inter for : %s", name);
261 evas_object_del(win);
266 if (!elm_win_socket_listen(win, name_inter, 0, EINA_FALSE)) {
267 ERR("Fail to elm win socket listen");
268 evas_object_del(win);
272 pd = malloc(sizeof(struct _provider_data));
274 ERR("Fail to alloc memory");
275 evas_object_del(win);
280 memset(pd, 0x00, sizeof(struct _provider_data));
281 pd->name = name_inter;
282 pd->state = MINICTRL_STATE_READY;
284 pd->priority = _minictrl_get_priroty_by_name(name);
286 evas_object_data_set(win ,MINICTRL_DATA_KEY,pd);
288 elm_win_autodel_set(win, EINA_TRUE);
290 evas_object_event_callback_add(win, EVAS_CALLBACK_DEL,
291 _minictrl_win_del, pd);
293 evas_object_event_callback_add(win, EVAS_CALLBACK_SHOW,
294 _minictrl_win_show, pd);
296 evas_object_event_callback_add(win, EVAS_CALLBACK_HIDE,
297 _minictrl_win_hide, pd);
299 evas_object_event_callback_add(win, EVAS_CALLBACK_RESIZE,
300 _minictrl_win_resize, pd);
302 pd->sh = _minictrl_dbus_sig_handle_attach(MINICTRL_DBUS_SIG_RUNNING_REQ,
303 _running_req_cb, pd);
305 INFO("new minicontrol win[%p] created - %s, priority[%d]",
306 win, pd->name, pd->priority);