Return permission denied error when vconf failed with that cause
[platform/core/multimedia/libmm-sound.git] / common / mm_sound_utils.c
1 /*
2  * libmm-sound
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Seungbae Shin <seungbae.shin@samsung.com>
7  *
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
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
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.
19  *
20  */
21
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <fcntl.h>
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <unistd.h>
28 #include <errno.h>
29 #include <glib.h>
30
31 #include <vconf.h>
32 #include <vconf-keys.h>
33 #include <mm_types.h>
34 #include <mm_error.h>
35 #include <mm_debug.h>
36 #include "../include/mm_sound_private.h"
37 #include "../include/mm_sound.h"
38 #include "../include/mm_sound_common.h"
39 #include "../include/mm_sound_utils.h"
40
41 #define MM_SOUND_DEFAULT_VOLUME_SYSTEM                  9
42 #define MM_SOUND_DEFAULT_VOLUME_NOTIFICATION    11
43 #define MM_SOUND_DEFAULT_VOLUME_ALARAM                  7
44 #define MM_SOUND_DEFAULT_VOLUME_RINGTONE                11
45 #define MM_SOUND_DEFAULT_VOLUME_MEDIA                   7
46 #define MM_SOUND_DEFAULT_VOLUME_CALL                    4
47 #define MM_SOUND_DEFAULT_VOLUME_VOIP                    4
48 #define MM_SOUND_DEFAULT_VOLUME_VOICE                   7
49 #define MM_SOUND_DEFAULT_VOLUME_ANDROID                 0
50
51 static char *g_volume_vconf[VOLUME_TYPE_MAX] = {
52         VCONF_KEY_VOLUME_TYPE_SYSTEM,           /* VOLUME_TYPE_SYSTEM */
53         VCONF_KEY_VOLUME_TYPE_NOTIFICATION,     /* VOLUME_TYPE_NOTIFICATION */
54         VCONF_KEY_VOLUME_TYPE_ALARM,            /* VOLUME_TYPE_ALARM */
55         VCONF_KEY_VOLUME_TYPE_RINGTONE,         /* VOLUME_TYPE_RINGTONE */
56         VCONF_KEY_VOLUME_TYPE_MEDIA,            /* VOLUME_TYPE_MEDIA */
57         VCONF_KEY_VOLUME_TYPE_CALL,                     /* VOLUME_TYPE_CALL */
58         VCONF_KEY_VOLUME_TYPE_VOIP,                     /* VOLUME_TYPE_VOIP */
59         VCONF_KEY_VOLUME_TYPE_VOICE,            /* VOLUME_TYPE_VOICE */
60         VCONF_KEY_VOLUME_TYPE_ANDROID           /* VOLUME_TYPE_FIXED */
61 };
62 static char *g_volume_str[VOLUME_TYPE_MAX] = {
63         "SYSTEM",
64         "NOTIFICATION",
65         "ALARM",
66         "RINGTONE",
67         "MEDIA",
68         "CALL",
69         "VOIP",
70         "VOICE",
71         "FIXED",
72 };
73
74 EXPORT_API
75 int mm_sound_util_volume_get_value_by_type(volume_type_t type, unsigned int *value)
76 {
77         int ret = MM_ERROR_NONE;
78         int vconf_value = 0;
79
80         /* Get volume value from VCONF */
81         if (vconf_get_int(g_volume_vconf[type], &vconf_value)) {
82                 debug_error ("vconf_get_int(%s) failed..\n", g_volume_vconf[type]);
83                 return MM_ERROR_SOUND_INTERNAL;
84         }
85
86         *value = vconf_value;
87         if (ret == MM_ERROR_NONE)
88                 debug_log("volume_get_value %s %d",  g_volume_str[type], *value);
89
90         return ret;
91 }
92
93 EXPORT_API
94 int mm_sound_util_volume_set_value_by_type(volume_type_t type, unsigned int value)
95 {
96         int ret = MM_ERROR_NONE;
97         int vconf_value = 0;
98
99         vconf_value = value;
100         debug_log("volume_set_value %s %d",  g_volume_str[type], value);
101
102         /* Set volume value to VCONF */
103         if ((ret = vconf_set_int(g_volume_vconf[type], vconf_value)) != 0) {
104                 int vconf_errno;
105                 vconf_errno = vconf_get_ext_errno();
106                 debug_error ("vconf_set_int(%s) failed..ret[%d] errno[%d]\n", g_volume_vconf[type], ret, vconf_errno);
107                 if (vconf_errno == VCONF_ERROR_FILE_PERM)
108                         return MM_ERROR_SOUND_PERMISSION_DENIED;
109                 else
110                         return MM_ERROR_SOUND_INTERNAL;
111         }
112         return ret;
113 }
114
115 EXPORT_API
116 bool mm_sound_util_is_recording (void)
117 {
118         /* FIXME : is this function needs anymore ??? */
119         return false;
120 }
121
122 EXPORT_API
123 bool mm_sound_util_is_process_alive(pid_t pid)
124 {
125         gchar *tmp = NULL;
126         int ret = -1;
127
128         if (pid > 999999 || pid < 2)
129                 return false;
130
131         if ((tmp = g_strdup_printf("/proc/%d", pid))) {
132                 ret = access(tmp, F_OK);
133                 g_free(tmp);
134         }
135
136         if (ret == -1) {
137                 if (errno == ENOENT) {
138                         debug_warning ("/proc/%d not exist", pid);
139                         return false;
140                 } else {
141                         debug_error ("/proc/%d access errno[%d]", pid, errno);
142
143                         /* FIXME: error occured but file exists */
144                         return true;
145                 }
146         }
147
148         return true;
149 }