r1476: Options to forward button-3 clicks on the pinboard to the window manager
[rox-filer.git] / ROX-Filer / src / sc.c
blob8c816eebeadaadb0f9ea96f8c7f860671734e861
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 while(list)
74 prop = (SmProperty *)list->data;
75 if(strcmp(prop->prop.name, name) == 0)
76 return prop;
77 list = g_slist_next(list);
79 return NULL;
82 static SmProperty *new_property(SmClient *client,
83 const gchar *name, const gchar *type)
85 SmProperty *prop;
87 prop = g_new(SmProperty, 1);
88 prop->prop.name = g_strdup(name);
89 prop->prop.type = g_strdup(type);
90 prop->prop.vals = NULL;
91 prop->prop.num_vals = 0;
92 #ifdef DEBUG
93 g_printerr("appending prop %s\n", prop->prop.name);
94 #endif
95 client->props = g_slist_append(client->props, prop);
97 return prop;
100 gint close_connection(gpointer data)
102 SmClient *client = (SmClient *)data;
104 SmcCloseConnection(client->conn, 0, NULL);
106 return 0;
109 /* Called when there's data to be read on the ICE file descriptor.
110 Unpacks the message and triggers the correct callback... I think */
112 void poll_ice_messages(gpointer data, gint source, 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 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 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 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 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 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(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 while(list)
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);
239 list = g_slist_next(list);
241 g_slist_free(client->props);
242 g_free(client->id);
243 g_free(client);
246 /* Set a property with a SmLISTofARRAY8 value as in SmRestartCommand,
247 SmCloneCommand and SmDiscardCommand */
249 void sc_set_list_of_array_prop(SmClient *client,
250 const gchar *name,
251 const char *vals[], gint nvals)
253 SmProperty *prop = find_property(client, name);
254 SmPropValue *value = make_list_of_array_value(vals, nvals);
256 if(prop == NULL)
257 prop = new_property(client, name, SmLISTofARRAY8);
259 else
260 g_free(prop->prop.vals);
262 prop->prop.vals = value;
263 prop->prop.num_vals = nvals;
264 prop->set = TRUE;
267 /* Set a prop with a SmARRAY8 value, SmProgram, SmUserID and
268 SmDiscardCommand (if using XSM) are suitable victims. */
270 void sc_set_array_prop(SmClient *client, const gchar *name, const gchar *vals)
272 SmProperty *prop = find_property(client, name);
273 SmPropValue *value = make_array_value(vals);
275 if(prop == NULL)
276 prop = new_property(client, name, SmARRAY8);
278 else
279 g_free(prop->prop.vals);
281 prop->prop.vals = value;
282 prop->prop.num_vals = 1;
283 prop->set = TRUE;
286 /* This one is for the SmRestarStyleHint */
288 void sc_set_card_prop(SmClient *client, const gchar *name, const gchar val)
290 SmProperty *prop = find_property(client, name);
291 SmPropValue *value = make_card_value(val);
293 if(prop == NULL)
294 prop = new_property(client, name, SmCARD8);
296 else
297 g_free(prop->prop.vals);
299 prop->prop.vals = value;
300 prop->prop.num_vals = 1;
301 prop->set = TRUE;
304 /* Puts a pointer to a SmPropValue in val_ret and
305 the number of values in nvals_ret (they are in an array ).
306 It looks like this:
307 typedef struct {
308 int length;
309 SmPointer value;
310 } SmPropValue;
311 The values are those stored in the SmClient struct,
312 They're not fetched from the session manager */
314 void sc_get_prop_value(SmClient *client, const gchar *name,
315 SmPropValue **val_ret, gint *nvals_ret)
317 SmProperty *prop = find_property(client, name);
319 if(!prop)
321 *val_ret = NULL;
322 *nvals_ret = 0;
324 else
326 *val_ret = prop->prop.vals;
327 *nvals_ret = prop->prop.num_vals;
331 /* Stores set properties in the session manager */
333 void sc_register_properties(SmClient *client)
335 GPtrArray *set_props= g_ptr_array_new();
336 GSList *list = client->props;
337 SmProperty *prop;
338 #ifdef DEBUG
339 gint i;
340 #endif
342 while(list)
344 prop = (SmProperty *)list->data;
345 if(prop->set == TRUE)
347 g_ptr_array_add(set_props, &prop->prop);
348 prop->set = FALSE;
350 list = g_slist_next(list);
352 #ifdef DEBUG
353 g_printerr("Registering props:\n");
354 for(i = 0; i < set_props->len; i++)
356 prop = g_ptr_array_index(set_props, i);
357 g_printerr("%s\n", prop->prop.name);
359 #endif
360 if(set_props->len > 0)
361 SmcSetProperties(client->conn, set_props->len, (SmProp **)set_props->pdata);
363 g_ptr_array_free(set_props, TRUE);
366 /* Connects to the session manager */
368 gboolean sc_connect(SmClient *client)
370 gchar error_str[256];
371 gchar *client_id_ret = NULL;
372 SmcConn conn = NULL;
373 SmcCallbacks callbacks;
375 callbacks.save_yourself.callback = &sc_save_yourself;
376 callbacks.save_yourself.client_data = (SmPointer)client;
377 callbacks.die.callback = &sc_die;
378 callbacks.die.client_data = (SmPointer)client;
379 callbacks.save_complete.callback = &sc_save_complete;
380 callbacks.save_complete.client_data = (SmPointer)client;
381 callbacks.shutdown_cancelled.callback = &sc_shutdown_cancelled;
382 callbacks.shutdown_cancelled.client_data = (SmPointer)client;
384 if(IceAddConnectionWatch(&ice_watch_fn, client) == 0)
386 g_printerr("Session Manager: IceAddConnectionWatch failed\n");
387 return FALSE;
390 if((conn = SmcOpenConnection(NULL, /* network ids */
391 NULL, /* context */
392 1, 0, /* protocol major, minor */
393 SmcSaveYourselfProcMask |
394 SmcSaveCompleteProcMask |
395 SmcShutdownCancelledProcMask |
396 SmcDieProcMask,
397 &callbacks,
398 client->id, &client_id_ret,
399 sizeof(error_str), error_str)) == NULL)
401 g_printerr("Session Manager: Init error\n");
402 return FALSE;
405 client->id = g_strdup(client_id_ret);
406 client->conn = conn;
407 client->ice_conn = SmcGetIceConnection(conn);
408 gdk_set_sm_client_id(client->id);
409 XFree(client_id_ret);
411 gtk_quit_add(0, &close_connection, client);
413 return TRUE;