~azzar1/unity/fix-crash-acc-controller

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/*
 * Copyright (C) 2011 Canonical Ltd
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Authored by: Alejandro PiƱeiro Iglesias <apinheiro@igalia.com>
 */

/**
 * SECTION:unity-accessible-root
 * @short_description: Root object for the UNITY accessible support
 *
 * #UnityRootAccessible is the root object of the accessibility
 * tree-like hierarchy, exposing the application level. You can see it
 * as the one exposing UnityScreen information to the a11y framework
 *
 */

#include "unity-root-accessible.h"
#include "unity-util-accessible.h"
#include "nux-base-window-accessible.h"
#include "unitya11y.h"

#include <UnityCore/Variant.h>

#include "UBusWrapper.h"
#include "UBusMessages.h"

/* GObject */
static void unity_root_accessible_class_init(UnityRootAccessibleClass* klass);
static void unity_root_accessible_init(UnityRootAccessible* root);
static void unity_root_accessible_finalize(GObject* object);

/* AtkObject.h */
static void       unity_root_accessible_initialize(AtkObject* accessible,
                                                   gpointer   data);
static gint       unity_root_accessible_get_n_children(AtkObject* obj);
static AtkObject* unity_root_accessible_ref_child(AtkObject* obj,
                                                  gint i);
static AtkObject* unity_root_accessible_get_parent(AtkObject* obj);
/* private */
static void       check_active_window(UnityRootAccessible* self);
static void       register_interesting_messages(UnityRootAccessible* self);
static void       add_window(UnityRootAccessible* self,
                             nux::BaseWindow* window);
static void       remove_window(UnityRootAccessible* self,
                                nux::BaseWindow* window);

#define UNITY_ROOT_ACCESSIBLE_GET_PRIVATE(obj)                          \
  (G_TYPE_INSTANCE_GET_PRIVATE ((obj), UNITY_TYPE_ROOT_ACCESSIBLE, UnityRootAccessiblePrivate))

G_DEFINE_TYPE(UnityRootAccessible, unity_root_accessible,  ATK_TYPE_OBJECT)

struct _UnityRootAccessiblePrivate
{
  /* we save on window_list the accessible object for the windows
     registered */
  GSList* window_list;
  nux::BaseWindow* active_window;
  nux::BaseWindow* launcher_window;
};

static void
unity_root_accessible_class_init(UnityRootAccessibleClass* klass)
{
  GObjectClass* gobject_class = G_OBJECT_CLASS(klass);
  AtkObjectClass* atk_class = ATK_OBJECT_CLASS(klass);

  gobject_class->finalize = unity_root_accessible_finalize;

  /* AtkObject */
  atk_class->get_n_children = unity_root_accessible_get_n_children;
  atk_class->ref_child = unity_root_accessible_ref_child;
  atk_class->get_parent = unity_root_accessible_get_parent;
  atk_class->initialize = unity_root_accessible_initialize;

  g_type_class_add_private(gobject_class, sizeof(UnityRootAccessiblePrivate));
}

static void
unity_root_accessible_init(UnityRootAccessible*      root)
{
  root->priv = UNITY_ROOT_ACCESSIBLE_GET_PRIVATE(root);

  root->priv->window_list = NULL;
  root->priv->active_window = NULL;
  root->priv->launcher_window = NULL;
}

AtkObject*
unity_root_accessible_new(void)
{
  AtkObject* accessible = NULL;

  accessible = ATK_OBJECT(g_object_new(UNITY_TYPE_ROOT_ACCESSIBLE, NULL));

  atk_object_initialize(accessible, NULL);

  return accessible;
}

static void
unity_root_accessible_finalize(GObject* object)
{
  UnityRootAccessible* root = UNITY_ROOT_ACCESSIBLE(object);

  g_return_if_fail(UNITY_IS_ROOT_ACCESSIBLE(object));

  if (root->priv->window_list)
  {
    g_slist_free_full(root->priv->window_list, g_object_unref);
    root->priv->window_list = NULL;
  }

  G_OBJECT_CLASS(unity_root_accessible_parent_class)->finalize(object);
}

/* AtkObject.h */
static void
unity_root_accessible_initialize(AtkObject* accessible,
                                 gpointer data)
{
  accessible->role = ATK_ROLE_APPLICATION;

  // FIXME: compiz doesn't set the program name using g_set_prgname,
  // and AFAIK, there isn't a way to get it. Requires further investigation.
  // accessible->name = g_get_prgname();
  atk_object_set_name(accessible, "unity");
  atk_object_set_parent(accessible, NULL);

  register_interesting_messages(UNITY_ROOT_ACCESSIBLE(accessible));

  ATK_OBJECT_CLASS(unity_root_accessible_parent_class)->initialize(accessible, data);
}

static gint
unity_root_accessible_get_n_children(AtkObject* obj)
{
  UnityRootAccessible* root = UNITY_ROOT_ACCESSIBLE(obj);

  return g_slist_length(root->priv->window_list);
}

static AtkObject*
unity_root_accessible_ref_child(AtkObject* obj,
                                gint i)
{
  UnityRootAccessible* root = NULL;
  gint num = 0;
  AtkObject* item = NULL;

  root = UNITY_ROOT_ACCESSIBLE(obj);
  num = atk_object_get_n_accessible_children(obj);
  g_return_val_if_fail((i < num) && (i >= 0), NULL);

  item = ATK_OBJECT(g_slist_nth_data(root->priv->window_list, i));

  if (!item)
    return NULL;

  g_object_ref(item);

  return item;
}

static AtkObject*
unity_root_accessible_get_parent(AtkObject* obj)
{
  return NULL;
}


/* private */
/*
 * Call all the children (NuxBaseWindowAccessible) to check if they
 * are in the proper active or deactive status.
 */
static void
check_active_window(UnityRootAccessible* self)
{
  GSList* iter = NULL;
  NuxBaseWindowAccessible* window = NULL;

  for (iter = self->priv->window_list; iter != NULL; iter = g_slist_next(iter))
  {
    window = NUX_BASE_WINDOW_ACCESSIBLE(iter->data);
    nux_base_window_accessible_check_active(window, self->priv->active_window);
  }
}

/*
 * It adds a window to the internal window_list managed by the Root object
 *
 * Checks if the object is already present. Adds a reference to the
 * accessible object of the window.
 */
static void
add_window(UnityRootAccessible* self,
           nux::BaseWindow* window)
{
  AtkObject* window_accessible = NULL;
  gint index = 0;

  g_return_if_fail(UNITY_IS_ROOT_ACCESSIBLE(self));

  window_accessible =
    unity_a11y_get_accessible(window);

  /* FIXME: temporal */
  atk_object_set_name (window_accessible, window->GetWindowName().c_str());

  if (g_slist_find(self->priv->window_list, window_accessible))
    return;

  self->priv->window_list =
    g_slist_append(self->priv->window_list, window_accessible);
  g_object_ref(window_accessible);

  index = g_slist_index(self->priv->window_list, window_accessible);

  explore_children(window_accessible);

  g_signal_emit_by_name(self, "children-changed::add",
                        index, window_accessible, NULL);
}


/*
 * It removes the window to the internal window_list managed by the
 * Root object
 *
 * Checks if the object is already present. Removes a reference to the
 * accessible object of the window.
 */
static void
remove_window(UnityRootAccessible* self,
              nux::BaseWindow* window)
{
  AtkObject* window_accessible = NULL;
  gint index = 0;

  g_return_if_fail(UNITY_IS_ROOT_ACCESSIBLE(self));

  window_accessible =
    unity_a11y_get_accessible(window);

  return;
  
  if (!g_slist_find(self->priv->window_list, window_accessible))
    return;

  index = g_slist_index(self->priv->window_list, window_accessible);

  self->priv->window_list =
    g_slist_remove(self->priv->window_list, window_accessible);
  g_object_unref(window_accessible);

  g_signal_emit_by_name(self, "children-changed::remove",
                        index, window_accessible, NULL);
}

static void
set_active_window(UnityRootAccessible* self,
                  nux::BaseWindow* window)
{
  g_return_if_fail(UNITY_IS_ROOT_ACCESSIBLE(self));
  g_return_if_fail(window != NULL);

  self->priv->active_window = window;
  check_active_window(self);
}

nux::BaseWindow*
search_for_launcher_window(UnityRootAccessible* self)
{
  GSList*iter = NULL;
  nux::Object* nux_object = NULL;
  nux::BaseWindow* bwindow = NULL;
  NuxObjectAccessible* accessible = NULL;
  gboolean found = FALSE;

  for (iter = self->priv->window_list; iter != NULL; iter = g_slist_next(iter))
  {
    accessible = NUX_OBJECT_ACCESSIBLE(iter->data);

    nux_object = nux_object_accessible_get_object(accessible);
    bwindow = dynamic_cast<nux::BaseWindow*>(nux_object);

    if ((bwindow!= NULL) && (g_strcmp0(bwindow->GetWindowName().c_str(), "LauncherWindow") == 0))
    {
      found = TRUE;
      break;
    }
  }

  if (found)
    return bwindow;
  else
    return NULL;
}

static void
ubus_launcher_register_interest_cb(unity::glib::Variant const& variant,
                                   UnityRootAccessible* self)
{
  //launcher window is the same during all the life of Unity
  if (self->priv->launcher_window == NULL)
    self->priv->launcher_window = search_for_launcher_window(self);

  //launcher window became the active window
  set_active_window(self, self->priv->launcher_window);
}


static void
wc_change_visibility_window_cb(nux::BaseWindow* window,
                               UnityRootAccessible* self,
                               gboolean visible)
{
  if (visible)
  {
    add_window(self, window);
    //for the dash and quicklist
    set_active_window(self, window);
  }
  else
  {
    AtkObject* accessible = NULL;

    accessible = unity_a11y_get_accessible(window);
    nux_base_window_accessible_check_active(NUX_BASE_WINDOW_ACCESSIBLE(accessible),
                                            NULL);
    remove_window(self, window);
  }
}

static void
register_interesting_messages(UnityRootAccessible* self)
{
  static unity::UBusManager ubus_manager;

  ubus_manager.RegisterInterest(UBUS_LAUNCHER_START_KEY_NAV,
                                sigc::bind(sigc::ptr_fun(ubus_launcher_register_interest_cb),
                                self));

  ubus_manager.RegisterInterest(UBUS_LAUNCHER_START_KEY_SWITCHER,
                                sigc::bind(sigc::ptr_fun(ubus_launcher_register_interest_cb),
                                self));

  nux::GetWindowCompositor().sigVisibleViewWindow.
  connect(sigc::bind(sigc::ptr_fun(wc_change_visibility_window_cb), self, TRUE));

  nux::GetWindowCompositor().sigHiddenViewWindow.
  connect(sigc::bind(sigc::ptr_fun(wc_change_visibility_window_cb), self, FALSE));
}