X-Git-Url: https://plomlompom.com/repos/foo.html?a=blobdiff_plain;f=src%2Fclient%2Fwindows.c;h=fa626848aacfe5f3b0ff9e5abc8d41e3173176b4;hb=a6fd795234fd4e4f9aebcfdc1771afd22c3c9c23;hp=9b923fad24ef1ebe6ac94b38b9b5ff7676209f70;hpb=f9d5829b125ef2df8d63bc08761be33f93c65691;p=plomrogue
diff --git a/src/client/windows.c b/src/client/windows.c
index 9b923fa..fa62684 100644
--- a/src/client/windows.c
+++ b/src/client/windows.c
@@ -1,5 +1,11 @@
-/* src/client/windows.c */
+/* src/client/windows.c
+ *
+ * This file is part of PlomRogue. PlomRogue is licensed under the GPL version 3
+ * or any later version. For details on its copyright, license, and warranties,
+ * see the file NOTICE in the root directory of the PlomRogue source package.
+ */
+#define _POSIX_C_SOURCE 200809L /* strnlen() */
#include "windows.h"
#include /* chtype, getmaxx(), getmaxy(), erase(), werase(),
* endwin(), delwin(), wnoutrefresh(), pnoutrefresh(),
@@ -9,26 +15,19 @@
#include /* NULL */
#include /* uint8_t, uint16_t, uint32_t, UINT16_MAX */
#include /* sprintf() */
-#include /* free(), atoi() */
-#include /* memcpy(), strlen(), strnlen(), strchr(), memset() */
-#include "../common/err_try_fgets.h" /* err_try_fgets(), err_line() */
-#include "../common/readwrite.h" /* try_fputc(), try_write(), try_fgetc() */
+#include /* free() */
+#include /* memcpy(), strlen(), strnlen(), memset() */
#include "../common/rexit.h" /* exit_trouble(), exit_err() */
#include "../common/try_malloc.h" /* try_malloc() */
-#include "../common/yx_uint16.h" /* struct yx_uint16 */
#include "draw_wins.h" /* draw_winconf_geometry(), draw_winconf_keybindings(),
* draw_win_inventory(), draw_win_info(), draw_win_log(),
- * draw_win_available_keybindings(), draw_win_map(),
* draw_win_keybindings_winconf_keybindings(),
* draw_win_keybindings_winconf_geometry(),
- * draw_win_keybindings_global()
+ * draw_win_keybindings_global(), draw_win_map(),
+ * draw_win_terrain_stack()
*/
-#include "keybindings.h" /* write_keybidings_to_file(),
- * read_keybindings_from_file()
- */
-#include "misc.h" /* array_append() */
#include "wincontrol.h" /* toggle_window() */
-#include "world.h" /* global world */
+#include "world.h" /* world */
@@ -120,9 +119,9 @@ static void (* get_drawfunc_by_char(char c)) ()
{
void (* f) (struct Win *) = NULL;
if ( match_func(c, &f, 'c', draw_win_inventory)
+ || match_func(c, &f, 's', draw_win_terrain_stack)
|| match_func(c, &f, 'i', draw_win_info)
|| match_func(c, &f, 'l', draw_win_log)
- || match_func(c, &f, 'k', draw_win_available_keybindings)
|| match_func(c, &f, 'm', draw_win_map)
|| match_func(c, &f, '0', draw_win_keybindings_global)
|| match_func(c, &f, '1', draw_win_keybindings_winconf_geometry)
@@ -160,8 +159,10 @@ static void scroll_hint(struct yx_uint16 fsize, char dir, uint16_t dist,
{
dsc_space = fsize.y;
} /* vv-- 10 = max strlen for uint16_t */
- char scrolldsc[1 + strlen(more) + 1 + 10 + 1 + strlen(unit) + 1 + 1];
- sprintf(scrolldsc, " %d %s %s ", dist, more, unit);
+ uint16_t size = 1 + strlen(more) + 1 + 10 + 1 + strlen(unit) + 1 + 1;
+ char * scrolldsc = try_malloc(size, __func__);
+ int test = sprintf(scrolldsc, " %d %s %s ", dist, more, unit);
+ exit_trouble(test < 0, __func__, "sprintf");
/* Decide on offset of the description text inside the scroll hint line. */
uint16_t dsc_offset = 1;
@@ -195,6 +196,7 @@ static void scroll_hint(struct yx_uint16 fsize, char dir, uint16_t dist,
}
mvwaddch(world.winDB.v_screen, start.y + draw_offset, start.x + q, c);
}
+ free(scrolldsc);
}
@@ -237,7 +239,7 @@ static void draw_win_borderlines(struct Win * w)
offset = (w->frame_size.x - (strlen(w->title) + 2)) / 2;
} /* +2 is for padding/decoration */
uint16_t length_visible = strnlen(w->title, w->frame_size.x - 2);
- char title[length_visible + 3];
+ char * title = try_malloc(length_visible + 3, "draw_win_borderlines");
char decoration = ' ';
if (w->id == world.winDB.active)
{
@@ -247,6 +249,7 @@ static void draw_win_borderlines(struct Win * w)
title[0] = title[length_visible + 1] = decoration;
title[length_visible + 2] = '\0';
mvwaddstr(world.winDB.v_screen, w->start.y-1, w->start.x+offset, title);
+ free(title);
}
}
@@ -306,7 +309,7 @@ static void draw_wins(struct Win * w)
{
chtype ch = w->winmap[(y * w->winmap_size.x) + x];
mvwaddch(world.winDB.v_screen, w->start.y + (y - offset_y),
- w->start.x + (x - offset_x), ch);
+ w->start.x + (x - offset_x), ch);
}
}
free(w->winmap); /* NULL so draw_wins.c's try_resize_winmap() may always */
@@ -392,145 +395,17 @@ extern struct Win * get_win_by_id(char id)
-extern uint8_t read_winconf_from_file(char * line, uint32_t linemax,
- FILE * file)
-{
- char * f_name = "read_winconf_from_file()";
- char * context = "Failed reading individual window's configuration from "
- "interface config file. ";
- char * err_id = "Illegal ID(s) selected.";
- char * err_2 = "Double-initialized window.";
- char * err_brk = "Illegal line break type index.";
- int test_for_end = try_fgetc(file, f_name);
- if (EOF == test_for_end || '\n' == test_for_end)
- {
- return 0;
- }
- exit_trouble(EOF == ungetc(test_for_end, file), f_name, "ungetc()");
- err_try_fgets(line, linemax, file, context, "ns");
- err_line(NULL == strchr(world.winDB.legal_ids, line[0]), line, context,
- err_id);
- exit_err(world.winDB.ids && NULL!=strchr(world.winDB.ids, line[0]), err_2);
- struct Win win;
- memset(&win, 0, sizeof(struct Win));
- win.id = (char) line[0];
- err_try_fgets(line, linemax, file, context, "0n");
- win.title = try_malloc(strlen(line), f_name);
- memcpy(win.title, line, strlen(line) - 1); /* Eliminate newline char */
- win.title[strlen(line) - 1] = '\0'; /* char at end of string. */
- err_try_fgets(line, linemax, file, context, "0nsi");
- err_line(atoi(line) > 2, line, context, err_brk);
- win.linebreak = atoi(line);
- err_try_fgets(line, linemax, file, context, "0ni");
- win.target_height = atoi(line);
- win.target_height_type = (0 >= win.target_height);
- err_try_fgets(line, linemax, file, context, "0ni");
- win.target_width = atoi(line);
- win.target_width_type = (0 >= win.target_width);
- read_keybindings_from_file(line, linemax, file, &win.kb);
- if (world.winDB.ids)
- {
- uint8_t old_ids_size = strlen(world.winDB.ids);
- char * new_ids = try_malloc(old_ids_size + 1 + 1, f_name);
- sprintf(new_ids, "%s%c", world.winDB.ids, win.id);
- free(world.winDB.ids);
- world.winDB.ids = new_ids;
- array_append(old_ids_size, sizeof(struct Win), (void *) &win,
- (void **) &world.winDB.wins);
- return 1;
- }
- world.winDB.ids = try_malloc(2, f_name);
- sprintf(world.winDB.ids, "%c", win.id);
- world.winDB.wins = try_malloc(sizeof(struct Win), f_name);
- world.winDB.wins[0] = win;
- return 1;
-}
-
-
-
-extern void write_winconf_of_id_to_file(FILE * file, char c)
-{
- char * f_name = "write_winconf_of_id_to_file()";
- struct Win * wc = get_win_by_id(c);
- uint8_t size = strlen(wc->title) + 2;
- if (size < 7) /* Ensure that at least 5 + 2 char fit into line so that */
- { /* the digit representation of any uint16_t may be stored. */
- size = 7;
- }
- char line[size];
- sprintf(line, "%c\n", wc->id);
- try_fwrite(line, sizeof(char), strlen(line), file, f_name);
- sprintf(line, "%s\n", wc->title);
- try_fwrite(line, sizeof(char), strlen(line), file, f_name);
- sprintf(line, "%d\n", wc->linebreak);
- try_fwrite(line, sizeof(char), strlen(line), file, f_name);
- sprintf(line, "%d\n", wc->target_height);
- try_fwrite(line, sizeof(char), strlen(line), file, f_name);
- sprintf(line, "%d\n", wc->target_width);
- try_fwrite(line, sizeof(char), strlen(line), file, f_name);
- write_keybindings_to_file(file, &wc->kb);
-}
-
-
-
-extern void read_order_wins_visible_active(char * line, uint32_t linemax,
- FILE * file, char ** tmp_order,
- char * tmp_active)
-{
- char * f_name = "read_order_wins_visible_active()";
- char * context = "Failed reading order and activation of visible windows "
- "from interface config file. ";
- char * err_id = "Illegal ID(s) selected.";
- err_try_fgets(line, linemax, file, context, "01");
- uint32_t i = 0;
- for (; i < strlen(line) - 1; i++)
- {
- char * test = strchr(world.winDB.legal_ids, line[i]);
- err_line(!test, line, context, err_id);
- }
- line[strlen(line) - 1] = '\0';
- *tmp_order = try_malloc(strlen(line) + 1, f_name);
- sprintf(*tmp_order, "%s", line);
- if (*tmp_order[0])
- {
- err_try_fgets(line, linemax, file, context, "0nfs");
- err_line(NULL == strchr(*tmp_order, line[0]), line, context, err_id);
- *tmp_active = line[0];
- }
- else
- {
- err_try_fgets(line, linemax, file, context, "0ne");
- *tmp_active = '\0';
- }
- err_try_fgets(line, linemax, file, context, "d");
-}
-
-
-
-extern void write_order_wins_visible_active(FILE * file)
-{
- char * f_name = "write_order_wins_visible_active()";
- try_fwrite(world.winDB.order, strlen(world.winDB.order), 1, file, f_name);
- try_fputc('\n', file, f_name);
- try_fputc(world.winDB.active, file, f_name);
- try_fputc('\n', file, f_name);
- try_fwrite(world.delim, strlen(world.delim), 1, file, f_name);
-}
-
-
-
extern void make_v_screen_and_init_win_sizes()
{
- char * f_name = "make_v_screen_and_init_win_sizes()";
char * err_s = "creating an illegaly large virtual screen";
- char * err_m = "triggering a memory allocation error via newpad()";
+ char * err_m = "triggering a memory allocation error via newpad";
uint32_t maxy_test = getmaxy(world.winDB.t_screen);
uint32_t maxx_test = getmaxx(world.winDB.t_screen);
- exit_trouble(maxy_test>UINT16_MAX || maxx_test>UINT16_MAX, f_name, err_s);
+ exit_trouble(maxy_test>UINT16_MAX || maxx_test>UINT16_MAX, __func__, err_s);
world.winDB.v_screen_size.y = maxy_test;
world.winDB.v_screen_size.x = maxx_test;
world.winDB.v_screen = newpad(world.winDB.v_screen_size.y, 1);
- exit_trouble(NULL == world.winDB.v_screen, f_name, err_m);
+ exit_trouble(!world.winDB.v_screen, __func__, err_m);
char id;
while (0 != (id = get_next_win_id()))
{
@@ -548,10 +423,8 @@ extern void free_winDB()
struct Win * wc = get_win_by_id(id);
free(wc->title);
free(wc->kb.kbs);
- wc->kb.kbs = NULL;
}
- free(world.winDB.ids); /* NULL this too since add_win_to_winDB() checks */
- world.winDB.ids = NULL; /* for it to detect its first post-DB-purge round.*/
+ free(world.winDB.ids);
free(world.winDB.wins);
free(world.winDB.order);
}
@@ -569,14 +442,16 @@ extern void reset_windows_on_winch()
{
endwin(); /* "[S]tandard way" to recalibrate ncurses post SIGWINCH, says */
refresh(); /* . */
- char tmp_order[strlen(world.winDB.order) + 1];
- sprintf(tmp_order, "%s", world.winDB.order);
+ char * tmp_order = try_malloc(strlen(world.winDB.order) + 1, __func__);
+ int test = sprintf(tmp_order, "%s", world.winDB.order);
+ exit_trouble(test < 0, __func__, "sprintf");
uint8_t i;
char tmp_active = world.winDB.active;
for (i = 0; i < strlen(tmp_order); toggle_window(tmp_order[i]), i++);
delwin(world.winDB.v_screen);
make_v_screen_and_init_win_sizes();
for (i = 0; i < strlen(tmp_order); toggle_window(tmp_order[i]), i++);
+ free(tmp_order);
world.winDB.active = tmp_active;
}