+#ifdef USE_SM
+
+#define SM_ERR_LEN 1024
+
+ SmcCallbacks cb;
+ char sm_err[SM_ERR_LEN];
+
+ cb.save_yourself.callback = sm_save_yourself;
+ cb.save_yourself.client_data = NULL;
+
+ cb.die.callback = sm_die;
+ cb.die.client_data = NULL;
+
+ cb.save_complete.callback = sm_save_complete;
+ cb.save_complete.client_data = NULL;
+
+ cb.shutdown_cancelled.callback = sm_shutdown_cancelled;
+ cb.shutdown_cancelled.client_data = NULL;
+
+ ob_sm_conn = SmcOpenConnection(NULL, NULL, 1, 0,
+ SmcSaveYourselfProcMask |
+ SmcDieProcMask |
+ SmcSaveCompleteProcMask |
+ SmcShutdownCancelledProcMask,
+ &cb, ob_sm_id, &ob_sm_id,
+ SM_ERR_LEN, sm_err);
+ if (ob_sm_conn == NULL)
+ g_warning("Failed to connect to session manager: %s", sm_err);
+ else {
+ SmPropValue val_prog;
+ SmPropValue val_uid;
+ SmPropValue val_hint;
+ SmPropValue val_pri;
+ SmPropValue val_pid;
+ SmProp prop_cmd = { SmCloneCommand, SmLISTofARRAY8, 1, };
+ SmProp prop_res = { SmRestartCommand, SmLISTofARRAY8, };
+ SmProp prop_prog = { SmProgram, SmARRAY8, 1, };
+ SmProp prop_uid = { SmUserID, SmARRAY8, 1, };
+ SmProp prop_hint = { SmRestartStyleHint, SmCARD8, 1, };
+ SmProp prop_pid = { SmProcessID, SmARRAY8, 1, };
+ SmProp prop_pri = { "_GSM_Priority", SmCARD8, 1, };
+ SmProp *props[7];
+ gulong hint, pri;
+ gchar pid[32];
+ gint i, j;
+ gboolean has_id;
+
+ for (i = 1; i < argc - 1; ++i)
+ if (strcmp(argv[i], "--sm-client-id") == 0)
+ break;
+ has_id = (i < argc - 1);
+
+ prop_cmd.vals = g_new(SmPropValue, (has_id ? argc-2 : argc));
+ prop_cmd.num_vals = (has_id ? argc-2 : argc);
+ for (i = 0, j = 0; i < argc; ++i, ++j) {
+ if (strcmp (argv[i], "--sm-client-id") == 0) {
+ ++i, --j; /* skip the next as well, keep j where it is */
+ } else {
+ prop_cmd.vals[j].value = argv[i];
+ prop_cmd.vals[j].length = strlen(argv[i]);
+ }
+ }
+
+ prop_res.vals = g_new(SmPropValue, (has_id ? argc : argc+2));
+ prop_res.num_vals = (has_id ? argc : argc+2);
+ for (i = 0, j = 0; i < argc; ++i, ++j) {
+ if (strcmp (argv[i], "--sm-client-id") == 0) {
+ ++i, --j; /* skip the next as well, keep j where it is */
+ } else {
+ prop_res.vals[j].value = argv[i];
+ prop_res.vals[j].length = strlen(argv[i]);
+ }
+ }
+ prop_res.vals[j].value = "--sm-client-id";
+ prop_res.vals[j++].length = strlen("--sm-client-id");
+ prop_res.vals[j].value = ob_sm_id;
+ prop_res.vals[j++].length = strlen(ob_sm_id);
+
+ val_prog.value = argv[0];
+ val_prog.length = strlen(argv[0]);
+
+ val_uid.value = g_strdup(g_get_user_name());
+ val_uid.length = strlen(val_uid.value);
+
+ hint = SmRestartImmediately;
+ val_hint.value = &hint;
+ val_hint.length = 1;
+
+ sprintf(pid, "%ld", (long)getpid());
+ val_pid.value = pid;
+ val_pid.length = strlen(pid);
+
+ /* priority with gnome-session-manager, low to run before other apps */
+ pri = 20;
+ val_pri.value = &pri;
+ val_pri.length = 1;
+
+ prop_prog.vals = &val_prog;
+ prop_uid.vals = &val_uid;
+ prop_hint.vals = &val_hint;
+ prop_pid.vals = &val_pid;
+ prop_pri.vals = &val_pri;
+
+ props[0] = &prop_prog;
+ props[1] = &prop_cmd;
+ props[2] = &prop_res;
+ props[3] = &prop_uid;
+ props[4] = &prop_hint;
+ props[5] = &prop_pid;
+ props[6] = &prop_pri;
+
+ SmcSetProperties(ob_sm_conn, 7, props);
+
+ g_free(val_uid.value);
+ g_free(prop_cmd.vals);
+ g_free(prop_res.vals);