merge tizen 2.4
[platform/core/appfw/event-system.git] / src / esd_system_event.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <glib.h>
4 #include <dlog.h>
5 #include <vconf.h>
6 #include <bundle.h>
7 #include <eventsystem.h>
8
9 #include "eventsystem_daemon.h"
10
11 /* table item : sent system-event by esd */
12 static GHashTable *esd_sent_table;
13
14 typedef struct __esd_sent_table_item {
15         char *event_name;
16         bundle *event_data;
17 } esd_sent_item;
18
19 struct esd_vconf_handler {
20         const char *key;
21         void (*esd_vconfcb_fn) (keynode_t *node, void *user_data);
22 };
23
24 static int __esd_event_data_compare(bundle *b1, bundle *b2, const char *key)
25 {
26         int ret = 0;
27         int tmp1 = 0;
28         int tmp2 = 0;
29         char *str1 = NULL;
30         char *str2 = NULL;
31
32         if (bundle_get_count(b1) == bundle_get_count(b2)) {
33                 tmp1 = bundle_get_str(b1, key, &str1);
34                 tmp2 = bundle_get_str(b2, key, &str2);
35                 if (tmp1 == BUNDLE_ERROR_NONE && tmp2 == BUNDLE_ERROR_NONE) {
36                         if (strcmp(str1, str2) != 0) {
37                                 _D("new event_data : value check");
38                                 ret = 1;
39                         }
40                 }
41         } else {
42                 _D("new event_data : bundle_count check");
43                 ret = 1;
44         }
45
46         if (ret == 0) {
47                 _D("same event_data");
48         }
49
50         return ret;
51 }
52
53 static int __esd_send_system_event(const char *event_name, bundle *b, const char *key)
54 {
55         int ret = ES_R_OK;
56
57         esd_sent_item *item =
58                 (esd_sent_item *)g_hash_table_lookup(esd_sent_table, event_name);
59
60         if (item && __esd_event_data_compare(item->event_data, b, key) == 0) {
61                 _D("skip send: same with previous data");
62         } else {
63                 ret = eventsystem_send_system_event(event_name, b);
64                 if (ret != ES_R_OK) {
65                         _E("failed to send event");
66                         goto out;
67                 }
68
69                 if (item) {
70                         bundle_free(item->event_data);
71                         item->event_data = bundle_dup(b);
72                 } else {
73                         item = calloc(1, sizeof(esd_sent_item));
74                         if (item == NULL) {
75                                 _E("memory alloc failed");
76                                 ret = ES_R_ERROR;
77                                 goto out;
78                         }
79                         item->event_name = strdup(event_name);
80                         if (item->event_name == NULL) {
81                                 _E("out of memory");
82                                 FREE_AND_NULL(item);
83                                 ret = ES_R_ERROR;
84                                 goto out;
85                         }
86                         item->event_data = bundle_dup(b);
87                 }
88
89                 g_hash_table_insert(esd_sent_table, item->event_name, item);
90         }
91
92 out:
93         return ret;
94 }
95
96 static void __esd_vconfcb_location_use_mylocation(keynode_t *node, void *user_data)
97 {
98         int ret = 0;
99         int enabled = 0;
100         bundle *b = NULL;
101         const char *key = NULL;
102         const char *val = NULL;
103
104         _D("vconfcb called");
105
106         ret = vconf_get_int(VCONFKEY_LOCATION_USE_MY_LOCATION, &enabled);
107         if (ret != VCONF_OK) {
108                 _E("failed to get vconf (%d)", ret);
109                 return;
110         }
111
112         key = EVT_KEY_LOCATION_ENABLE_STATE;
113
114         if (enabled) {
115                 val = EVT_VAL_LOCATION_ENABLED;
116         } else {
117                 val = EVT_VAL_LOCATION_DISABLED;
118         }
119
120         b = bundle_create();
121         bundle_add_str(b, key, val);
122
123         if (__esd_send_system_event(SYS_EVENT_LOCATION_ENABLE_STATE, b, key) != ES_R_OK) {
124                 _E("failed to send event");
125         }
126
127         if (b) {
128                 bundle_free(b);
129         }
130 }
131
132 static void __esd_vconfcb_location_enabled(keynode_t *node, void *user_data)
133 {
134         int ret = 0;
135         int enabled = 0;
136         bundle *b = NULL;
137         const char *key = NULL;
138         const char *val = NULL;
139
140         _D("vconfcb called");
141
142         ret = vconf_get_int(VCONFKEY_LOCATION_ENABLED, &enabled);
143         if (ret != VCONF_OK) {
144                 _E("failed to get vconf (%d)", ret);
145                 return;
146         }
147
148         key = EVT_KEY_GPS_ENABLE_STATE;
149
150         if (enabled) {
151                 val = EVT_VAL_GPS_ENABLED;
152         } else {
153                 val = EVT_VAL_GPS_DISABLED;
154         }
155
156         b = bundle_create();
157         bundle_add_str(b, key, val);
158
159         if (__esd_send_system_event(SYS_EVENT_GPS_ENABLE_STATE, b, key) != ES_R_OK) {
160                 _E("failed to send event");
161         }
162
163         if (b) {
164                 bundle_free(b);
165         }
166 }
167
168 static void __esd_vconfcb_location_network_enabled(keynode_t *node, void *user_data)
169 {
170         int ret = 0;
171         int enabled = 0;
172         bundle *b = NULL;
173         const char *key = NULL;
174         const char *val = NULL;
175
176         _D("vconfcb called");
177
178         ret = vconf_get_int(VCONFKEY_LOCATION_NETWORK_ENABLED, &enabled);
179         if (ret != VCONF_OK) {
180                 _E("failed to get vconf (%d)", ret);
181                 return;
182         }
183
184         key = EVT_KEY_NPS_ENABLE_STATE;
185
186         if (enabled) {
187                 val = EVT_VAL_NPS_ENABLED;
188         } else {
189                 val = EVT_VAL_NPS_DISABLED;
190         }
191
192         b = bundle_create();
193         bundle_add_str(b, key, val);
194
195         if (__esd_send_system_event(SYS_EVENT_NPS_ENABLE_STATE, b, key) != ES_R_OK) {
196                 _E("failed to send event");
197         }
198
199         if (b) {
200                 bundle_free(b);
201         }
202 }
203
204 static void __esd_vconfcb_language_set(keynode_t *node, void *user_data)
205 {
206         char *str = 0;
207         bundle *b = NULL;
208         const char *key = NULL;
209
210         _D("vconfcb called");
211
212         str = vconf_get_str(VCONFKEY_LANGSET);
213         if (str == NULL) {
214                 _E("failed to get vconf str");
215                 return;
216         }
217
218         key = EVT_KEY_LANGUAGE_SET;
219
220         b = bundle_create();
221         bundle_add_str(b, key, str);
222
223         if (__esd_send_system_event(SYS_EVENT_LANGUAGE_SET, b, key) != ES_R_OK) {
224                 _E("failed to send event");
225         }
226
227         if (b) {
228                 bundle_free(b);
229         }
230 }
231
232 static void __esd_vconfcb_hour_format(keynode_t *node, void *user_data)
233 {
234         int ret = 0;
235         int hours = 0;
236         bundle *b = NULL;
237         const char *key = NULL;
238         const char *val = NULL;
239
240         _D("vconfcb called");
241
242         ret = vconf_get_int(VCONFKEY_REGIONFORMAT_TIME1224, &hours);
243         if (ret != VCONF_OK) {
244                 _E("failed to get vconf (%d)", ret);
245                 return;
246         }
247
248         key = EVT_KEY_HOUR_FORMAT;
249
250         if (hours == VCONFKEY_TIME_FORMAT_24)
251                 val = EVT_VAL_HOURFORMAT_24;
252         else
253                 val = EVT_VAL_HOURFORMAT_12;
254
255         b = bundle_create();
256         bundle_add_str(b, key, val);
257
258         if (__esd_send_system_event(SYS_EVENT_HOUR_FORMAT, b, key) != ES_R_OK) {
259                 _E("failed to send event");
260         }
261
262         if (b) {
263                 bundle_free(b);
264         }
265 }
266
267 static void __esd_vconfcb_region_format(keynode_t *node, void *user_data)
268 {
269         char *str = 0;
270         bundle *b = NULL;
271         const char *key = NULL;
272
273         _D("vconfcb called");
274
275         str = vconf_get_str(VCONFKEY_REGIONFORMAT);
276         if (str == NULL) {
277                 _E("failed to get vconf str");
278                 return;
279         }
280
281         key = EVT_KEY_REGION_FORMAT;
282
283         b = bundle_create();
284         bundle_add_str(b, key, str);
285
286         if (__esd_send_system_event(SYS_EVENT_REGION_FORMAT, b, key) != ES_R_OK) {
287                 _E("failed to send event");
288         }
289
290         if (b) {
291                 bundle_free(b);
292         }
293 }
294
295 static void __esd_vconfcb_vibration_status(keynode_t *node, void *user_data)
296 {
297         int ret = 0;
298         int vibration_on = 0;
299         int sound_on = 0;
300         bundle *b = NULL;
301         char *key = NULL;
302         char *val = NULL;
303
304         _D("vconfcb called");
305
306         ret = vconf_get_bool(VCONFKEY_SETAPPL_VIBRATION_STATUS_BOOL, &vibration_on);
307         if (ret != VCONF_OK) {
308                 _E("failed to get vconf (%d)", ret);
309                 return;
310         }
311
312         ret = vconf_get_bool(VCONFKEY_SETAPPL_SOUND_STATUS_BOOL, &sound_on);
313         if (ret != VCONF_OK) {
314                 _E("failed to get vconf (%d)", ret);
315                 return;
316         }
317
318         if (vibration_on) {
319                 key = EVT_KEY_VIBRATION_STATE;
320                 val = EVT_VAL_VIBRATION_ON;
321                 b = bundle_create();
322                 bundle_add_str(b, key, val);
323                 if (__esd_send_system_event(SYS_EVENT_VIBRATION_STATE, b, key) != ES_R_OK) {
324                         _E("failed to send event");
325                 }
326                 if (b) {
327                         bundle_free(b);
328                 }
329
330                 key = EVT_KEY_SILENT_MODE;
331                 val = EVT_VAL_SILENTMODE_OFF;
332                 b = bundle_create();
333                 bundle_add_str(b, key, val);
334                 if (__esd_send_system_event(SYS_EVENT_SILENT_MODE, b, key) != ES_R_OK) {
335                         _E("failed to send event");
336                 }
337                 if (b) {
338                         bundle_free(b);
339                 }
340         } else {
341                 key = EVT_KEY_VIBRATION_STATE;
342                 val = EVT_VAL_VIBRATION_OFF;
343                 b = bundle_create();
344                 bundle_add_str(b, key, val);
345                 if (__esd_send_system_event(SYS_EVENT_VIBRATION_STATE, b, key) != ES_R_OK) {
346                         _E("failed to send event");
347                 }
348                 if (b) {
349                         bundle_free(b);
350                 }
351
352                 if (!sound_on) {
353                         key = EVT_KEY_SILENT_MODE;
354                         val = EVT_VAL_SILENTMODE_ON;
355                         b = bundle_create();
356                         bundle_add_str(b, key, val);
357                         if (__esd_send_system_event(SYS_EVENT_SILENT_MODE, b, key) != ES_R_OK) {
358                                 _E("failed to send event");
359                         }
360                         if (b) {
361                                 bundle_free(b);
362                         }
363                 }
364         }
365 }
366
367 static void __esd_vconfcb_sound_status(keynode_t *node, void *user_data)
368 {
369         int ret = 0;
370         int vibration_on = 0;
371         int sound_on = 0;
372         bundle *b = NULL;
373         char *key = NULL;
374         char *val = NULL;
375
376         _D("vconfcb called");
377
378         ret = vconf_get_bool(VCONFKEY_SETAPPL_VIBRATION_STATUS_BOOL, &vibration_on);
379         if (ret != VCONF_OK) {
380                 _E("failed to get vconf (%d)", ret);
381                 return;
382         }
383
384         ret = vconf_get_bool(VCONFKEY_SETAPPL_SOUND_STATUS_BOOL, &sound_on);
385         if (ret != VCONF_OK) {
386                 _E("failed to get vconf (%d)", ret);
387                 return;
388         }
389
390         if (sound_on) {
391                 key = EVT_KEY_VIBRATION_STATE;
392                 val = EVT_VAL_VIBRATION_OFF;
393                 b = bundle_create();
394                 bundle_add_str(b, key, val);
395                 if (__esd_send_system_event(SYS_EVENT_VIBRATION_STATE, b, key) != ES_R_OK) {
396                         _E("failed to send event");
397                 }
398                 if (b) {
399                         bundle_free(b);
400                 }
401
402                 key = EVT_KEY_SILENT_MODE;
403                 val = EVT_VAL_SILENTMODE_OFF;
404                 b = bundle_create();
405                 bundle_add_str(b, key, val);
406                 if (__esd_send_system_event(SYS_EVENT_SILENT_MODE, b, key) != ES_R_OK) {
407                         _E("failed to send event");
408                 }
409                 if (b) {
410                         bundle_free(b);
411                 }
412         } else {
413                 if (!vibration_on) {
414                         key = EVT_KEY_SILENT_MODE;
415                         val = EVT_VAL_SILENTMODE_ON;
416                         b = bundle_create();
417                         bundle_add_str(b, key, val);
418                         if (__esd_send_system_event(SYS_EVENT_SILENT_MODE, b, key) != ES_R_OK) {
419                                 _E("failed to send event");
420                         }
421                         if (b) {
422                                 bundle_free(b);
423                         }
424                 }
425         }
426 }
427
428 static void __esd_vconfcb_auto_rotate(keynode_t *node, void *user_data)
429 {
430         int ret = 0;
431         int enabled = 0;
432         bundle *b = NULL;
433         const char *key = NULL;
434         const char *val = NULL;
435
436         _D("vconfcb called");
437
438         ret = vconf_get_bool(VCONFKEY_SETAPPL_AUTO_ROTATE_SCREEN_BOOL, &enabled);
439         if (ret != VCONF_OK) {
440                 _E("failed to get vconf (%d)", ret);
441                 return;
442         }
443
444         key = EVT_KEY_SCREEN_AUTOROTATE_STATE;
445
446         if (enabled)
447                 val = EVT_VAL_SCREEN_AUTOROTATE_ON;
448         else
449                 val = EVT_VAL_SCREEN_AUTOROTATE_OFF;
450
451         b = bundle_create();
452         bundle_add_str(b, key, val);
453
454         if (__esd_send_system_event(SYS_EVENT_SCREEN_AUTOROTATE_STATE, b, key) != ES_R_OK) {
455                 _E("failed to send event");
456         }
457
458         if (b) {
459                 bundle_free(b);
460         }
461 }
462
463 static void __esd_vconfcb_mobiledata_state(keynode_t *node, void *user_data)
464 {
465         int ret = 0;
466         int enabled = 0;
467         bundle *b = NULL;
468         const char *key = NULL;
469         const char *val = NULL;
470
471         _D("vconfcb called");
472
473         ret = vconf_get_bool(VCONFKEY_3G_ENABLE, &enabled);
474         if (ret != VCONF_OK) {
475                 _E("failed to get vconf (%d)", ret);
476                 return;
477         }
478
479         key = EVT_KEY_MOBILE_DATA_STATE;
480
481         if (enabled)
482                 val = EVT_VAL_MOBILE_DATA_ON;
483         else
484                 val = EVT_VAL_MOBILE_DATA_OFF;
485
486         b = bundle_create();
487         bundle_add_str(b, key, val);
488
489         if (__esd_send_system_event(SYS_EVENT_MOBILE_DATA_STATE, b, key) != ES_R_OK) {
490                 _E("failed to send event");
491         }
492
493         if (b) {
494                 bundle_free(b);
495         }
496 }
497
498 static void __esd_vconfcb_roaming_state(keynode_t *node, void *user_data)
499 {
500         int ret = 0;
501         int enabled = 0;
502         bundle *b = NULL;
503         const char *key = NULL;
504         const char *val = NULL;
505
506         _D("vconfcb called");
507
508         ret = vconf_get_bool(VCONFKEY_SETAPPL_STATE_DATA_ROAMING_BOOL, &enabled);
509         if (ret != VCONF_OK) {
510                 _E("failed to get vconf (%d)", ret);
511                 return;
512         }
513
514         key = EVT_KEY_DATA_ROAMING_STATE;
515
516         if (enabled)
517                 val = EVT_VAL_DATA_ROAMING_ON;
518         else
519                 val = EVT_VAL_DATA_ROAMING_OFF;
520
521         b = bundle_create();
522         bundle_add_str(b, key, val);
523
524         if (__esd_send_system_event(SYS_EVENT_DATA_ROAMING_STATE, b, key) != ES_R_OK) {
525                 _E("failed to send event");
526         }
527
528         if (b) {
529                 bundle_free(b);
530         }
531 }
532
533 static void __esd_vconfcb_font_set(keynode_t *node, void *user_data)
534 {
535         char *str = 0;
536         bundle *b = NULL;
537         const char *key = NULL;
538
539         _D("vconfcb called");
540
541         str = vconf_get_str(VCONFKEY_SETAPPL_ACCESSIBILITY_FONT_NAME);
542         if (str == NULL) {
543                 _E("failed to get vconf str");
544                 return;
545         }
546
547         key = EVT_KEY_FONT_SET;
548
549         b = bundle_create();
550         bundle_add_str(b, key, str);
551
552         if (__esd_send_system_event(SYS_EVENT_FONT_SET, b, key) != ES_R_OK) {
553                 _E("failed to send event");
554         }
555
556         if (b) {
557                 bundle_free(b);
558         }
559 }
560
561 static struct esd_vconf_handler vconf_callbacks[] = {
562         {VCONFKEY_LOCATION_USE_MY_LOCATION, __esd_vconfcb_location_use_mylocation},
563         {VCONFKEY_LOCATION_ENABLED, __esd_vconfcb_location_enabled},
564         {VCONFKEY_LOCATION_NETWORK_ENABLED, __esd_vconfcb_location_network_enabled},
565         {VCONFKEY_LANGSET, __esd_vconfcb_language_set},
566         {VCONFKEY_REGIONFORMAT_TIME1224, __esd_vconfcb_hour_format},
567         {VCONFKEY_REGIONFORMAT, __esd_vconfcb_region_format},
568         {VCONFKEY_SETAPPL_VIBRATION_STATUS_BOOL, __esd_vconfcb_vibration_status},
569         {VCONFKEY_SETAPPL_SOUND_STATUS_BOOL, __esd_vconfcb_sound_status},
570         {VCONFKEY_SETAPPL_AUTO_ROTATE_SCREEN_BOOL, __esd_vconfcb_auto_rotate},
571         {VCONFKEY_3G_ENABLE, __esd_vconfcb_mobiledata_state},
572         {VCONFKEY_SETAPPL_STATE_DATA_ROAMING_BOOL, __esd_vconfcb_roaming_state},
573         {VCONFKEY_SETAPPL_ACCESSIBILITY_FONT_NAME, __esd_vconfcb_font_set},
574 };
575
576 static int vconfcbs_size = sizeof(vconf_callbacks)/sizeof(struct esd_vconf_handler);
577
578 int __esd_register_vconf_callbacks(void)
579 {
580         int i = 0;
581         int ret = 0;
582         int result = ES_R_OK;
583
584 #if (GLIB_MAJOR_VERSION <= 2 && GLIB_MINOR_VERSION < 36)
585         g_type_init();
586 #endif
587
588         esd_sent_table = g_hash_table_new(g_str_hash, g_str_equal);
589
590         _D("vconf callbacks size(%d)", vconfcbs_size);
591         for (i = 0; i < vconfcbs_size; i++) {
592                 ret = vconf_notify_key_changed(vconf_callbacks[i].key,
593                         vconf_callbacks[i].esd_vconfcb_fn, NULL);
594                 if (ret != VCONF_OK) {
595                         _E("failed to register vconf callback (%s)", vconf_callbacks[i].key);
596                         result = ES_R_ERROR;
597                         break;
598                 }
599         }
600
601         return result;
602 }
603