Changes to introspection generation to remove DOCTYPE and XML
[platform/core/uifw/at-spi2-atk.git] / cspi / bonobo / cspi-bonobo.c
1 /*
2  * AT-SPI - Assistive Technology Service Provider Interface
3  * (Gnome Accessibility Project; http://developer.gnome.org/projects/gap)
4  *
5  * Copyright 2001, 2002 Sun Microsystems Inc.,
6  * Copyright 2001, 2002 Ximian, Inc.
7  *           2002 Ximian Inc.
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Library General Public
11  * License as published by the Free Software Foundation; either
12  * version 2 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Library General Public License for more details.
18  *
19  * You should have received a copy of the GNU Library General Public
20  * License along with this library; if not, write to the
21  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22  * Boston, MA 02111-1307, USA.
23  */
24
25 #include <string.h>
26 #include <libbonobo.h>
27 #include "../cspi-lowlevel.h"
28 #include <X11/Xlib.h>
29 #include <X11/Xatom.h>
30
31 CORBA_Object
32 cspi_dup_ref (CORBA_Object object)
33 {
34   return bonobo_object_dup_ref (object, cspi_ev ());
35 }
36
37 void
38 cspi_release_unref (CORBA_Object object)
39 {
40     bonobo_object_release_unref (object, NULL); 
41 }
42
43 SPIBoolean
44 cspi_check_ev (const char *error_string)
45 {
46   CORBA_Environment *ev = cspi_peek_ev ();
47
48   if (ev->_major != CORBA_NO_EXCEPTION)
49     {
50       char *err;
51
52       err = bonobo_exception_get_text (ev);
53
54       if (!cspi_exception_throw (ev, (char *) error_string)) {
55         fprintf (stderr, "Warning: AT-SPI error: %s: %s\n",
56                  error_string, err);
57       }
58
59       g_free (err);
60
61       CORBA_exception_free (ev);
62
63       return FALSE;
64     }
65   else
66     {
67       return TRUE;
68     }
69 }
70
71 char *
72 cspi_exception_get_text (void)
73 {
74   char *ret, *txt;
75
76   txt = bonobo_exception_get_text (cspi_ev ());
77   ret = strdup (txt);
78   g_free (txt);
79
80   return ret;
81 }
82
83 /* 
84  * Returns a 'canonicalized' value for DISPLAY,
85  * with the screen number stripped off if present.
86  */
87 static const gchar*
88 cspi_display_name (void)
89 {
90     static const char *canonical_display_name = NULL;
91     if (!canonical_display_name)
92     {
93         const gchar *display_env = g_getenv ("AT_SPI_DISPLAY");
94         if (!display_env)
95         {
96             display_env = g_getenv ("DISPLAY");
97             if (!display_env || !display_env[0]) 
98                 canonical_display_name = ":0";
99             else
100             {
101                 gchar *display_p, *screen_p;
102                 canonical_display_name = g_strdup (display_env);
103                 display_p = strrchr (canonical_display_name, ':');
104                 screen_p = strrchr (canonical_display_name, '.');
105                 if (screen_p && display_p && (screen_p > display_p))
106                 {
107                     *screen_p = '\0';
108                 }
109             }
110         }
111         else
112         {
113             canonical_display_name = display_env;
114         }
115     }
116     return canonical_display_name;
117 }
118
119 static gchar *
120 cspi_get_registry_ior (void)
121 {
122   Atom AT_SPI_IOR;
123   Atom actual_type;
124   int actual_format;
125   unsigned char *data = NULL;
126   unsigned long nitems;
127   unsigned long leftover;
128   static Display *display = NULL;
129   if (!display)
130     display = XOpenDisplay (cspi_display_name ());
131
132   AT_SPI_IOR = XInternAtom (display, "AT_SPI_IOR", False);
133   XGetWindowProperty(display,
134                      XDefaultRootWindow (display),
135                      AT_SPI_IOR, 0L,
136                      (long)BUFSIZ, False,
137                      (Atom) 31, &actual_type, &actual_format,
138                      &nitems, &leftover, &data);
139   if (data == NULL)
140     g_warning ("AT_SPI_REGISTRY was not started at session startup.");
141
142   return (gchar *) data;
143 }
144
145 CORBA_Object
146 cspi_init (void)
147 {
148   CORBA_Object registry = NULL;
149   CORBA_Environment ev;
150   char *ior =  NULL;
151
152   if (!bonobo_init (NULL, NULL))
153     {
154       g_error ("Could not initialize Bonobo");
155     }
156
157   CORBA_exception_init (&ev);
158
159   ior = (char *) cspi_get_registry_ior ();
160   if (ior != NULL)
161     {
162       registry = CORBA_ORB_string_to_object (bonobo_activation_orb_get (),
163                                                  ior, &ev);
164     }
165
166   if (ev._major != CORBA_NO_EXCEPTION)
167     {
168       g_error ("AT-SPI error: during registry activation: %s\n",
169                bonobo_exception_get_text (&ev));
170     }
171
172   if (registry == CORBA_OBJECT_NIL)
173     {
174       g_warning ("Could not locate registry");
175     }
176
177   bonobo_activate ();
178
179   return registry;
180 }
181
182 SPIBoolean
183 cspi_ping (CORBA_Object object)
184 {
185         return bonobo_unknown_ping (object, NULL);
186 }
187
188 void
189 cspi_main (void)
190 {
191   bonobo_main ();
192 }
193
194 void
195 cspi_main_quit (void)
196 {
197   bonobo_main_quit ();
198 }