[LICENSE] change to Flora-1.1 license
[profile/tv/apps/native/screen-reader.git] / src / window_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 "window_tracker.h"
23 #include "logger.h"
24
25 static Window_Tracker_Cb user_cb;
26 static void *user_data;
27 static AtspiEventListener *listener;
28 static AtspiAccessible *last_active_win;
29
30 static void
31 _on_atspi_window_cb(const AtspiEvent *event)
32 {
33    ERROR("Event: %s: %s", event->type, atspi_accessible_get_name(event->source, NULL));
34
35    if ( !strcmp(event->type, "window:activate") &&
36          last_active_win != event->source) //if we got activate 2 times
37       {
38
39          if (user_cb) user_cb(user_data, event->source);
40          last_active_win = event->source;
41       }
42 }
43
44 static AtspiAccessible*
45 _get_active_win(void)
46 {
47    DEBUG("START");
48    int i, j;
49    last_active_win = NULL;
50    AtspiAccessible *desktop = atspi_get_desktop(0);
51    if (!desktop)
52       ERROR("DESKTOP NOT FOUND");
53
54    for (i = 0; i < atspi_accessible_get_child_count(desktop, NULL); i++)
55       {
56          AtspiAccessible *app = atspi_accessible_get_child_at_index(desktop, i, NULL);
57          for (j = 0; j < atspi_accessible_get_child_count(app, NULL); j++)
58             {
59                AtspiAccessible *win = atspi_accessible_get_child_at_index(app, j, NULL);
60                AtspiStateSet *states = atspi_accessible_get_state_set(win);
61                AtspiRole role = atspi_accessible_get_role(win, NULL);
62
63                if ((atspi_state_set_contains(states, ATSPI_STATE_ACTIVE)) && (role == ATSPI_ROLE_WINDOW))
64                   {
65                      g_object_unref(states);
66                      last_active_win = win;
67                      DEBUG("END");
68                      return last_active_win;
69                   }
70             }
71       }
72    ERROR("END");
73    return NULL;
74 }
75
76 void window_tracker_init(void)
77 {
78    DEBUG("START");
79    listener = atspi_event_listener_new_simple(_on_atspi_window_cb, NULL);
80    atspi_event_listener_register(listener, "window:create", NULL);
81    atspi_event_listener_register(listener, "window:activate", NULL);
82    atspi_event_listener_register(listener, "window:restore", NULL);
83 }
84
85 void window_tracker_shutdown(void)
86 {
87    DEBUG("START");
88    atspi_event_listener_deregister(listener, "window:create", NULL);
89    atspi_event_listener_deregister(listener, "window:activate", NULL);
90    atspi_event_listener_deregister(listener, "window:restore", NULL);
91    g_object_unref(listener);
92    listener = NULL;
93    user_cb = NULL;
94    user_data = NULL;
95    last_active_win = NULL;
96 }
97
98 void window_tracker_register(Window_Tracker_Cb cb, void *data)
99 {
100    DEBUG("START");
101    user_cb = cb;
102    user_data = data;
103 }
104
105 void window_tracker_active_window_request(void)
106 {
107    DEBUG("START");
108    _get_active_win();
109    if (user_cb) user_cb(user_data, last_active_win);
110 }