Merge branch 'backport' into work
[dana/openbox.git] / openbox / session.c
index 267d17e..758e988 100644 (file)
@@ -33,6 +33,8 @@ GSList *session_desktop_names = NULL;
 void session_startup(gint argc, gchar **argv) {}
 void session_shutdown(gboolean permanent) {}
 GList* session_state_find(struct _ObClient *c) { return NULL; }
+void session_request_logout(gboolean silent) {}
+gboolean session_connected(void) { return FALSE; }
 #else
 
 #include "debug.h"
@@ -104,7 +106,7 @@ void session_startup(gint argc, gchar **argv)
     obt_paths_unref(p), p = NULL;
 
     if (!obt_paths_mkdir_path(dir, 0700)) {
-        g_message(_("Unable to make directory '%s': %s"),
+        g_message(_("Unable to make directory \"%s\": %s"),
                   dir, g_strerror(errno));
     }
 
@@ -158,8 +160,13 @@ void session_shutdown(gboolean permanent)
     }
 }
 
+gboolean session_connected(void)
+{
+    return !!sm_conn;
+}
+
 /*! Connect to the session manager and set up our callback functions */
-static gboolean session_connect()
+static gboolean session_connect(void)
 {
     SmcCallbacks cb;
     gchar *oldid;
@@ -193,7 +200,7 @@ static gboolean session_connect()
     return sm_conn != NULL;
 }
 
-static void session_setup_program()
+static void session_setup_program(void)
 {
     SmPropValue vals = {
         .value = sm_argv[0],
@@ -212,7 +219,7 @@ static void session_setup_program()
     g_free(prop.type);
 }
 
-static void session_setup_user()
+static void session_setup_user(void)
 {
     char *user = g_strdup(g_get_user_name());
 
@@ -255,7 +262,7 @@ static void session_setup_restart_style(gboolean restart)
     g_free(prop.type);
 }
 
-static void session_setup_pid()
+static void session_setup_pid(void)
 {
     gchar *pid = g_strdup_printf("%ld", (glong) getpid());
 
@@ -278,7 +285,7 @@ static void session_setup_pid()
 }
 
 /*! This is a gnome-session-manager extension */
-static void session_setup_priority()
+static void session_setup_priority(void)
 {
     gchar priority = 20; /* 20 is a lower prioity to run before other apps */
 
@@ -299,7 +306,7 @@ static void session_setup_priority()
     g_free(prop.type);
 }
 
-static void session_setup_clone_command()
+static void session_setup_clone_command(void)
 {
     gint i;
 
@@ -325,7 +332,7 @@ static void session_setup_clone_command()
     g_free(vals);
 }
 
-static void session_setup_restart_command()
+static void session_setup_restart_command(void)
 {
     gint i;
 
@@ -367,7 +374,7 @@ static void session_setup_restart_command()
     g_free(vals);
 }
 
-static ObSMSaveData *sm_save_get_data()
+static ObSMSaveData *sm_save_get_data(void)
 {
     ObSMSaveData *savedata = g_new0(ObSMSaveData, 1);
     /* save the active desktop and client.
@@ -400,14 +407,28 @@ static void sm_save_yourself_2(SmcConn conn, SmPointer data)
     SmcSaveYourselfDone(conn, success);
 }
 
-
 static void sm_save_yourself(SmcConn conn, SmPointer data, gint save_type,
                              Bool shutdown, gint interact_style, Bool fast)
 {
     ObSMSaveData *savedata = NULL;
     gchar *vendor;
 
-    ob_debug_type(OB_DEBUG_SM, "Session save requested");
+#ifdef DEBUG
+    {
+        const gchar *sname =
+            (save_type == SmSaveLocal ? "SmSaveLocal" :
+             (save_type == SmSaveGlobal ? "SmSaveGlobal" :
+              (save_type == SmSaveBoth ? "SmSaveBoth" : "INVALID!!")));
+        ob_debug_type(OB_DEBUG_SM, "Session save requested, type %s", sname);
+    }
+#endif
+
+    if (save_type == SmSaveGlobal) {
+        /* we have no data to save.  we only store state to get back to where
+           we were, we don't keep open writable files or anything */
+        SmcSaveYourselfDone(conn, TRUE);
+        return;
+    }
 
     vendor = SmcVendor(sm_conn);
     ob_debug_type(OB_DEBUG_SM, "Session manager's vendor: %s", vendor);
@@ -452,7 +473,7 @@ static gboolean session_save_to_file(const ObSMSaveData *savedata)
     f = fopen(ob_sm_save_file, "w");
     if (!f) {
         success = FALSE;
-        g_message(_("Unable to save the session to '%s': %s"),
+        g_message(_("Unable to save the session to \"%s\": %s"),
                   ob_sm_save_file, g_strerror(errno));
     } else {
         fprintf(f, "<?xml version=\"1.0\"?>\n\n");
@@ -475,10 +496,14 @@ static gboolean session_save_to_file(const ObSMSaveData *savedata)
 
         if (screen_desktop_names) {
             gint i;
+            gchar *t;
 
             fprintf(f, "<desktopnames>\n");
-            for (i = 0; screen_desktop_names[i]; ++i)
-                fprintf(f, "  <name>%s</name>\n", screen_desktop_names[i]);
+            for (i = 0; screen_desktop_names[i]; ++i){
+                t = g_markup_escape_text(screen_desktop_names[i], -1);
+                fprintf(f, "  <name>%s</name>\n", t);
+                g_free(t);
+            }
             fprintf(f, "</desktopnames>\n");
         }
 
@@ -533,8 +558,11 @@ static gboolean session_save_to_file(const ObSMSaveData *savedata)
 
             if (c->sm_client_id)
                 fprintf(f, "<window id=\"%s\">\n", c->sm_client_id);
-            else
-                fprintf(f, "<window command=\"%s\">\n", c->wm_command);
+            else {
+                t = g_markup_escape_text(c->wm_command, -1);
+                fprintf(f, "<window command=\"%s\">\n", t);
+                g_free(t);
+            }
 
             t = g_markup_escape_text(c->name, -1);
             fprintf(f, "\t<name>%s</name>\n", t);
@@ -584,7 +612,7 @@ static gboolean session_save_to_file(const ObSMSaveData *savedata)
 
         if (fflush(f)) {
             success = FALSE;
-            g_message(_("Error while saving the session to '%s': %s"),
+            g_message(_("Error while saving the session to \"%s\": %s"),
                       ob_sm_save_file, g_strerror(errno));
         }
         fclose(f);
@@ -663,6 +691,7 @@ static void session_load_file(const gchar *path)
     i = obt_parse_instance_new();
 
     if (!obt_parse_load_file(i, path, "openbox_session")) {
+        ob_debug_type(OB_DEBUG_SM, "ERROR: session file is missing root node");
         obt_parse_instance_unref(i);
         return;
     }
@@ -696,6 +725,7 @@ static void session_load_file(const gchar *path)
         }
     }
 
+    ob_debug_type(OB_DEBUG_SM, "loading windows");
     for (node = obt_parse_find_node(node->children, "window"); node != NULL;
          node = obt_parse_find_node(node->next, "window"))
     {
@@ -760,9 +790,11 @@ static void session_load_file(const gchar *path)
         /* save this. they are in the file in stacking order, so preserve
            that order here */
         session_saved_state = g_list_append(session_saved_state, state);
+        ob_debug_type(OB_DEBUG_SM, "loaded %s", state->name);
         continue;
 
     session_load_bail:
+        ob_debug_type(OB_DEBUG_SM, "loading FAILED");
         session_state_free(state);
     }
 
@@ -797,6 +829,7 @@ static void session_load_file(const gchar *path)
                 !strcmp(s1->class, s2->class) &&
                 !strcmp(s1->role, s2->role))
             {
+                ob_debug_type(OB_DEBUG_SM, "removing duplicate %s", s2->name);
                 session_state_free(s2);
                 session_saved_state =
                     g_list_delete_link(session_saved_state, jt);
@@ -805,6 +838,7 @@ static void session_load_file(const gchar *path)
         }
 
         if (founddup) {
+            ob_debug_type(OB_DEBUG_SM, "removing duplicate %s", s1->name);
             session_state_free(s1);
             session_saved_state = g_list_delete_link(session_saved_state, it);
         }
@@ -813,4 +847,20 @@ static void session_load_file(const gchar *path)
     obt_parse_instance_unref(i);
 }
 
+void session_request_logout(gboolean silent)
+{
+    if (sm_conn) {
+        SmcRequestSaveYourself(sm_conn,
+                               SmSaveGlobal,
+                               TRUE, /* logout */
+                               (silent ?
+                                SmInteractStyleNone : SmInteractStyleAny),
+                               TRUE,  /* if false, with GSM, it shows the old
+                                         logout prompt */
+                               TRUE); /* global */
+    }
+    else
+        g_message(_("Not connected to a session manager"));
+}
+
 #endif