tizen 2.4 release
[framework/uifw/libeom.git] / src / dbus / eom-connect.c
1 /**************************************************************************
2
3 eom (external output manager)
4
5 Copyright 2014 Samsung Electronics co., Ltd. All Rights Reserved.
6
7 Contact:
8 SooChan Lim <sc1.lim@samsung.com>
9 Boram Park <boram1288.park@samsung.com>
10 Changyeon Lee <cyeon.lee@samsung.com>
11
12 Permission is hereby granted, free of charge, to any person obtaining a
13 copy of this software and associated documentation files (the
14 "Software"), to deal in the Software without restriction, including
15 without limitation the rights to use, copy, modify, merge, publish,
16 distribute, sub license, and/or sell copies of the Software, and to
17 permit persons to whom the Software is furnished to do so, subject to
18 the following conditions:
19
20 The above copyright notice and this permission notice (including the
21 next paragraph) shall be included in all copies or substantial portions
22 of the Software.
23
24 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
25 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
27 IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
28 ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
29 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
30 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31
32 **************************************************************************/
33
34 #include <config.h>
35
36 #include "eom.h"
37 #include "eom-log.h"
38 #include "eom-dbus.h"
39 #include "eom-connect.h"
40 #include "eom-private.h"
41
42 API int
43 eom_output_set_mode(eom_output_id output_id, eom_output_mode_e mode)
44 {
45         bool ret = false;
46         GValueArray *msg_array;
47         GValueArray *ret_array;
48         GValue v = G_VALUE_INIT;
49
50         RETV_IF_FAIL(mode < EOM_OUTPUT_MODE_MAX, EOM_ERROR_INVALID_PARAMETER);
51
52         _eom_mutex_lock();
53
54         INFO("mode: %d\n", mode);
55
56         msg_array = g_value_array_new(0);
57
58         g_value_init(&v, G_TYPE_INT);
59         g_value_set_int(&v, output_id);
60         msg_array = g_value_array_append(msg_array, &v);
61         g_value_set_int(&v, mode);
62         msg_array = g_value_array_append(msg_array, &v);
63
64         ret_array = eom_dbus_client_send_message("SetMode", msg_array);
65         g_value_array_free(msg_array);
66         if (!ret_array) {
67                 _eom_mutex_unlock();
68                 return EOM_ERROR_MESSAGE_SENDING_FAILURE;
69         }
70
71         ret = g_value_get_int(g_value_array_get_nth(ret_array, 0));
72
73         g_value_array_free(ret_array);
74
75         INFO("SetMode: %s", (ret) ? "success" : "failed");
76
77         _eom_mutex_unlock();
78
79         return (ret) ? EOM_ERROR_NONE : EOM_ERROR_MESSAGE_OPERATION_FAILURE;
80 }
81
82 static int
83 _eom_dbus_convert_gvalue_to_message_temp(GArray *array, DBusMessage *msg)
84 {
85         DBusMessageIter iter;
86         GValue *v = NULL;
87         GType type;
88         int i;
89
90         if (!array)
91                 return 1;
92
93         if (array->len <= 0)
94                 return 1;
95
96         dbus_message_iter_init_append(msg, &iter);
97
98         INFO("[EOM_CLIENT:%s] len(%d)", client_info.name, array->len);
99
100         for (i = 0; i < array->len; i++) {
101                 v = &g_array_index(array, GValue, i);
102                 type = v->g_type;
103
104                 INFO("[EOM_CLIENT:%s] type(%d)", client_info.name, (int)type);
105
106                 switch (type) {
107                 case G_TYPE_INT:
108                         {
109                                 int integer = g_value_get_int(v);
110
111                                 if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_INT32, &integer)) {
112                                         ERR("[EOM_CLIENT:%s] failed: int append", client_info.name);
113                                         return 0;
114                                 }
115                         }
116                         break;
117                 case G_TYPE_UINT:
118                         {
119                                 unsigned int uinteger = g_value_get_uint(v);
120
121                                 if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_UINT32, &uinteger)) {
122                                         ERR("[EOM_CLIENT:%s] failed: uint append", client_info.name);
123                                         return 0;
124                                 }
125                         }
126                         break;
127                 case G_TYPE_STRING:
128                         {
129                                 char *string = (char *)g_value_get_string(v);
130
131                                 if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, (void *)&string)) {
132                                         ERR("[EOM_CLIENT:%s] failed: uint append", client_info.name);
133                                         return 0;
134                                 }
135                         }
136                         break;
137                 case G_TYPE_VARIANT:
138                         {
139                                 GVariant *variant = g_value_get_variant(v);
140                                 int data_size = g_variant_get_size(variant);
141                                 void *data = (void *)g_variant_get_data(variant);
142                                 DBusMessageIter sub;
143
144                                 RETV_IF_FAIL(data != NULL, 0);
145                                 RETV_IF_FAIL(data_size > 0, 0);
146
147                                 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "y", &sub);
148                                 if (!dbus_message_iter_append_fixed_array(&sub, DBUS_TYPE_BYTE, (void *)&data, data_size)) {
149                                         ERR("[EOM_CLIENT:%s] failed: uint append", client_info.name);
150                                         return 0;
151                                 }
152                                 dbus_message_iter_close_container(&iter, &sub);
153                         }
154                         break;
155                 default:
156                         return 0;
157                 }
158         }
159
160         return 1;
161 }
162
163 static GArray*
164 _eom_dbus_convert_message_to_gvalue_temp(DBusMessage *msg)
165 {
166         GArray *array;
167         DBusMessageIter iter;
168
169         if (!dbus_message_iter_init(msg, &iter))
170                 return NULL;
171
172         array = g_array_new(FALSE, FALSE, sizeof(GValue));
173
174         do {
175                 int type = dbus_message_iter_get_arg_type(&iter);
176                 GValue v = G_VALUE_INIT;
177
178                 INFO("[EOM_CLIENT:%s] type(%c(%d))", client_info.name, (char)type, type);
179
180                 switch (type) {
181                 case DBUS_TYPE_INT32:
182                         {
183                                 int integer = 0;
184                                 dbus_message_iter_get_basic(&iter, &integer);
185                                 g_value_init(&v, G_TYPE_INT);
186                                 g_value_set_int(&v, integer);
187                                 array = g_array_append_val(array, v);
188                                 g_value_unset(&v);
189                         }
190                         break;
191                 case DBUS_TYPE_UINT32:
192                         {
193                                 unsigned int uinteger = 0;
194                                 dbus_message_iter_get_basic(&iter, &uinteger);
195                                 g_value_init(&v, G_TYPE_UINT);
196                                 g_value_set_uint(&v, uinteger);
197                                 array = g_array_append_val(array, v);
198                                 g_value_unset(&v);
199                         }
200                         break;
201                 case DBUS_TYPE_STRING:
202                         {
203                                 char *string = NULL;
204                                 dbus_message_iter_get_basic(&iter, &string);
205                                 g_value_init(&v, G_TYPE_STRING);
206                                 g_value_set_string(&v, string);
207                                 array = g_array_append_val(array, v);
208                                 g_value_unset(&v);
209                         }
210                         break;
211                 default:
212                         NEVER_GET_HERE();
213                         g_array_free(array, FALSE);
214                         return NULL;
215                 }
216         } while (dbus_message_iter_has_next(&iter) && dbus_message_iter_next(&iter));
217
218         return array;
219 }
220