From: Christophe CURIS <christophe.cu...@free.fr>

Recent patches has introduced the ability to exit cleanly from the WINGs
library, but this introduced some side effects because a function is
registered with 'atexit' to save user config on exit, which may not work
anymore because WMReleaseApplication frees some stuff needed for that task.

This patch handles this so that both method works, in case user of the lib
would forget to call the clean exit function.

Signed-off-by: Christophe CURIS <christophe.cu...@free.fr>
---
 WINGs/Makefile.am    |  1 +
 WINGs/userdefaults.c | 19 ++++++++++++++++---
 WINGs/userdefaults.h | 34 ++++++++++++++++++++++++++++++++++
 WINGs/wapplication.c |  9 +++++++++
 4 files changed, 60 insertions(+), 3 deletions(-)
 create mode 100644 WINGs/userdefaults.h

diff --git a/WINGs/Makefile.am b/WINGs/Makefile.am
index e70988d..863ba79 100644
--- a/WINGs/Makefile.am
+++ b/WINGs/Makefile.am
@@ -81,6 +81,7 @@ libWUtil_la_SOURCES =         \
        string.c \
        tree.c \
        userdefaults.c \
+       userdefaults.h \
        usleep.c \
        wapplication.c \
        wconfig.h \
diff --git a/WINGs/userdefaults.c b/WINGs/userdefaults.c
index 160a08b..876371d 100644
--- a/WINGs/userdefaults.c
+++ b/WINGs/userdefaults.c
@@ -10,6 +10,9 @@
 #include "wconfig.h"
 
 #include "WINGs.h"
+#include "WINGsP.h"
+#include "userdefaults.h"
+
 
 typedef struct W_UserDefaults {
        WMPropList *defaults;
@@ -114,9 +117,19 @@ char *wglobaldefaultspathfordomain(const char *domain)
        return t;
 }
 
-static void
-saveDefaultsChanges(void)
+void w_save_defaults_changes(void)
 {
+       if (WMApplication.applicationName == NULL) {
+               /*
+                * This means that the user has properly exited by calling the
+                * function 'WMReleaseApplication' (which has already called us)
+                * but we're being called again by the fallback 'atexit' method
+                * (the legacy way of saving changes on exit which is kept for
+                * application that would forget to call 'WMReleaseApplication')
+                */
+               return;
+       }
+
        /* save the user defaults databases */
        synchronizeUserDefaults(NULL);
 }
@@ -127,7 +140,7 @@ static void registerSaveOnExit(void)
        static Bool registeredSaveOnExit = False;
 
        if (!registeredSaveOnExit) {
-               atexit(saveDefaultsChanges);
+               atexit(w_save_defaults_changes);
                registeredSaveOnExit = True;
        }
 }
diff --git a/WINGs/userdefaults.h b/WINGs/userdefaults.h
new file mode 100644
index 0000000..3e4b46b
--- /dev/null
+++ b/WINGs/userdefaults.h
@@ -0,0 +1,34 @@
+/* WUtil / userdefaults.h
+ *
+ *  Copyright (c) 2014 Window Maker Team
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License along
+ *  with this program; if not, write to the Free Software Foundation, Inc.,
+ *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#ifndef WUTIL_USERDEFAULTS_H
+#define WUTIL_USERDEFAULTS_H
+
+/*
+ * This file is not part of WUtil public API
+ *
+ * It defines internal things for the user configuration handling functions
+ */
+
+
+/* Save user configuration, to be used when application exits only */
+void w_save_defaults_changes(void);
+
+
+#endif /* WUTIL_USERDEFAULTS_H */
diff --git a/WINGs/wapplication.c b/WINGs/wapplication.c
index beca03d..1478ced 100644
--- a/WINGs/wapplication.c
+++ b/WINGs/wapplication.c
@@ -4,6 +4,7 @@
 
 #include "WINGsP.h"
 #include "wconfig.h"
+#include "userdefaults.h"
 
 
 struct W_Application WMApplication;
@@ -51,6 +52,14 @@ void WMInitializeApplication(const char *applicationName, 
int *argc, char **argv
 void WMReleaseApplication(void) {
        int i;
 
+       /*
+        * We save the configuration on exit, this used to be handled
+        * through an 'atexit' registered function but if application
+        * properly calls WMReleaseApplication then the info to that
+        * will have been freed by us.
+        */
+       w_save_defaults_changes();
+
        W_ReleaseNotificationCenter();
 
        if (WMApplication.applicationName)
-- 
1.9.2


-- 
To unsubscribe, send mail to wmaker-dev-unsubscr...@lists.windowmaker.org.

Reply via email to