fcbbaecf66baf34cffe98c53248f64dbc4585e89
[platform/core/uifw/at-spi2-atk.git] / test / login-helper-client-test.c
1 /* 
2  * test utility for LoginHelper client API and services.
3  *
4  * Copyright Sun Microsystems 2004
5  *
6  */
7
8 #include <X11/X.h>
9 #include <libbonobo.h>
10 #include <login-helper/Accessibility_LoginHelper.h>
11
12 gint main (int argc, char **argv)
13 {
14     Bonobo_ServerInfoList *server_list;
15     Accessibility_LoginHelper *helper_list = NULL;
16     CORBA_boolean safe;
17     CORBA_Environment ev;
18     Accessibility_LoginHelper_WindowList *windows;
19     int i;
20
21     CORBA_exception_init (&ev);
22     if (!bonobo_init (&argc, argv))
23     {
24         g_error ("Can't initialize Bonobo");
25     }
26
27     /* bonobo-activation query lists existing instances */
28     server_list = bonobo_activation_query (
29         "(repo_ids.has('IDL:Accessibility/LoginHelper:1.0')) AND _active", 
30         NULL, &ev);
31
32     if (BONOBO_EX (&ev)) 
33     {
34         bonobo_debug_shutdown ();
35         g_error ("LoginHelper query failed : %s",
36                  bonobo_exception_get_text (&ev));
37         /* not reached (below) because g_error exits */
38         CORBA_exception_free (&ev);
39     }
40
41     g_message ("%d LoginHelpers are running.", 
42                server_list ? server_list->_length : 0);
43
44     helper_list = g_new0 (Accessibility_LoginHelper, server_list->_length);
45
46     /* for each instance... */
47     for (i = 0; i < server_list->_length; i++)
48     {
49         Accessibility_LoginHelper helper;
50         Bonobo_Unknown server;
51         Bonobo_ServerInfo info = server_list->_buffer[i];
52
53         server = bonobo_activation_activate_from_id (
54             info.iid, Bonobo_ACTIVATION_FLAG_EXISTING_ONLY, NULL, &ev);
55
56         if (BONOBO_EX (&ev))
57         {
58             g_warning ("Error activating server %d: %s", i, bonobo_exception_get_text (&ev));
59             CORBA_exception_free (&ev);
60             continue;
61         }
62         else if (server == CORBA_OBJECT_NIL)
63         {
64             g_warning ("Activated server %d is NIL!", i);
65             continue;
66         }
67
68         bonobo_activate ();
69
70         helper = Bonobo_Unknown_queryInterface (
71             server, 
72             "IDL:Accessibility/LoginHelper:1.0",
73             &ev);
74
75         if (BONOBO_EX (&ev))
76         {
77             g_warning ("Error performing interface query: %s", bonobo_exception_get_text (&ev));
78             CORBA_exception_free (&ev);
79             continue;
80         }
81         else if (helper == CORBA_OBJECT_NIL)
82         {
83             g_warning ("Activated an object which advertised LoginHelper but does not implement it!");
84             continue;
85         }
86         helper_list[i] = helper;
87         bonobo_object_release_unref (server, &ev);
88
89         if (helper && !BONOBO_EX (&ev))
90         {
91             /* ask the helper to go into safe mode */
92             safe = Accessibility_LoginHelper_setSafe (helper, TRUE, &ev);
93             if (BONOBO_EX (&ev))
94             {
95                 g_warning ("setSafe(TRUE) failed: %s", 
96                            bonobo_exception_get_text (&ev));
97                 CORBA_exception_free (&ev);
98             }
99             /* get the raise window list (if the program went into safe mode) */
100             if (safe) 
101             {
102                 int j;
103                 gboolean needs_windows_raised = FALSE;
104                 Accessibility_LoginHelper_DeviceReqList *list;
105
106                 g_message ("safe");
107
108                 /* does this helper need to have windows raised? */
109                 list = Accessibility_LoginHelper_getDeviceReqs (helper, &ev);
110
111                 if (BONOBO_EX (&ev)) {
112                     g_warning ("Bonobo exception getting Device Requirements: %s",
113                              bonobo_exception_get_text (&ev));
114                     CORBA_exception_free (&ev);
115                 }
116                 else
117                 {
118                     g_message ("LoginHelper device requirements: ");
119                     if (list->_length == 0) 
120                         g_message ("\tNone.");
121
122                     for (j = 0; j < list->_length; j++) 
123                     {
124                         switch (list->_buffer[j])
125                         {
126                             case Accessibility_LoginHelper_GUI_EVENTS:
127                                 g_message ("\tNeeds access to the GUI event subsystem (e.g. Xserver)");
128                                 break;
129                             case Accessibility_LoginHelper_EXT_INPUT:
130                                 g_message ("\tReads XInput extended input devices");
131                                 break;
132                             case Accessibility_LoginHelper_POST_WINDOWS:
133                                 g_message ("\tPosts windows");
134                                 needs_windows_raised = TRUE;
135                                 break;
136                             case Accessibility_LoginHelper_AUDIO_OUT:
137                                 g_message ("\tWrites to audio device");
138                                 break;
139                             case Accessibility_LoginHelper_AUDIO_IN:
140                                 g_message ("\tReads from audio device");
141                                 break;
142                             case Accessibility_LoginHelper_LOCALHOST:
143                                 g_message ("\tNeeds LOCALHOST network connection");
144                                 break;
145                             case Accessibility_LoginHelper_SERIAL_OUT:
146                                 g_message ("\tNeeds to write to one or more serial ports");
147                                 break;
148                             default:
149                                 break;
150                         }
151                     }
152                     CORBA_free (list);
153                 }
154                 if (needs_windows_raised) 
155                 {
156                     /* don't raise in this test, but do something with each wid */
157                     windows = Accessibility_LoginHelper_getRaiseWindows (helper, &ev);
158                     if (BONOBO_EX (&ev))
159                     {
160                         g_warning ("getRaiseWindows failed: %s", 
161                                    bonobo_exception_get_text (&ev));
162                         CORBA_exception_free (&ev);
163                     }
164                     g_message ("%d windows need raising", windows->_length);
165                     for (j = 0; j < windows->_length; j++)      
166                     {
167                         Window wid;
168                         wid = windows->_buffer[j].winID;
169                         g_message ("Window ID = %ld", (long) wid);
170                     }    
171                 }
172             }
173             else
174             {
175                 g_warning ("LoginHelper %d did not go into safe mode", i);
176             }
177         }
178         else
179         {
180             if (BONOBO_EX (&ev))
181             {
182                 g_warning ("Error activating %s: %s", 
183                            info.iid, bonobo_exception_get_text (&ev));
184                 CORBA_exception_free (&ev);
185             }
186             else
187             {
188                 g_warning ("no active instance of %s found", info.iid);
189             }
190         }
191     }
192     
193     /* 
194      * In an actual login/authentication setting, login/authentication 
195      * would take place here, and code below would be executed after 
196      * authentication was complete.
197      */
198     
199     /* now tell the apps they can resume their normal activities */
200     for (i = 0; i < server_list->_length; i++)
201     {
202         Accessibility_LoginHelper helper = helper_list[i];
203         /* really no need to check the return value this time */
204         Accessibility_LoginHelper_setSafe (helper, FALSE, &ev);
205         if (BONOBO_EX (&ev))
206         {
207             g_warning ("setSafe(FALSE) failed: %s", 
208                        bonobo_exception_get_text (&ev));
209             CORBA_exception_free (&ev);
210         }
211         CORBA_Object_release (helper, &ev);
212     }   
213     CORBA_free (server_list);
214
215     return bonobo_debug_shutdown (); /* see if we leaked stuff */
216 }