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
;
72 for (; list
; list
= list
->next
)
74 prop
= (SmProperty
*)list
->data
;
75 if (strcmp(prop
->prop
.name
, name
) == 0)
81 static SmProperty
*new_property(SmClient
*client
,
82 const gchar
*name
, const gchar
*type
)
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;
92 g_printerr("appending prop %s\n", prop
->prop
.name
);
94 client
->props
= g_slist_append(client
->props
, prop
);
99 static gint
close_connection(gpointer data
)
101 SmClient
*client
= (SmClient
*)data
;
103 SmcCloseConnection(client
->conn
, 0, NULL
);
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
;
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 static 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 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
;
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 static 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 static 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 static 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(const 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");
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
);
240 g_slist_free(client
->props
);
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
,
250 const char *vals
[], gint nvals
)
252 SmProperty
*prop
= find_property(client
, name
);
253 SmPropValue
*value
= make_list_of_array_value(vals
, nvals
);
256 prop
= new_property(client
, name
, SmLISTofARRAY8
);
259 g_free(prop
->prop
.vals
);
261 prop
->prop
.vals
= value
;
262 prop
->prop
.num_vals
= nvals
;
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
);
275 prop
= new_property(client
, name
, SmARRAY8
);
278 g_free(prop
->prop
.vals
);
280 prop
->prop
.vals
= value
;
281 prop
->prop
.num_vals
= 1;
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
);
293 prop
= new_property(client
, name
, SmCARD8
);
296 g_free(prop
->prop
.vals
);
298 prop
->prop
.vals
= value
;
299 prop
->prop
.num_vals
= 1;
303 /* Puts a pointer to a SmPropValue in val_ret and
304 the number of values in nvals_ret (they are in an array ).
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
);
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
;
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
);
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
);
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
;
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");
388 if((conn
= SmcOpenConnection(NULL
, /* network ids */
390 1, 0, /* protocol major, minor */
391 SmcSaveYourselfProcMask
|
392 SmcSaveCompleteProcMask
|
393 SmcShutdownCancelledProcMask
|
396 client
->id
, &client_id_ret
,
397 sizeof(error_str
), error_str
)) == NULL
)
399 g_printerr("Session Manager: Init error\n");
403 client
->id
= g_strdup(client_id_ret
);
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
);