summaryrefslogtreecommitdiff
path: root/src/minicontrol-internal.c
blob: 561320a8eaba5e763c91758919ee9038f2c222ba (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
/*
 * 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 <stdlib.h>
#include <glib.h>
#include <gio/gio.h>
#include <bundle.h>

#include "minicontrol-error.h"
#include "minicontrol-type.h"
#include "minicontrol-internal.h"
#include "minicontrol-log.h"

#define MINICTRL_DBUS_PATH "/org/tizen/minicontrol"
#define MINICTRL_DBUS_INTERFACE "org.tizen.minicontrol.signal"

#define PROC_DBUS_OBJECT	"/Org/Tizen/ResourceD/Process"
#define PROC_DBUS_INTERFACE	"org.tizen.resourced.process"
#define PROC_DBUS_METHOD	"ProcExclude"
#define PROC_DBUS_EXCLUDE	"exclude"
#define PROC_DBUS_INCLUDE	"include"

struct _minictrl_sig_handle {
	GDBusConnection *conn;
	guint s_id;
	void (*callback)(void *data, GVariant *parameters);
	void *user_data;
	char *signal;
};

static int __send_signal(const char *object_path, const char *interface_name,
		const char *signal_name, GVariant *parameters)
{
	GError *err = NULL;
	GDBusConnection *conn;
	gboolean ret;

	conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
	if (conn == NULL) {
		/* LCOV_EXCL_START */
		ERR("g_bus_get_sync() failed. %s", err->message);
		g_error_free(err);
		return MINICONTROL_ERROR_IPC_FAILURE;
		/* LCOV_EXCL_STOP */
	}

	ret = g_dbus_connection_emit_signal(conn, NULL, object_path,
			interface_name, signal_name, parameters, &err);
	if (!ret) {
		/* LCOV_EXCL_START */
		ERR("g_dbus_connection_emit_signal() failed. %s", err->message);
		g_error_free(err);
		g_object_unref(conn);
		return MINICONTROL_ERROR_IPC_FAILURE;
		/* LCOV_EXCL_STOP */
	}

	g_dbus_connection_flush_sync(conn, NULL, &err);
	g_object_unref(conn);
	g_clear_error(&err);

	return MINICONTROL_ERROR_NONE;
}

int _minictrl_viewer_req_message_send(void)
{
	int ret;

	ret = __send_signal(MINICTRL_DBUS_PATH, MINICTRL_DBUS_INTERFACE,
			MINICTRL_DBUS_SIG_RUNNING_REQ, NULL);

	return ret;
}

int _minictrl_provider_proc_send(int type)
{
	int ret;
	GVariant *param;
	const char *type_str = PROC_DBUS_INCLUDE;
	int pid = getpid();

	if (type == MINICONTROL_DBUS_PROC_EXCLUDE)
		type_str = PROC_DBUS_EXCLUDE;

	DBG("pid: %d, type: %d(%s)", pid, type, type_str);

	param = g_variant_new("(si)", type_str, pid);
	if (param == NULL) {
		/* LCOV_EXCL_START */
		ERR("out of memory");
		return MINICONTROL_ERROR_OUT_OF_MEMORY;
		/* LCOV_EXCL_STOP */
	}

	ret = __send_signal(PROC_DBUS_OBJECT, PROC_DBUS_INTERFACE,
			PROC_DBUS_METHOD, param);

	return ret;
}

int _minictrl_send_event(const char *signal_name, const char *minicontrol_name,
		int event, bundle *signal_arg)
{
	int ret;
	bundle_raw *serialized_arg = NULL;
	unsigned int serialized_arg_length = 0;
	GVariant *param;

	if (minicontrol_name == NULL || signal_name == NULL) {
		ERR("Invaild parameter");
		return MINICONTROL_ERROR_INVALID_PARAMETER;
	}

	if (signal_arg) {
		ret = bundle_encode(signal_arg, &serialized_arg,
				(int *)&serialized_arg_length);
		if (ret != BUNDLE_ERROR_NONE) {
			/* LCOV_EXCL_START */
			ERR("Failed to serialize bundle argument");
			return MINICONTROL_ERROR_OUT_OF_MEMORY;
			/* LCOV_EXCL_STOP */
		}
	} else {
		serialized_arg = (bundle_raw *)strdup("");
		if (serialized_arg == NULL) {
			/* LCOV_EXCL_START */
			ERR("out of memory");
			return MINICONTROL_ERROR_OUT_OF_MEMORY;
			/* LCOV_EXCL_STOP */
		}
		serialized_arg_length = 0;
	}

	param = g_variant_new("(sisu)", minicontrol_name, event,
			serialized_arg, serialized_arg_length);
	if (param == NULL) {
		/* LCOV_EXCL_START */
		ERR("out of memory");
		free(serialized_arg);
		return MINICONTROL_ERROR_OUT_OF_MEMORY;
		/* LCOV_EXCL_STOP */
	}

	ret = __send_signal(MINICTRL_DBUS_PATH, MINICTRL_DBUS_INTERFACE,
			signal_name, param);

	free(serialized_arg);

	return ret;
}

int _minictrl_provider_message_send(int event, const char *minicontrol_name,
		unsigned int witdh, unsigned int height,
		minicontrol_priority_e priority)
{
	int ret;
	bundle *event_arg_bundle;
	char bundle_value_buffer[BUNDLE_BUFFER_LENGTH];

	event_arg_bundle = bundle_create();
	if (event_arg_bundle == NULL) {
		/* LCOV_EXCL_START */
		ERR("Fail to create a bundle instance");
		return MINICONTROL_ERROR_OUT_OF_MEMORY;
		/* LCOV_EXCL_STOP */
	}

	snprintf(bundle_value_buffer, sizeof(bundle_value_buffer),
			"%s", minicontrol_name);

	bundle_add_str(event_arg_bundle, "minicontrol_name",
			bundle_value_buffer);
	bundle_add_byte(event_arg_bundle, "width", (void *)&witdh, sizeof(int));
	bundle_add_byte(event_arg_bundle, "height", (void *)&height,
			sizeof(int));
	bundle_add_byte(event_arg_bundle, "priority", (void *)&priority,
			sizeof(int));

	ret = _minictrl_send_event(MINICTRL_DBUS_SIG_TO_VIEWER,
			minicontrol_name, event, event_arg_bundle);
	bundle_free(event_arg_bundle);
	return ret;
}

static void __minictrl_signal_filter(GDBusConnection *connection,
		const gchar *sender_name, const gchar *object_path,
		const gchar *interface_name, const gchar *signal_name,
		GVariant *parameters, gpointer user_data)
{
	minictrl_sig_handle *handle = (minictrl_sig_handle *)user_data;

	if (handle == NULL)
		return;

	if (g_strcmp0(signal_name, handle->signal) == 0) {
		if (handle->callback)
			handle->callback(handle->user_data, parameters);
	}
}

minictrl_sig_handle *_minictrl_dbus_sig_handle_attach(const char *signal,
		void (*callback)(void *data, GVariant *parameters), void *data)
{
	GError *err = NULL;
	minictrl_sig_handle *handle;

	if (signal == NULL || callback == NULL) {
		ERR("Invalid prameter");
		return NULL;
	}

	handle = (minictrl_sig_handle *)malloc(sizeof(minictrl_sig_handle));
	if (handle == NULL) {
		/* LCOV_EXCL_START */
		ERR("out of memory");
		return NULL;
		/* LCOV_EXCL_STOP */
	}

	handle->conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
	if (handle->conn == NULL) {
		/* LCOV_EXCL_START */
		ERR("g_bus_get_sync() failed. %s", err->message);
		g_error_free(err);
		free(handle);
		return NULL;
		/* LCOV_EXCL_STOP */
	}

	handle->s_id = g_dbus_connection_signal_subscribe(handle->conn,
			NULL, MINICTRL_DBUS_INTERFACE, signal,
			MINICTRL_DBUS_PATH, NULL, G_DBUS_SIGNAL_FLAGS_NONE,
			__minictrl_signal_filter, handle, NULL);
	if (handle->s_id == 0) {
		/* LCOV_EXCL_START */
		ERR("g_dbus_connection_signal_subscribe() failed.");
		g_object_unref(handle->conn);
		free(handle);
		return NULL;
		/* LCOV_EXCL_STOP */
	}

	handle->callback = callback;
	handle->user_data = data;
	handle->signal = strdup(signal);

	INFO("success to attach signal[%s]-[%p, %p]", signal, callback, data);
	g_clear_error(&err);

	return handle;
}

void _minictrl_dbus_sig_handle_dettach(minictrl_sig_handle *handle)
{
	if (!handle) {
		/* LCOV_EXCL_START */
		ERR("handle is NULL");
		return;
		/* LCOV_EXCL_STOP */
	}

	g_dbus_connection_signal_unsubscribe(handle->conn, handle->s_id);
	g_object_unref(handle->conn);
	free(handle->signal);
	free(handle);
}