r1992: Code tidying (Bernard Jungen).
[rox-filer.git] / ROX-Filer / src / sc.c
blob9aedf2e59c11d12fa87fb1b196a5292d1ed8c212
1 /*
2 * $Id$
4 * ROX-Filer, filer for the ROX desktop project
5 * Copyright (C) 2002, the ROX-Filer team.
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the Free
9 * Software Foundation; either version 2 of the License, or (at your option)
10 * any later version.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
19 * Place, Suite 330, Boston, MA 02111-1307 USA
22 /* sc.c - XSMP client support */
24 #include <stdlib.h>
25 #include <fcntl.h>
26 #include <gtk/gtk.h>
27 #include <X11/Xlib.h>
28 #include <X11/SM/SMlib.h>
29 #include <pwd.h>
30 #include <string.h>
32 #include "sc.h"
34 static SmPropValue *make_list_of_array_value(const gchar *vals[], gint nvals)
36 SmPropValue *values = g_new(SmPropValue, nvals);
37 gint i;
39 for(i = 0; i < nvals; i++)
41 values[i].value = g_strdup(vals[i]);
42 values[i].length = strlen(vals[i]);
44 return values;
47 static SmPropValue *make_array_value(const gchar *val)
49 SmPropValue *value = g_new(SmPropValue, 1);
51 value->value = g_strdup(val);
52 value->length = strlen(val);
54 return value;
57 static SmPropValue *make_card_value(gchar val)
59 SmPropValue *value = g_new(SmPropValue, 1);
61 value->value = g_memdup(&val, sizeof(gchar));
62 value->length = 1;
64 return value;
67 static SmProperty *find_property(SmClient *client, const gchar *name)
69 GSList *list = client->props;
70 SmProperty *prop;
72 for (; list; list = list->next)
74 prop = (SmProperty *)list->data;
75 if (strcmp(prop->prop.name, name) == 0)
76 return prop;
78 return NULL;
81 static SmProperty *new_property(SmClient *client,
82 const gchar *name, const gchar *type)
84 SmProperty *prop;
86 prop = g_new(SmProperty, 1);
87 prop->prop.name = g_strdup(name);
88 prop->prop.type = g_strdup(type);
89 prop->prop.vals = NULL;
90 prop->prop.num_vals = 0;
91 #ifdef DEBUG
92 g_printerr("appending prop %s\n", prop->prop.name);
93 #endif
94 client->props = g_slist_append(client->props, prop);
96 return prop;
99 static gint close_connection(gpointer data)
101 SmClient *client = (SmClient *)data;
103 SmcCloseConnection(client->conn, 0, NULL);
105 return 0;
108 /* Called when there's data to be read on the ICE file descriptor.
109 Unpacks the message and triggers the correct callback... I think */
111 static void poll_ice_messages(gpointer data, gint source,
112 GdkInputCondition condition)
114 SmClient *client = (SmClient *)data;
115 Bool ret;
117 if (IceProcessMessages(client->ice_conn, NULL, &ret) ==
118 IceProcessMessagesIOError)
119 SmcCloseConnection(client->conn, 0, NULL);
120 return;
123 /* Called whenever an ICE connection is opened or closed */
125 static void ice_watch_fn(IceConn conn, IcePointer client_data,
126 Bool opening, IcePointer *watch_data)
128 SmClient *client = (SmClient *)client_data;
130 if(opening)
132 #ifdef DEBUG
133 g_printerr("Ice connection opened for id %s\n", client->id);
134 #endif
135 client->fd = IceConnectionNumber(conn);
136 fcntl(client->fd, F_SETFD, FD_CLOEXEC);
137 client->input_tag = gdk_input_add(client->fd, GDK_INPUT_READ, &poll_ice_messages, client);
139 else
141 if (IceConnectionNumber(conn) == client->fd)
143 #ifdef DEBUG
144 g_printerr("Ice connection closed for id %s\n", client->id);
145 #endif
146 gdk_input_remove(client->input_tag);
147 sc_destroy(client);
152 /* Callbacks for different SM messages */
154 static void sc_save_yourself(SmcConn conn, SmPointer client_data, int save_type,
155 Bool shutdown, int interact_style, Bool fast)
157 SmClient *client = (SmClient *)client_data;
158 gboolean success = TRUE;
159 #ifdef DEBUG
160 g_printerr("%s saving state\n", client->id);
161 #endif
162 if(client->save_yourself_fn)
163 success = client->save_yourself_fn(client);
164 if(success)
165 sc_register_properties(client);
166 SmcSaveYourselfDone(client->conn, success);
169 static void sc_shutdown_cancelled(SmcConn conn, SmPointer client_data)
171 SmClient *client = (SmClient *)client_data;
172 #ifdef DEBUG
173 g_printerr("%s shutdown cancelled\n", client->id);
174 #endif
175 if(client->shutdown_cancelled_fn)
176 client->shutdown_cancelled_fn(client);
179 static void sc_save_complete(SmcConn conn, SmPointer client_data)
181 SmClient *client = (SmClient *)client_data;
182 #ifdef DEBUG
183 g_printerr("%s save complete\n", client->id);
184 #endif
185 if(client->save_complete_fn)
186 client->save_complete_fn(client);
189 static void sc_die(SmcConn conn, SmPointer client_data)
191 SmClient *client = (SmClient *)client_data;
192 #ifdef DEBUG
193 g_printerr("%s dying\n", client->id);
194 #endif
195 if(client->die_fn)
196 client->die_fn(client);
199 gboolean sc_session_up()
201 if(getenv("SESSION_MANAGER") == NULL)
202 return FALSE;
203 return TRUE;
206 SmClient *sc_new(const gchar *client_id)
208 SmClient *client;
210 #ifdef DEBUG
211 g_printerr("Creating new sm-client\n");
212 #endif
213 client = g_new(SmClient, 1);
214 client->id = g_strdup(client_id);
215 client->props = NULL;
216 client->conn = NULL;
217 client->ice_conn = NULL;
218 client->fd = -1;
220 return client;
223 void sc_destroy(SmClient *client)
225 GSList *list = client->props;
226 SmProperty *prop;
228 #ifdef DEBUG
229 g_printerr("destroying client\n");
230 #endif
231 for (; list; list = list->next)
233 prop = (SmProperty *)list->data;
234 g_free(prop->prop.vals->value);
235 g_free(prop->prop.vals);
236 g_free(prop->prop.name);
237 g_free(prop->prop.type);
238 g_free(prop);
240 g_slist_free(client->props);
241 g_free(client->id);
242 g_free(client);
245 /* Set a property with a SmLISTofARRAY8 value as in SmRestartCommand,
246 SmCloneCommand and SmDiscardCommand */
248 void sc_set_list_of_array_prop(SmClient *client,
249 const gchar *name,
250 const char *vals[], gint nvals)
252 SmProperty *prop = find_property(client, name);
253 SmPropValue *value = make_list_of_array_value(vals, nvals);
255 if(prop == NULL)
256 prop = new_property(client, name, SmLISTofARRAY8);
258 else
259 g_free(prop->prop.vals);
261 prop->prop.vals = value;
262 prop->prop.num_vals = nvals;
263 prop->set = TRUE;
266 /* Set a prop with a SmARRAY8 value, SmProgram, SmUserID and
267 SmDiscardCommand (if using XSM) are suitable victims. */
269 void sc_set_array_prop(SmClient *client, const gchar *name, const gchar *vals)
271 SmProperty *prop = find_property(client, name);
272 SmPropValue *value = make_array_value(vals);
274 if(prop == NULL)
275 prop = new_property(client, name, SmARRAY8);
277 else
278 g_free(prop->prop.vals);
280 prop->prop.vals = value;
281 prop->prop.num_vals = 1;
282 prop->set = TRUE;
285 /* This one is for the SmRestarStyleHint */
287 void sc_set_card_prop(SmClient *client, const gchar *name, const gchar val)
289 SmProperty *prop = find_property(client, name);
290 SmPropValue *value = make_card_value(val);
292 if(prop == NULL)
293 prop = new_property(client, name, SmCARD8);
295 else
296 g_free(prop->prop.vals);
298 prop->prop.vals = value;
299 prop->prop.num_vals = 1;
300 prop->set = TRUE;
303 /* Puts a pointer to a SmPropValue in val_ret and
304 the number of values in nvals_ret (they are in an array ).
305 It looks like this:
306 typedef struct {
307 int length;
308 SmPointer value;
309 } SmPropValue;
310 The values are those stored in the SmClient struct,
311 They're not fetched from the session manager */
313 void sc_get_prop_value(SmClient *client, const gchar *name,
314 SmPropValue **val_ret, gint *nvals_ret)
316 SmProperty *prop = find_property(client, name);
318 if(!prop)
320 *val_ret = NULL;
321 *nvals_ret = 0;
323 else
325 *val_ret = prop->prop.vals;
326 *nvals_ret = prop->prop.num_vals;
330 /* Stores set properties in the session manager */
332 void sc_register_properties(SmClient *client)
334 GPtrArray *set_props= g_ptr_array_new();
335 GSList *list = client->props;
336 SmProperty *prop;
337 #ifdef DEBUG
338 gint i;
339 #endif
341 for (; list; list = list->next)
343 prop = (SmProperty *)list->data;
344 if(prop->set == TRUE)
346 g_ptr_array_add(set_props, &prop->prop);
347 prop->set = FALSE;
350 #ifdef DEBUG
351 g_printerr("Registering props:\n");
352 for(i = 0; i < set_props->len; i++)
354 prop = g_ptr_array_index(set_props, i);
355 g_printerr("%s\n", prop->prop.name);
357 #endif
358 if(set_props->len > 0)
359 SmcSetProperties(client->conn, set_props->len, (SmProp **)set_props->pdata);
361 g_ptr_array_free(set_props, TRUE);
364 /* Connects to the session manager */
366 gboolean sc_connect(SmClient *client)
368 gchar error_str[256];
369 gchar *client_id_ret = NULL;
370 SmcConn conn = NULL;
371 SmcCallbacks callbacks;
373 callbacks.save_yourself.callback = &sc_save_yourself;
374 callbacks.save_yourself.client_data = (SmPointer)client;
375 callbacks.die.callback = &sc_die;
376 callbacks.die.client_data = (SmPointer)client;
377 callbacks.save_complete.callback = &sc_save_complete;
378 callbacks.save_complete.client_data = (SmPointer)client;
379 callbacks.shutdown_cancelled.callback = &sc_shutdown_cancelled;
380 callbacks.shutdown_cancelled.client_data = (SmPointer)client;
382 if(IceAddConnectionWatch(&ice_watch_fn, client) == 0)
384 g_printerr("Session Manager: IceAddConnectionWatch failed\n");
385 return FALSE;
388 if((conn = SmcOpenConnection(NULL, /* network ids */
389 NULL, /* context */
390 1, 0, /* protocol major, minor */
391 SmcSaveYourselfProcMask |
392 SmcSaveCompleteProcMask |
393 SmcShutdownCancelledProcMask |
394 SmcDieProcMask,
395 &callbacks,
396 client->id, &client_id_ret,
397 sizeof(error_str), error_str)) == NULL)
399 g_printerr("Session Manager: Init error\n");
400 return FALSE;
403 client->id = g_strdup(client_id_ret);
404 client->conn = conn;
405 client->ice_conn = SmcGetIceConnection(conn);
406 gdk_set_sm_client_id(client->id);
407 XFree(client_id_ret);
409 gtk_quit_add(0, &close_connection, client);
411 return TRUE;