Fix coverity issue 1149129
[apps/native/volume-app.git] / src / earphone.c
1 /*
2  * Copyright (c) 2009-2015 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <vconf.h>
18 #include <vconf-keys.h>
19
20 #include "main.h"
21 #include "_util_log.h"
22
23 int earphone_is_connected;
24
25 static void _volume_earphone_changed_cb(keynode_t *node, void *data)
26 {
27         int status = 0;
28         int ret;
29
30         ret = vconf_get_int(VCONFKEY_SYSMAN_EARJACK, &status);
31         if (ret < 0)
32                 return;
33
34         switch (status) {
35         case VCONFKEY_SYSMAN_EARJACK_3WIRE:
36         case VCONFKEY_SYSMAN_EARJACK_4WIRE:
37         case VCONFKEY_SYSMAN_EARJACK_TVOUT:
38                 _D("Earphone is connected");
39                 earphone_is_connected = 1;
40                 break;
41         default:
42                 _D("Earphone is disconnected");
43                 earphone_is_connected = 0;
44                 break;
45         }
46 }
47
48 static int register_earphone_module(void *data)
49 {
50         int ret;
51
52         ret = vconf_notify_key_changed(VCONFKEY_SYSMAN_EARJACK, _volume_earphone_changed_cb, NULL);
53         if (ret < 0) {
54                 _E("Failed to set earphone changed callback");
55                 return VOLUME_ERROR_FAIL;
56         }
57
58         _volume_earphone_changed_cb(NULL, NULL);
59
60         return VOLUME_ERROR_OK;
61 }
62
63 static int deregister_earphone_module(void *data)
64 {
65         int ret;
66
67         ret = vconf_ignore_key_changed(VCONFKEY_SYSMAN_EARJACK, _volume_earphone_changed_cb);
68         if (ret < 0) {
69                 _E("Failed to set earphone changed callback");
70                 return VOLUME_ERROR_FAIL;
71         }
72
73         return VOLUME_ERROR_OK;
74 }
75
76 void earphone_init(void)
77 {
78         _D("Initialization Earphone");
79
80         int ret;
81
82         ret = register_earphone_module(NULL);
83         _D("Initializaion result: %d", ret);
84 }
85
86 void earphone_fini(void)
87 {
88         _D("Deinitialization Earphone");
89         int ret;
90
91         ret = deregister_earphone_module(NULL);
92         _D("Deinitialization result: %d", ret);
93 }
94
95 int earphone_get_earphone_is_connected(void)
96 {
97         return earphone_is_connected;
98 }