Tk Source Code

Check-in [2a98bd3d]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Some progress on segfaults
Downloads: Tarball | ZIP archive
Timelines: family | ancestors | descendants | both | tka11y
Files: files | file ages | folders
SHA3-256: 2a98bd3d8c40fabf99ca542daed10e031574e9c1df0765b70f1d8f87f3685b3b
User & Date: kevin_walzer 2025-08-15 01:17:32.662
Context
2025-08-15
01:40
Fix typo check-in: c22c6911 user: kevin_walzer tags: tka11y
01:17
Some progress on segfaults check-in: 2a98bd3d user: kevin_walzer tags: tka11y
00:25
Fix typo check-in: 600ab331 user: kevin_walzer tags: tka11y
Changes
Unified Diff Ignore Whitespace Patch
Changes to unix/tkUnixAccessibility.c.
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
    gchar *cached_name;
    gchar *cached_description;
    gchar *cached_value;
    AtkRole cached_role;
    gint x, y, width, height;
    gboolean is_mapped;
    gboolean has_focus;



    GList *children;  /* Track child accessible objects. */

} TkAtkAccessible;

typedef struct _TkAtkAccessibleClass {
    AtkObjectClass parent_class;
} TkAtkAccessibleClass;


/* Structs for passing data to main thread. */
typedef struct {
    gpointer (*func)(gpointer);
    gpointer user_data;
    gpointer result;
    gboolean done;
    GMutex mutex;
    GCond cond;
} TkMainThreadCall;

typedef struct {
Tk_Window tkwin;
Tcl_Interp *interp;
gpointer result;
const char *windowName;
const char *key;
} MainThreadData;



/* Structs to map Tk roles into Atk roles. */

typedef struct AtkRoleMap {







>
>
>
|
>


















|
|
|
|
|







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
    gchar *cached_name;
    gchar *cached_description;
    gchar *cached_value;
    AtkRole cached_role;
    gint x, y, width, height;
    gboolean is_mapped;
    gboolean has_focus;
    gboolean is_being_destroyed;
    gboolean is_being_created;     /* NEW: Prevent recursion during creation */
    gboolean children_initialized; /* NEW: Track if children are set up */
    GList *children;
    GMutex cleanup_mutex;
} TkAtkAccessible;

typedef struct _TkAtkAccessibleClass {
    AtkObjectClass parent_class;
} TkAtkAccessibleClass;


/* Structs for passing data to main thread. */
typedef struct {
    gpointer (*func)(gpointer);
    gpointer user_data;
    gpointer result;
    gboolean done;
    GMutex mutex;
    GCond cond;
} TkMainThreadCall;

typedef struct {
    Tk_Window tkwin;
    Tcl_Interp *interp;
    gpointer result;
    const char *windowName;
    const char *key;
} MainThreadData;



/* Structs to map Tk roles into Atk roles. */

typedef struct AtkRoleMap {
141
142
143
144
145
146
147


148
149
150
151
152
153
154
/* Variables for managing Atk objects. */
static AtkObject *tk_root_accessible = NULL;
static GList *toplevel_accessible_objects = NULL; /* This list will hold refs to toplevels. */
static GHashTable *tk_to_atk_map = NULL; /* Maps Tk_Window to AtkObject. */
extern Tcl_HashTable *TkAccessibilityObject; /* Hash table for managing accessibility attributes. */
static GMainContext *glib_context = NULL;
static gboolean in_process_pending = FALSE;



/* Thread management functions. */
gpointer RunOnMainThread(gpointer (*func)(gpointer user_data), gpointer user_data);
void RunOnMainThreadAsync(GSourceFunc func, gpointer user_data);
void EmitBoundsChanged(AtkObject *obj, gint x, gint y, gint width, gint height);
static gboolean run_main_thread_callback(gpointer data);








>
>







145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/* Variables for managing Atk objects. */
static AtkObject *tk_root_accessible = NULL;
static GList *toplevel_accessible_objects = NULL; /* This list will hold refs to toplevels. */
static GHashTable *tk_to_atk_map = NULL; /* Maps Tk_Window to AtkObject. */
extern Tcl_HashTable *TkAccessibilityObject; /* Hash table for managing accessibility attributes. */
static GMainContext *glib_context = NULL;
static gboolean in_process_pending = FALSE;
static GHashTable *creation_in_progress = NULL;  /* Track windows being created */


/* Thread management functions. */
gpointer RunOnMainThread(gpointer (*func)(gpointer user_data), gpointer user_data);
void RunOnMainThreadAsync(GSourceFunc func, gpointer user_data);
void EmitBoundsChanged(AtkObject *obj, gint x, gint y, gint width, gint height);
static gboolean run_main_thread_callback(gpointer data);

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

/* Registration and mapping functions. */
static void RegisterChildWidgets(Tcl_Interp *interp, Tk_Window tkwin, AtkObject *parent_obj);
static void RegisterToplevelWindow(Tcl_Interp *interp, Tk_Window tkwin, AtkObject *accessible);
static void UnregisterToplevelWindow(AtkObject *accessible);
AtkObject *TkCreateAccessibleAtkObject(Tcl_Interp *interp, Tk_Window tkwin, const char *path);
void InitAtkTkMapping(void);

void RegisterAtkObjectForTkWindow(Tk_Window tkwin, AtkObject *atkobj);
AtkObject *GetAtkObjectForTkWindow(Tk_Window tkwin);
void UnregisterAtkObjectForTkWindow(Tk_Window tkwin);
static AtkObject *tk_util_get_root(void);
AtkObject *atk_get_root(void);

/* Cache update functions. */
static void UpdateGeometryCache(TkAtkAccessible *acc);
static void UpdateNameCache(TkAtkAccessible *acc);
static void UpdateDescriptionCache(TkAtkAccessible *acc);
static void UpdateValueCache(TkAtkAccessible *acc);
static void UpdateRoleCache(TkAtkAccessible *acc);
static void UpdateStateCache(TkAtkAccessible *acc);
static void UpdateChildrenCache(TkAtkAccessible *acc);


/* Event handlers. */
void TkAtkAccessible_RegisterEventHandlers(Tk_Window tkwin, void *tkAccessible);
static void TkAtkAccessible_DestroyHandler(ClientData clientData, XEvent *eventPtr);
static void TkAtkAccessible_ConfigureHandler(ClientData clientData, XEvent *eventPtr);
static void TkAtkAccessible_MapHandler(ClientData clientData, XEvent *eventPtr);
static void TkAtkAccessible_UnmapHandler(ClientData clientData, XEvent *eventPtr);







>














>







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

/* Registration and mapping functions. */
static void RegisterChildWidgets(Tcl_Interp *interp, Tk_Window tkwin, AtkObject *parent_obj);
static void RegisterToplevelWindow(Tcl_Interp *interp, Tk_Window tkwin, AtkObject *accessible);
static void UnregisterToplevelWindow(AtkObject *accessible);
AtkObject *TkCreateAccessibleAtkObject(Tcl_Interp *interp, Tk_Window tkwin, const char *path);
void InitAtkTkMapping(void);
static void InitRecursionTracking(void);
void RegisterAtkObjectForTkWindow(Tk_Window tkwin, AtkObject *atkobj);
AtkObject *GetAtkObjectForTkWindow(Tk_Window tkwin);
void UnregisterAtkObjectForTkWindow(Tk_Window tkwin);
static AtkObject *tk_util_get_root(void);
AtkObject *atk_get_root(void);

/* Cache update functions. */
static void UpdateGeometryCache(TkAtkAccessible *acc);
static void UpdateNameCache(TkAtkAccessible *acc);
static void UpdateDescriptionCache(TkAtkAccessible *acc);
static void UpdateValueCache(TkAtkAccessible *acc);
static void UpdateRoleCache(TkAtkAccessible *acc);
static void UpdateStateCache(TkAtkAccessible *acc);
static void UpdateChildrenCache(TkAtkAccessible *acc);
static gboolean DeferredChildrenUpdate(gpointer user_data);

/* Event handlers. */
void TkAtkAccessible_RegisterEventHandlers(Tk_Window tkwin, void *tkAccessible);
static void TkAtkAccessible_DestroyHandler(ClientData clientData, XEvent *eventPtr);
static void TkAtkAccessible_ConfigureHandler(ClientData clientData, XEvent *eventPtr);
static void TkAtkAccessible_MapHandler(ClientData clientData, XEvent *eventPtr);
static void TkAtkAccessible_UnmapHandler(ClientData clientData, XEvent *eventPtr);
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
/* Child management functions. */
static void AddChildToParent(TkAtkAccessible *parent, AtkObject *child);
static void RemoveChildFromParent(TkAtkAccessible *parent, AtkObject *child);


/* Define custom Atk object bridged to Tcl/Tk. */
#define TK_ATK_TYPE_ACCESSIBLE (tk_atk_accessible_get_type())

G_DEFINE_TYPE_WITH_CODE(TkAtkAccessible, tk_atk_accessible, ATK_TYPE_OBJECT,
			G_IMPLEMENT_INTERFACE(ATK_TYPE_COMPONENT, tk_atk_component_interface_init)
			G_IMPLEMENT_INTERFACE(ATK_TYPE_ACTION, tk_atk_action_interface_init)
			G_IMPLEMENT_INTERFACE(ATK_TYPE_VALUE, tk_atk_value_interface_init)
			)
			
/* Helper function to integrate strings. */         
static gchar *sanitize_utf8(const gchar *str) 
{
    if (!str) return NULL;
    return g_utf8_make_valid(str, -1);
}

/*
 *----------------------------------------------------------------------
 *
 * Thread management functions. at-spi works on background/worker threads
 * and calls to Tcl/Tk and Atk must be directed to the main thread.  
 *
 *----------------------------------------------------------------------
 */
            
/* 
 * Callback function to main thread. This runs on 
 * the main thread (GLib main context). 
 */
 
static gboolean run_main_thread_callback(gpointer data)
{
    TkMainThreadCall *call = (TkMainThreadCall *)data;

    call->result = call->func(call->user_data);

    g_mutex_lock(&call->mutex);
    call->done = TRUE;







>






<
<
<
<
<
<
<














|







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
/* Child management functions. */
static void AddChildToParent(TkAtkAccessible *parent, AtkObject *child);
static void RemoveChildFromParent(TkAtkAccessible *parent, AtkObject *child);


/* Define custom Atk object bridged to Tcl/Tk. */
#define TK_ATK_TYPE_ACCESSIBLE (tk_atk_accessible_get_type())
#define TK_ATK_IS_ACCESSIBLE(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), TK_ATK_TYPE_ACCESSIBLE))
G_DEFINE_TYPE_WITH_CODE(TkAtkAccessible, tk_atk_accessible, ATK_TYPE_OBJECT,
			G_IMPLEMENT_INTERFACE(ATK_TYPE_COMPONENT, tk_atk_component_interface_init)
			G_IMPLEMENT_INTERFACE(ATK_TYPE_ACTION, tk_atk_action_interface_init)
			G_IMPLEMENT_INTERFACE(ATK_TYPE_VALUE, tk_atk_value_interface_init)
			)
			







/*
 *----------------------------------------------------------------------
 *
 * Thread management functions. at-spi works on background/worker threads
 * and calls to Tcl/Tk and Atk must be directed to the main thread.  
 *
 *----------------------------------------------------------------------
 */
            
/* 
 * Callback function to main thread. This runs on 
 * the main thread (GLib main context). 
 */
 
    static gboolean run_main_thread_callback(gpointer data)
{
    TkMainThreadCall *call = (TkMainThreadCall *)data;

    call->result = call->func(call->user_data);

    g_mutex_lock(&call->mutex);
    call->done = TRUE;
400
401
402
403
404
405
406
407
408


409
410


411



412
413


414
415
416
417
418





419





420
421
422
423
424
425
426
427
428








429
430
431
432
433
434
435
 * Child management functions
 *
 *----------------------------------------------------------------------
 */

static void AddChildToParent(TkAtkAccessible *parent, AtkObject *child) 
{
    if (!parent || !child) return;
    


    /* Prevent duplicates. */
    if (g_list_find(parent->children, child)) return;


    



    parent->children = g_list_append(parent->children, child);
    atk_object_set_parent(child, ATK_OBJECT(parent));


}

static void RemoveChildFromParent(TkAtkAccessible *parent, AtkObject *child) 
{
    if (!parent || !child) return;





    parent->children = g_list_remove(parent->children, child);





}

/*
 *----------------------------------------------------------------------
 *
 * Helper functions for main thread operations.
 *
 *----------------------------------------------------------------------
 */









/* Get accessible role. */
static gpointer get_atk_role_for_widget_main(gpointer data) 
{
    MainThreadData *mt_data = (MainThreadData *)data;
    return (gpointer)(uintptr_t)GetAtkRoleForWidget(mt_data->tkwin);
}







|

>
>

|
>
>
|
>
>
>


>
>





>
>
>
>
>
|
>
>
>
>
>









>
>
>
>
>
>
>
>







402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
 * Child management functions
 *
 *----------------------------------------------------------------------
 */

static void AddChildToParent(TkAtkAccessible *parent, AtkObject *child) 
{
    if (!parent || !child || !G_IS_OBJECT(child)) return;
    
    g_mutex_lock(&parent->cleanup_mutex);
    
    /* Prevent duplicates. */
    if (g_list_find(parent->children, child)) {
        g_mutex_unlock(&parent->cleanup_mutex);
        return;
    }
    
    /* Take explicit reference when adding to parent. */
    g_object_ref(child);
    parent->children = g_list_append(parent->children, child);
    atk_object_set_parent(child, ATK_OBJECT(parent));
    
    g_mutex_unlock(&parent->cleanup_mutex);
}

static void RemoveChildFromParent(TkAtkAccessible *parent, AtkObject *child) 
{
    if (!parent || !child) return;
    
    g_mutex_lock(&parent->cleanup_mutex);
    
    GList *found = g_list_find(parent->children, child);
    if (found) {
        parent->children = g_list_remove(parent->children, child);
        /* Release the reference we took in AddChildToParent. */
        g_object_unref(child);
    }
    
    g_mutex_unlock(&parent->cleanup_mutex);
}

/*
 *----------------------------------------------------------------------
 *
 * Helper functions for main thread operations.
 *
 *----------------------------------------------------------------------
 */
 
/* Helper function to integrate strings. */         
static gchar *sanitize_utf8(const gchar *str) 
{
    if (!str) return NULL;
    return g_utf8_make_valid(str, -1);
}


/* Get accessible role. */
static gpointer get_atk_role_for_widget_main(gpointer data) 
{
    MainThreadData *mt_data = (MainThreadData *)data;
    return (gpointer)(uintptr_t)GetAtkRoleForWidget(mt_data->tkwin);
}
575
576
577
578
579
580
581







582






583


584
585

586
587

588


589


590
591
592
593

594
595
596
597
598







599





600


601
602

603
604

605


606


607
608
609
610

611
612
613
614
615
616
617
 *----------------------------------------------------------------------
 */

/* Emit children-changed::add signal. */
static gpointer emit_children_changed_add(gpointer data)
{
    ChildrenChangedAddData *cad = (ChildrenChangedAddData *)data;







    if (cad && cad->parent && G_IS_OBJECT(cad->parent) && cad->child && G_IS_OBJECT(cad->child)) {






        g_signal_emit_by_name(cad->parent, "children-changed::add", cad->index, cad->child);


    } else {
        g_warning("emit_children_changed_add: Invalid parent or child object");

    }
    if (cad) {

        if (cad->parent && G_IS_OBJECT(cad->parent)) g_object_unref(cad->parent);


        if (cad->child && G_IS_OBJECT(cad->child)) g_object_unref(cad->child);


        g_free(cad);
    }
    return G_SOURCE_REMOVE;
}


/* Emit children-changed::remove signal. */
static gpointer emit_children_changed_remove(gpointer data)
{
    ChildrenChangedRemoveData *crd = (ChildrenChangedRemoveData *)data;







    if (crd && crd->parent && G_IS_OBJECT(crd->parent) && crd->child && G_IS_OBJECT(crd->child)) {





        g_signal_emit_by_name(crd->parent, "children-changed::remove", crd->index, crd->child);


    } else {
        g_warning("emit_children_changed_remove: Invalid parent or child object");

    }
    if (crd) {

        if (crd->parent && G_IS_OBJECT(crd->parent)) g_object_unref(crd->parent);


        if (crd->child && G_IS_OBJECT(crd->child)) g_object_unref(crd->child);


        g_free(crd);
    }
    return G_SOURCE_REMOVE;
}


/* Emit value-changed signal. */
static gboolean emit_value_changed(gpointer data)
{
    if (data) {
        ValueChangedData *vcd = (ValueChangedData *)data;
        g_signal_emit_by_name(vcd->obj, "value-changed", &vcd->value);







>
>
>
>
>
>
>
|
>
>
>
>
>
>
|
>
>

|
>

|
>
|
>
>
|
>
>
|
|


>





>
>
>
>
>
>
>
|
>
>
>
>
>
|
>
>

|
>

|
>
|
>
>
|
>
>
|
|


>







604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
 *----------------------------------------------------------------------
 */

/* Emit children-changed::add signal. */
static gpointer emit_children_changed_add(gpointer data)
{
    ChildrenChangedAddData *cad = (ChildrenChangedAddData *)data;
    
    if (!cad) {
        g_warning("emit_children_changed_add: NULL data");
        return G_SOURCE_REMOVE;
    }
    
    /* Validate objects before emission. */
    if (cad->parent && G_IS_OBJECT(cad->parent) && 
        cad->child && G_IS_OBJECT(cad->child)) {
        
        /* Check if objects are still valid (not being destroyed). */
        if (TK_ATK_IS_ACCESSIBLE(cad->parent)) {
            TkAtkAccessible *parent_acc = (TkAtkAccessible *)cad->parent;
            if (!parent_acc->is_being_destroyed) {
                g_signal_emit_by_name(cad->parent, "children-changed::add", cad->index, cad->child);
            }
        }
    } else {
        g_warning("emit_children_changed_add: Invalid parent=%p or child=%p object", 
                  cad->parent, cad->child);
    }
    
    /* Safe cleanup .*/
    if (cad->parent && G_IS_OBJECT(cad->parent)) {
        g_object_unref(cad->parent);
    }
    if (cad->child && G_IS_OBJECT(cad->child)) {
        g_object_unref(cad->child);
    }
    g_free(cad);
    
    return G_SOURCE_REMOVE;
}


/* Emit children-changed::remove signal. */
static gpointer emit_children_changed_remove(gpointer data)
{
    ChildrenChangedRemoveData *crd = (ChildrenChangedRemoveData *)data;
    
    if (!crd) {
        g_warning("emit_children_changed_remove: NULL data");
        return G_SOURCE_REMOVE;
    }
    
    /* Validate objects before emission. */
    if (crd->parent && G_IS_OBJECT(crd->parent) && 
        crd->child && G_IS_OBJECT(crd->child)) {
        
        if (TK_ATK_IS_ACCESSIBLE(crd->parent)) {
            TkAtkAccessible *parent_acc = (TkAtkAccessible *)crd->parent;
            if (!parent_acc->is_being_destroyed) {
                g_signal_emit_by_name(crd->parent, "children-changed::remove", crd->index, crd->child);
            }
        }
    } else {
        g_warning("emit_children_changed_remove: Invalid parent=%p or child=%p object", 
                  crd->parent, crd->child);
    }
    
    /* Safe cleanup */
    if (crd->parent && G_IS_OBJECT(crd->parent)) {
        g_object_unref(crd->parent);
    }
    if (crd->child && G_IS_OBJECT(crd->child)) {
        g_object_unref(crd->child);
    }
    g_free(crd);
    
    return G_SOURCE_REMOVE;
}


/* Emit value-changed signal. */
static gboolean emit_value_changed(gpointer data)
{
    if (data) {
        ValueChangedData *vcd = (ValueChangedData *)data;
        g_signal_emit_by_name(vcd->obj, "value-changed", &vcd->value);
965
966
967
968
969
970
971



972

973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993






994
995

996
997
998




999




1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018

1019
1020
1021
1022
1023




1024
1025
1026
1027
1028
1029
1030
    self->cached_role = ATK_ROLE_UNKNOWN;
    self->x = 0;
    self->y = 0;
    self->width = 0;
    self->height = 0;
    self->is_mapped = FALSE;
    self->has_focus = FALSE;



    self->children = NULL;

}

static void tk_atk_accessible_finalize(GObject *gobject)
{
    TkAtkAccessible *self = (TkAtkAccessible*)gobject;

    if (self->tkwin) {
        if (RunOnMainThread(is_toplevel_main, &(MainThreadData){self->tkwin, NULL, NULL, NULL, NULL})) {
            toplevel_accessible_objects = g_list_remove(toplevel_accessible_objects, self);
            /* Remove from root's children. */
            gint index = g_list_index(((TkAtkAccessible *)tk_root_accessible)->children, self);
            if (index >= 0) {
                RemoveChildFromParent((TkAtkAccessible *)tk_root_accessible, ATK_OBJECT(self));
                ChildrenChangedRemoveData *crd = g_new0(ChildrenChangedRemoveData, 1);
                crd->parent = tk_root_accessible;
                crd->index = index;
                crd->child = ATK_OBJECT(self);
                if (crd->parent) g_object_ref(crd->parent);
                if (crd->child) g_object_ref(crd->child);
                RunOnMainThread(emit_children_changed_remove, crd);
            }






        }
        UnregisterAtkObjectForTkWindow(self->tkwin);

    }

    /* Clean up children. */




    GList *iter = self->children;




    while (iter) {
        AtkObject *child = (AtkObject *)iter->data;
        if (child && G_IS_OBJECT(child)) {
            gint index = g_list_index(self->children, child);
            RemoveChildFromParent(self, child);
            ChildrenChangedRemoveData *crd = g_new0(ChildrenChangedRemoveData, 1);
            crd->parent = ATK_OBJECT(self);
            crd->index = index;
            crd->child = child;
            if (crd->parent) g_object_ref(crd->parent);
            if (crd->child) g_object_ref(crd->child);
            RunOnMainThread(emit_children_changed_remove, crd);
            g_object_unref(child);
        }
        iter = g_list_next(iter);
    }
    g_list_free(self->children);
    self->children = NULL;


    g_free(self->path);
    g_free(self->cached_name);
    g_free(self->cached_description);
    g_free(self->cached_value);





    G_OBJECT_CLASS(tk_atk_accessible_parent_class)->finalize(gobject);
}

static void tk_atk_accessible_class_init(TkAtkAccessibleClass *klass)
{
    GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
    AtkObjectClass *atk_class = ATK_OBJECT_CLASS(klass);







>
>
>

>

<



|
|
|
|
|
<
<
<
<
<
|
|
<
<
<
|
>
>
>
>
>
>


>


|
>
>
>
>
|
>
>
>
>



|
<
<
<
<
<
<
<
<




|
<

>




|
>
>
>
>







1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049

1050
1051
1052
1053
1054
1055
1056
1057





1058
1059



1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085








1086
1087
1088
1089
1090

1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
    self->cached_role = ATK_ROLE_UNKNOWN;
    self->x = 0;
    self->y = 0;
    self->width = 0;
    self->height = 0;
    self->is_mapped = FALSE;
    self->has_focus = FALSE;
    self->is_being_destroyed = FALSE;
    self->is_being_created = FALSE;      /* NEW */
    self->children_initialized = FALSE;  /* NEW */
    self->children = NULL;
    g_mutex_init(&self->cleanup_mutex);
}

static void tk_atk_accessible_finalize(GObject *gobject)
{
    TkAtkAccessible *self = (TkAtkAccessible*)gobject;
    
    if (!self) return;

    /* Mark as being destroyed. */
    g_mutex_lock(&self->cleanup_mutex);





    self->is_being_destroyed = TRUE;
    g_mutex_unlock(&self->cleanup_mutex);




    /* Clean up Tk window association. */
    if (self->tkwin) {
        /* Check if this is a toplevel. */
        MainThreadData data = {self->tkwin, self->interp, NULL, NULL, NULL};
        if ((gboolean)(uintptr_t)RunOnMainThread(is_toplevel_main, &data)) {
            UnregisterToplevelWindow(ATK_OBJECT(self));
        }
        UnregisterAtkObjectForTkWindow(self->tkwin);
        self->tkwin = NULL;
    }

    /* Clean up children safely. */
    g_mutex_lock(&self->cleanup_mutex);
    GList *children_copy = g_list_copy(self->children);
    /* Clear the list but don't free yet. */
    g_list_free(self->children);
    self->children = NULL;
    g_mutex_unlock(&self->cleanup_mutex);

    /* Release children references outside mutex.*/
    GList *iter = children_copy;
    while (iter) {
        AtkObject *child = (AtkObject *)iter->data;
        if (child && G_IS_OBJECT(child)) {
            /* Only unref if we actually own a reference. */








            g_object_unref(child);
        }
        iter = g_list_next(iter);
    }
    g_list_free(children_copy);


    /* Clean up cached strings. */
    g_free(self->path);
    g_free(self->cached_name);
    g_free(self->cached_description);
    g_free(self->cached_value);
    
    /* Clear mutex last. */
    g_mutex_clear(&self->cleanup_mutex);

    /* Call parent finalize. */
    G_OBJECT_CLASS(tk_atk_accessible_parent_class)->finalize(gobject);
}

static void tk_atk_accessible_class_init(TkAtkAccessibleClass *klass)
{
    GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
    AtkObjectClass *atk_class = ATK_OBJECT_CLASS(klass);
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177


1178
1179
1180
1181
1182
1183
1184
        
        toplevel_accessible_objects = g_list_remove(toplevel_accessible_objects, accessible);
    }
}


/* 
* Register child widgets of a given window 
* as accessible objects. 
*/

static void RegisterChildWidgets(Tcl_Interp *interp, Tk_Window tkwin, AtkObject *parent_obj) 
{


    if (!tkwin || !parent_obj || !G_IS_OBJECT(parent_obj)) {
	g_warning("RegisterChildWidgets: Invalid tkwin or parent_obj");
	return;
    }

    TkAtkAccessible *acc = (TkAtkAccessible *)parent_obj;
    /* Update children cache asynchronously. */







|
|
|



>
>







1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
        
        toplevel_accessible_objects = g_list_remove(toplevel_accessible_objects, accessible);
    }
}


/* 
 * Register child widgets of a given window 
 * as accessible objects. 
 */

static void RegisterChildWidgets(Tcl_Interp *interp, Tk_Window tkwin, AtkObject *parent_obj) 
{
    (void) interp;
    
    if (!tkwin || !parent_obj || !G_IS_OBJECT(parent_obj)) {
	g_warning("RegisterChildWidgets: Invalid tkwin or parent_obj");
	return;
    }

    TkAtkAccessible *acc = (TkAtkAccessible *)parent_obj;
    /* Update children cache asynchronously. */
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226

















1227
1228
1229
1230


1231

1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295


1296

1297
1298
1299
1300
1301
1302

1303
1304
1305
1306




1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338

1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358


1359
1360
1361
1362
1363
1364
1365


1366
1367
1368
1369
1370
1371
1372
1373
1374

1375
1376
1377
1378

1379
1380
1381
1382









1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396







1397
1398
1399
1400
1401
1402
1403
    return tk_util_get_root();
}

/* ATK-Tk object creation with proper parent/child relationship. */
AtkObject *TkCreateAccessibleAtkObject(Tcl_Interp *interp, Tk_Window tkwin, const char *path)
{
    if (!interp || !tkwin || !path) {
	g_warning("TkCreateAccessibleAtkObject: Invalid parameters");
	return NULL;
    }


















    TkAtkAccessible *acc = g_object_new(TK_ATK_TYPE_ACCESSIBLE, NULL);
    acc->interp = interp;
    acc->tkwin = tkwin;
    acc->path = sanitize_utf8(path);




    MainThreadData data = {tkwin, interp, NULL, NULL, NULL, NULL};
    if ((gboolean)(uintptr_t)RunOnMainThread(is_toplevel_main, &data) &&
	tkwin != (Tk_Window)RunOnMainThread(get_main_window_main, &(MainThreadData){NULL, interp, NULL, NULL, NULL})) {
	RunOnMainThread(make_window_exist_main, &data);
	RunOnMainThread(map_window_main, &data);
    }

    /* Update cached properties. */
    UpdateGeometryCache(acc);
    UpdateNameCache(acc);
    UpdateDescriptionCache(acc);
    UpdateValueCache(acc);
    UpdateRoleCache(acc);
    UpdateStateCache(acc);
    UpdateChildrenCache(acc);

    AtkObject *obj = ATK_OBJECT(acc);
    AtkRole role = acc->cached_role;
    if (role == ATK_ROLE_UNKNOWN &&
	((gboolean)(uintptr_t)RunOnMainThread(is_toplevel_main, &data) ||
	 tkwin == (Tk_Window)RunOnMainThread(get_main_window_main, &(MainThreadData){NULL, interp, NULL, NULL, NULL}))) {
	role = ATK_ROLE_WINDOW;
    }
    atk_object_set_role(obj, role);

    /*
     * Set ATK name and description. Return description for name
     * because name is generally the same as the role, and we do not
     * want redundant labels from screen reader. 
     */
    if (acc->cached_description && *acc->cached_description) {
	atk_object_set_name(obj, acc->cached_description);
    } else if (acc->cached_name && *acc->cached_name) {
	atk_object_set_name(obj, acc->cached_name);
    } else {
	atk_object_set_name(obj, path);
    }
    if (acc->cached_description && *acc->cached_description) {
	atk_object_set_description(obj, acc->cached_description);
    } else {
	atk_object_set_description(obj, path);
    }

    /* Set states. */
    AtkStateSet *state_set = atk_object_ref_state_set(obj);
    if (state_set != NULL) {
	atk_state_set_add_state(state_set, ATK_STATE_VISIBLE);
	atk_state_set_add_state(state_set, ATK_STATE_SHOWING);
	atk_state_set_add_state(state_set, ATK_STATE_ENABLED);
	if (role == ATK_ROLE_PUSH_BUTTON || role == ATK_ROLE_ENTRY ||
	    role == ATK_ROLE_COMBO_BOX || role == ATK_ROLE_CHECK_BOX ||
	    role == ATK_ROLE_RADIO_BUTTON || role == ATK_ROLE_SLIDER ||
	    role == ATK_ROLE_SPIN_BUTTON) {
	    atk_state_set_add_state(state_set, ATK_STATE_FOCUSABLE);
	}
	atk_object_notify_state_change(obj, ATK_STATE_VISIBLE, TRUE);
	atk_object_notify_state_change(obj, ATK_STATE_SHOWING, TRUE);
	atk_object_notify_state_change(obj, ATK_STATE_ENABLED, TRUE);
	if (atk_state_set_contains_state(state_set, ATK_STATE_FOCUSABLE)) {
	    atk_object_notify_state_change(obj, ATK_STATE_FOCUSABLE, TRUE);
	}
	g_object_unref(state_set);
    }



    /* Set parent. */

    Tk_Window parent_tkwin = (Tk_Window)RunOnMainThread(get_window_parent_main, &data);
    AtkObject *parent_obj = NULL;

    if (parent_tkwin) {
	parent_obj = GetAtkObjectForTkWindow(parent_tkwin);
	if (!parent_obj) {

	    TkAtkAccessible *parent_acc = g_object_new(TK_ATK_TYPE_ACCESSIBLE, NULL);
	    parent_acc->interp = interp;
	    parent_acc->tkwin = parent_tkwin;
	    parent_acc->path = sanitize_utf8((const char *)RunOnMainThread(get_window_name_main, &(MainThreadData){parent_tkwin, interp, NULL, NULL, NULL}));




	    UpdateGeometryCache(parent_acc);
	    UpdateNameCache(parent_acc);
	    UpdateDescriptionCache(parent_acc);
	    UpdateValueCache(parent_acc);
	    UpdateRoleCache(parent_acc);
	    UpdateStateCache(parent_acc);
	    UpdateChildrenCache(parent_acc); /* Initialize parent children. */
	    parent_obj = ATK_OBJECT(parent_acc);
	    atk_object_set_role(parent_obj, ATK_ROLE_WINDOW);
	    if (parent_acc->cached_name) {
		atk_object_set_name(parent_obj, parent_acc->cached_name);
	    } else {
		atk_object_set_name(parent_obj, parent_acc->path);
	    }
	    if (parent_acc->cached_description) {
		atk_object_set_description(parent_obj, parent_acc->cached_description);
	    } else {
		atk_object_set_description(parent_obj, parent_acc->path);
	    }
	    state_set = atk_object_ref_state_set(parent_obj);
	    if (state_set != NULL) {
		atk_state_set_add_state(state_set, ATK_STATE_VISIBLE);
		atk_state_set_add_state(state_set, ATK_STATE_SHOWING);
		atk_state_set_add_state(state_set, ATK_STATE_ENABLED);
		atk_object_notify_state_change(parent_obj, ATK_STATE_VISIBLE, TRUE);
		atk_object_notify_state_change(parent_obj, ATK_STATE_SHOWING, TRUE);
		atk_object_notify_state_change(parent_obj, ATK_STATE_ENABLED, TRUE);
		g_object_unref(state_set);
	    }
	    RegisterAtkObjectForTkWindow(parent_tkwin, parent_obj);
	    TkAtkAccessible_RegisterEventHandlers(parent_tkwin, parent_acc);


	    Tk_Window grandparent_tkwin = (Tk_Window)RunOnMainThread(get_window_parent_main, &(MainThreadData){parent_tkwin, interp, NULL, NULL, NULL});
	    AtkObject *grandparent_obj = grandparent_tkwin ? GetAtkObjectForTkWindow(grandparent_tkwin) : tk_root_accessible;
	    if (grandparent_obj && G_IS_OBJECT(grandparent_obj)) {
		atk_object_set_parent(parent_obj, grandparent_obj);
		AddChildToParent((TkAtkAccessible *)grandparent_obj, parent_obj);
		ChildrenChangedAddData *cad = g_new0(ChildrenChangedAddData, 1);
		cad->parent = grandparent_obj;
		cad->index = g_list_length(((TkAtkAccessible *)grandparent_obj)->children) - 1;
		cad->child = parent_obj;
		if (cad->parent) g_object_ref(cad->parent);
		if (cad->child) g_object_ref(cad->child);
		RunOnMainThreadAsync(emit_children_changed_add, cad);
	    }

	    if ((gboolean)(uintptr_t)RunOnMainThread(is_toplevel_main, &(MainThreadData){parent_tkwin, interp, NULL, NULL, NULL})) {
		if (!g_list_find(toplevel_accessible_objects, parent_obj)) {
		    toplevel_accessible_objects = g_list_append(toplevel_accessible_objects, parent_obj);
		}
	    }
	}


    } else {
	parent_obj = tk_root_accessible;
    }

    if (parent_obj && G_IS_OBJECT(parent_obj)) {
	atk_object_set_parent(obj, parent_obj);
	AddChildToParent((TkAtkAccessible *)parent_obj, obj);


	ChildrenChangedAddData *cad = g_new0(ChildrenChangedAddData, 1);
	cad->parent = parent_obj;
	cad->index = g_list_length(((TkAtkAccessible *)parent_obj)->children) - 1;
	cad->child = obj;
	if (cad->parent) g_object_ref(cad->parent);
	if (cad->child) g_object_ref(cad->child);
	RunOnMainThreadAsync(emit_children_changed_add, cad);
    } else {
	g_warning("TkCreateAccessibleAtkObject: Invalid parent for %s", path);

	g_object_unref(obj);
	return NULL;
    }


    if ((gboolean)(uintptr_t)RunOnMainThread(is_toplevel_main, &data)) {
	RegisterToplevelWindow(interp, tkwin, obj);
    }










    return obj;
}

/*
 * Functions to map Tk window to its corresponding Atk object.
 */

void InitAtkTkMapping(void)
{
    if (!tk_to_atk_map) {
	tk_to_atk_map = g_hash_table_new_full(g_direct_hash, g_direct_equal,
					      NULL, (GDestroyNotify)g_object_unref);
    }
}








void RegisterAtkObjectForTkWindow(Tk_Window tkwin, AtkObject *atkobj)
{
    if (!tkwin || !atkobj) return; 
    InitAtkTkMapping();
    g_object_ref(atkobj); /*Increment ref count because hash table takes ownership. */
    g_hash_table_insert(tk_to_atk_map, tkwin, atkobj);







|
|


>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>




>
>

>
|

|
|
|


|






|




|
|
|



<
|
<
<
<

|

|

|


|

|


|


|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|


>
>
|
>




|
|
>
|
|
|
|
>
>
>
>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<
<
<
<
<
<
<
<
<
|
|
|
|
>
|
|
|
|
|
<
<
<
<
<
<
<
|

|
|
|
|
|
|
>
>

|



|
|
>
>
|
|
|
|
<
<
|

|
>
|
|


>

|


>
>
>
>
>
>
>
>
>














>
>
>
>
>
>
>







1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356

1357



1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429









1430
1431
1432
1433
1434
1435
1436
1437
1438
1439







1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462


1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
    return tk_util_get_root();
}

/* ATK-Tk object creation with proper parent/child relationship. */
AtkObject *TkCreateAccessibleAtkObject(Tcl_Interp *interp, Tk_Window tkwin, const char *path)
{
    if (!interp || !tkwin || !path) {
        g_warning("TkCreateAccessibleAtkObject: Invalid parameters");
        return NULL;
    }

    /* Check if this window is already being created (recursion detection). */
    if (g_hash_table_contains(creation_in_progress, tkwin)) {
        g_debug("TkCreateAccessibleAtkObject: Recursion detected for %s, skipping", path);
        return NULL;  /* Return NULL to break recursion */
    }

    /* Check if already exists. */
    AtkObject *existing = GetAtkObjectForTkWindow(tkwin);
    if (existing) {
        g_debug("TkCreateAccessibleAtkObject: Object already exists for %s", path);
        return existing;
    }

    /* Mark this window as being created. */
    g_hash_table_add(creation_in_progress, tkwin);

    /* Create the accessible object. */
    TkAtkAccessible *acc = g_object_new(TK_ATK_TYPE_ACCESSIBLE, NULL);
    acc->interp = interp;
    acc->tkwin = tkwin;
    acc->path = sanitize_utf8(path);
    acc->is_being_created = TRUE;      /* Mark as being created */
    acc->children_initialized = FALSE; /* Children not set up yet */

    /* Handle toplevel special case. */
    MainThreadData data = {tkwin, interp, NULL, NULL, NULL};
    if ((gboolean)(uintptr_t)RunOnMainThread(is_toplevel_main, &data) &&
        tkwin != (Tk_Window)RunOnMainThread(get_main_window_main, &(MainThreadData){NULL, interp, NULL, NULL, NULL})) {
        RunOnMainThread(make_window_exist_main, &data);
        RunOnMainThread(map_window_main, &data);
    }

    /* Update basic cached properties (but NOT children yet). */
    UpdateGeometryCache(acc);
    UpdateNameCache(acc);
    UpdateDescriptionCache(acc);
    UpdateValueCache(acc);
    UpdateRoleCache(acc);
    UpdateStateCache(acc);
    /* DON'T call UpdateChildrenCache here - this causes recursion! */

    AtkObject *obj = ATK_OBJECT(acc);
    AtkRole role = acc->cached_role;
    if (role == ATK_ROLE_UNKNOWN &&
        ((gboolean)(uintptr_t)RunOnMainThread(is_toplevel_main, &data) ||
         tkwin == (Tk_Window)RunOnMainThread(get_main_window_main, &(MainThreadData){NULL, interp, NULL, NULL, NULL}))) {
        role = ATK_ROLE_WINDOW;
    }
    atk_object_set_role(obj, role);


    /* Set ATK name and description. */



    if (acc->cached_description && *acc->cached_description) {
        atk_object_set_name(obj, acc->cached_description);
    } else if (acc->cached_name && *acc->cached_name) {
        atk_object_set_name(obj, acc->cached_name);
    } else {
        atk_object_set_name(obj, path);
    }
    if (acc->cached_description && *acc->cached_description) {
        atk_object_set_description(obj, acc->cached_description);
    } else {
        atk_object_set_description(obj, path);
    }

    /* Set initial states. */
    AtkStateSet *state_set = atk_object_ref_state_set(obj);
    if (state_set != NULL) {
        atk_state_set_add_state(state_set, ATK_STATE_VISIBLE);
        atk_state_set_add_state(state_set, ATK_STATE_SHOWING);
        atk_state_set_add_state(state_set, ATK_STATE_ENABLED);
        if (role == ATK_ROLE_PUSH_BUTTON || role == ATK_ROLE_ENTRY ||
            role == ATK_ROLE_COMBO_BOX || role == ATK_ROLE_CHECK_BOX ||
            role == ATK_ROLE_RADIO_BUTTON || role == ATK_ROLE_SLIDER ||
            role == ATK_ROLE_SPIN_BUTTON) {
            atk_state_set_add_state(state_set, ATK_STATE_FOCUSABLE);
        }
        atk_object_notify_state_change(obj, ATK_STATE_VISIBLE, TRUE);
        atk_object_notify_state_change(obj, ATK_STATE_SHOWING, TRUE);
        atk_object_notify_state_change(obj, ATK_STATE_ENABLED, TRUE);
        if (atk_state_set_contains_state(state_set, ATK_STATE_FOCUSABLE)) {
            atk_object_notify_state_change(obj, ATK_STATE_FOCUSABLE, TRUE);
        }
        g_object_unref(state_set);
    }

    /* Register the object BEFORE setting up parent/child relationships. */
    RegisterAtkObjectForTkWindow(tkwin, obj);
    
    /* Set up parent relationship. */
    Tk_Window parent_tkwin = (Tk_Window)RunOnMainThread(get_window_parent_main, &data);
    AtkObject *parent_obj = NULL;

    if (parent_tkwin) {
        parent_obj = GetAtkObjectForTkWindow(parent_tkwin);
        if (!parent_obj && !g_hash_table_contains(creation_in_progress, parent_tkwin)) {
            /* Create parent only if not in recursion. */
            TkAtkAccessible *parent_acc = g_object_new(TK_ATK_TYPE_ACCESSIBLE, NULL);
            parent_acc->interp = interp;
            parent_acc->tkwin = parent_tkwin;
            parent_acc->path = sanitize_utf8((const char *)RunOnMainThread(get_window_name_main, &(MainThreadData){parent_tkwin, interp, NULL, NULL, NULL}));
            parent_acc->is_being_created = TRUE;
            parent_acc->children_initialized = FALSE;
            
            /* Update parent basic properties (no children). */
            UpdateGeometryCache(parent_acc);
            UpdateNameCache(parent_acc);
            UpdateDescriptionCache(parent_acc);
            UpdateValueCache(parent_acc);
            UpdateRoleCache(parent_acc);
            UpdateStateCache(parent_acc);
            
            parent_obj = ATK_OBJECT(parent_acc);
            atk_object_set_role(parent_obj, ATK_ROLE_WINDOW);
            if (parent_acc->cached_name) {
                atk_object_set_name(parent_obj, parent_acc->cached_name);
            } else {
                atk_object_set_name(parent_obj, parent_acc->path);
            }
            if (parent_acc->cached_description) {
                atk_object_set_description(parent_obj, parent_acc->cached_description);
            } else {
                atk_object_set_description(parent_obj, parent_acc->path);
            }









            
            RegisterAtkObjectForTkWindow(parent_tkwin, parent_obj);
            TkAtkAccessible_RegisterEventHandlers(parent_tkwin, parent_acc);
            
            /* Handle parent's parent. */
            Tk_Window grandparent_tkwin = (Tk_Window)RunOnMainThread(get_window_parent_main, &(MainThreadData){parent_tkwin, interp, NULL, NULL, NULL});
            AtkObject *grandparent_obj = grandparent_tkwin ? GetAtkObjectForTkWindow(grandparent_tkwin) : tk_root_accessible;
            if (grandparent_obj && G_IS_OBJECT(grandparent_obj)) {
                atk_object_set_parent(parent_obj, grandparent_obj);
                AddChildToParent((TkAtkAccessible *)grandparent_obj, parent_obj);







            }

            if ((gboolean)(uintptr_t)RunOnMainThread(is_toplevel_main, &(MainThreadData){parent_tkwin, interp, NULL, NULL, NULL})) {
                if (!g_list_find(toplevel_accessible_objects, parent_obj)) {
                    toplevel_accessible_objects = g_list_append(toplevel_accessible_objects, parent_obj);
                }
            }
            
            parent_acc->is_being_created = FALSE;
        }
    } else {
        parent_obj = tk_root_accessible;
    }

    if (parent_obj && G_IS_OBJECT(parent_obj)) {
        atk_object_set_parent(obj, parent_obj);
        AddChildToParent((TkAtkAccessible *)parent_obj, obj);
        
        /* Emit child addition signal */
        ChildrenChangedAddData *cad = g_new0(ChildrenChangedAddData, 1);
        cad->parent = g_object_ref(parent_obj);
        cad->index = g_list_length(((TkAtkAccessible *)parent_obj)->children) - 1;
        cad->child = g_object_ref(obj);


        RunOnMainThreadAsync(emit_children_changed_add, cad);
    } else {
        g_warning("TkCreateAccessibleAtkObject: Invalid parent for %s", path);
        g_hash_table_remove(creation_in_progress, tkwin);  /* Clean up tracking */
        g_object_unref(obj);
        return NULL;
    }

    /* Handle toplevel registration. */
    if ((gboolean)(uintptr_t)RunOnMainThread(is_toplevel_main, &data)) {
        RegisterToplevelWindow(interp, tkwin, obj);
    }

    /* Mark creation as complete. */
    acc->is_being_created = FALSE;
    
    /* Remove from creation tracking. */
    g_hash_table_remove(creation_in_progress, tkwin);

    /* Shedule children update for LATER, not now. This breaks the recursion cycle. */
    g_idle_add_full(G_PRIORITY_LOW, (GSourceFunc)DeferredChildrenUpdate, acc, NULL);

    return obj;
}

/*
 * Functions to map Tk window to its corresponding Atk object.
 */

void InitAtkTkMapping(void)
{
    if (!tk_to_atk_map) {
	tk_to_atk_map = g_hash_table_new_full(g_direct_hash, g_direct_equal,
					      NULL, (GDestroyNotify)g_object_unref);
    }
}

static void InitRecursionTracking(void)
{
    if (!creation_in_progress) {
        creation_in_progress = g_hash_table_new(g_direct_hash, g_direct_equal);
    }
}

void RegisterAtkObjectForTkWindow(Tk_Window tkwin, AtkObject *atkobj)
{
    if (!tkwin || !atkobj) return; 
    InitAtkTkMapping();
    g_object_ref(atkobj); /*Increment ref count because hash table takes ownership. */
    g_hash_table_insert(tk_to_atk_map, tkwin, atkobj);
1561
1562
1563
1564
1565
1566
1567


1568





1569



1570





1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594

1595
1596
1597
1598



1599




1600



1601

1602

1603

1604









1605
1606















1607



1608


1609
1610



1611



1612




1613




1614










1615




1616

1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
	    parent = (Tk_Window)RunOnMainThread(get_window_parent_main, &(MainThreadData){parent, NULL, NULL, NULL, NULL});
	}
    }
}

static void UpdateChildrenCache(TkAtkAccessible *acc)
{








    if (!acc || !acc->tkwin || !acc->interp) return;









    MainThreadData data = {acc->tkwin, acc->interp, NULL, NULL, NULL};
    TkWindow *winPtr = (TkWindow *)RunOnMainThread(get_window_handle_main, &data);
    if (!winPtr) return;

    /* Clear existing children list. */
    GList *iter = acc->children;
    while (iter) {
	AtkObject *child = (AtkObject *)iter->data;
	if (child && G_IS_OBJECT(child)) {
	    RemoveChildFromParent(acc, child);
	    ChildrenChangedRemoveData *crd = g_new0(ChildrenChangedRemoveData, 1);
	    crd->parent = ATK_OBJECT(acc);
	    crd->index = g_list_index(acc->children, child);
	    crd->child = child;
	    if (crd->parent) g_object_ref(crd->parent);
	    if (crd->child) g_object_ref(crd->child);
	    RunOnMainThreadAsync(emit_children_changed_remove, crd);
	    g_object_unref(child);
	}
	iter = g_list_next(iter);
    }
    g_list_free(acc->children);
    acc->children = NULL;


    /* Repopulate children list. */
    for (TkWindow *childPtr = winPtr->childList; childPtr != NULL; childPtr = childPtr->nextPtr) {
	Tk_Window child = (Tk_Window)childPtr;
	AtkObject *child_obj = GetAtkObjectForTkWindow(child);



	if (!child_obj) {




	    MainThreadData child_data = {child, acc->interp, NULL, NULL, NULL};



	    const char *child_path = (const char *)RunOnMainThread(get_window_name_main, &child_data);

	    child_obj = TkCreateAccessibleAtkObject(acc->interp, child, child_path);

	    if (child_obj) {

		RegisterAtkObjectForTkWindow(child, child_obj);









		TkAtkAccessible_RegisterEventHandlers(child, (TkAtkAccessible *)child_obj);
	    }















	}



	if (child_obj && G_IS_OBJECT(child_obj)) {


	    AddChildToParent(acc, child_obj);
	    ChildrenChangedAddData *cad = g_new0(ChildrenChangedAddData, 1);



	    cad->parent = ATK_OBJECT(acc);



	    cad->index = g_list_length(acc->children) - 1;




	    cad->child = child_obj;




	    if (cad->parent) g_object_ref(cad->parent);










	    if (cad->child) g_object_ref(cad->child);




	    RunOnMainThreadAsync(emit_children_changed_add, cad);

	}
    }
}


/*
 *----------------------------------------------------------------------
 *
 * Event handlers - update Tk and ATK in response to various X events. 
 *







>
>

>
>
>
>
>
|
>
>
>
|
>
>
>
>
>


|
|
<
<
<
<
<
<
<
<
<
<
<
|
<
|
|
<
|
|
|
|
>
|

|
|
>
>
>
|
>
>
>
>
|
>
>
>
|
>
|
>
|
>
|
>
>
>
>
>
>
>
>
>
|
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
>
>
>
|
>
>
|
|
>
>
>
|
>
>
>
|
>
>
>
>
|
>
>
>
>
|
>
>
>
>
>
>
>
>
>
>
|
>
>
>
>
|
>
|
|
|







1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698











1699

1700
1701

1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
	    parent = (Tk_Window)RunOnMainThread(get_window_parent_main, &(MainThreadData){parent, NULL, NULL, NULL, NULL});
	}
    }
}

static void UpdateChildrenCache(TkAtkAccessible *acc)
{
    if (!acc || !acc->tkwin || !acc->interp) return;
    if (acc->is_being_destroyed || acc->is_being_created) return;

    /* Prevent recursive calls. */
    static GHashTable *updating_objects = NULL;
    if (!updating_objects) {
        updating_objects = g_hash_table_new(g_direct_hash, g_direct_equal);
    }
    
    if (g_hash_table_contains(updating_objects, acc)) {
        g_debug("UpdateChildrenCache: Recursive call detected for %s", acc->path);
        return;
    }
    
    g_hash_table_add(updating_objects, acc);

    g_mutex_lock(&acc->cleanup_mutex);

    MainThreadData data = {acc->tkwin, acc->interp, NULL, NULL, NULL};
    TkWindow *winPtr = (TkWindow *)RunOnMainThread(get_window_handle_main, &data);
    if (!winPtr) {
        g_mutex_unlock(&acc->cleanup_mutex);











        g_hash_table_remove(updating_objects, acc);

        return;
    }


    /* Store old children for comparison. */
    GList *old_children = g_list_copy(acc->children);
    
    /* Build new children list - but DON'T create objects if they don't exist.*/
    GList *new_children = NULL;
    for (TkWindow *childPtr = winPtr->childList; childPtr != NULL; childPtr = childPtr->nextPtr) {
        Tk_Window child = (Tk_Window)childPtr;
        AtkObject *child_obj = GetAtkObjectForTkWindow(child);
        
        /* Only add existing objects - don't create new ones here. */
        if (child_obj && G_IS_OBJECT(child_obj)) {
            g_object_ref(child_obj);
            new_children = g_list_append(new_children, child_obj);
            atk_object_set_parent(child_obj, ATK_OBJECT(acc));
        }
    }

    /* Replace children list. */
    acc->children = new_children;
    acc->children_initialized = TRUE;
    
    g_mutex_unlock(&acc->cleanup_mutex);

    /* Clean up old children. */
    GList *iter = old_children;
    while (iter) {
        AtkObject *old_child = (AtkObject *)iter->data;
        if (old_child && G_IS_OBJECT(old_child)) {
            if (!g_list_find(new_children, old_child)) {
                gint old_index = g_list_index(old_children, old_child);
                ChildrenChangedRemoveData *crd = g_new0(ChildrenChangedRemoveData, 1);
                crd->parent = g_object_ref(ATK_OBJECT(acc));
                crd->index = old_index;
                crd->child = g_object_ref(old_child);
                RunOnMainThreadAsync(emit_children_changed_remove, crd);
            }
            g_object_unref(old_child);
        }
        iter = g_list_next(iter);
    }
    g_list_free(old_children);

    /* Emit addition signals for genuinely new children. */
    iter = new_children;
    gint index = 0;
    while (iter) {
        AtkObject *new_child = (AtkObject *)iter->data;
        if (new_child && !g_list_find(old_children, new_child)) {
            ChildrenChangedAddData *cad = g_new0(ChildrenChangedAddData, 1);
            cad->parent = g_object_ref(ATK_OBJECT(acc));
            cad->index = index;
            cad->child = g_object_ref(new_child);
            RunOnMainThreadAsync(emit_children_changed_add, cad);
        }
        iter = g_list_next(iter);
        index++;
    }

    g_hash_table_remove(updating_objects, acc);
}


static gboolean DeferredChildrenUpdate(gpointer user_data)
{
    TkAtkAccessible *acc = (TkAtkAccessible *)user_data;
    
    if (!acc || acc->is_being_destroyed || acc->children_initialized) {
        return G_SOURCE_REMOVE;
    }

    /* Now it's safe to create missing child objects. */
    if (acc->tkwin && acc->interp) {
        MainThreadData data = {acc->tkwin, acc->interp, NULL, NULL, NULL};
        TkWindow *winPtr = (TkWindow *)RunOnMainThread(get_window_handle_main, &data);
        
        if (winPtr) {
            for (TkWindow *childPtr = winPtr->childList; childPtr != NULL; childPtr = childPtr->nextPtr) {
                Tk_Window child = (Tk_Window)childPtr;
                AtkObject *child_obj = GetAtkObjectForTkWindow(child);
                
                if (!child_obj) {
                    /* Now it's safe to create the child. */
                    MainThreadData child_data = {child, acc->interp, NULL, NULL, NULL};
                    const char *child_path = (const char *)RunOnMainThread(get_window_name_main, &child_data);
                    child_obj = TkCreateAccessibleAtkObject(acc->interp, child, child_path);
                    if (child_obj) {
                        TkAtkAccessible_RegisterEventHandlers(child, (TkAtkAccessible *)child_obj);
                    }
                }
            }
            
            /* Update the children cache now that all objects exist. */
            UpdateChildrenCache(acc);
        }
    }

    return G_SOURCE_REMOVE;
}




/*
 *----------------------------------------------------------------------
 *
 * Event handlers - update Tk and ATK in response to various X events. 
 *
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648

1649

1650
1651
1652
1653
1654
1655










1656
1657
1658



1659
1660


1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675

1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695





1696

1697
1698
1699
1700
1701
1702
1703


1704
1705



1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
			  TkAtkAccessible_ConfigureHandler, tkAccessible);
    Tk_CreateEventHandler(tkwin, MapNotify,
			  TkAtkAccessible_MapHandler, tkAccessible);
    Tk_CreateEventHandler(tkwin, UnmapNotify,
			  TkAtkAccessible_UnmapHandler, tkAccessible);
    Tk_CreateEventHandler(tkwin, FocusChangeMask,
			  TkAtkAccessible_FocusHandler, tkAccessible);
	Tk_CreateEventHandler(tkwin, CreateNotify,
			  TkAtkAccessible_CreateHandler, tkAccessible);
}

/* Respond to <Destroy> events. */
static void TkAtkAccessible_DestroyHandler(ClientData clientData, XEvent *eventPtr) {

    if (eventPtr->type == DestroyNotify) {

	TkAtkAccessible *tkAccessible = (TkAtkAccessible *)clientData;
	if (!tkAccessible || !tkAccessible->tkwin) {
	    g_warning("TkAtkAccessible_DestroyHandler: Invalid or null tkAccessible/tkwin");
	    return;
	}











	AtkObject *parent = atk_object_get_parent(ATK_OBJECT(tkAccessible));
	if (parent && G_IS_OBJECT(parent)) {
	    gint index = g_list_index(((TkAtkAccessible *)parent)->children, tkAccessible);



	    if (index >= 0) {
		RemoveChildFromParent((TkAtkAccessible *)parent, ATK_OBJECT(tkAccessible));


		ChildrenChangedRemoveData *crd = g_new0(ChildrenChangedRemoveData, 1);
		crd->parent = parent;
		crd->index = index;
		crd->child = ATK_OBJECT(tkAccessible);
		if (crd->parent) g_object_ref(crd->parent);
		if (crd->child) g_object_ref(crd->child);
		RunOnMainThreadAsync(emit_children_changed_remove, crd);
	    } else {
		g_warning("TkAtkAccessible_DestroyHandler: Object not found in parent's children list");
	    }
	} else {
	    g_warning("TkAtkAccessible_DestroyHandler: Invalid or null parent object");
	}

	/* Unregister from tk_to_atk_map */

	UnregisterAtkObjectForTkWindow(tkAccessible->tkwin);

	/* Unref the accessible object */
	g_object_unref(tkAccessible);
    }
}


/* Respond to <Confgure> event. */
static void TkAtkAccessible_ConfigureHandler(ClientData clientData, XEvent *eventPtr) 
{
	
    if (eventPtr->type != ConfigureNotify) return;

    TkAtkAccessible *acc = (TkAtkAccessible *)clientData;
    if (!acc || !acc->tkwin) {
	g_warning("TkAtkAccessible_ConfigureHandler: Invalid or null acc/tkwin");
	return;
    }






    /* Update all caches. */

    UpdateGeometryCache(acc);
    UpdateNameCache(acc);
    UpdateDescriptionCache(acc);
    UpdateValueCache(acc);
    UpdateRoleCache(acc);
    UpdateStateCache(acc);
    UpdateChildrenCache(acc); /* Added to update children on configure */



    /* Notify ATK of changes */



    if (acc->width > 0 && acc->height > 0 && acc->is_mapped) {
	BoundsChangedData *bcd = g_new0(BoundsChangedData, 1);
	bcd->obj = ATK_OBJECT(acc);
	bcd->rect.x = acc->x;
	bcd->rect.y = acc->y;
	bcd->rect.width = acc->width;
	bcd->rect.height = acc->height;
	RunOnMainThreadAsync(emit_bounds_changed, bcd);
    }

    if (acc->cached_name) {
	tk_set_name(ATK_OBJECT(acc), acc->cached_name);
    }
    if (acc->cached_description) {
	atk_object_set_description(ATK_OBJECT(acc), acc->cached_description);
    }

    StateChangeData *visible_scd = g_new0(StateChangeData, 1);
    visible_scd->obj = ATK_OBJECT(acc);
    visible_scd->name = g_strdup("visible");
    visible_scd->state = acc->is_mapped;
    RunOnMainThreadAsync(emit_state_change, visible_scd);

    StateChangeData *showing_scd = g_new0(StateChangeData, 1);
    showing_scd->obj = ATK_OBJECT(acc);
    showing_scd->name = g_strdup("showing");
    showing_scd->state = acc->is_mapped;
    RunOnMainThreadAsync(emit_state_change, showing_scd);
}


/* Respond to <Map> event. */
static void TkAtkAccessible_MapHandler(ClientData clientData, XEvent *eventPtr)
{
    if (eventPtr->type != MapNotify) return;
    TkAtkAccessible *acc = (TkAtkAccessible *)clientData;
    if (!acc || !acc->tkwin) {







|




|
>
|
>
|
|
|
|
|

>
>
>
>
>
>
>
>
>
>
|
|
|
>
>
>
|
|
>
>
|
|
|
|
<
<
|
<
<
|
<
|
|

|
>
|
|
<
<







<



|
<
|


>
>
>
>
>
|
>
|
|
|
|
|
|
|
>
>
|
|
>
>
>
|
|
|
|
|
|
|
|

|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<







1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864


1865


1866

1867
1868
1869
1870
1871
1872
1873


1874
1875
1876
1877
1878
1879
1880

1881
1882
1883
1884

1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918




















1919
1920
1921
1922
1923
1924
1925
			  TkAtkAccessible_ConfigureHandler, tkAccessible);
    Tk_CreateEventHandler(tkwin, MapNotify,
			  TkAtkAccessible_MapHandler, tkAccessible);
    Tk_CreateEventHandler(tkwin, UnmapNotify,
			  TkAtkAccessible_UnmapHandler, tkAccessible);
    Tk_CreateEventHandler(tkwin, FocusChangeMask,
			  TkAtkAccessible_FocusHandler, tkAccessible);
    Tk_CreateEventHandler(tkwin, CreateNotify,
			  TkAtkAccessible_CreateHandler, tkAccessible);
}

/* Respond to <Destroy> events. */
static void TkAtkAccessible_DestroyHandler(ClientData clientData, XEvent *eventPtr) 
{
    if (eventPtr->type != DestroyNotify) return;
    
    TkAtkAccessible *tkAccessible = (TkAtkAccessible *)clientData;
    if (!tkAccessible || !G_IS_OBJECT(tkAccessible)) {
        g_warning("TkAtkAccessible_DestroyHandler: Invalid tkAccessible");
        return;
    }

    /* Thread-safe cleanup check. */
    g_mutex_lock(&tkAccessible->cleanup_mutex);
    if (tkAccessible->is_being_destroyed) {
        g_mutex_unlock(&tkAccessible->cleanup_mutex);
        return; /* Already being cleaned up. */
    }
    tkAccessible->is_being_destroyed = TRUE;
    g_mutex_unlock(&tkAccessible->cleanup_mutex);

    /* Remove from parent's children list safely. */
    AtkObject *parent = atk_object_get_parent(ATK_OBJECT(tkAccessible));
    if (parent && G_IS_OBJECT(parent) && TK_ATK_IS_ACCESSIBLE(parent)) {
        TkAtkAccessible *parent_acc = (TkAtkAccessible *)parent;
        g_mutex_lock(&parent_acc->cleanup_mutex);
        
        gint index = g_list_index(parent_acc->children, tkAccessible);
        if (index >= 0) {
            RemoveChildFromParent(parent_acc, ATK_OBJECT(tkAccessible));
            
            /* Emit signal safely. */
            ChildrenChangedRemoveData *crd = g_new0(ChildrenChangedRemoveData, 1);
            crd->parent = g_object_ref(parent);  /* Take explicit ref */
            crd->index = index;
            crd->child = g_object_ref(ATK_OBJECT(tkAccessible));  /* Take explicit ref. */


            RunOnMainThreadAsync(emit_children_changed_remove, crd);


        }

        g_mutex_unlock(&parent_acc->cleanup_mutex);
    }

    /* Unregister from mapping - this is safe.*/
    if (tkAccessible->tkwin) {
        UnregisterAtkObjectForTkWindow(tkAccessible->tkwin);
        tkAccessible->tkwin = NULL; /* Prevent reuse */


    }
}


/* Respond to <Confgure> event. */
static void TkAtkAccessible_ConfigureHandler(ClientData clientData, XEvent *eventPtr) 
{

    if (eventPtr->type != ConfigureNotify) return;

    TkAtkAccessible *acc = (TkAtkAccessible *)clientData;
    if (!acc || !acc->tkwin || acc->is_being_destroyed) {

        return;
    }

    /* Prevent recursive updates */
    static gboolean updating = FALSE;
    if (updating) return;
    updating = TRUE;

    /* Update caches safely */
    if (!acc->is_being_destroyed) {
        UpdateGeometryCache(acc);
        UpdateNameCache(acc);
        UpdateDescriptionCache(acc);
        UpdateValueCache(acc);
        UpdateRoleCache(acc);
        UpdateStateCache(acc);
        
        /* Update children cache asynchronously to avoid recursion */
        g_idle_add((GSourceFunc)UpdateChildrenCache, acc);
    }

    updating = FALSE;

    /* Emit signals for valid objects only */
    if (!acc->is_being_destroyed && acc->width > 0 && acc->height > 0 && acc->is_mapped) {
        BoundsChangedData *bcd = g_new0(BoundsChangedData, 1);
        bcd->obj = ATK_OBJECT(acc);
        bcd->rect.x = acc->x;
        bcd->rect.y = acc->y;
        bcd->rect.width = acc->width;
        bcd->rect.height = acc->height;
        RunOnMainThreadAsync(emit_bounds_changed, bcd);
    }
}





















/* Respond to <Map> event. */
static void TkAtkAccessible_MapHandler(ClientData clientData, XEvent *eventPtr)
{
    if (eventPtr->type != MapNotify) return;
    TkAtkAccessible *acc = (TkAtkAccessible *)clientData;
    if (!acc || !acc->tkwin) {
2169
2170
2171
2172
2173
2174
2175

2176
2177
2178
2179
2180
2181
2182
	atk_object_notify_state_change(tk_root_accessible, ATK_STATE_VISIBLE, TRUE);
	atk_object_notify_state_change(tk_root_accessible, ATK_STATE_SHOWING, TRUE);
	atk_object_notify_state_change(tk_root_accessible, ATK_STATE_ENABLED, TRUE);
	g_object_unref(state_set);
    }

    InitAtkTkMapping();


    /* Initialize main window. */
    Tk_Window mainWin = Tk_MainWindow(interp);
    if (!mainWin) {
        Tcl_SetResult(interp, "Failed to get main window", TCL_STATIC);
        return TCL_ERROR;
    }







>







2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
	atk_object_notify_state_change(tk_root_accessible, ATK_STATE_VISIBLE, TRUE);
	atk_object_notify_state_change(tk_root_accessible, ATK_STATE_SHOWING, TRUE);
	atk_object_notify_state_change(tk_root_accessible, ATK_STATE_ENABLED, TRUE);
	g_object_unref(state_set);
    }

    InitAtkTkMapping();
    InitRecursionTracking();

    /* Initialize main window. */
    Tk_Window mainWin = Tk_MainWindow(interp);
    if (!mainWin) {
        Tcl_SetResult(interp, "Failed to get main window", TCL_STATIC);
        return TCL_ERROR;
    }