Cleaned up some suspect int* casts, and added assertions to text calls in libspi
[platform/core/uifw/at-spi2-atk.git] / libspi / application.c
1 /*
2  * AT-SPI - Assistive Technology Service Provider Interface
3  * (Gnome Accessibility Project; http://developer.gnome.org/projects/gap)
4  *
5  * Copyright 2001 Sun Microsystems Inc.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 /*
24  * application.c: implements Application.idl
25  *
26  */
27 #include <config.h>
28 #include <bonobo/Bonobo.h>
29 #include <atk/atkutil.h>
30
31 /*
32  * This pulls the CORBA definitions for the "Accessibility::Accessible" server
33  */
34 #include <libspi/Accessibility.h>
35
36 /*
37  * This pulls the definition for the BonoboObject (GObject Type)
38  */
39 #include "application.h"
40
41 /*
42  * Our parent Gtk object type
43  */
44 #define PARENT_TYPE ACCESSIBLE_TYPE
45
46 /*
47  * A pointer to our parent object class
48  */
49 static AccessibleClass *application_parent_class;
50
51 Accessibility_EventListener the_toolkit_listener;
52
53 /*
54  * Implemented GObject::finalize
55  */
56 static void
57 accessible_application_finalize (GObject *object)
58 {
59   /* TODO: any necessary cleanup */
60   Accessible *accessible = ACCESSIBLE (object);
61
62   g_object_unref (accessible->atko);
63   accessible->atko = NULL;
64
65   /* TODO: chain to parent class instead */
66 }
67
68 static CORBA_string
69 impl_accessibility_application_get_toolkit_name (PortableServer_Servant servant,
70                                                  CORBA_Environment *ev)
71 {
72   CORBA_char *retval;
73   Application *application = APPLICATION (bonobo_object_from_servant (servant));
74   retval = CORBA_string_dup (atk_get_toolkit_name ());
75   return retval;
76 }
77
78 static CORBA_string
79 impl_accessibility_application_get_version (PortableServer_Servant servant,
80                                             CORBA_Environment *ev)
81 {
82   CORBA_char *retval;
83   Application *application = APPLICATION (bonobo_object_from_servant (servant));
84   retval = CORBA_string_dup (atk_get_toolkit_version ());
85   return retval;
86 }
87
88 static CORBA_long
89 impl_accessibility_application_get_id (PortableServer_Servant servant,
90                                        CORBA_Environment *ev)
91 {
92   CORBA_long retval;
93   Application *application = APPLICATION (bonobo_object_from_servant (servant));
94   retval = (CORBA_long) application->id;
95   return retval;
96 }
97
98 static void
99 impl_accessibility_application_set_id (PortableServer_Servant servant,
100                                        const CORBA_long id,
101                                        CORBA_Environment *ev)
102 {
103   Application *application = APPLICATION (bonobo_object_from_servant (servant));
104   application->id = id;
105 }
106
107 #define APP_STATIC_BUFF_SZ 64
108
109 static gboolean
110 application_toolkit_listener (GSignalInvocationHint *signal_hint,
111                               guint n_param_values,
112                               const GValue *param_values,
113                               gpointer data)
114 {
115   Accessibility_Event *e = g_new0(Accessibility_Event, 1);
116   AtkObject *aobject;
117   GObject *gobject;
118   CORBA_Environment ev;
119   GSignalQuery signal_query;
120   gchar *name;
121   char sbuf[APP_STATIC_BUFF_SZ];
122
123   g_signal_query (signal_hint->signal_id, &signal_query);
124   name = signal_query.signal_name;
125   fprintf (stderr, "Received signal %s:%s\n", g_type_name (signal_query.itype), name);
126
127   /* TODO: move GTK dependency out of app.c into bridge */
128   snprintf(sbuf, APP_STATIC_BUFF_SZ, "Gtk:%s:%s", g_type_name (signal_query.itype), name);
129
130
131   gobject = g_value_get_object (param_values + 0);
132   /* notify the actual listeners */
133   if (ATK_IS_IMPLEMENTOR (gobject))
134     {
135       aobject = atk_implementor_ref_accessible (ATK_IMPLEMENTOR (gobject));
136       e->type = CORBA_string_dup (sbuf);
137       e->source = bonobo_object_corba_objref (bonobo_object (accessible_new (aobject)));
138       e->detail1 = 0;
139       e->detail2 = 0;
140       Accessibility_EventListener_notifyEvent (the_toolkit_listener, e, &ev);
141       g_object_unref (aobject);
142     }
143   return TRUE;
144 }
145
146 static void
147 impl_accessibility_application_register_toolkit_event_listener (PortableServer_Servant servant,
148                                                                 Accessibility_EventListener listener,
149                                                                 const CORBA_char *event_name,
150                                                                 CORBA_Environment *ev)
151 {
152   guint listener_id;
153   listener_id =
154      atk_add_global_event_listener (application_toolkit_listener, event_name);
155   the_toolkit_listener = CORBA_Object_duplicate (listener, ev);
156 #ifdef SPI_DEBUG
157   fprintf (stderr, "registered %d for toolkit events named: %s\n",
158            listener_id,
159            event_name);
160 #endif
161 }
162
163 static void
164 application_class_init (ApplicationClass *klass)
165 {
166   GObjectClass * object_class = (GObjectClass *) klass;
167   POA_Accessibility_Application__epv *epv = &klass->epv;
168
169   application_parent_class = g_type_class_ref (ACCESSIBLE_TYPE);
170
171   object_class->finalize = accessible_application_finalize;
172
173   epv->_get_toolkitName = impl_accessibility_application_get_toolkit_name;
174   epv->_get_version = impl_accessibility_application_get_version;
175   epv->_get_id = impl_accessibility_application_get_id;
176   epv->_set_id = impl_accessibility_application_set_id;
177   epv->registerToolkitEventListener = impl_accessibility_application_register_toolkit_event_listener;
178 }
179
180 static void
181 application_init (Application  *application)
182 {
183   ACCESSIBLE (application)->atko = g_object_new (atk_object_get_type(), NULL);
184 }
185
186 GType
187 application_get_type (void)
188 {
189         static GType type = 0;
190
191         if (!type) {
192                 static const GTypeInfo tinfo = {
193                         sizeof (ApplicationClass),
194                         (GBaseInitFunc) NULL,
195                         (GBaseFinalizeFunc) NULL,
196                         (GClassInitFunc) application_class_init,
197                         (GClassFinalizeFunc) NULL,
198                         NULL, /* class data */
199                         sizeof (Application),
200                         0, /* n preallocs */
201                         (GInstanceInitFunc) application_init,
202                         NULL /* value table */
203                 };
204                 /*
205                  * Bonobo_type_unique auto-generates a load of
206                  * CORBA structures for us. All derived types must
207                  * use bonobo_type_unique.
208                  */
209                 type = bonobo_type_unique (
210                         PARENT_TYPE,
211                         POA_Accessibility_Application__init,
212                         NULL,
213                         G_STRUCT_OFFSET (ApplicationClass, epv),
214                         &tinfo,
215                         "Application");
216         }
217
218         return type;
219 }
220
221 Application *
222 application_new (AtkObject *app_root)
223 {
224     Application *retval =
225                APPLICATION (g_object_new (application_get_type (), NULL));
226     ACCESSIBLE (retval)->atko = app_root;
227     return retval;
228 }