tizen 2.3.1 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 #include <Ecore_X.h>
23 #include <Ecore_Input.h>
24 #include <utilX.h>
25 #include <Ecore.h>
26 #include <Evas.h>
27 #include <aul.h>
28 #include <glib.h>
29
30 #include "amd_config.h"
31 #include "amd_key.h"
32 #include "simple_util.h"
33 #include "app_sock.h"
34 #include "launch.h"
35
36 static struct {
37         Ecore_X_Window win;
38         Ecore_Event_Handler *key_up;
39         Ecore_Event_Handler *key_down;
40 } key_info = {
41         .win = NULL,
42         .key_up = NULL,
43         .key_down = NULL,
44 };
45
46 GSList *key_pid_list = NULL;
47
48 static Eina_Bool __key_release_cb(void *data, int type, void *event);
49 static Eina_Bool __key_press_cb(void *data, int type, void *event);
50
51 static Eina_Bool __key_release_cb(void *data, int type, void *event)
52 {
53         Evas_Event_Key_Up *ev = event;
54         int ret;
55         GSList *entry;
56         int *pid_data;
57         bundle *kb;
58
59         _D("Released");
60
61         if (!ev) {
62                 _D("Invalid event object");
63                 return ECORE_CALLBACK_RENEW;
64         }
65
66         entry = key_pid_list;
67         if (entry && entry->data) {
68                 pid_data = (int *) entry->data;
69
70                 kb = bundle_create();
71                 bundle_add(kb, AUL_K_MULTI_KEY, ev->keyname);
72                 bundle_add(kb, AUL_K_MULTI_KEY_EVENT, AUL_V_KEY_RELEASED);
73
74                 ret = app_send_cmd_with_noreply(*pid_data, APP_KEY_EVENT, kb);
75                 if (ret < 0)
76                         _E("app_send_cmd failed with error %d\n", ret);
77
78                 bundle_free(kb);
79         }
80
81         return ECORE_CALLBACK_RENEW;
82 }
83
84
85 static Eina_Bool __key_press_cb(void *data, int type, void *event)
86 {
87         Evas_Event_Key_Down *ev = event;
88         int ret;
89         GSList *entry;
90         int *pid_data;
91         bundle *kb;
92
93         _D("Pressed");
94
95         if (!ev) {
96                 _D("Invalid event object");
97                 return ECORE_CALLBACK_RENEW;
98         }
99
100         entry = key_pid_list;
101         if (entry && entry->data) {
102                 pid_data = (int *) entry->data;
103
104                 kb = bundle_create();
105                 bundle_add(kb, AUL_K_MULTI_KEY, ev->keyname);
106                 bundle_add(kb, AUL_K_MULTI_KEY_EVENT, AUL_V_KEY_PRESSED);
107
108                 ret = app_send_cmd_with_noreply(*pid_data, APP_KEY_EVENT, kb);
109                 if (ret < 0)
110                         _E("app_send_cmd failed with error %d\n", ret);
111
112                 bundle_free(kb);
113         }
114
115         return ECORE_CALLBACK_RENEW;
116 }
117
118 int _register_key_event(int pid)
119 {
120         int *pid_data;
121         GSList *entry;
122
123         pid_data = malloc(sizeof(int));
124         if (pid_data == NULL) {
125                 _E("out of memory");
126                 return -1;
127         }
128
129         *pid_data = pid;
130
131         key_pid_list = g_slist_prepend(key_pid_list, pid_data);
132
133         _D("===key stack===");
134
135         for (entry = key_pid_list; entry; entry = entry->next) {
136                 if (entry->data) {
137                         pid_data = (int *) entry->data;
138                         _D("pid : %d",*pid_data);
139                 }
140         }
141
142         return 0;
143 }
144
145 int _unregister_key_event(int pid)
146 {
147         GSList *entry;
148         int *pid_data;
149
150         for (entry = key_pid_list; entry;) {
151                 if (entry->data) {
152                         pid_data = (int *) entry->data;
153                         entry = entry->next;
154                         if(pid == *pid_data) {
155                                 key_pid_list = g_slist_remove(key_pid_list, pid_data);
156                                 free(pid_data);
157                         }
158                 }
159         }
160
161         _D("===key stack===");
162
163         for (entry = key_pid_list; entry; entry = entry->next) {
164                 if (entry->data) {
165                         pid_data = (int *) entry->data;
166                         _D("pid : %d",*pid_data);
167                 }
168         }
169
170         return 0;
171 }
172
173 Ecore_X_Window _input_window_get()
174 {
175         return key_info.win;
176 }
177
178 int _key_init()
179 {
180         key_info.win = ecore_x_window_input_new(ecore_x_window_root_first_get(), 0, 0, 1, 1);
181         if (!key_info.win) {
182                 _D("Failed to create hidden window");
183         }
184
185         _D("_key_init, win : %x", key_info.win);
186
187         ecore_x_icccm_title_set(key_info.win, "acdaemon,key,receiver");
188         ecore_x_netwm_name_set(key_info.win, "acdaemon,key,receiver");
189         ecore_x_netwm_pid_set(key_info.win, getpid());
190
191         ecore_x_window_show(key_info.win);
192
193         utilx_grab_key(ecore_x_display_get(), key_info.win, KEY_PLAYCD, SHARED_GRAB);
194         utilx_grab_key(ecore_x_display_get(), key_info.win, KEY_STOPCD, SHARED_GRAB);
195         utilx_grab_key(ecore_x_display_get(), key_info.win, KEY_PAUSECD, SHARED_GRAB);
196         utilx_grab_key(ecore_x_display_get(), key_info.win, KEY_NEXTSONG, SHARED_GRAB);
197         utilx_grab_key(ecore_x_display_get(), key_info.win, KEY_PREVIOUSSONG, SHARED_GRAB);
198         utilx_grab_key(ecore_x_display_get(), key_info.win, KEY_REWIND, SHARED_GRAB);
199         utilx_grab_key(ecore_x_display_get(), key_info.win, KEY_FASTFORWARD, SHARED_GRAB);
200         utilx_grab_key(ecore_x_display_get(), key_info.win, KEY_PLAYPAUSE, SHARED_GRAB);
201
202         key_info.key_up = ecore_event_handler_add(ECORE_EVENT_KEY_UP, __key_release_cb, NULL);
203         if (!key_info.key_up) {
204                 _D("Failed to register a key up event handler");
205         }
206
207         key_info.key_down = ecore_event_handler_add(ECORE_EVENT_KEY_DOWN, __key_press_cb, NULL);
208         if (!key_info.key_down) {
209                 _D("Failed to register a key down event handler");
210         }
211
212         return 0;
213 }
214