Fixes for #149962, #136986, #144086.
[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
29 CORBA_Object
30 cspi_dup_ref (CORBA_Object object)
31 {
32   return bonobo_object_dup_ref (object, cspi_ev ());
33 }
34
35 void
36 cspi_release_unref (CORBA_Object object)
37 {
38     bonobo_object_release_unref (object, NULL); 
39 }
40
41 SPIBoolean
42 cspi_check_ev (const char *error_string)
43 {
44   CORBA_Environment *ev = cspi_peek_ev ();
45
46   if (ev->_major != CORBA_NO_EXCEPTION)
47     {
48       char *err;
49
50       err = bonobo_exception_get_text (ev);
51
52       if (!_cspi_exception_throw (ev, (char *) error_string)) {
53         fprintf (stderr, "Warning: AT-SPI error: %s: %s\n",
54                  error_string, err);
55       }
56
57       g_free (err);
58
59       CORBA_exception_free (ev);
60
61       return FALSE;
62     }
63   else
64     {
65       return TRUE;
66     }
67 }
68
69 char *
70 cspi_exception_get_text (void)
71 {
72   char *ret, *txt;
73
74   txt = bonobo_exception_get_text (cspi_ev ());
75   ret = strdup (txt);
76   g_free (txt);
77
78   return ret;
79 }
80
81 /* 
82  * Returns a 'canonicalized' value for DISPLAY,
83  * with the screen number stripped off if present.
84  */
85 static const gchar*
86 cspi_display_name (void)
87 {
88     static const char *canonical_display_name = NULL;
89     if (!canonical_display_name)
90     {
91         const gchar *display_env = g_getenv ("AT_SPI_DISPLAY");
92         if (!display_env)
93         {
94             display_env = g_getenv ("DISPLAY");
95             if (!display_env || !display_env[0]) 
96                 canonical_display_name = ":0";
97             else
98             {
99                 canonical_display_name = g_strdup (display_env);
100                 gchar *display_p = strrchr (canonical_display_name, ':');
101                 gchar *screen_p = strrchr (canonical_display_name, '.');
102                 if (screen_p && display_p && ((guint) screen_p > (guint) display_p))
103                 {
104                     *screen_p = '\0';
105                 }
106             }
107         }
108         else
109         {
110             canonical_display_name = display_env;
111         }
112     }
113     return canonical_display_name;
114 }
115
116 CORBA_Object
117 cspi_init (void)
118 {
119   char *obj_id;
120   CORBA_Object registry;
121   CORBA_Environment ev;
122
123   if (!bonobo_init (0, NULL))
124     {
125       g_error ("Could not initialize Bonobo");
126     }
127
128   obj_id = "OAFIID:Accessibility_Registry:1.0";
129
130   CORBA_exception_init (&ev);
131
132   bonobo_activation_set_activation_env_value ("AT_SPI_DISPLAY", 
133                                               cspi_display_name ());
134
135   registry = bonobo_activation_activate_from_id (
136     obj_id, 0, NULL, &ev);
137
138   if (ev._major != CORBA_NO_EXCEPTION)
139     {
140       g_error ("AT-SPI error: during registry activation: %s\n",
141                bonobo_exception_get_text (&ev));
142     }
143
144   if (registry == CORBA_OBJECT_NIL)
145     {
146       g_error ("Could not locate registry");
147     }
148
149   bonobo_activate ();
150
151   return registry;
152 }
153
154 SPIBoolean
155 cspi_ping (CORBA_Object object)
156 {
157         return bonobo_unknown_ping (object, NULL);
158 }
159
160 void
161 cspi_main (void)
162 {
163   bonobo_main ();
164 }
165
166 void
167 cspi_main_quit (void)
168 {
169   bonobo_main_quit ();
170 }