2 * Copyright (c) 2015 Samsung Electronics Co., Ltd. All rights reserved.
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
8 * http://floralicense.org/license/
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.
18 #include <atspi/atspi.h>
23 #include "keyboard_tracker.h"
25 #include "screen_reader_tts.h"
26 static AtspiDeviceListener *listener;
27 static Keyboard_Tracker_Cb user_cb;
28 static void *user_data;
30 static Ecore_Event_Handler *root_xwindow_property_changed_hld = NULL;
31 static Ecore_Event_Handler *active_xwindow_property_changed_hld = NULL;
33 static void _check_keyboard_state(Ecore_X_Window keyboard_win)
35 Ecore_X_Virtual_Keyboard_State keyboard_state;
36 static Ecore_X_Virtual_Keyboard_State last_keyboard_state = ECORE_X_VIRTUAL_KEYBOARD_STATE_OFF;
43 keyboard_state = ecore_x_e_virtual_keyboard_state_get(keyboard_win);
44 if (keyboard_state == last_keyboard_state)
49 if (keyboard_state == ECORE_X_VIRTUAL_KEYBOARD_STATE_ON)
51 tts_speak (_("IDS_VISUAL_KEYBOARD_ENABLED"), EINA_FALSE);
52 last_keyboard_state = keyboard_state;
54 else if (keyboard_state == ECORE_X_VIRTUAL_KEYBOARD_STATE_OFF)
56 tts_speak (_("IDS_VISUAL_KEYBOARD_DISABLED"), EINA_FALSE);
57 last_keyboard_state = keyboard_state;
61 static Eina_Bool _active_xwindow_property_changed_cb(void *data, int type, void *event)
63 Ecore_X_Event_Window_Property *wp;
64 wp = (Ecore_X_Event_Window_Property *)event;
71 if (wp->atom == ECORE_X_ATOM_E_VIRTUAL_KEYBOARD_STATE)
73 DEBUG("keyboard state event");
74 _check_keyboard_state(wp->win);
80 void active_xwindow_property_tracker_register()
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);
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);
91 void active_xwindow_property_tracker_unregister()
93 if (active_xwindow_property_changed_hld)
95 ecore_event_handler_del(active_xwindow_property_changed_hld);
96 active_xwindow_property_changed_hld = NULL;
100 static Eina_Bool _root_xwindow_property_changed_cb(void *data, int type, void *event)
102 Ecore_X_Event_Window_Property *wp;
103 wp = (Ecore_X_Event_Window_Property *)event;
110 if (wp->atom == ECORE_X_ATOM_NET_ACTIVE_WINDOW)
112 DEBUG("active window change");
113 active_xwindow_property_tracker_unregister();
114 active_xwindow_property_tracker_register();
120 void root_xwindow_property_tracker_register()
122 Ecore_X_Window root_window;
124 root_window = ecore_x_window_root_first_get();
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);
132 void root_xwindow_property_tracker_unregister()
134 if (root_xwindow_property_changed_hld)
136 ecore_event_handler_del(root_xwindow_property_changed_hld);
137 root_xwindow_property_changed_hld = NULL;
141 static gboolean device_cb(const AtspiDeviceEvent * stroke, void *data)
144 if (!strcmp(stroke->event_string, "KP_Up"))
146 else if (!strcmp(stroke->event_string, "KP_Down"))
148 else if (!strcmp(stroke->event_string, "KP_Left"))
150 else if (!strcmp(stroke->event_string, "KP_Right"))
156 user_cb(user_data, k);
161 void keyboard_tracker_init(void)
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);
166 active_xwindow_property_tracker_register();
167 root_xwindow_property_tracker_register();
169 DEBUG("keyboard tracker init");
172 void keyboard_tracker_register(Keyboard_Tracker_Cb cb, void *data)
178 void keyboard_tracker_shutdown(void)
180 atspi_deregister_keystroke_listener(listener, NULL, 0, ATSPI_KEY_PRESSED, NULL);
182 root_xwindow_property_tracker_unregister();
183 active_xwindow_property_tracker_unregister();
185 DEBUG("keyboard tracker shutdown");