summaryrefslogtreecommitdiff
path: root/mm_sound_device.c
blob: a212ae7e175ae3afbb4111841b507ed3257923c1 (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
/*
 * libmm-sound
 *
 * Copyright (c) 2000 - 2014 Samsung Electronics Co., Ltd. All rights reserved.
 *
 * Contact: Sangchul Lee <sc11.lee@samsung.com>
 *
 * 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 <unistd.h>
#include <stdio.h>
#include <vconf.h>

#include <mm_debug.h>

#include "include/mm_sound.h"
#include "include/mm_sound_device.h"

__thread bool is_new_device_list = true;

static int check_for_valid_mask (mm_sound_device_flags_e flags)
{
	int ret = MM_ERROR_NONE;
	bool at_least_cond = false;

	if (flags > 0 && flags <= MM_SOUND_DEVICE_ALL_FLAG) {
		if (flags & MM_SOUND_DEVICE_IO_DIRECTION_IN_FLAG)
			at_least_cond = true;
		if (!at_least_cond && (flags & MM_SOUND_DEVICE_IO_DIRECTION_OUT_FLAG))
			at_least_cond = true;
		if (!at_least_cond && (flags & MM_SOUND_DEVICE_IO_DIRECTION_BOTH_FLAG))
			at_least_cond = true;
		if (!at_least_cond && (flags & MM_SOUND_DEVICE_TYPE_INTERNAL_FLAG))
			at_least_cond = true;
		if (!at_least_cond && (flags & MM_SOUND_DEVICE_TYPE_EXTERNAL_FLAG))
			at_least_cond = true;
		if (!at_least_cond && (flags & MM_SOUND_DEVICE_STATE_DEACTIVATED_FLAG))
			at_least_cond = true;
		if (!at_least_cond && (flags & MM_SOUND_DEVICE_STATE_ACTIVATED_FLAG))
			at_least_cond = true;
	} else {
		ret = MM_ERROR_INVALID_ARGUMENT;
	}
	if (!at_least_cond) {
		ret = MM_ERROR_INVALID_ARGUMENT;
	}
	if (ret) {
		debug_error("flags[0x%x] is not valid\n", flags);
	}
	return ret;
}

EXPORT_API
int mm_sound_add_device_connected_callback(mm_sound_device_flags_e flags, mm_sound_device_connected_cb func, void *user_data)
{
	int ret = MM_ERROR_NONE;

	if (func == NULL) {
		debug_error("argument is not valid\n");
		return MM_ERROR_INVALID_ARGUMENT;
	}
	ret = check_for_valid_mask(flags);
	if (ret == MM_ERROR_NONE) {
		ret = _mm_sound_client_add_device_connected_callback(flags, func, user_data);
		if (ret < 0) {
			debug_error("Could not add device connected callback, ret = %x\n", ret);
		}
	}

	return ret;
}

EXPORT_API
int mm_sound_remove_device_connected_callback(void)
{
	int ret = MM_ERROR_NONE;

	ret = _mm_sound_client_remove_device_connected_callback();
	if (ret < 0) {
		debug_error("Could not remove device connected callback, ret = %x\n", ret);
	}

	return ret;
}

EXPORT_API
int mm_sound_add_device_information_changed_callback(mm_sound_device_flags_e flags, mm_sound_device_info_changed_cb func, void *user_data)
{
	int ret = MM_ERROR_NONE;

	if (func == NULL) {
		debug_error("argument is not valid\n");
		return MM_ERROR_INVALID_ARGUMENT;
	}
	ret = check_for_valid_mask(flags);
	if (ret == MM_ERROR_NONE) {
		ret = _mm_sound_client_add_device_info_changed_callback(flags, func, user_data);
		if (ret < 0) {
			debug_error("Could not add device information changed callback, ret = %x\n", ret);
		}
	}

	return ret;
}

EXPORT_API
int mm_sound_remove_device_information_changed_callback()
{
	int ret = MM_ERROR_NONE;

	ret = _mm_sound_client_remove_device_info_changed_callback();
	if (ret < 0) {
		debug_error("Could not remove device information changed callback, ret = %x\n", ret);
	}

	return ret;
}

EXPORT_API
int mm_sound_get_current_device_list(mm_sound_device_flags_e flags, MMSoundDeviceList_t *device_list)
{
	int ret = MM_ERROR_NONE;

	if (!device_list) {
		return MM_ERROR_INVALID_ARGUMENT;
	}
	ret = check_for_valid_mask(flags);
	if (ret == MM_ERROR_NONE) {
		ret = _mm_sound_client_get_current_connected_device_list(flags, (mm_sound_device_list_t**)device_list);
		if (ret < 0) {
			debug_error("Could not get current connected device list, ret = %x\n", ret);
		} else {
			is_new_device_list = true;
		}
	}

	return ret;
}

EXPORT_API
int mm_sound_get_next_device (MMSoundDeviceList_t device_list, MMSoundDevice_t *device)
{
	int ret = MM_ERROR_NONE;
	mm_sound_device_list_t *device_list_t = NULL;
	mm_sound_device_t *device_h = NULL;
	GList *node = NULL;
	if (!device_list || !device) {
		return MM_ERROR_INVALID_ARGUMENT;
	}
	device_list_t = (mm_sound_device_list_t*) device_list;
	if (is_new_device_list) {
		node = g_list_first(device_list_t->list);
	} else {
		node = g_list_next(device_list_t->list);
	}
	if (!node) {
		*device = NULL;
		ret = MM_ERROR_SOUND_NO_DATA;
	} else {
		if (is_new_device_list) {
			is_new_device_list = false;
		} else {
			device_list_t->list = node;
		}
		*device = (mm_sound_device_t*)node->data;
		debug_log("next device[0x%x]\n", *device);
	}
	return ret;
}

EXPORT_API
int mm_sound_get_prev_device (MMSoundDeviceList_t device_list, MMSoundDevice_t *device)
{
	int ret = MM_ERROR_NONE;
	mm_sound_device_list_t *device_list_t = NULL;
	mm_sound_device_t *device_h = NULL;
	GList *node = NULL;
	if (!device_list || !device) {
		return MM_ERROR_INVALID_ARGUMENT;
	}
	device_list_t = (mm_sound_device_list_t*) device_list;
	node = g_list_previous(device_list_t->list);
	if (!node) {
		*device = NULL;
		ret = MM_ERROR_SOUND_NO_DATA;
		debug_error("Could not get previous device, ret = %x\n", ret);
	} else {
		device_list_t->list = node;
		*device = (mm_sound_device_t*)node->data;
		debug_log("previous device[0x%x]\n", *device);
	}
	return ret;
}

EXPORT_API
int mm_sound_get_device_type(MMSoundDevice_t device_h, mm_sound_device_type_e *type)
{
	mm_sound_device_t *device = (mm_sound_device_t*)device_h;
	if(!device) {
		debug_error("invalid handle\n");
		return MM_ERROR_INVALID_ARGUMENT;
	}
	*type = device->type;
	debug_log("device_handle:0x%x, type:%d\n", device, *type);

	return MM_ERROR_NONE;
}

EXPORT_API
int mm_sound_get_device_io_direction(MMSoundDevice_t device_h, mm_sound_device_io_direction_e *io_direction)
{
	mm_sound_device_t *device = (mm_sound_device_t*)device_h;
	if(!device) {
		debug_error("invalid handle\n");
		return MM_ERROR_INVALID_ARGUMENT;
	}
	*io_direction = device->io_direction;
	debug_log("device_handle:0x%x, io_direction:%d (0:IN,1:OUT,2:INOUT)\n", device, *io_direction);

	return MM_ERROR_NONE;
}

EXPORT_API
int mm_sound_get_device_id(MMSoundDevice_t device_h, int *id)
{
	mm_sound_device_t *device = (mm_sound_device_t*)device_h;
	if(!device) {
		debug_error("invalid handle\n");
		return MM_ERROR_INVALID_ARGUMENT;
	}
	*id = device->id;
	debug_log("device_handle:0x%x, id:%d\n", device, *id);

	return MM_ERROR_NONE;
}

EXPORT_API
int mm_sound_get_device_state(MMSoundDevice_t device_h, mm_sound_device_state_e *state)
{
	mm_sound_device_t *device = (mm_sound_device_t*)device_h;
	if(!device) {
		debug_error("invalid handle\n");
		return MM_ERROR_INVALID_ARGUMENT;
	}
	*state = device->state;
	debug_log("device_handle:0x%x, state:%d (0:INACTIVATED,1:ACTIVATED)\n", device, *state);

	return MM_ERROR_NONE;
}

EXPORT_API
int mm_sound_get_device_name(MMSoundDevice_t device_h, char **name)
{
	mm_sound_device_t *device = (mm_sound_device_t*)device_h;
	if(!device) {
		debug_error("invalid handle\n");
		return MM_ERROR_INVALID_ARGUMENT;
	}
	*name = device->name;
	debug_log("device_handle:0x%x, name:%s\n", device, *name);

	return MM_ERROR_NONE;
}