summaryrefslogtreecommitdiff
path: root/atspi/atspi-stateset.c
blob: be71aae7a6c0f9af25f151becf17ee52749ba660 (plain)
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
/*
 * AT-SPI - Assistive Technology Service Provider Interface
 * (Gnome Accessibility Project; http://developer.gnome.org/projects/gap)
 *
 * Copyright 2001, 2002 Sun Microsystems Inc.,
 * Copyright 2001, 2002 Ximian, Inc.
 * Copyright 2010, 2011 Novell, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include "atspi-private.h"

/**
 * AtspiStateSet:
 *
 * The atspi-stateset objects implement wrappers around a
 * bitmap of accessible states.
 */

static void atspi_state_set_class_init (AtspiStateSetClass *klass);

G_DEFINE_TYPE (AtspiStateSet, atspi_state_set, G_TYPE_OBJECT)

static void
atspi_state_set_init (AtspiStateSet *set)
{
  set->states = 0;
}

static void
atspi_state_set_class_init (AtspiStateSetClass *klass)
{
}

/**
 * atspi_state_set_new:
 * @states: (element-type AtspiStateType): An array of states with which the
 *          method initializes the state set.
 *
 * Generates an #AtspiStateSet with the given @states.
 *
 * Returns: A new #AtspiStateSet with the given states.
 **/
AtspiStateSet *
atspi_state_set_new (GArray *states)
{
  AtspiStateSet *set = g_object_new (ATSPI_TYPE_STATE_SET, NULL);
  gint i;

  if (!set || !states)
    return set;

  for (i = 0; i < states->len; i++)
    atspi_state_set_add (set, g_array_index (states, AtspiStateType, i));
  return set;
}

AtspiStateSet *
_atspi_state_set_new_internal (AtspiAccessible *accessible, gint64 states)
{
  AtspiStateSet *set;

  set = g_object_new (ATSPI_TYPE_STATE_SET, NULL);
  g_return_val_if_fail (set != NULL, NULL);

  set->accessible = accessible;
  set->states = states;
  return set;
}

/**
 * atspi_state_set_set_by_name:
 * @set: a pointer to the #AtspiStateSet object on which to operate.
 * @name: a string corresponding to a state name.
 * @enabled: if #TRUE, @name should be enabled in the @set in question;
 *          otherwise, it should be disabled.
 *
 * Enables/disables a state in an #AtspiStateSet according to its @name.
 **/
void
atspi_state_set_set_by_name (AtspiStateSet *set, const gchar *name, gboolean enabled)
{
  GTypeClass *type_class;
  GEnumValue *value;

  if (set->accessible &&
      !(set->accessible->cached_properties & ATSPI_CACHE_STATES))
    return;

  type_class = g_type_class_ref (ATSPI_TYPE_STATE_TYPE);

  value = g_enum_get_value_by_nick (G_ENUM_CLASS (type_class), name);

  if (!value)
    {
      g_warning ("AT-SPI: Attempt to set unknown state '%s'", name);
    }
  else if (enabled)
    set->states |= ((gint64) 1 << value->value);
  else
    set->states &= ~((gint64) 1 << value->value);

  g_type_class_unref (type_class);
}

static void
refresh_states (AtspiStateSet *set)
{
  GArray *state_array;
  dbus_uint32_t *states;

  if (!set->accessible ||
      (set->accessible->cached_properties & ATSPI_CACHE_STATES))
    return;

  if (!_atspi_dbus_call (set->accessible, atspi_interface_accessible, "GetState", NULL, "=>au", &state_array))
    return;

  states = (dbus_uint32_t *) state_array->data;

  set->states = ((gint64) states[1]) << 32;
  set->states |= (gint64) states[0];
  g_array_free (state_array, TRUE);
}

/**
 * atspi_state_set_add:
 * @set: a pointer to the #AtspiStateSet object on which to operate.
 * @state: an #AtspiStateType to be added to the specified #AtspiStateSet.
 *
 * Adds a particular #AtspiState to an #AtspiStateSet (i.e. sets the
 *       given state to #TRUE in the stateset).
 *
 **/
void
atspi_state_set_add (AtspiStateSet *set, AtspiStateType state)
{
  g_return_if_fail (set != NULL);
  set->states |= (((gint64) 1) << state);
}

/**
 * atspi_state_set_compare:
 * @set: a pointer to the first #AtspiStateSet object on which to operate.
 * @set2: a pointer to the second #AtspiStateSet object on which to operate.
 *
 * Determines the differences between two instances of #AtspiStateSet.
 *
 * @see #atspi_state_set_equals.
 *
 * Returns: (transfer full): an #AtspiStateSet object containing all states
 * contained on one of the two sets but not the other.
 *
 **/
AtspiStateSet *
atspi_state_set_compare (AtspiStateSet *set,
                         AtspiStateSet *set2)
{
  g_return_val_if_fail (set != NULL, NULL);
  g_return_val_if_fail (set2 != NULL, NULL);

  return _atspi_state_set_new_internal (NULL, set->states ^ set2->states);
}

/**
 * atspi_state_set_contains:
 * @set: a pointer to the #AtspiStateSet object on which to operate.
 * @state: an #AtspiStateType for which the specified #AtspiStateSet
 *          will be queried.
 *
 * Determines whether a given #AtspiStateSet includes a given state; that is,
 *          whether @state is true for the @set in question.
 *
 * Returns: #TRUE if @state is true/included in the given #AtspiStateSet,
 *          otherwise #FALSE.
 *
 **/
gboolean
atspi_state_set_contains (AtspiStateSet *set,
                          AtspiStateType state)
{
  if (!set)
    return FALSE;
  refresh_states (set);
  return (set->states & ((gint64) 1 << state)) ? TRUE : FALSE;
}

/**
 * atspi_state_set_equals:
 * @set: a pointer to the first #AtspiStateSet object on which to operate.
 * @set2: a pointer to the second #AtspiStateSet object on which to operate.
 *
 * Determines whether two instances of #AtspiStateSet are equivalent (i.e.
 *          consist of the same #AtspiStates).  Useful for checking multiple
 *          state variables at once.
 *
 * @see #atspi_state_set_compare.
 *
 * Returns: #TRUE if the two #AtspiStateSets are equivalent,
 * otherwise #FALSE.
 *
 **/
gboolean
atspi_state_set_equals (AtspiStateSet *set,
                        AtspiStateSet *set2)
{
  if (set == set2)
    return TRUE;
  if (set == NULL || set2 == NULL)
    return FALSE;
  return (set->states == set2->states);
}

/**
 * atspi_state_set_get_states:
 * @set: The #AtspiStateSet to be queried.
 *
 * Returns the states in an #AtspiStateSet as an array.
 *
 * Returns: (element-type AtspiStateType) (transfer full): A #GArray of state
 *          types representing the current state.
 **/
GArray *
atspi_state_set_get_states (AtspiStateSet *set)
{
  gint i = 0;
  guint64 val = 1;
  GArray *ret;

  g_return_val_if_fail (set != NULL, NULL);
  refresh_states (set);
  ret = g_array_new (TRUE, TRUE, sizeof (AtspiStateType));
  if (!ret)
    return NULL;
  for (i = 0; i < 64; i++)
    {
      if (set->states & val)
        ret = g_array_append_val (ret, i);
      val <<= 1;
    }
  return ret;
}

/**
 * atspi_state_set_is_empty:
 * @set: The #AtspiStateSet to query.
 *
 * Returns: #TRUE if the state set contains no states; #FALSE otherwise.
 **/
gboolean
atspi_state_set_is_empty (AtspiStateSet *set)
{
  return (set->states == 0);
}

/**
 * atspi_state_set_remove:
 * @set: a pointer to the #AtspiStateSet object on which to operate.
 * @state: an #AtspiStateType to remove from the specified @set.
 *
 * Removes a particular #AtspiState to an #AtspiStateSet (i.e. sets the
 *       given state to #FALSE in the stateset.)
 *
 **/
void
atspi_state_set_remove (AtspiStateSet *set, AtspiStateType state)
{
  g_return_if_fail (set != NULL);
  set->states &= ~((gint64) 1 << state);
}