tizen 2.3.1 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
23 #include <stdio.h>
24 #include <string.h>
25 #include <stdlib.h>
26 #include <utilX.h>
27 #include <glib.h>
28 #include <poll.h>
29 #include <bundle.h>
30
31 #include "aul.h"
32 #include "aul_api.h"
33 #include "menu_db_util.h"
34 #include "simple_util.h"
35 #include "app_sock.h"
36 #include "aul_util.h"
37 #include "launch.h"
38
39
40 static int (*_aul_key_handler) (bundle *kb, void *data) = NULL;
41 static void *_aul_key_data = NULL;
42
43 extern GSourceFuncs funcs;
44
45 int app_key_event(bundle *kb)
46 {
47         if (_aul_key_handler)
48                 _aul_key_handler(kb, _aul_key_data);
49         return 0;
50 }
51
52 int aul_register_key_init_callback(
53         int (*aul_handler) (bundle *, void *), void *data)
54 {
55         /* Save start handler function in static var */
56         _aul_key_handler = aul_handler;
57         _aul_key_data = data;
58         return 0;
59 }
60
61 SLPAPI int aul_key_init(int (*aul_handler) (bundle *, void *), void *data)
62 {
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                 _E("out of memory");
80                 g_source_unref(src);
81                 close(fd);
82                 return AUL_R_ERROR;
83         }
84
85         gpollfd->events = POLLIN;
86         gpollfd->fd = fd;
87
88         g_source_add_poll(src, gpollfd);
89         g_source_set_callback(src, (GSourceFunc) __aul_glib_handler,
90                               (gpointer) gpollfd, NULL);
91         g_source_set_priority(src, G_PRIORITY_LOW);
92
93         ret = g_source_attach(src, NULL);
94         if (ret == 0)
95                 return AUL_R_ERROR;
96
97         g_source_unref(src);
98
99         return AUL_R_OK;
100 }
101
102 SLPAPI int aul_key_reserve()
103 {
104         bundle *kb;
105         int ret;
106
107         kb = bundle_create();
108         ret = app_send_cmd(AUL_UTIL_PID, APP_KEY_RESERVE, kb);
109         bundle_free(kb);
110
111         return ret;
112 }
113
114 SLPAPI int aul_key_release()
115 {
116         bundle *kb;
117         int ret;
118
119         kb = bundle_create();
120         ret = app_send_cmd(AUL_UTIL_PID, APP_KEY_RELEASE, kb);
121         bundle_free(kb);
122
123         return ret;
124 }
125
126
127