From e49cd07e79574ba404a0a8fc867344cc630d7794 Mon Sep 17 00:00:00 2001
From: Christian Heller <c.heller@plomlompom.de>
Date: Thu, 19 Sep 2013 21:21:13 +0200
Subject: [PATCH] Unified (and heavily re-factored) (un-)loading/saving of
 keybindings and window confiugrations. They were heavily entangled anyways.

---
 config/commands                        |  5 ++--
 config/keybindings_global              |  5 ++--
 config/windows/toggle_order_and_active |  2 +-
 src/control.c                          | 28 +++++++-----------
 src/main.c                             | 25 +++++-----------
 src/misc.c                             | 41 +++++++++++++++++++++++++-
 src/misc.h                             |  7 +++++
 src/rexit.c                            | 16 +++++++---
 src/rexit.h                            | 13 ++++----
 src/wincontrol.c                       | 14 ---------
 src/wincontrol.h                       |  3 +-
 11 files changed, 89 insertions(+), 70 deletions(-)

diff --git a/config/commands b/config/commands
index cdd65b9..4eebf3f 100644
--- a/config/commands
+++ b/config/commands
@@ -35,9 +35,8 @@
 35 wk_keys_d win keybindings config keybindings nav down
 36 wk_keys_u win keybindings config keybindings nav up
 37 wk_keys_m win keybindings config keybindings modify
-38 reload_wins reload windows configuration
-39 save_winconf save windows' configuration
-40 save_keys save global keybindings
+38 reload_conf reload windows and keybindings configuration
+39 save_conf save windows and keybindings
 41 map_u map up
 42 map_d map down
 43 map_l map left
diff --git a/config/keybindings_global b/config/keybindings_global
index 7d825a6..739af7d 100644
--- a/config/keybindings_global
+++ b/config/keybindings_global
@@ -12,6 +12,5 @@
 60 cyc_win_b
 262 scrl_l
 360 scrl_r
-114 reload_wins
-75 save_keys
-87 save_winconf
+114 reload_conf
+67 save_conf
diff --git a/config/windows/toggle_order_and_active b/config/windows/toggle_order_and_active
index 2ae7c2c..f188d83 100644
--- a/config/windows/toggle_order_and_active
+++ b/config/windows/toggle_order_and_active
@@ -1,2 +1,2 @@
 kmicl
-m
+m
\ No newline at end of file
diff --git a/src/control.c b/src/control.c
index 8531b4c..8455aaf 100644
--- a/src/control.c
+++ b/src/control.c
@@ -5,19 +5,21 @@
 #include "windows.h" /* for cycle_active_win(), shift_active_win(), struct Win,
                       *  struct WinMeta
                       */
-#include "keybindings.h" /* for get_keycode_to_action(), save_keybindings(),
-                          * move_keyb_mod_selection(), mod_selected_keyb()
+#include "keybindings.h" /* for get_keycode_to_action(), mod_selected_keyb(),
+                          * move_keyb_mod_selection()
                           */
 #include "map.h" /* for map_scroll(), map_center_player(), dir enum */
 #include "main.h" /* for World struct */
 #include "rexit.h" /* for exit_err() */
 #include "wincontrol.h" /* for scroll_pad(), toggle_window(),
-                         * growshrink_active_window(), reload_win_config()
-                         * toggle_winconfig(), save_win_configs(),
+                         * growshrink_active_window(), toggle_winconfig(),
                          * toggle_win_height_type(), toggle_win_width_type()
                          */
 #include "map_object_actions.h" /* for player_wait(), move_player() */
 #include "command_db.h" /* for is_command_id_shortdsc() */
+#include "misc.h" /* for load_interface_conf(), unload_interface_conf(),
+                   * save_interface_conf()
+                   */
 
 
 
@@ -242,14 +244,9 @@ extern uint8_t meta_control(int key, struct World * world)
     {
         exit_err(toggle_window(win_meta, win_log), world, err_toggle);
     }
-    else if (key == get_available_keycode_to_action(world, "save_keys"))
+    else if (key == get_available_keycode_to_action(world, "save_conf"))
     {
-        save_keybindings(world, "config/keybindings_global",
-                         &world->kb_global);
-        save_keybindings(world, "config/keybindings_wingeom",
-                         &world->kb_wingeom);
-        save_keybindings(world, "config/keybindings_winkeys",
-                         &world->kb_winkeys);
+        save_interface_conf(world);
     }
     else if (key == get_available_keycode_to_action(world, "g_keys_u"))
     {
@@ -307,17 +304,14 @@ extern uint8_t meta_control(int key, struct World * world)
     {
         map_center_player(world->map, world->player, win_map->frame.size);
     }
-    else if (key == get_available_keycode_to_action(world, "reload_wins"))
+    else if (key == get_available_keycode_to_action(world, "reload_conf"))
     {
-        reload_win_config(world);
+        unload_interface_conf(world);
+        load_interface_conf(world);
     }
     else if (key == get_available_keycode_to_action(world, "winconf"))
     {
         toggle_winconfig(world, world->wmeta->active);
     }
-    else if (key == get_available_keycode_to_action(world, "save_winconf"))
-    {
-        save_win_configs(world);
-    }
     return 0;
 }
diff --git a/src/main.c b/src/main.c
index 2c096ae..8d00a5d 100644
--- a/src/main.c
+++ b/src/main.c
@@ -7,8 +7,7 @@
 #include <time.h> /* for time() */
 #include <unistd.h> /* for getopt(), optarg */
 #include <stdint.h> /* for uint16_t, uint32_t */
-#include "windows.h" /* for structs WinMeta, Win, init_win(), init_win_meta(),
-                      * draw_all_wins()
+#include "windows.h" /* for structs WinMeta, Win, init_win_meta(), draw_all_wins()
                       */
 #include "readwrite.h" /* for [read/write]_uint[8/16/32][_bigendian](),
                         * try_fopen(), try_fclose(), try_fclose_unlink_rename()
@@ -19,18 +18,14 @@
                           */
 #include "map.h" /* for struct Map, init_map() */
 #include "misc.h" /* for update_log(), find_passable_pos(), save_game(),
-                   * try_calloc(), check_tempfile(), check_xor_files()
+                   * try_calloc(), check_tempfile(), check_xor_files(),
+                   * load_interface_conf()
                    */
-#include "wincontrol.h" /* for create_winconfs(), init_winconfs(), init_wins(),
-                         * sorted_wintoggle_and_activate()
-                         */
+#include "wincontrol.h" /* get_win_by_id(), get_winconf_by_win() */
 #include "rrand.h" /* for rrand(), rrand_seed() */
 #include "rexit.h" /* for exit_game(), exit_err() */
 #include "command_db.h" /* for init_command_db() */
-#include "control.h" /* for *_control() */
-#include "keybindings.h" /* for init_keybindings(),
-                          * get_available_keycode_to_action()
-                          */
+#include "control.h" /* for *_control(), get_available_keycode_to_action() */
 
 
 
@@ -175,17 +170,11 @@ int main(int argc, char *argv[])
     curs_set(0);
     keypad(screen, TRUE);
     raw();
-    init_keybindings(&world, "config/keybindings_global",  &world.kb_global);
-    init_keybindings(&world, "config/keybindings_wingeom", &world.kb_wingeom);
-    init_keybindings(&world, "config/keybindings_winkeys", &world.kb_winkeys);
-    set_cleanup_flag(CLEANUP_KEYBINDINGS);
     char * err_winmem = "Trouble with init_win_meta() in main ().";
     exit_err(init_win_meta(screen, &world.wmeta), &world, err_winmem);
     set_cleanup_flag(CLEANUP_WIN_META);
-    init_winconfs(&world);
-    init_wins(&world);
-    set_cleanup_flag(CLEANUP_WINCONFS);
-    sorted_wintoggle_and_activate(&world);
+    load_interface_conf(&world);
+    set_cleanup_flag(CLEANUP_INTERFACE_CONF);
     err_winmem = "Trouble with draw_all_wins() in main().";
 
     /* Focus map on player. */
diff --git a/src/misc.c b/src/misc.c
index 50fee64..e3499c1 100644
--- a/src/misc.c
+++ b/src/misc.c
@@ -16,7 +16,10 @@
 #include "yx_uint16.h" /* for yx_uint16 struct */
 #include "rrand.h" /* for rrand(), rrand_seed() */
 #include "rexit.h" /* for exit_err() */
-
+#include "wincontrol.h" /* for init_winconfs(), init_wins(), free_winconfs(),
+                         * sorted_wintoggle_and_activate()
+                         */
+#include "windows.h" /* for suspend_win() */
 
 
 extern char * trouble_msg(struct World * w, char * parent, char * child)
@@ -95,6 +98,42 @@ extern void check_tempfile(char * path, struct World * w)
 
 
 
+extern void save_interface_conf(struct World * world)
+{
+    save_keybindings(world, "config/keybindings_global", &world->kb_global);
+    save_keybindings(world, "config/keybindings_wingeom", &world->kb_wingeom);
+    save_keybindings(world, "config/keybindings_winkeys", &world->kb_winkeys);
+    save_win_configs(world);
+}
+
+
+
+extern void load_interface_conf(struct World * world)
+{
+    init_keybindings(world, "config/keybindings_global",  &world->kb_global);
+    init_keybindings(world, "config/keybindings_wingeom", &world->kb_wingeom);
+    init_keybindings(world, "config/keybindings_winkeys", &world->kb_winkeys);
+    init_winconfs(world);
+    init_wins(world);
+    sorted_wintoggle_and_activate(world);
+}
+
+
+
+extern void unload_interface_conf(struct World * world)
+{
+    free_keybindings(world->kb_global.kbs);
+    free_keybindings(world->kb_wingeom.kbs);
+    free_keybindings(world->kb_winkeys.kbs);
+    while (0 != world->wmeta->active)
+    {
+        suspend_win(world->wmeta, world->wmeta->active);
+    }
+    free_winconfs(world);
+}
+
+
+
 extern void update_log(struct World * world, char * text)
 {
     char * f_name = "update_log()";
diff --git a/src/misc.h b/src/misc.h
index 782a6fa..831c4d1 100644
--- a/src/misc.h
+++ b/src/misc.h
@@ -37,6 +37,13 @@ extern void check_files_xor(char * p1, char * p2, struct World * w);
 
 
 
+/* Save / load / unload (free) interface configuration data. */
+extern void save_interface_conf(struct World * world);
+extern void load_interface_conf(struct World * world);
+extern void unload_interface_conf(struct World * world);
+
+
+
 /* Update game log by appending "text", or by appending a "." if "text" is the
  * same as the last one passed.
  */
diff --git a/src/rexit.c b/src/rexit.c
index e35e8a1..1bc639f 100644
--- a/src/rexit.c
+++ b/src/rexit.c
@@ -13,6 +13,8 @@
 #include "windows.h" /* for Win struct, free_win(), free_winmeta() */
 #include "map_objects.h" /* for free_item_defs(), free_monster_defs() */
 #include "wincontrol.h" /* for free_winconfs() */
+#include "misc.h" /* for unload_interface_conf() */
+
 
 
 /* The clean-up routine and the flag resource by which it decides what to do. */
@@ -49,20 +51,26 @@ static void cleanup(struct World * world)
     {
         free(world->map->cells);
     }
-    if (cleanup_flags & CLEANUP_KEYBINDINGS)
+    if (cleanup_flags & CLEANUP_INTERFACE_CONF)
     {
-        free_keybindings(world->kb_global.kbs);
-        free_keybindings(world->kb_wingeom.kbs);
-        free_keybindings(world->kb_winkeys.kbs);
+        unload_interface_conf(world);
     }
     if (cleanup_flags & CLEANUP_WIN_META)
     {
         free_winmeta(world->wmeta);
     }
+/*
+    if (cleanup_flags & CLEANUP_KEYBINDINGS)
+    {
+        free_keybindings(world->kb_global.kbs);
+        free_keybindings(world->kb_wingeom.kbs);
+        free_keybindings(world->kb_winkeys.kbs);
+    }
     if (cleanup_flags & CLEANUP_WINCONFS)
     {
         free_winconfs(world);
     }
+*/
 }
 
 
diff --git a/src/rexit.h b/src/rexit.h
index 4738844..4c6ddb0 100644
--- a/src/rexit.h
+++ b/src/rexit.h
@@ -22,13 +22,12 @@ enum cleanup_flag
 {
     CLEANUP_NCURSES         = 0x0001,
     CLEANUP_MAP             = 0x0002,
-    CLEANUP_KEYBINDINGS     = 0x0004,
-    CLEANUP_LOG             = 0x0008,
-    CLEANUP_COMMAND_DB      = 0x0010,
-    CLEANUP_MAP_OBJECTS     = 0x0020,
-    CLEANUP_MAP_OBJECT_DEFS = 0x0040,
-    CLEANUP_WIN_META        = 0x0080,
-    CLEANUP_WINCONFS        = 0x0100
+    CLEANUP_LOG             = 0x0004,
+    CLEANUP_COMMAND_DB      = 0x0008,
+    CLEANUP_MAP_OBJECTS     = 0x0010,
+    CLEANUP_MAP_OBJECT_DEFS = 0x0020,
+    CLEANUP_WIN_META        = 0x0040,
+    CLEANUP_INTERFACE_CONF  = 0x0080
 };
 extern void set_cleanup_flag(enum cleanup_flag flag);
 
diff --git a/src/wincontrol.c b/src/wincontrol.c
index 529457c..9143181 100644
--- a/src/wincontrol.c
+++ b/src/wincontrol.c
@@ -465,20 +465,6 @@ extern void sorted_wintoggle_and_activate(struct World * world)
 
 
 
-extern void reload_win_config(struct World * world)
-{
-    while (0 != world->wmeta->active)
-    {
-        suspend_win(world->wmeta, world->wmeta->active);
-    }
-    free_winconfs(world);
-    init_winconfs(world);
-    init_wins(world);
-    sorted_wintoggle_and_activate(world);
-}
-
-
-
 extern void save_win_configs(struct World * world)
 {
     char * f_name = "save_win_configs()";
diff --git a/src/wincontrol.h b/src/wincontrol.h
index b516a4f..416cc91 100644
--- a/src/wincontrol.h
+++ b/src/wincontrol.h
@@ -61,8 +61,7 @@ extern void init_wins(struct World * world);
  */
 extern void sorted_wintoggle_and_activate(struct World * world);
 
-/* Reload windows in order and sizes defined in win config. */
-extern void reload_win_config(struct World * world);
+
 
 /* Save all window's configurations to their configuration files. */
 extern void save_win_configs(struct World * world);
-- 
2.30.2