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)
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
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 */
28 #include <X11/SM/SMlib.h>
34 static SmPropValue
*make_list_of_array_value(const gchar
*vals
[], gint nvals
)
36 SmPropValue
*values
= g_new(SmPropValue
, nvals
);
39 for(i
= 0; i
< nvals
; i
++)
41 values
[i
].value
= g_strdup(vals
[i
]);
42 values
[i
].length
= strlen(vals
[i
]);
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
);
57 static SmPropValue
*make_card_value(gchar val
)
59 SmPropValue
*value
= g_new(SmPropValue
, 1);
61 value
->value
= g_memdup(&val
, sizeof(gchar
));
67 static SmProperty
*find_property(SmClient
*client
, const gchar
*name
)
69 GSList
*list
= client
->props
;
74 prop
= (SmProperty
*)list
->data
;
75 if(strcmp(prop
->prop
.name
, name
) == 0)
77 list
= g_slist_next(list
);
82 static SmProperty
*new_property(SmClient
*client
,
83 const gchar
*name
, const gchar
*type
)
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;
93 g_printerr("appending prop %s\n", prop
->prop
.name
);
95 client
->props
= g_slist_append(client
->props
, prop
);
100 gint
close_connection(gpointer data
)
102 SmClient
*client
= (SmClient
*)data
;
104 SmcCloseConnection(client
->conn
, 0, NULL
);
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
;
117 if (IceProcessMessages(client
->ice_conn
, NULL
, &ret
) ==
118 IceProcessMessagesIOError
)
119 SmcCloseConnection(client
->conn
, 0, NULL
);
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
;
133 g_printerr("Ice connection opened for id %s\n", client
->id
);
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
);
141 if (IceConnectionNumber(conn
) == client
->fd
)
144 g_printerr("Ice connection closed for id %s\n", client
->id
);
146 gdk_input_remove(client
->input_tag
);
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
;
160 g_printerr("%s saving state\n", client
->id
);
162 if(client
->save_yourself_fn
)
163 success
= client
->save_yourself_fn(client
);
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
;
173 g_printerr("%s shutdown cancelled\n", client
->id
);
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
;
183 g_printerr("%s save complete\n", client
->id
);
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
;
193 g_printerr("%s dying\n", client
->id
);
196 client
->die_fn(client
);
199 gboolean
sc_session_up()
201 if(getenv("SESSION_MANAGER") == NULL
)
206 SmClient
*sc_new(gchar
*client_id
)
211 g_printerr("Creating new sm-client\n");
213 client
= g_new(SmClient
, 1);
214 client
->id
= g_strdup(client_id
);
215 client
->props
= NULL
;
217 client
->ice_conn
= NULL
;
223 void sc_destroy(SmClient
*client
)
225 GSList
*list
= client
->props
;
229 g_printerr("destroying client\n");
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
);
239 list
= g_slist_next(list
);
241 g_slist_free(client
->props
);
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
,
251 const char *vals
[], gint nvals
)
253 SmProperty
*prop
= find_property(client
, name
);
254 SmPropValue
*value
= make_list_of_array_value(vals
, nvals
);
257 prop
= new_property(client
, name
, SmLISTofARRAY8
);
260 g_free(prop
->prop
.vals
);
262 prop
->prop
.vals
= value
;
263 prop
->prop
.num_vals
= nvals
;
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
);
276 prop
= new_property(client
, name
, SmARRAY8
);
279 g_free(prop
->prop
.vals
);
281 prop
->prop
.vals
= value
;
282 prop
->prop
.num_vals
= 1;
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
);
294 prop
= new_property(client
, name
, SmCARD8
);
297 g_free(prop
->prop
.vals
);
299 prop
->prop
.vals
= value
;
300 prop
->prop
.num_vals
= 1;
304 /* Puts a pointer to a SmPropValue in val_ret and
305 the number of values in nvals_ret (they are in an array ).
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
);
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
;
344 prop
= (SmProperty
*)list
->data
;
345 if(prop
->set
== TRUE
)
347 g_ptr_array_add(set_props
, &prop
->prop
);
350 list
= g_slist_next(list
);
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
);
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
;
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");
390 if((conn
= SmcOpenConnection(NULL
, /* network ids */
392 1, 0, /* protocol major, minor */
393 SmcSaveYourselfProcMask
|
394 SmcSaveCompleteProcMask
|
395 SmcShutdownCancelledProcMask
|
398 client
->id
, &client_id_ret
,
399 sizeof(error_str
), error_str
)) == NULL
)
401 g_printerr("Session Manager: Init error\n");
405 client
->id
= g_strdup(client_id_ret
);
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
);