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