tizen 2.4 release
[framework/appfw/aul-1.git] / am_daemon / amd_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 <Ecore_X.h>
24 #include <Ecore_Input.h>
25 #ifdef _APPFW_FEATURE_AMD_KEY
26 #include <utilX.h>
27 #endif
28 #include <Ecore.h>
29 #include <Evas.h>
30 #include <aul.h>
31 #include <glib.h>
32 #include <bundle_internal.h>
33
34 #include "amd_config.h"
35 #include "amd_key.h"
36 #include "simple_util.h"
37 #include "app_sock.h"
38 #include "launch.h"
39
40 static struct {
41         Ecore_X_Window win;
42         Ecore_Event_Handler *key_up;
43         Ecore_Event_Handler *key_down;
44 } key_info = {
45         .win = 0,
46         .key_up = NULL,
47         .key_down = NULL,
48 };
49
50 GSList *key_pid_list = NULL;
51
52 #ifdef _APPFW_FEATURE_AMD_KEY
53 static Eina_Bool __key_release_cb(void *data, int type, void *event);
54 static Eina_Bool __key_press_cb(void *data, int type, void *event);
55
56 static Eina_Bool __key_release_cb(void *data, int type, void *event)
57 {
58         Evas_Event_Key_Up *ev = event;
59         int ret;
60         GSList *entry;
61         int *pid_data;
62         bundle *kb;
63
64         _D("Released");
65
66         if (!ev) {
67                 _D("Invalid event object");
68                 return ECORE_CALLBACK_RENEW;
69         }
70
71         if (strcmp(ev->keyname, X_KEY_BACK) == 0) {
72                 _D("skip back key case");
73                 return ECORE_CALLBACK_RENEW;
74         }
75
76         entry = key_pid_list;
77         if (entry && entry->data) {
78                 pid_data = (int *) entry->data;
79
80                 kb = bundle_create();
81                 bundle_add(kb, AUL_K_MULTI_KEY, ev->keyname);
82                 bundle_add(kb, AUL_K_MULTI_KEY_EVENT, AUL_V_KEY_RELEASED);
83
84                 ret = app_send_cmd_with_noreply(*pid_data, APP_KEY_EVENT, kb);
85                 if (ret < 0)
86                         _E("app_send_cmd failed with error %d\n", ret);
87
88                 bundle_free(kb);
89         }
90
91         return ECORE_CALLBACK_RENEW;
92 }
93
94
95 static Eina_Bool __key_press_cb(void *data, int type, void *event)
96 {
97         Evas_Event_Key_Down *ev = event;
98         int ret;
99         GSList *entry;
100         int *pid_data;
101         bundle *kb;
102
103         _D("Pressed");
104
105         if (!ev) {
106                 _D("Invalid event object");
107                 return ECORE_CALLBACK_RENEW;
108         }
109
110         if (strcmp(ev->keyname, X_KEY_BACK) == 0) {
111                 _D("skip back key case");
112                 return ECORE_CALLBACK_RENEW;
113         }
114
115         entry = key_pid_list;
116         if (entry && entry->data) {
117                 pid_data = (int *) entry->data;
118
119                 kb = bundle_create();
120                 bundle_add(kb, AUL_K_MULTI_KEY, ev->keyname);
121                 bundle_add(kb, AUL_K_MULTI_KEY_EVENT, AUL_V_KEY_PRESSED);
122
123                 ret = app_send_cmd_with_noreply(*pid_data, APP_KEY_EVENT, kb);
124                 if (ret < 0)
125                         _E("app_send_cmd failed with error %d\n", ret);
126
127                 bundle_free(kb);
128         }
129
130         return ECORE_CALLBACK_RENEW;
131 }
132 #endif
133
134 int _register_key_event(int pid)
135 {
136 #ifdef _APPFW_FEATURE_AMD_KEY
137         int *pid_data;
138         GSList *entry;
139
140         pid_data = malloc(sizeof(int));
141         if (pid_data == NULL) {
142                 _E("out of memory");
143                 return -1;
144         }
145
146         *pid_data = pid;
147
148         key_pid_list = g_slist_prepend(key_pid_list, pid_data);
149
150         _D("===key stack===");
151
152         for (entry = key_pid_list; entry; entry = entry->next) {
153                 if (entry->data) {
154                         pid_data = (int *) entry->data;
155                         _D("pid : %d",*pid_data);
156                 }
157         }
158 #endif
159         return 0;
160 }
161
162 int _unregister_key_event(int pid)
163 {
164 #ifdef _APPFW_FEATURE_AMD_KEY
165         GSList *entry;
166         int *pid_data;
167
168         for (entry = key_pid_list; entry;) {
169                 if (entry->data) {
170                         pid_data = (int *) entry->data;
171                         entry = entry->next;
172                         if(pid == *pid_data) {
173                                 key_pid_list = g_slist_remove(key_pid_list, pid_data);
174                                 free(pid_data);
175                         }
176                 }
177         }
178
179         _D("===key stack===");
180
181         for (entry = key_pid_list; entry; entry = entry->next) {
182                 if (entry->data) {
183                         pid_data = (int *) entry->data;
184                         _D("pid : %d",*pid_data);
185                 }
186         }
187 #endif
188         return 0;
189 }
190
191 Ecore_X_Window _input_window_get()
192 {
193         return key_info.win;
194 }
195
196 int _key_init()
197 {
198 #ifdef _APPFW_FEATURE_AMD_KEY
199         key_info.win = ecore_x_window_input_new(ecore_x_window_root_first_get(), 0, 0, 1, 1);
200         if (!key_info.win) {
201                 _D("Failed to create hidden window");
202         }
203
204         _D("_key_init, win : %x", key_info.win);
205
206         ecore_x_icccm_title_set(key_info.win, "acdaemon,key,receiver");
207         ecore_x_netwm_name_set(key_info.win, "acdaemon,key,receiver");
208         ecore_x_netwm_pid_set(key_info.win, getpid());
209
210         ecore_x_window_show(key_info.win);
211
212         utilx_grab_key(ecore_x_display_get(), key_info.win, X_KEY_PLAYCD, SHARED_GRAB);
213         utilx_grab_key(ecore_x_display_get(), key_info.win, X_KEY_STOPCD, SHARED_GRAB);
214         utilx_grab_key(ecore_x_display_get(), key_info.win, X_KEY_PAUSECD, SHARED_GRAB);
215         utilx_grab_key(ecore_x_display_get(), key_info.win, X_KEY_NEXTSONG, SHARED_GRAB);
216         utilx_grab_key(ecore_x_display_get(), key_info.win, X_KEY_PREVIOUSSONG, SHARED_GRAB);
217         utilx_grab_key(ecore_x_display_get(), key_info.win, X_KEY_REWIND, SHARED_GRAB);
218         utilx_grab_key(ecore_x_display_get(), key_info.win, X_KEY_FASTFORWARD, SHARED_GRAB);
219         utilx_grab_key(ecore_x_display_get(), key_info.win, X_KEY_PLAYPAUSE, SHARED_GRAB);
220
221         key_info.key_up = ecore_event_handler_add(ECORE_EVENT_KEY_UP, __key_release_cb, NULL);
222         if (!key_info.key_up) {
223                 _D("Failed to register a key up event handler");
224         }
225
226         key_info.key_down = ecore_event_handler_add(ECORE_EVENT_KEY_DOWN, __key_press_cb, NULL);
227         if (!key_info.key_down) {
228                 _D("Failed to register a key down event handler");
229         }
230 #endif
231         return 0;
232 }
233
234 int _key_grab(const char* key, int grab_mode)
235 {
236 #ifdef _APPFW_FEATURE_AMD_KEY
237         int ret;
238
239         if (!key_info.win) {
240                 _D("There is no created hidden window");
241         }
242
243         _D("_key_grab, win : %x", key_info.win);
244
245         ret = utilx_grab_key(ecore_x_display_get(), key_info.win, key, grab_mode);
246         if(ret < 0) {
247                 _W("fail(%d) to grab key(%s-%d)", ret, key, grab_mode);
248         }
249
250         return ret;
251 #else
252         return 0;
253 #endif
254 }
255
256 int _key_ungrab(const char* key)
257 {
258 #ifdef _APPFW_FEATURE_AMD_KEY
259         int ret;
260
261         if (!key_info.win) {
262                 _D("There is no created hidden window");
263         }
264
265         _D("_key_ungrab, win : %x", key_info.win);
266
267         ret = utilx_ungrab_key(ecore_x_display_get(), key_info.win, key);
268         if(ret < 0) {
269                 _W("fail(%d) to ungrab key(%s)", ret, key);
270         }
271
272         return ret;
273 #else
274         return 0;
275 #endif
276 }