4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Hyungdeuk Kim <hd3.kim@samsung.com>
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
29 #include <sys/types.h>
31 #include <mm_sound_private.h>
33 #include <devman_haptic.h>
42 #define SVI_RETRY_CNT 1
43 #define MAX_FILE_PATH 512
46 #define API __attribute__ ((visibility("default")))
50 static long long ms = 0;
52 #define MICROSECONDS(tv) ((tv.tv_sec * 1000000ll) + tv.tv_usec)
54 #define ESTIMATE_PERFORMANCE() \
58 gettimeofday(&tv, NULL); \
59 ms = MICROSECONDS(tv); \
60 fprintf(stderr, "%s start time : %lld", __func__, ms); \
62 gettimeofday(&tv, NULL); \
63 fprintf(stderr, "%s elapsed time : %lld", __func__, MICROSECONDS(tv) - ms); \
68 #define ESTIMATE_PERFORMANCE()
71 static int soundon = -1;
72 static int vib_level = -1;
73 static int sndstatus = -1;
74 static int vibstatus = -1;
76 static void __svi_soundon_cb(keynode_t *key, void* data)
78 soundon = vconf_keynode_get_bool(key);
79 SVILOG("[[[[[[[[[[[[[[soundon changed!! new soundon => %d", soundon);
83 static void __svi_vib_cb(keynode_t *key, void* data)
85 vib_level = vconf_keynode_get_int(key);
87 SVILOG("[[[[[[[[[[[[[[vib_level changed!! new vib_level => %d", vib_level);
92 static void __svi_sndstatus_cb(keynode_t *key, void* data)
94 sndstatus = vconf_keynode_get_int(key);
96 SVILOG("[[[[[[[[[[[[[[sndstatus changed!! new sndstatus => %d", sndstatus);
101 static void __svi_vibstatus_cb(keynode_t *key, void* data)
103 vibstatus = vconf_keynode_get_bool(key);
105 SVILOG("[[[[[[[[[[[[[[vibstatus changed!! new vibstatus => %d", vibstatus);
110 static volume_type_t __svi_get_volume_type(sound_type sound_key)
112 volume_type_t type = VOLUME_TYPE_SYSTEM;
114 if (sound_key >= SVI_SND_OPERATION_POWERON && sound_key <= SVI_SND_OPERATION_SCRCAPTURE)
115 type = VOLUME_TYPE_SYSTEM;
117 type = VOLUME_TYPE_SYSTEM;
122 static int __svi_restore_default_file(int svi_type, int svi_enum)
124 vibration_type vib_enum_type = SVI_VIB_NONE;
125 sound_type snd_enum_type = SVI_SND_NONE;
126 const char* cur_path = NULL;
127 char default_path[MAX_FILE_PATH] = {0,};
131 if(svi_type <=SVI_TYPE_NONE || svi_type >= SVI_TYPE_END) {
132 SVILOG("ERROR!! invalid svi_type(%d).", svi_type);
136 if (svi_type == SVI_TYPE_SND) {
137 snd_enum_type = (sound_type)svi_enum;
139 if (snd_enum_type <= SVI_SND_NONE || snd_enum_type >= SVI_SND_ENUM_END) {
140 SVILOG("ERROR! invalid svi_enum(%d)", snd_enum_type);
144 cur_path = snd_file[snd_enum_type];
146 vib_enum_type = (vibration_type)svi_enum;
148 if (vib_enum_type <= SVI_VIB_NONE || vib_enum_type >= SVI_VIB_ENUM_END) {
149 SVILOG("ERROR! invalid svi_enum(%d)", vib_enum_type);
153 cur_path = haptic_file[vib_enum_type];
156 if( (cur_path == NULL) || (strlen(cur_path) == 0) ) {
157 SVILOG("ERROR! current path is invalid");
161 temp = strcat(default_path, SVI_ORIGIN_DATA_DIR);
163 strcat(temp, cur_path+strlen(SVI_DATA_DIR));
165 SVILOG("default_path : %s", default_path);
167 if(stat(default_path, &buf)) { /*check file existence*/
168 SVILOG("ERROR!! default file for type(%d),enum(%d) is not presents", svi_type, svi_enum);
172 if(unlink(cur_path) < 0) {
173 SVILOG("WARNING!! unlink(%s) error(%d)", cur_path, errno);
176 if(symlink(default_path, cur_path) < 0) {
177 SVILOG("ERROR!! symlink(%s) error(%d)", default_path, errno);
184 API int svi_init(int *handle)
189 if (vconf_get_bool(VCONFKEY_SETAPPL_SOUND_STATUS_BOOL, &soundon) < 0) {
190 SVILOG("vconf_get_bool(VCONFKEY_SETAPPL_SOUND_STATUS_BOOL, &soundon) ==> FAIL!!");
193 SVILOG("vconf_get_int(VCONFKEY_SETAPPL_SOUND_STATUS_BOOL, &soundon) ==> %d", soundon);
196 if (vconf_get_int(VCONFKEY_SOUND_STATUS, &sndstatus) < 0) {
197 SVILOG("vconf_get_int(VCONFKEY_SOUND_STATUS, &sndstatus) ==> FAIL!!");
200 SVILOG("vconf_get_int(VCONFKEY_SOUND_STATUS, &sndstatus) ==> %d", sndstatus);
204 v_handle = device_haptic_open( DEV_IDX_0 , 0x01); // new haptic lib.
206 SVILOG("device_haptic_open(DEV_IDX_0) ==> FAIL!!");
209 /* check vibration status */
210 if (vconf_get_bool(VCONFKEY_SETAPPL_VIBRATION_STATUS_BOOL, &vibstatus) < 0) {
211 SVILOG("vconf_get_bool(VCONFKEY_SETAPPL_VIBRATION_STATUS_BOOL, &vibstatus) ==> FAIL!!");
214 SVILOG("vconf_get_bool(VCONFKEY_SETAPPL_VIBRATION_STATUS_BOOL, &vibstatus) ==> %d", vibstatus);
217 /* check vib_level */
218 if (vconf_get_int(VCONFKEY_SETAPPL_TOUCH_FEEDBACK_VIBRATION_LEVEL_INT, &vib_level) < 0) {
219 SVILOG("vconf_get_int(VCONFKEY_SETAPPL_VIB_FEEDBACK_INT, &vib_level) ==> FAIL!!");
222 SVILOG("vconf_get_int(VCONFKEY_SETAPPL_VIB_FEEDBACK_INT, &vib_level) ==> %d", vib_level);
228 /* add watch for status value */
229 vconf_notify_key_changed(VCONFKEY_SETAPPL_SOUND_STATUS_BOOL, __svi_soundon_cb, NULL);
230 vconf_notify_key_changed(VCONFKEY_SOUND_STATUS, __svi_sndstatus_cb, NULL);
231 vconf_notify_key_changed(VCONFKEY_SETAPPL_VIBRATION_STATUS_BOOL, __svi_vibstatus_cb, NULL);
232 vconf_notify_key_changed(VCONFKEY_SETAPPL_TOUCH_FEEDBACK_VIBRATION_LEVEL_INT, __svi_vib_cb, NULL);
237 API int svi_fini(int handle)
239 vconf_ignore_key_changed(VCONFKEY_SETAPPL_SOUND_STATUS_BOOL, __svi_soundon_cb);
240 vconf_ignore_key_changed(VCONFKEY_SOUND_STATUS, __svi_sndstatus_cb);
241 vconf_ignore_key_changed(VCONFKEY_SETAPPL_VIBRATION_STATUS_BOOL, __svi_vibstatus_cb);
242 vconf_ignore_key_changed(VCONFKEY_SETAPPL_TOUCH_FEEDBACK_VIBRATION_LEVEL_INT, __svi_vib_cb);
245 if (device_haptic_close(handle) < 0) {
255 API int svi_play_sound(int handle, sound_type sound_key)
257 int ret_snd = SVI_SUCCESS;
262 SVILOG("ERROR!! Please call svi_init() for sound init ");
265 if (sound_key > SVI_SND_NONE && sound_key <= SVI_SND_OPERATION_SCRCAPTURE) {
266 SVILOG("sound_key = %d, soundon = %d, sndstatus = %d ", sound_key, soundon, sndstatus);
267 if (soundon != 0 && sndstatus == 0) {
269 ret = mm_sound_play_keysound(snd_file[sound_key], __svi_get_volume_type(sound_key));
270 if(ret == MM_ERROR_NONE) {
271 SVILOG("Play success! SND filename is %s", snd_file[sound_key]);
274 if(ret == MM_ERROR_SOUND_FILE_NOT_FOUND) {
275 SVILOG("mm_sound_play_keysound MM_ERROR_SOUND_FILE_NOT_FOUND error");
276 if(__svi_restore_default_file(SVI_TYPE_SND, sound_key) == SVI_ERROR) {
277 SVILOG("ERROR!! __svi_restore_origin_file(%d/%d) error", SVI_TYPE_SND, sound_key);
281 SVILOG("sound file link is restored. sound play will be retried. ");
284 SVILOG("ERROR!! mm_sound_play_keysound() returned error(%d)", ret);
291 } else if (sound_key != SVI_SND_NONE) {
299 API int svi_play_vib(int handle, vibration_type vibration_key)
301 int ret_vib = SVI_SUCCESS;
305 SVILOG("ERROR!! Please call svi_init() for vibration init ");
308 if (vibration_key > SVI_VIB_NONE && vibration_key < SVI_VIB_ENUM_END) {
310 if (vibration_key == SVI_VIB_OPERATION_FULLCHARGED || vibration_key == SVI_VIB_OPERATION_LOWBATT) {
316 SVILOG("key = %d, vibstatus = %d, vib_level = %d", vibration_key, vibstatus, vib_lev);
318 if (vibstatus != 0) {
321 if(stat(haptic_file[vibration_key], &buf)) { /*check file existence*/
322 SVILOG("ERROR!! %s is not presents", haptic_file[vibration_key]);
323 if(__svi_restore_default_file(SVI_TYPE_VIB, vibration_key) == SVI_ERROR) {
324 SVILOG("ERROR!! __svi_restore_default_file(%d/%d) error", SVI_TYPE_VIB, vibration_key);
327 SVILOG("%s is restored", haptic_file[vibration_key]);
332 ret = device_haptic_play_file(handle, haptic_file[vibration_key], 1, vib_lev);
334 SVILOG("ERROR!! device_haptic_play_file(%s) returned error(%d).", haptic_file[vibration_key], ret);
347 API int svi_play(int handle, vibration_type vibration_key, sound_type sound_key)
349 int ret_snd = svi_play_sound(handle, sound_key);
350 int ret_vib = svi_play_vib(handle, vibration_key);
352 if (ret_snd == SVI_ERROR || ret_vib == SVI_ERROR) {
359 API int svi_set_path(int svi_type, int svi_enum, char* path)
361 vibration_type vib_enum_type = SVI_VIB_NONE;
362 sound_type snd_enum_type = SVI_SND_NONE;
363 const char* cur_path = NULL;
365 if(svi_type <=SVI_TYPE_NONE || svi_type >= SVI_TYPE_END) {
366 SVILOG("ERROR!! invalid svi_type(%d).", svi_type);
371 SVILOG("ERROR!! invalid path param.");
374 if(access(path, F_OK) != 0) {
375 SVILOG("ERROR!! path does not exist.");
380 if (svi_type == SVI_TYPE_SND) {
381 snd_enum_type = (sound_type)svi_enum;
383 if (snd_enum_type <= SVI_SND_NONE || snd_enum_type >= SVI_SND_ENUM_END) {
384 SVILOG("ERROR! invalid svi_enum(%d)", snd_enum_type);
388 cur_path = snd_file[snd_enum_type];
390 vib_enum_type = (vibration_type)svi_enum;
392 if (vib_enum_type <= SVI_VIB_NONE || vib_enum_type >= SVI_VIB_ENUM_END) {
393 SVILOG("ERROR! invalid svi_enum(%d)", vib_enum_type);
397 cur_path = haptic_file[vib_enum_type];
400 if( (cur_path == NULL) || (strlen(cur_path) == 0) ) {
401 SVILOG("ERROR! current path is invalid");
405 if(unlink(cur_path) < 0) {
406 SVILOG("ERROR!! unlink(%s) error(%d)", cur_path, errno);
410 if(symlink(path,cur_path) < 0) {
411 SVILOG("ERROR!! symlink(%s) error(%d)", path, errno);
418 API int svi_get_path(int svi_type, int svi_enum, char* buf, unsigned int bufLen)
420 vibration_type vib_enum_type = SVI_VIB_NONE;
421 sound_type snd_enum_type = SVI_SND_NONE;
422 const char* cur_path = NULL;
423 int retry = SVI_RETRY_CNT;
425 if (buf == NULL || bufLen <= 0) {
426 SVILOG("ERROR!! invalid input parameters.");
430 if (!(svi_type == SVI_TYPE_SND || svi_type == SVI_TYPE_VIB)) {
431 SVILOG("ERROR!! invalid svi_type(%d).", svi_type);
435 if (svi_type == SVI_TYPE_SND) {
436 snd_enum_type = (sound_type)svi_enum;
438 if (snd_enum_type <= SVI_SND_NONE || snd_enum_type >= SVI_SND_ENUM_END) {
439 SVILOG("ERROR! invalid svi_enum(%d)", snd_enum_type);
443 cur_path = snd_file[snd_enum_type];
445 } else if (svi_type == SVI_TYPE_VIB) {
446 vib_enum_type = (vibration_type) svi_enum;
448 if (vib_enum_type <= SVI_VIB_NONE || vib_enum_type >= SVI_VIB_ENUM_END) {
449 SVILOG("ERROR! invalid svi_enum(%d)", vib_enum_type);
453 cur_path = haptic_file[vib_enum_type];
457 if(readlink(cur_path, buf, bufLen) < 0) {
458 if(errno == ENOENT) {
459 /* restore svi origin path because of invalid link */
460 if(__svi_restore_default_file(svi_type, svi_enum) == SVI_ERROR) {
461 SVILOG("ERROR!! __svi_restore_default_file(%d/%d) error", svi_type, svi_enum);
465 SVILOG("ERROR!! readlink(%s) error(%d)", cur_path, errno);