2 * Copyright (c) 2015 Samsung Electronics Co., Ltd. All rights reserved.
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
9 * http://floralicense.org/license/
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.
17 * Please, see the LICENSE file for the original copyright owner and
22 #include "window_tracker.h"
25 static Window_Tracker_Cb user_cb;
26 static void *user_data;
27 static AtspiEventListener *listener;
28 static AtspiAccessible *last_active_win;
31 _on_atspi_window_cb(const AtspiEvent *event)
33 ERROR("Event: %s: %s", event->type, atspi_accessible_get_name(event->source, NULL));
35 if ( !strcmp(event->type, "window:activate") &&
36 last_active_win != event->source) //if we got activate 2 times
39 if (user_cb) user_cb(user_data, event->source);
40 last_active_win = event->source;
44 static AtspiAccessible*
49 last_active_win = NULL;
50 AtspiAccessible *desktop = atspi_get_desktop(0);
52 ERROR("DESKTOP NOT FOUND");
54 for (i = 0; i < atspi_accessible_get_child_count(desktop, NULL); i++)
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++)
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);
63 if ((atspi_state_set_contains(states, ATSPI_STATE_ACTIVE)) && (role == ATSPI_ROLE_WINDOW))
65 g_object_unref(states);
66 last_active_win = win;
68 return last_active_win;
76 void window_tracker_init(void)
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);
85 void window_tracker_shutdown(void)
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);
95 last_active_win = NULL;
98 void window_tracker_register(Window_Tracker_Cb cb, void *data)
105 void window_tracker_active_window_request(void)
109 if (user_cb) user_cb(user_data, last_active_win);