[LICENSE] change to Flora-1.1 license
[profile/tv/apps/native/screen-reader.git] / tests / atspi / atspi.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 "atspi.h"
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25
26 static AtspiValue *value = NULL;
27 static AtspiText *text = NULL;
28 static AtspiEditableText *editable_text = NULL;
29 static AtspiAction *action = NULL;
30 //static AtspiComponent *component = NULL;
31
32 G_DEFINE_TYPE(AtspiAccessible, atspi_accessible, G_TYPE_OBJECT);
33 G_DEFINE_TYPE(AtspiAction, atspi_action, G_TYPE_OBJECT);
34 G_DEFINE_TYPE(AtspiComponent, atspi_component, G_TYPE_OBJECT);
35 G_DEFINE_TYPE(AtspiStateSet, atspi_state_set, G_TYPE_OBJECT);
36
37 void atspi_rect_free (AtspiRect *rect)
38 {
39    g_free (rect);
40 }
41
42 AtspiRect *atspi_rect_copy (AtspiRect *src)
43 {
44    AtspiRect *dst = g_new (AtspiRect, 1);
45    dst->x = src->x;
46    dst->y = src->y;
47    dst->height = src->height;
48    dst->width = src->width;
49    return dst;
50 }
51
52 G_DEFINE_BOXED_TYPE (AtspiRect, atspi_rect, atspi_rect_copy, atspi_rect_free)
53
54 void atspi_alloc_memory()
55 {
56    value = (AtspiValue*)malloc(sizeof(AtspiValue));
57    text = (AtspiText*)malloc(sizeof(AtspiText));
58    editable_text = (AtspiEditableText*)malloc(sizeof(AtspiEditableText));
59 }
60
61 void atspi_free_memory(void)
62 {
63    free(editable_text);
64    free(value);
65    free(text);
66 }
67
68 gchar * atspi_accessible_get_name (AtspiAccessible *obj, GError **error)
69 {
70    if(!obj || !obj->name) return strdup("\0");
71
72    return strdup(obj->name);
73 }
74
75 gchar * atspi_accessible_get_role_name (AtspiAccessible *obj, GError **error)
76 {
77    if(!obj) return strdup("\0");
78    AtspiRole role = obj->role;
79    switch(role)
80       {
81       case ATSPI_ROLE_APPLICATION:
82          return strdup("Application");
83       case ATSPI_ROLE_PUSH_BUTTON:
84          return strdup("Push button");
85       case ATSPI_ROLE_ICON:
86          return strdup("Icon");
87       case ATSPI_ROLE_CHECK_BOX:
88          return strdup("Check box");
89       case ATSPI_ROLE_ENTRY:
90          return strdup("Entry");
91       case ATSPI_ROLE_FILLER:
92          return strdup("filler");
93       case ATSPI_ROLE_SCROLL_PANE:
94          return strdup("scroll pane");
95       case ATSPI_ROLE_IMAGE:
96          return strdup("image");
97       case ATSPI_ROLE_SPLIT_PANE:
98          return strdup("split pane");
99       case ATSPI_ROLE_UNKNOWN:
100          return strdup("unknown");
101       case ATSPI_ROLE_RULER:
102          return strdup("ruler");
103       case ATSPI_ROLE_FOOTER:
104          return strdup("footer");
105       case ATSPI_ROLE_INFO_BAR:
106          return strdup("infobar");
107       case ATSPI_ROLE_LINK:
108          return strdup("link");
109       default:
110          return strdup("\0");
111       }
112 }
113
114 gchar * atspi_accessible_get_localized_role_name (AtspiAccessible *obj, GError **error)
115 {
116    if(!obj) return strdup("\0");
117    AtspiRole role = obj->role;
118    switch(role)
119       {
120       case ATSPI_ROLE_APPLICATION:
121          return strdup("Application");
122       case ATSPI_ROLE_PUSH_BUTTON:
123          return strdup("Push button");
124       case ATSPI_ROLE_ICON:
125          return strdup("Icon");
126       case ATSPI_ROLE_CHECK_BOX:
127          return strdup("Check box");
128       case ATSPI_ROLE_ENTRY:
129          return strdup("Entry");
130       case ATSPI_ROLE_FILLER:
131          return strdup("filler");
132       default:
133          return strdup("\0");
134       }
135 }
136
137 gchar * atspi_accessible_get_toolkit_name (AtspiAccessible *obj, GError **error)
138 {
139    return "fake atspi";
140 }
141
142 gchar * atspi_accessible_get_description (AtspiAccessible *obj, GError **error)
143 {
144    if(!obj || !obj->description) return strdup("\0");
145    return strdup(obj->description);
146 }
147
148 AtspiAction * atspi_accessible_get_action_iface (AtspiAccessible *obj)
149 {
150    if(!obj) return NULL;
151    return action;
152 }
153
154 AtspiText * atspi_accessible_get_text_iface (AtspiAccessible *obj)
155 {
156    if(!obj) return NULL;
157    return text;
158 }
159
160 gint atspi_text_get_character_count (AtspiText *obj, GError **error)
161 {
162    if(!obj) return -1;
163    return 6;
164 }
165
166 gint atspi_text_get_caret_offset (AtspiText *obj, GError **error)
167 {
168    if(!obj) return -1;
169    return 5;
170 }
171
172 gchar * atspi_text_get_text (AtspiText *obj, gint start_offset, gint end_offset, GError **error)
173 {
174    if(!obj) return NULL;
175    return "AtspiText text";
176 }
177
178 AtspiValue * atspi_accessible_get_value_iface (AtspiAccessible *obj)
179 {
180    if(!obj) return NULL;
181    return value;
182 }
183
184 gdouble atspi_value_get_current_value (AtspiValue *obj, GError **error)
185 {
186    return 1.0;
187 }
188
189 gdouble atspi_value_get_maximum_value (AtspiValue *obj, GError **error)
190 {
191    return 2.0;
192 }
193
194 gdouble atspi_value_get_minimum_value (AtspiValue *obj, GError **error)
195 {
196    return 0.0;
197 }
198
199 AtspiEventListener *atspi_event_listener_new (AtspiEventListenerCB callback,
200       gpointer user_data,
201       GDestroyNotify callback_destroyed)
202 {
203    return NULL;
204 }
205
206 gboolean atspi_event_listener_register (AtspiEventListener *listener,
207                                         const gchar *event_type,
208                                         GError **error)
209 {
210    return FALSE;
211 }
212
213 gboolean atspi_event_listener_deregister (AtspiEventListener *listener,
214       const gchar *event_type,
215       GError **error)
216 {
217    return FALSE;
218 }
219
220 AtspiStateSet * atspi_accessible_get_state_set (AtspiAccessible *obj)
221 {
222    if (!obj || !obj->states) return NULL;
223    return obj->states;
224 }
225
226 gboolean atspi_state_set_contains (AtspiStateSet *set, AtspiStateType state)
227 {
228    if (!set)
229       return FALSE;
230    return (set->states & ((gint64)1 << state)) ? TRUE : FALSE;
231 }
232
233 void atspi_state_set_add (AtspiStateSet *set, AtspiStateType state)
234 {
235    if (!set) return;
236    set->states |= (((gint64)1) << state);
237 }
238
239 gboolean atspi_component_grab_highlight (AtspiComponent *obj, GError **error)
240 {
241    return FALSE;
242 }
243
244 AtspiScrollable *atspi_accessible_get_scrollable (AtspiAccessible *accessible)
245 {
246    return NULL;
247 }
248
249 gboolean atspi_component_clear_highlight (AtspiComponent *obj, GError **error)
250 {
251    return FALSE;
252 }
253
254 gboolean atspi_component_contains (AtspiComponent *obj, gint x, gint y, AtspiCoordType ctype, GError **error)
255 {
256    return TRUE;
257 }
258
259 GArray *atspi_state_set_get_states (AtspiStateSet *set)
260 {
261    gint i = 0;
262    guint64 val = 1;
263    GArray *ret;
264
265    g_return_val_if_fail (set != NULL, NULL);
266    ret = g_array_new (TRUE, TRUE, sizeof (AtspiStateType));
267    if (!ret)
268       return NULL;
269    for (i = 0; i < 64; i++)
270       {
271          if (set->states & val)
272             ret = g_array_append_val (ret, i);
273          val <<= 1;
274       }
275    return ret;
276
277 }
278
279 AtspiRole atspi_accessible_get_role (AtspiAccessible *obj, GError **error)
280 {
281    if(!obj) return ATSPI_ROLE_INVALID;
282    return obj->role;
283 }
284
285 gint atspi_accessible_get_child_count (AtspiAccessible *obj, GError **error)
286 {
287    if(!obj || !obj->children) return 0;
288    return g_list_length(obj->children);
289 }
290
291 AtspiAccessible * atspi_accessible_get_child_at_index (AtspiAccessible *obj, gint child_index, GError **error)
292 {
293    if(!obj || child_index >= g_list_length(obj->children)) return NULL;
294    return g_object_ref(g_list_nth_data(obj->children, child_index));
295 }
296
297 AtspiComponent * atspi_accessible_get_component_iface (AtspiAccessible *obj)
298 {
299    if(!obj) return NULL;
300    AtspiComponent *component = g_object_new(ATSPI_COMPONENT_OBJECT_TYPE, 0);
301    *(component->role) = obj->role;
302    return component;
303 }
304
305 AtspiStateSet * atspi_state_set_new (GArray *states)
306 {
307    AtspiStateSet *set = g_object_new (ATSPI_STATE_OBJECT_TYPE, NULL);
308    if (!set) return NULL;
309    int i;
310    for (i = 0; i < states->len; i++)
311       {
312          atspi_state_set_add (set, g_array_index (states, AtspiStateType, i));
313       }
314    return set;
315 }
316
317 AtspiRect *atspi_component_get_extents (AtspiComponent *component, AtspiCoordType ctype, GError **error)
318 {
319    if(!component) return NULL;
320
321    AtspiRect rect;
322    if(*(component->role) == ATSPI_ROLE_APPLICATION)
323       {
324          rect.x = 0;
325          rect.y = 0;
326          rect.width = 100;
327          rect.height = 100;
328       }
329    else if(*(component->role) == ATSPI_ROLE_PUSH_BUTTON)
330       {
331          rect.x = 1;
332          rect.y = 1;
333          rect.width = 50;
334          rect.height = 50;
335       }
336    else if(*(component->role) == ATSPI_ROLE_ICON)
337       {
338          rect.x = 50;
339          rect.y = 0;
340          rect.width = 50;
341          rect.height = 50;
342       }
343    else if(*(component->role) == ATSPI_ROLE_CHECK_BOX)
344       {
345          rect.x = 0;
346          rect.y = 50;
347          rect.width = 50;
348          rect.height = 50;
349       }
350    else if(*(component->role) == ATSPI_ROLE_ENTRY)
351       {
352          rect.x = 50;
353          rect.y = 50;
354          rect.width = 50;
355          rect.height = 50;
356       }
357    else
358       {
359          rect.x = 0;
360          rect.y = 0;
361          rect.width = 0;
362          rect.height = 0;
363       }
364    return atspi_rect_copy(&rect);
365 }
366
367 AtspiAccessible *atspi_create_accessible()
368 {
369    AtspiAccessible *obj = g_object_new(ATSPI_ACCESSIBLE_OBJECT_TYPE, 0);
370    obj->children = NULL;
371    obj->index_in_parent = 0;
372    obj->child_count = 0;
373
374    GArray *states = g_array_new (TRUE, TRUE, sizeof (AtspiStateType));
375    AtspiStateType s[] =
376    {
377       ATSPI_STATE_VISIBLE,
378       ATSPI_STATE_SHOWING,
379       ATSPI_STATE_FOCUSABLE,
380       ATSPI_STATE_LAST_DEFINED
381    };
382    int i;
383    for (i=0; i<(int)(sizeof(s)/sizeof(s[0])); i++)
384       g_array_append_val (states, s[i]);
385    obj->states = atspi_state_set_new(states);
386
387    return obj;
388 }
389
390 void atspi_delete_accessible(AtspiAccessible *obj)
391 {
392    if(!obj) return;
393    if(obj->children)
394       {
395          atpis_accessible_remove_children(obj);
396       }
397    g_object_unref(obj);
398 }
399
400 void atspi_accessible_add_child(AtspiAccessible *obj, AtspiAccessible *child)
401 {
402    child->index_in_parent = obj->child_count;
403    child->accessible_parent = obj;
404    obj->children = g_list_append(obj->children, child);
405    obj->child_count++;
406 }
407
408 void atpis_accessible_remove_children(AtspiAccessible *obj)
409 {
410    GList *l = obj->children;
411    while (l != NULL)
412       {
413          GList *next = l->next;
414          if(l->data)
415             {
416                atspi_delete_accessible(l->data);
417             }
418          l = next;
419       }
420    g_list_free(obj->children);
421 }
422
423 static void atspi_state_set_init(AtspiStateSet* set)
424 {
425 }
426
427 static void atspi_state_set_class_init(AtspiStateSetClass *_class)
428 {
429 }
430
431 static void atspi_action_class_init(AtspiActionClass *_class)
432 {
433 }
434
435 static void atspi_action_init(AtspiAction* obj)
436 {
437 }
438
439 static void atspi_accessible_class_init(AtspiAccessibleClass *_class)
440 {
441 }
442
443 static void atspi_accessible_init(AtspiAccessible* obj)
444 {
445 }
446
447 static void atspi_component_finalize(GObject *obj)
448 {
449    AtspiComponent *component = (AtspiComponent*)obj;
450    free(component->role);
451 }
452
453 static void atspi_component_class_init(AtspiComponentClass *class)
454 {
455    GObjectClass *gobject_class = G_OBJECT_CLASS (class);
456
457    gobject_class->finalize = atspi_component_finalize;
458 }
459
460 static void atspi_component_init(AtspiComponent* obj)
461 {
462    obj->role = (AtspiRole*)malloc(sizeof(AtspiRole));
463 }
464
465 AtspiEditableText * atspi_accessible_get_editable_text_iface (AtspiAccessible *obj)
466 {
467    return editable_text;
468 }
469
470 GArray * atspi_accessible_get_relation_set (AtspiAccessible *obj, GError **error)
471 {
472    return NULL;
473 }
474
475 AtspiRelationType atspi_relation_get_relation_type (AtspiRelation *obj)
476 {
477    return ATSPI_RELATION_NULL;
478 }
479
480 gint atspi_relation_get_n_targets (AtspiRelation *obj)
481 {
482    return 0;
483 }
484
485 AtspiAccessible * atspi_relation_get_target (AtspiRelation *obj, gint i)
486 {
487    return NULL;
488 }
489
490 AtspiAccessible * atspi_accessible_get_parent (AtspiAccessible *obj, GError **error)
491 {
492    return obj->accessible_parent;
493 }
494
495 int atspi_component_get_highlight_index(AtspiComponent *obj, GError **error)
496 {
497    return 0;
498 }
499
500 gint atspi_action_get_n_actions(AtspiAction *obj, GError **error)
501 {
502    return 0;
503 }
504
505 gchar * atspi_action_get_action_name (AtspiAction *obj, gint i, GError **error)
506 {
507     return strdup("");
508 }
509
510 gint atspi_accessible_get_index_in_parent (AtspiAccessible *obj, GError **error)
511 {
512    return obj->index_in_parent;
513 }
514
515 int atspi_exit(void)
516 {
517    return 1;
518 }