[LICENSE] change to Flora-1.1 license
[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  * This file is a modified version of BSD licensed file and
5  * licensed under the Flora License, Version 1.1 (the License);
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://floralicense.org/license/
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an AS IS BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * Please, see the LICENSE file for the original copyright owner and
18  * license.
19  */
20
21 #include <string.h>
22 #include <atspi/atspi.h>
23 #include "keyboard_tracker.h"
24 #include "logger.h"
25
26 static AtspiDeviceListener *listener;
27 static Keyboard_Tracker_Cb user_cb;
28 static void *user_data;
29
30 static gboolean device_cb(const AtspiDeviceEvent *stroke, void *data)
31 {
32    Key k;
33    if (!strcmp(stroke->event_string, "KP_Up"))
34       k = KEY_UP;
35    else if (!strcmp(stroke->event_string, "KP_Down"))
36       k = KEY_DOWN;
37    else if (!strcmp(stroke->event_string, "KP_Left"))
38       k = KEY_LEFT;
39    else if (!strcmp(stroke->event_string, "KP_Right"))
40       k = KEY_RIGHT;
41    else
42       return FALSE;
43
44    if(user_cb)
45       user_cb(user_data, k);
46
47    return TRUE;
48 }
49
50 void keyboard_tracker_init(void)
51 {
52    atspi_init();
53    listener =  atspi_device_listener_new(device_cb, NULL, NULL);
54    atspi_register_keystroke_listener(listener, NULL, 0, ATSPI_KEY_PRESSED, ATSPI_KEYLISTENER_SYNCHRONOUS|ATSPI_KEYLISTENER_CANCONSUME, NULL);
55    DEBUG("keyboard tracker init");
56 }
57
58 void keyboard_tracker_register(Keyboard_Tracker_Cb cb, void *data)
59 {
60    user_cb = cb;
61    user_data = data;
62 }
63
64 void keyboard_tracker_shutdown(void)
65 {
66    atspi_deregister_keystroke_listener(listener, NULL, 0, ATSPI_KEY_PRESSED, NULL);
67    DEBUG("keyboard tracker shutdown");
68 }