From bdd8f79ade82ef1a7445580fa61a53fd1a5311db Mon Sep 17 00:00:00 2001
From: Christian Heller <c.heller@plomlompom.de>
Date: Wed, 22 Jan 2014 15:28:14 +0100
Subject: [PATCH] Client: Moved pad creation/sizing/deletion into
 (un)load_interface(), so that calling them also fits the window config into a
 new terminal screen size.

---
 src/client/cleanup.c | 12 ++++++------
 src/client/main.c    |  4 ++--
 src/client/misc.c    | 14 +++++++++++++-
 src/client/misc.h    |  5 ++++-
 src/client/windows.c | 30 ++++++++----------------------
 src/client/windows.h | 10 +++-------
 6 files changed, 36 insertions(+), 39 deletions(-)

diff --git a/src/client/cleanup.c b/src/client/cleanup.c
index 7df98e7..bcf52ac 100644
--- a/src/client/cleanup.c
+++ b/src/client/cleanup.c
@@ -1,11 +1,11 @@
 /* src/client/cleanup.c */
 
 #include "cleanup.h"
+#include <ncurses.h> /* for endwin() */
 #include <stdint.h> /* uint32_t */
 #include <stdlib.h> /* free() */
 #include "command_db.h" /* free_command_db() */
 #include "misc.h" /* unload_interface_conf() */
-#include "windows.h" /* free_winmeta_and_endwin() */
 #include "world.h" /* world global */
 
 
@@ -20,13 +20,13 @@ extern void cleanup()
     free(world.map.cells);
     free(world.log);
     free(world.player_inventory);
-    if (cleanup_flags & CLEANUP_INTERFACE)/* Must come pre ncurses cleanup,   */
-    {                                     /* for by closing all windows it    */
-        unload_interface_conf();          /* relies on world.wmeta data freed */
-    }                                     /* by free_winmeta_and_endwin().    */
+    if (cleanup_flags & CLEANUP_INTERFACE)
+    {
+        unload_interface_conf();
+    }
     if (cleanup_flags & CLEANUP_NCURSES)
     {
-        free_winmeta_and_endwin();
+        endwin();
     }
     if (cleanup_flags & CLEANUP_COMMANDS)
     {
diff --git a/src/client/main.c b/src/client/main.c
index 6a71592..4fb6b3b 100644
--- a/src/client/main.c
+++ b/src/client/main.c
@@ -6,7 +6,7 @@
 #include "command_db.h" /* init_command_db() */
 #include "io.h" /* io_loop(), try_send() */
 #include "misc.h" /* load_interface_conf() */
-#include "windows.h" /* init_win_meta(); */
+#include "windows.h" /* init_wmeta_and_ncurses(); */
 #include "world.h" /* struct World */
 
 
@@ -24,7 +24,7 @@ int main()
     set_cleanup_func(cleanup);
 
     /* Initialize the whole interface. */
-    init_win_meta();
+    init_wmeta_and_ncurses();
     keypad(world.wmeta.screen, TRUE);
     init_command_db();      /* The command DB needs to be initialized before  */
     load_interface_conf();  /* the interface, whose keybindings depend on it. */
diff --git a/src/client/misc.c b/src/client/misc.c
index 0b052de..12f80bb 100644
--- a/src/client/misc.c
+++ b/src/client/misc.c
@@ -1,7 +1,9 @@
 /* src/client/misc.c */
 
 #include "misc.h"
-#include <stdint.h> /* uint8_t, uint16_t */
+#include <ncurses.h> /* delwin(), getmaxy(), getmaxx(), newpad() */
+#include <stdint.h> /* uint8_t, uint16_t, uint32_t */
+#include "../common/rexit.h" /* exit_err() */
 #include "cleanup.h" /* for set_cleanup_flag() */
 #include "keybindings.h" /* init_keybindings(), free_keybindings(),
                           * save_keybindings()
@@ -30,6 +32,15 @@ extern void load_interface_conf()
     init_keybindings("confclient/keybindings_wingeom", &world.kb_wingeom);
     init_keybindings("confclient/keybindings_winkeys", &world.kb_winkeys);
     init_winconfs();
+    char * err_s = "load_interface_conf() makes illegaly large virtual screen.";
+    char * err_m = "load_interface_conf(): memory alloc error via newpad().";
+    uint32_t maxy_test = getmaxy(world.wmeta.screen);
+    uint32_t maxx_test = getmaxx(world.wmeta.screen);
+    exit_err(maxy_test > UINT16_MAX || maxx_test > UINT16_MAX, err_s);
+    world.wmeta.padsize.y = maxy_test;
+    world.wmeta.padsize.x = maxx_test;
+    world.wmeta.pad = newpad(world.wmeta.padsize.y, 1);
+    exit_err(NULL == world.wmeta.pad, err_m);
     init_wins();
     sorted_wintoggle_and_activate();
     set_cleanup_flag(CLEANUP_INTERFACE);
@@ -47,6 +58,7 @@ extern void unload_interface_conf()
         suspend_win(world.wmeta.active);
     }
     free_winconfs();
+    delwin(world.wmeta.pad);
 }
 
 
diff --git a/src/client/misc.h b/src/client/misc.h
index cd283ed..7ca1137 100644
--- a/src/client/misc.h
+++ b/src/client/misc.h
@@ -11,7 +11,10 @@
 
 
 
-/* Save / load / unload (free) / reload interface configuration data. */
+/* Save / load (init) / unload (free/dissolve) / reload interface configuration
+ * data, world.wmeta.pad (initialized before opening any windows to the height
+ * of the terminal screen and a width of 1) and window chains.
+ */
 extern void save_interface_conf();
 extern void load_interface_conf();
 extern void unload_interface_conf();
diff --git a/src/client/windows.c b/src/client/windows.c
index 0ebe56d..b983110 100644
--- a/src/client/windows.c
+++ b/src/client/windows.c
@@ -6,10 +6,10 @@
 #include <stdio.h> /* sprintf() */
 #include <stdlib.h> /* free() */
 #include <string.h> /* strlen(), memcpy(), strnlen() */
-#include <ncurses.h> /* pnoutrefresh(), doupdate(), werase(), wnoutrefresh(),
-                      * erase(), getmaxx(), getmaxy(), delwin(), endwin(),
-                      * initscr(), noecho(), curs_set(), newpad(), mvwaddch(),
-                      * mvwaddstr(), wresize(), chtype
+#include <ncurses.h> /* chtype, pnoutrefresh(), doupdate(), werase(), erase(),
+                      * wnoutrefresh(), getmaxx(), getmaxy(), initscr(),
+                      * noecho(), curs_set(), newpad(), mvwaddch(), mvwaddstr(),
+                      * wresize()
                       */
 #include "../common/rexit.h" /* for exit_err() */
 #include "../common/try_malloc.h" /* for try_malloc() */
@@ -423,24 +423,18 @@ static void shift_win_backward()
 
 
 
-extern void init_win_meta()
+extern void init_wmeta_and_ncurses()
 {
-    char * err_s = "init_win_meta() creates virtual screen beyond legal size.";
-    char * err_m = "init_win_meta() triggers memory alloc error via newpad().";
     world.wmeta.screen = initscr();
     set_cleanup_flag(CLEANUP_NCURSES);
     noecho();
     curs_set(0);
-    uint32_t maxy_test  = getmaxy(world.wmeta.screen);
-    uint32_t maxx_test  = getmaxx(world.wmeta.screen);
-    exit_err(maxy_test > UINT16_MAX || maxx_test > UINT16_MAX, err_s);
-    world.wmeta.padsize.y   = maxy_test;
-    world.wmeta.padsize.x   = maxx_test;
+    world.wmeta.padsize.y   = 0;
+    world.wmeta.padsize.x   = 0;
     world.wmeta.chain_start = 0;
     world.wmeta.chain_end   = 0;
     world.wmeta.pad_offset  = 0;
-    world.wmeta.pad         = newpad(world.wmeta.padsize.y, 1);
-    exit_err(NULL == world.wmeta.pad, err_m);
+    world.wmeta.pad         = NULL;
     world.wmeta.active      = 0;
 }
 
@@ -484,14 +478,6 @@ extern void init_win(struct Win ** wp, char * title, int16_t height,
 
 
 
-extern void free_winmeta_and_endwin()
-{
-    delwin(world.wmeta.pad);
-    endwin();
-}
-
-
-
 extern void free_win(struct Win * win)
 {
     free(win->title);
diff --git a/src/client/windows.h b/src/client/windows.h
index 5e99343..4652513 100644
--- a/src/client/windows.h
+++ b/src/client/windows.h
@@ -66,11 +66,10 @@ struct WinMeta
 
 
 
-/* Initialize ncurses and world.wmeta on terminal screen. All struct members
- * initialize to 0, except for .screen, the newly created virtual screen .pad
- * and its .padsize (height: that of the terminal screen; width: 1 cell).
+/* Initialize world.wmeta and ncurses on terminal screen. All struct members
+ * initialize to 0 (.pad = NULL), except for .screen (= initscr()).
  */
-extern void init_win_meta();
+extern void init_wmeta_and_ncurses();
 
 /* Initialize a Win child "wp" of "wmeta" to "title", "height" and "width" and
  * appoint "func"() as its .draw. Initialize other members to 0.
@@ -85,9 +84,6 @@ extern void init_win_meta();
 extern void init_win(struct Win ** wp, char * title, int16_t height,
                      int16_t width, void * func);
 
-/* Free memory initialized below world.wmeta and run endwin(). */
-extern void free_winmeta_and_endwin();
-
 /* Free memory initianized Win structs. */
 extern void free_win(struct Win * win);
 
-- 
2.30.2