summaryrefslogtreecommitdiff
path: root/src/sound.c
blob: 5ccadb882652db08763a17fe1e6117f4f0002bb1 (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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/*
 * Copyright (c) 2009-2014 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 <appcore-common.h>
#include <vconf.h>
#include <vconf-keys.h>
#include <feedback.h>

#include "main.h"
#include "_util_log.h"
#include "view.h"
#include "control.h"
#include "sound.h"

static struct _s_info {
	int sound_step;
	int sound_status;
	int vibration_status;
	Eina_Bool is_vibration;
} s_info = {
	.sound_step = 0,
	.sound_status = 0,
	.vibration_status = 0,
	.is_vibration = EINA_FALSE,
};

int volume_sound_step_get(void)
{
	return s_info.sound_step;
}

int volume_sound_sound_status_get(void)
{
	return s_info.sound_status;
}

int volume_sound_vibration_status_get(void)
{
	return s_info.vibration_status;
}

Eina_Bool volume_sound_is_vibration_get(void)
{
	return s_info.is_vibration;
}

void volume_sound_is_vibration_set(Eina_Bool val)
{
	s_info.is_vibration = val;
}

void volume_sound_feedback_play(int feedback_type, int pattern)
{
	_D("Feedback type : %d / pattern : %d", feedback_type, pattern);

	if (FEEDBACK_ERROR_NONE != feedback_play_type(feedback_type, pattern))
	{
		_E("Failed to play feedback");
	}
}

void volume_sound_vib_play(void)
{
	ret_if(s_info.is_vibration);

	_D("Play Feedback : vibration");

	if (FEEDBACK_ERROR_NONE != feedback_play_type(FEEDBACK_TYPE_VIBRATION, FEEDBACK_PATTERN_VIBRATION_ON))
	{
		_E("Failed to play feedback");
	}

	s_info.is_vibration = EINA_TRUE;
}

void volume_sound_play(void)
{
	sound_type_e sound_type = volume_control_sound_type_at_show_get();
	_D("sound type at show : %d", sound_type);

	ret_if(sound_type == SOUND_TYPE_ALARM);
	ret_if(sound_type == SOUND_TYPE_MEDIA);
	ret_if(sound_type == SOUND_TYPE_VOICE);
	ret_if(sound_type == SOUND_TYPE_NOTIFICATION);

	_D("Play Feedback : sound");
	if(FEEDBACK_ERROR_NONE != feedback_play(FEEDBACK_PATTERN_VOLUME_KEY)) {
		_E("Failed to play feedback");
	}

	return;
}

static int _setting_sound_enabled(void)
{
	int snd_status = 0;

	if(vconf_get_bool(VCONFKEY_SETAPPL_SOUND_STATUS_BOOL, &snd_status) < 0)
	{
		_E("Failed to get vconfkey : VCONFKEY_SETAPPL_SOUND_STATUS_BOOL");
	}

	if (snd_status == 1)
	{
		return 1;
	}

	return 0;
}

//void _play_setting_sound(void)
void volume_sound_setting_sound_play(void)
{
	int snd_enabled = _setting_sound_enabled();
	_D("snd_enabled(%d)", snd_enabled);

	if(snd_enabled == 1)
	{
		//feedback_play_type(FEEDBACK_TYPE_SOUND, FEEDBACK_PATTERN_TOUCH_TAP);
		volume_sound_feedback_play(FEEDBACK_TYPE_SOUND, FEEDBACK_PATTERN_TOUCH_TAP);
	}
}

int volume_sound_sound_manager_step_get(sound_type_e type)
{
	int ret = -1, step = 0;

	ret = sound_manager_get_max_volume(type, &step);
	retvm_if(ret < 0, -1, "Failed to get step");

	s_info.sound_step = step;

	return step;
}

int volume_sound_sound_manager_volume_get(sound_type_e type)
{
	int ret = -1, val = 0;
	ret = sound_manager_get_volume(type, &val);
	retvm_if(ret < 0, -1, "Failed to get volume");
	return val;
}

//sound_type_e _get_volume_type
sound_type_e volume_sound_sound_manager_type_get(void)
{
	_D(" Volume sound manager type get");
	int ret = 0;
	sound_type_e sound_type = -1;

	ret = sound_manager_get_current_sound_type(&sound_type);
	switch (ret) {
	case SOUND_MANAGER_ERROR_NONE:
		break;
	case SOUND_MANAGER_ERROR_NO_PLAYING_SOUND:
		sound_type = SOUND_TYPE_RINGTONE;
		break;
	default:
		_E("Failed to get sound type : %d", ret);
		sound_type = SOUND_TYPE_RINGTONE;
		return sound_type;
	}

	if(sound_type == SOUND_TYPE_SYSTEM) {
		sound_type = SOUND_TYPE_RINGTONE;
	}

	return sound_type;
}

void _mm_func(sound_type_e type, unsigned int volume, void *data)
{
	_D("type : %d, volume : %d", type, volume);
	if(type == volume_control_sound_type_at_show_get()){
		_D("current show type : %d, volume : %d", type, volume);
		/* Need to check sound type & device in media type volume. */
		if(type == SOUND_TYPE_MEDIA){
			int sound = volume_sound_vconf_status_get(TYPE_VCONF_SOUND_STATUS);
			_D("sound status : %d", sound);

			int vibration = volume_sound_vconf_status_get(TYPE_VCONF_VIBRATION_STATUS);
			_D("vibration : %d", vibration);
			volume_view_volume_icon_set(type, sound, vibration);
		}
		if(VOLUME_ERROR_OK != volume_view_slider_value_set(volume))
		{
			_E("Failed to set slider value");
		}
	}
}

//int _init_mm_sound(void *data);
void volume_sound_mm_sound_init(void)
{
	int ret = sound_manager_set_volume_changed_cb(_mm_func, NULL);
	if (ret != SOUND_MANAGER_ERROR_NONE)
	{
		_E("Failed to set volume changed event[%d]", ret);
	}
}

//int _get_sound_level(sound_type_e type, int *val)
int volume_sound_level_get(sound_type_e type)
{
	int val = -1;
	if(sound_manager_get_volume(type, &val) < 0)
	{
		_E("Failed to get Volume step");
		return -1;
	}

	return val;
}

//int _set_sound_level(sound_type_e type, int val)
int volume_sound_level_set(sound_type_e type, int val)
{
	int ret = -1;
	ret = sound_manager_set_volume(type, val);

	return ret;
}

void volume_sound_change_set(int val)
{
	sound_type_e sound_type = volume_control_sound_type_at_show_get();
	_D("sound type at show : %d", sound_type);

	int sound = volume_sound_vconf_status_get(TYPE_VCONF_SOUND_STATUS);
	_D("sound status : %d", sound);

	if (val)
	{
		if(sound_type != SOUND_TYPE_MEDIA)
		{
			if(!volume_sound_vconf_status_get(TYPE_VCONF_SOUND_STATUS))
			{
				volume_sound_vconf_status_set(TYPE_VCONF_SOUND_STATUS, 1);
				volume_sound_vconf_status_set(TYPE_VCONF_VIBRATION_STATUS, 0);
			}
		}
		if(volume_sound_level_set(sound_type, val) != 0)
		{
			volume_sound_level_set(sound_type, 9);
		}

		volume_sound_play();
	}
	else
	{
		if(sound_type != SOUND_TYPE_MEDIA)
		{
			volume_sound_vconf_status_set(TYPE_VCONF_SOUND_STATUS, 0);
		}
		volume_view_slider_value_set(0);
		volume_sound_level_set(sound_type, 0);

		if(sound_type == SOUND_TYPE_RINGTONE)
		{
			volume_sound_vconf_status_set(TYPE_VCONF_VIBRATION_STATUS, 1);
			volume_sound_vib_play();
		}
	}
}

static void _sound_status_changed_cb(keynode_t *key, void *data)
{
	const char *keyname = vconf_keynode_get_name(key);
	ret_if(!keyname);
	_D("keyname : %s", keyname);

	sound_type_e sound_type = volume_control_sound_type_at_show_get();
	_D("sound type at show : %d", sound_type);

	int sound = volume_sound_vconf_status_get(TYPE_VCONF_SOUND_STATUS);
	_D("sound status : %d", sound);
	s_info.sound_status = sound;

	int vibration = volume_sound_vconf_status_get(TYPE_VCONF_VIBRATION_STATUS);
	_D("vibration : %d", vibration);
	s_info.vibration_status = vibration;

	if(sound == 0 && vibration == 1)
	{
		_D("vibration mode");
	} else if(sound == 1 && vibration == 0)
	{
		_D("sound mode");
		s_info.is_vibration = EINA_FALSE;
	} else if(sound == 0 && vibration == 0)
	{
		_D("mute mode");
		s_info.is_vibration = EINA_FALSE;
	}

	volume_view_volume_icon_set(sound_type, sound, vibration);
}

void volume_sound_vconf_status_set(volume_vconf_type_e type, int val)
{
	char *vconfkey = NULL;
	switch(type)
	{
	case TYPE_VCONF_SOUND_STATUS:
		vconfkey = VCONFKEY_SETAPPL_SOUND_STATUS_BOOL;
		break;
	case TYPE_VCONF_VIBRATION_STATUS:
		vconfkey = VCONFKEY_SETAPPL_VIBRATION_STATUS_BOOL;
		break;
	default:
		_E("Failed to set vconfkey : Type error");
		return;
	}

	if(vconf_set_bool(vconfkey, val) < 0)
	{
		_E("Failed to set vconfkey : %s", vconfkey);
	}
}

int volume_sound_vconf_status_get(volume_vconf_type_e type)
{
	int ret = -1;
	char *vconfkey = NULL;

	switch(type)
	{
	case TYPE_VCONF_SOUND_STATUS:
		vconfkey = VCONFKEY_SETAPPL_SOUND_STATUS_BOOL;
		break;
	case TYPE_VCONF_VIBRATION_STATUS:
		vconfkey = VCONFKEY_SETAPPL_VIBRATION_STATUS_BOOL;
		break;
	default:
		_E("Failed to get vconfkey : Type error");
		return -1;
	}

	if(vconf_get_bool(vconfkey, &ret) < 0)
	{
		_E("Failed to get vconfkey : %s", vconfkey);
		return -1;
	}

	return ret;
}

void volume_sound_vconfkey_register(void)
{
	if(vconf_notify_key_changed(VCONFKEY_SETAPPL_SOUND_STATUS_BOOL, _sound_status_changed_cb, NULL) < 0)
	{
		_E("Failed to notify vconfkey : VCONFKEY_SETAPPL_SOUND_STATUS_BOOL");
	}

	if(vconf_notify_key_changed(VCONFKEY_SETAPPL_VIBRATION_STATUS_BOOL, _sound_status_changed_cb, NULL) < 0)
	{
		_E("Failed to notify vconfkey : VCONFKEY_SETAPPL_VIBRATION_STATUS_BOOL");
	}
	_sound_status_changed_cb(NULL, NULL);
}

void volume_sound_vconfkey_unregister(void)
{
	if(vconf_ignore_key_changed(VCONFKEY_SETAPPL_SOUND_STATUS_BOOL, _sound_status_changed_cb) < 0)
	{
		_E("Failed to ignore vconfkey : VCONFKEY_SETAPPL_SOUND_STATUS_BOOL");
	}

	if(vconf_ignore_key_changed(VCONFKEY_SETAPPL_VIBRATION_STATUS_BOOL, _sound_status_changed_cb) < 0)
	{
		_E("Failed to ignore vconfkey : VCONFKEY_SETAPPL_VIBRATION_STATUS_BOOL");
	}
}