mobile-lua: add additional LABELLED_BY relation
[profile/tv/apps/native/screen-reader.git] / src / keyboard_tracker.c
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd. All rights reserved.
3  *
4  * Licensed under the Flora License, Version 1.1 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://floralicense.org/license/
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <string.h>
18 #include <atspi/atspi.h>
19 #include <Ecore.h>
20 #ifdef X11_ENABLED
21 #include <Ecore_X.h>
22 #endif
23 #include "keyboard_tracker.h"
24 #include "logger.h"
25 #include "screen_reader_tts.h"
26 static AtspiDeviceListener *listener;
27 static Keyboard_Tracker_Cb user_cb;
28 static void *user_data;
29 #ifdef X11_ENABLED
30 static Ecore_Event_Handler *root_xwindow_property_changed_hld = NULL;
31 static Ecore_Event_Handler *active_xwindow_property_changed_hld = NULL;
32
33 static void _check_keyboard_state(Ecore_X_Window keyboard_win)
34 {
35         Ecore_X_Virtual_Keyboard_State keyboard_state;
36         static Ecore_X_Virtual_Keyboard_State last_keyboard_state = ECORE_X_VIRTUAL_KEYBOARD_STATE_OFF;
37
38         if (!keyboard_win)
39         {
40                 return;
41         }
42
43         keyboard_state = ecore_x_e_virtual_keyboard_state_get(keyboard_win);
44         if (keyboard_state == last_keyboard_state)
45         {
46                 return;
47         }
48
49         if (keyboard_state == ECORE_X_VIRTUAL_KEYBOARD_STATE_ON)
50         {
51                 tts_speak (_("IDS_VISUAL_KEYBOARD_ENABLED"), EINA_FALSE);
52                 last_keyboard_state = keyboard_state;
53         }
54         else if (keyboard_state == ECORE_X_VIRTUAL_KEYBOARD_STATE_OFF)
55         {
56                 tts_speak (_("IDS_VISUAL_KEYBOARD_DISABLED"), EINA_FALSE);
57                 last_keyboard_state = keyboard_state;
58         }
59 }
60
61 static Eina_Bool _active_xwindow_property_changed_cb(void *data, int type, void *event)
62 {
63         Ecore_X_Event_Window_Property *wp;
64         wp = (Ecore_X_Event_Window_Property *)event;
65
66         if (!wp)
67         {
68                 return EINA_FALSE;
69         }
70
71         if (wp->atom == ECORE_X_ATOM_E_VIRTUAL_KEYBOARD_STATE)
72         {
73                 DEBUG("keyboard state event");
74                 _check_keyboard_state(wp->win);
75         }
76
77         return EINA_TRUE;
78 }
79
80 void active_xwindow_property_tracker_register()
81 {
82         Ecore_X_Window active_window = 0;
83         ecore_x_window_prop_xid_get(ecore_x_window_root_first_get(), ECORE_X_ATOM_NET_ACTIVE_WINDOW, ECORE_X_ATOM_WINDOW, &active_window, 1);
84         if (active_window)
85         {
86                 ecore_x_event_mask_set(active_window, ECORE_X_EVENT_MASK_WINDOW_PROPERTY);
87                 active_xwindow_property_changed_hld = ecore_event_handler_add(ECORE_X_EVENT_WINDOW_PROPERTY, _active_xwindow_property_changed_cb, NULL);
88         }
89 }
90
91 void active_xwindow_property_tracker_unregister()
92 {
93         if (active_xwindow_property_changed_hld)
94         {
95                 ecore_event_handler_del(active_xwindow_property_changed_hld);
96                 active_xwindow_property_changed_hld = NULL;
97         }
98 }
99
100 static Eina_Bool _root_xwindow_property_changed_cb(void *data, int type, void *event)
101 {
102         Ecore_X_Event_Window_Property *wp;
103         wp = (Ecore_X_Event_Window_Property *)event;
104
105         if (!wp)
106         {
107                 return EINA_FALSE;
108         }
109
110         if (wp->atom == ECORE_X_ATOM_NET_ACTIVE_WINDOW)
111         {
112                 DEBUG("active window change");
113                 active_xwindow_property_tracker_unregister();
114                 active_xwindow_property_tracker_register();
115         }
116
117         return EINA_TRUE;
118 }
119
120 void root_xwindow_property_tracker_register()
121 {
122         Ecore_X_Window root_window;
123
124         root_window = ecore_x_window_root_first_get();
125         if (root_window)
126         {
127                 ecore_x_event_mask_set(root_window, ECORE_X_EVENT_MASK_WINDOW_PROPERTY);
128                 root_xwindow_property_changed_hld = ecore_event_handler_add(ECORE_X_EVENT_WINDOW_PROPERTY, _root_xwindow_property_changed_cb, NULL);
129         }
130 }
131
132 void root_xwindow_property_tracker_unregister()
133 {
134         if (root_xwindow_property_changed_hld)
135         {
136                 ecore_event_handler_del(root_xwindow_property_changed_hld);
137                 root_xwindow_property_changed_hld = NULL;
138         }
139 }
140 #endif
141 static gboolean device_cb(const AtspiDeviceEvent * stroke, void *data)
142 {
143         Key k;
144         if (!strcmp(stroke->event_string, "KP_Up"))
145                 k = KEY_UP;
146         else if (!strcmp(stroke->event_string, "KP_Down"))
147                 k = KEY_DOWN;
148         else if (!strcmp(stroke->event_string, "KP_Left"))
149                 k = KEY_LEFT;
150         else if (!strcmp(stroke->event_string, "KP_Right"))
151                 k = KEY_RIGHT;
152         else
153                 return FALSE;
154
155         if (user_cb)
156                 user_cb(user_data, k);
157
158         return TRUE;
159 }
160
161 void keyboard_tracker_init(void)
162 {
163         listener = atspi_device_listener_new(device_cb, NULL, NULL);
164         atspi_register_keystroke_listener(listener, NULL, 0, ATSPI_KEY_PRESSED, ATSPI_KEYLISTENER_SYNCHRONOUS | ATSPI_KEYLISTENER_CANCONSUME, NULL);
165 #ifdef X11_ENABLED
166         active_xwindow_property_tracker_register();
167         root_xwindow_property_tracker_register();
168 #endif
169         DEBUG("keyboard tracker init");
170 }
171
172 void keyboard_tracker_register(Keyboard_Tracker_Cb cb, void *data)
173 {
174         user_cb = cb;
175         user_data = data;
176 }
177
178 void keyboard_tracker_shutdown(void)
179 {
180         atspi_deregister_keystroke_listener(listener, NULL, 0, ATSPI_KEY_PRESSED, NULL);
181 #ifdef X11_ENABLED
182         root_xwindow_property_tracker_unregister();
183         active_xwindow_property_tracker_unregister();
184 #endif
185         DEBUG("keyboard tracker shutdown");
186 }