2001-11-23 Michael Meeks <michael@ximian.com>
[platform/core/uifw/at-spi2-atk.git] / cspi / spi_main.c
1 /*
2  *
3  * Basic SPI initialization and event loop function prototypes
4  *
5  */
6 #include <stdlib.h>
7 #include <libbonobo.h>
8 #include <cspi/spi-private.h>
9
10 static CORBA_Environment ev = { 0 };
11 static AccessibilityRegistry registry = CORBA_OBJECT_NIL;
12
13 CORBA_Environment *
14 spi_ev (void)
15 {
16   /* This method is an ugly hack */
17   return &ev;
18 }
19
20 AccessibilityRegistry
21 spi_registry (void)
22 {
23   /* This method is an ugly hack */
24   return registry;
25 }
26
27 boolean
28 spi_exception (void)
29 {
30   boolean retval;
31
32   if (BONOBO_EX (&ev))
33     {
34       CORBA_exception_free (&ev);
35       retval = TRUE;
36     }
37   else
38     {
39       retval = FALSE;
40     }
41
42   return retval;
43 }
44
45 Accessible *
46 spi_object_add (Accessible corba_object)
47 {
48   /* TODO: keep list of live object refs */
49   Accessible *oref = NULL;
50
51   if (corba_object != CORBA_OBJECT_NIL)
52     {
53       oref = g_malloc (sizeof (Accessible));
54       *oref = corba_object;
55     }
56  
57   return oref;
58 }
59
60 /**
61  * SPI_init:
62  *
63  * Connects to the accessibility registry and initializes the SPI.
64  *
65  * Returns: 0 on success, otherwise an integer error code.
66  **/
67 int
68 SPI_init (void)
69 {
70   int argc = 0;
71   char *obj_id;
72   CORBA_Object oclient;
73   static gboolean inited = FALSE;
74
75   if (inited)
76     {
77       return 1;
78     }
79
80   inited = TRUE;
81
82   CORBA_exception_init (&ev);
83
84   if (!bonobo_init (&argc, NULL))
85     {
86       g_error ("Could not initialize Bonobo");
87     }
88
89   obj_id = "OAFIID:Accessibility_Registry:proto0.1";
90
91   oclient = bonobo_activation_activate_from_id (
92           obj_id, 0, NULL, spi_ev ());
93
94   if (ev._major != CORBA_NO_EXCEPTION)
95     {
96       g_error ("AT-SPI error: during registry activation: %s\n",
97                bonobo_exception_get_text (spi_ev ()));
98     }
99
100   if (CORBA_Object_is_nil (oclient, spi_ev ()))
101     {
102       g_error ("Could not locate registry");
103     }
104
105   registry = (Accessibility_Registry) oclient;
106
107   bonobo_activate ();
108
109   return 0;
110 }
111
112 /**
113  * SPI_event_main:
114  * @isGNOMEApp: a #boolean indicating whether the client of the SPI
115  *              will use the Gnome event loop or not.  Clients that have
116  *              their own GUIS will usually specify #TRUE here, and must
117  *              do so if they use Gnome GUI components.
118  *
119  * Starts/enters the main event loop for the SPI services.
120  *
121  * (NOTE: This method does not return control, it is exited via a call to exit()
122  * from within an event handler).
123  *
124  **/
125 void
126 SPI_event_main (boolean isGNOMEApp)
127 {
128   if (isGNOMEApp)
129     {
130       g_atexit (SPI_exit);
131       bonobo_main ();
132     }
133   else
134     {
135       /* TODO: install signal handlers to do cleanup */
136       CORBA_ORB_run (bonobo_orb(), spi_ev ());
137       fprintf (stderr, "orb loop exited...\n");
138     }
139 }
140
141 /**
142  * SPI_eventIsReady:
143  *
144  * Checks to see if an SPI event is waiting in the event queue.
145  * Used by clients that don't wish to use SPI_event_main().
146  *
147  * Not Yet Implemented.
148  *
149  * Returns: #TRUE if an event is waiting, otherwise #FALSE.
150  *
151  **/
152 boolean
153 SPI_eventIsReady ()
154 {
155   return FALSE;
156 }
157
158 /**
159  * SPI_nextEvent:
160  * @waitForEvent: a #boolean indicating whether to block or not.
161  *
162  * Gets the next event in the SPI event queue; blocks if no event
163  * is pending and @waitForEvent is #TRUE.
164  * Used by clients that don't wish to use SPI_event_main().
165  *
166  * Not Yet Implemented.
167  *
168  * Returns: the next #AccessibleEvent in the SPI event queue.
169  *
170  **/
171 AccessibleEvent *
172 SPI_nextEvent (boolean waitForEvent)
173 {
174   return NULL;
175 }
176
177 /**
178  * SPI_exit:
179  *
180  * Disconnects from the Accessibility Registry and releases resources.
181  *
182  **/
183 void
184 SPI_exit (void)
185 {
186   fprintf (stderr, "bye-bye!\n");       
187   exit(0);
188 }
189