Merge branch 'tizen_3.0' into tizen
[sdk/target/sdbd.git] / src / default_plugin_event.c
1 /*
2  * Copyright (c) 2011 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 <glib.h>
19
20 #include "sysdeps.h"
21 #include "sdbd_plugin.h"
22 #include "default_plugin.h"
23
24 #include <system_info.h>
25
26 typedef enum {
27     TIZEN_PROFILE_UNKNOWN = 0,
28     TIZEN_PROFILE_MOBILE = 0x1,
29     TIZEN_PROFILE_WEARABLE = 0x2,
30     TIZEN_PROFILE_TV = 0x4,
31     TIZEN_PROFILE_IVI = 0x8,
32     TIZEN_PROFILE_COMMON = 0x10,
33 } tizen_profile_t;
34 static tizen_profile_t profile = TIZEN_PROFILE_UNKNOWN;
35 static tizen_profile_t _get_tizen_profile()
36 {
37     char *profileName;
38     system_info_get_platform_string("http://tizen.org/feature/profile", &profileName);
39     switch (*profileName) {
40     case 'm':
41     case 'M':
42         profile = TIZEN_PROFILE_MOBILE;
43         break;
44     case 'w':
45     case 'W':
46         profile = TIZEN_PROFILE_WEARABLE;
47         break;
48     case 't':
49     case 'T':
50         profile = TIZEN_PROFILE_TV;
51         break;
52     case 'i':
53     case 'I':
54         profile = TIZEN_PROFILE_IVI;
55         break;
56     default: // common or unknown ==> ALL ARE COMMON.
57         profile = TIZEN_PROFILE_COMMON;
58     }
59     free(profileName);
60
61     return profile;
62 }
63 static inline tizen_profile_t get_tizen_profile()
64 {
65     if (__builtin_expect(profile != TIZEN_PROFILE_UNKNOWN, 1))
66         return profile;
67
68     return _get_tizen_profile();
69 }
70
71 int plugin_pwlocked ( void )
72 {
73     int pwlock_status = 0;
74     int pwlock_type = 0;
75
76     if ( vconf_get_int ( VCONFKEY_IDLE_LOCK_STATE, &pwlock_status ) ) {
77         pwlock_status = 0;
78         PLUGIN_LOG ( "failed to get pw lock status\n" );
79     }
80     if (get_tizen_profile() == TIZEN_PROFILE_WEARABLE) {
81         PLUGIN_LOG ( "wearable lock applied\n" );
82         // for wearable which uses different VCONF key (lock type)
83         if ( vconf_get_int ( VCONFKEY_SETAPPL_PRIVACY_LOCK_TYPE_INT, &pwlock_type ) ) {
84             pwlock_type = 0;
85             PLUGIN_LOG ( "failed to get pw lock type\n" );
86         }
87         if ( ( pwlock_status == VCONFKEY_IDLE_LOCK ) && ( pwlock_type != SETTING_PRIVACY_LOCK_TYPE_NONE ) ) {
88             PLUGIN_LOG ( "device has been locked\n" );
89             return 1; // locked!
90         }
91     } else {
92         PLUGIN_LOG ( "mobile lock applied\n" );
93         // for mobile
94         if ( vconf_get_int ( VCONFKEY_SETAPPL_SCREEN_LOCK_TYPE_INT, &pwlock_type ) ) {
95             pwlock_type = 0;
96             PLUGIN_LOG ( "failed to get pw lock type\n" );
97         }
98         if ( pwlock_status == VCONFKEY_IDLE_LOCK && ( ( pwlock_type != SETTING_SCREEN_LOCK_TYPE_NONE ) && ( pwlock_type != SETTING_SCREEN_LOCK_TYPE_SWIPE ) ) ) {
99             PLUGIN_LOG ( "device has been locked\n" );
100             return 1; // locked!
101         }
102     }
103     return 0; // unlocked!
104 }
105
106 int get_lock_state ( parameters* in, parameters* out )
107 {
108     if ( in == NULL || in->number_of_parameter != 1 || in->array_of_parameter == NULL
109             || in->array_of_parameter[0].type != type_int32 ) {
110         PLUGIN_LOG ( "Invalid argument\n" );
111         return PLUGIN_CMD_FAIL;
112     }
113
114     if ( out == NULL ) {
115         PLUGIN_LOG ( "Invalid argument\n" );
116         return PLUGIN_CMD_FAIL;
117     }
118
119     PLUGIN_LOG ( "lock type: %d\n", in->array_of_parameter[0].v_int32);
120
121     out->number_of_parameter = 1;
122     out->array_of_parameter = ( parameter* ) malloc ( sizeof ( parameter ) );
123     out->array_of_parameter[0].type = type_int32;
124     out->array_of_parameter[0].v_int32 = ( plugin_pwlocked() == 1 ) ? PLUGIN_RET_ON : PLUGIN_RET_OFF;
125
126     return PLUGIN_CMD_SUCCESS;
127 }
128
129 static void pwlock_cb ( keynode_t *key, void* data )
130 {
131     PLUGIN_LOG ( "pwlock callback is issued\n" );
132     int pwlocked = plugin_pwlocked();
133
134     parameters* out = ( parameters* ) malloc ( sizeof ( parameters ) );
135     out->number_of_parameter = 1;
136     out->array_of_parameter = ( parameter* ) malloc ( sizeof ( parameter ) );
137     out->array_of_parameter[0].type = type_int32;
138     out->array_of_parameter[0].v_int32 = ( pwlocked == 1 ) ? PLUGIN_RET_ON : PLUGIN_RET_OFF;
139
140     event_handler ( PLUGIN_EVENT_PWLOCK, out );
141 }
142
143 static void register_pwlock_cb()
144 {
145     int ret = vconf_notify_key_changed ( VCONFKEY_IDLE_LOCK_STATE, pwlock_cb, NULL );
146     if ( ret != 0 ) {
147         PLUGIN_LOG ( "cannot register vconf callback.\n" );
148         return;
149     }
150     PLUGIN_LOG ( "registered vconf callback\n" );
151 }
152
153 static void unregister_pwlock_cb()
154 {
155     int ret = vconf_ignore_key_changed ( VCONFKEY_IDLE_LOCK_STATE, pwlock_cb );
156     if ( ret != 0 ) {
157         PLUGIN_LOG ( "cannot unregister vconf callback.\n" );
158         return;
159     }
160     PLUGIN_LOG ( "unregistered vconf callback\n" );
161 }
162
163 static void *pwlock_thread ( void *x )
164 {
165     GMainLoop *loop;
166     loop = g_main_loop_new ( NULL, FALSE );
167     register_pwlock_cb();
168     g_main_loop_run ( loop );
169     g_main_loop_unref ( loop );
170     unregister_pwlock_cb();
171     return NULL;
172 }
173
174 void create_pwlock_thread()
175 {
176     sdb_thread_t t;
177     if ( sdb_thread_create ( &t, pwlock_thread, NULL ) ) {
178         PLUGIN_LOG ( "default plugin : cannot create_pwlock_thread.\n" );
179         return;
180     }
181     PLUGIN_LOG ( "default plugin : created pwlock_thread\n" );
182 }