tizen 2.4 release
[framework/appfw/aul-1.git] / src / key.c
1 /*
2  *  aul
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Jayoun Lee <airjany@samsung.com>, Sewook Park <sewook7.park@samsung.com>, Jaeho Lee <jaeho81.lee@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 #define _GNU_SOURCE
23 #include <stdio.h>
24 #include <string.h>
25 #include <stdlib.h>
26 #include <glib.h>
27 #include <poll.h>
28 #include <bundle.h>
29
30 #include "aul.h"
31 #include "aul_api.h"
32 #include "menu_db_util.h"
33 #include "simple_util.h"
34 #include "app_sock.h"
35 #include "aul_util.h"
36 #include "launch.h"
37
38
39 static int (*_aul_key_handler) (bundle *kb, void *data) = NULL;
40 static void *_aul_key_data = NULL;
41
42 extern GSourceFuncs funcs;
43
44 int app_key_event(bundle *kb)
45 {
46         if (_aul_key_handler)
47                 _aul_key_handler(kb, _aul_key_data);
48         return 0;
49 }
50
51 int aul_register_key_init_callback(
52         int (*aul_handler) (bundle *, void *), void *data)
53 {
54         /* Save start handler function in static var */
55         _aul_key_handler = aul_handler;
56         _aul_key_data = data;
57         return 0;
58 }
59
60 SLPAPI int aul_key_init(int (*aul_handler) (bundle *, void *), void *data)
61 {
62 #ifdef _APPFW_FEATURE_AMD_KEY
63         int fd;
64         GPollFD *gpollfd;
65         GSource *src;
66         int ret;
67
68         if (aul_handler != NULL)
69                 aul_register_key_init_callback(aul_handler, data);
70
71         fd = aul_initialize();
72         if (fd < 0)
73                 return fd;
74
75         src = g_source_new(&funcs, sizeof(GSource));
76
77         gpollfd = (GPollFD *) g_malloc(sizeof(GPollFD));
78         if (gpollfd == NULL) {
79                 g_source_unref(src);
80                 return AUL_R_ERROR;
81         }
82
83         gpollfd->events = POLLIN;
84         gpollfd->fd = fd;
85
86         g_source_add_poll(src, gpollfd);
87         g_source_set_callback(src, (GSourceFunc) __aul_glib_handler,
88                               (gpointer) gpollfd, NULL);
89         g_source_set_priority(src, G_PRIORITY_LOW);
90
91         ret = g_source_attach(src, NULL);
92         if (ret == 0)
93                 return AUL_R_ERROR;
94
95         g_source_unref(src);
96 #endif
97         return AUL_R_OK;
98 }
99
100 SLPAPI int aul_key_reserve()
101 {
102 #ifdef _APPFW_FEATURE_AMD_KE
103         bundle *kb;
104         int ret;
105
106         kb = bundle_create();
107         ret = app_send_cmd(AUL_UTIL_PID, APP_KEY_RESERVE, kb);
108         bundle_free(kb);
109
110         return ret;
111 #else
112         return AUL_R_OK;
113 #endif
114 }
115
116 SLPAPI int aul_key_release()
117 {
118 #ifdef _APPFW_FEATURE_AMD_KE
119         bundle *kb;
120         int ret;
121
122         kb = bundle_create();
123         ret = app_send_cmd(AUL_UTIL_PID, APP_KEY_RELEASE, kb);
124         bundle_free(kb);
125
126         return ret;
127 #else
128         return AUL_R_OK;
129 #endif
130 }