From a61f5f6a6d474448a450564d8f11cd2badaf16ef Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Wed, 26 Feb 2014 05:23:11 -0500 Subject: [PATCH] properly close windows on exit --- src/chat.c | 57 ++++++++++++++++++++++++++------------------- src/chat.h | 1 + src/groupchat.c | 32 ++++++++++++++----------- src/groupchat.h | 1 + src/main.c | 1 + src/toxic_windows.h | 9 ++++++- src/windows.c | 14 +++++++++++ 7 files changed, 77 insertions(+), 38 deletions(-) diff --git a/src/chat.c b/src/chat.c index 29bcec1..66b0884 100644 --- a/src/chat.c +++ b/src/chat.c @@ -71,6 +71,24 @@ static void set_typingstatus(ToxWindow *self, Tox *m, bool is_typing) ctx->self_is_typing = is_typing; } +void kill_chat_window(ToxWindow *self) +{ + set_active_window(0); + ChatContext *ctx = self->chatwin; + StatusBar *statusbar = self->stb; + + write_to_log(ctx); + + int f_num = self->num; + delwin(ctx->linewin); + delwin(statusbar->topline); + del_window(self); + disable_chatwin(f_num); + + free(ctx); + free(statusbar); +} + static void chat_onMessage(ToxWindow *self, Tox *m, int num, uint8_t *msg, uint16_t len) { if (self->num != num) @@ -333,7 +351,6 @@ static void chat_onKey(ToxWindow *self, Tox *m, wint_t key) getyx(self->window, y, x); getmaxyx(self->window, y2, x2); int cur_len = 0; - bool close_win = false; if (key == 0x107 || key == 0x8 || key == 0x7f) { /* BACKSPACE key: Remove character behind pos */ if (ctx->pos > 0) { @@ -480,17 +497,17 @@ static void chat_onKey(ToxWindow *self, Tox *m, wint_t key) add_line_to_hist(ctx->line, ctx->len, ctx->ln_history, &ctx->hst_tot, &ctx->hst_pos); if (line[0] == '/') { - if (close_win = !strcmp(line, "/close")) { - write_to_log(ctx); - int f_num = self->num; - delwin(ctx->linewin); - delwin(statusbar->topline); - del_window(self); - disable_chatwin(f_num); - } else if (strncmp(line, "/me ", strlen("/me ")) == 0) + if (strcmp(line, "/close") == 0) { + if (ctx->self_is_typing) + set_typingstatus(self, m, false); + + kill_chat_window(self); + return; + } else if (strncmp(line, "/me ", strlen("/me ")) == 0) { send_action(self, ctx, m, line + strlen("/me ")); - else + } else { execute(ctx->history, self, m, line, CHAT_COMMAND_MODE); + } } else if (!string_is_empty(line)) { uint8_t selfname[TOX_MAX_NAME_LENGTH]; tox_get_self_name(m, selfname, TOX_MAX_NAME_LENGTH); @@ -516,18 +533,11 @@ static void chat_onKey(ToxWindow *self, Tox *m, wint_t key) } } - if (close_win) { - free(ctx); - free(statusbar); - } else { - reset_buf(ctx->line, &ctx->pos, &ctx->len); - } + reset_buf(ctx->line, &ctx->pos, &ctx->len); } - if (!close_win) { - if (ctx->len <= 0 && ctx->self_is_typing) - set_typingstatus(self, m, false); - } + if (ctx->len <= 0 && ctx->self_is_typing) + set_typingstatus(self, m, false); } static void chat_onDraw(ToxWindow *self, Tox *m) @@ -660,10 +670,8 @@ static void chat_onInit(ToxWindow *self, Tox *m) execute(ctx->history, self, m, "/help", CHAT_COMMAND_MODE); wmove(self->window, y2 - CURS_Y_OFFSET, 0); - if (self->name) { - ctx->log.log_on = true; - init_logging_session(self->name, friends[self->num].pub_key, ctx); - } + ctx->log.log_on = true; + init_logging_session(self->name, friends[self->num].pub_key, ctx); } ToxWindow new_chat(Tox *m, int friendnum) @@ -672,6 +680,7 @@ ToxWindow new_chat(Tox *m, int friendnum) memset(&ret, 0, sizeof(ret)); ret.active = true; + ret.is_chat = true; ret.onKey = &chat_onKey; ret.onDraw = &chat_onDraw; diff --git a/src/chat.h b/src/chat.h index 1801d10..fac132a 100644 --- a/src/chat.h +++ b/src/chat.h @@ -25,6 +25,7 @@ #include "toxic_windows.h" +void kill_chat_window(ToxWindow *self); ToxWindow new_chat(Tox *m, int friendnum); #endif /* end of include guard: CHAT_H_6489PZ13 */ diff --git a/src/groupchat.c b/src/groupchat.c index 7fd238f..db20627 100644 --- a/src/groupchat.c +++ b/src/groupchat.c @@ -71,8 +71,19 @@ int init_groupchat_win(ToxWindow *prompt, Tox *m, int groupnum) return -1; } -static void close_groupchatwin(Tox *m, int groupnum) +void kill_groupchat_window(ToxWindow *self) { + ChatContext *ctx = self->chatwin; + write_to_log(ctx); + + delwin(ctx->linewin); + del_window(self); + free(ctx); +} + +static void close_groupchat(ToxWindow *self, Tox *m, int groupnum) +{ + set_active_window(0); tox_del_groupchat(m, groupnum); free(groupchats[groupnum].peer_names); @@ -87,6 +98,8 @@ static void close_groupchatwin(Tox *m, int groupnum) } max_groupchat_index = i; + + kill_groupchat_window(self); } static void print_groupchat_help(ChatContext *ctx) @@ -480,19 +493,14 @@ static void groupchat_onKey(ToxWindow *self, Tox *m, wint_t key) wclear(ctx->linewin); wmove(self->window, y2 - CURS_Y_OFFSET, 0); wclrtobot(self->window); - bool close_win = false; if (!string_is_empty(line)) add_line_to_hist(ctx->line, ctx->len, ctx->ln_history, &ctx->hst_tot, &ctx->hst_pos); if (line[0] == '/') { - if (close_win = strcmp(line, "/close") == 0) { - write_to_log(ctx); - set_active_window(0); - int groupnum = self->num; - delwin(ctx->linewin); - del_window(self); - close_groupchatwin(m, groupnum); + if (strcmp(line, "/close") == 0) { + close_groupchat(self, m, self->num); + return; } else if (strcmp(line, "/help") == 0) print_groupchat_help(ctx); else if (strncmp(line, "/me ", strlen("/me ")) == 0) @@ -507,10 +515,7 @@ static void groupchat_onKey(ToxWindow *self, Tox *m, wint_t key) } } - if (close_win) - free(ctx); - else - reset_buf(ctx->line, &ctx->pos, &ctx->len); + reset_buf(ctx->line, &ctx->pos, &ctx->len); } } @@ -591,6 +596,7 @@ ToxWindow new_group_chat(Tox *m, int groupnum) memset(&ret, 0, sizeof(ret)); ret.active = true; + ret.is_groupchat = true; ret.onKey = &groupchat_onKey; ret.onDraw = &groupchat_onDraw; diff --git a/src/groupchat.h b/src/groupchat.h index ea282df..542510c 100644 --- a/src/groupchat.h +++ b/src/groupchat.h @@ -33,5 +33,6 @@ typedef struct { uint8_t groupkey[TOX_CLIENT_ID_SIZE]; } GroupChat; +void kill_groupchat_window(ToxWindow *self); int init_groupchat_win(ToxWindow *prompt, Tox *m, int groupnum); ToxWindow new_group_chat(Tox *m, int groupnum); diff --git a/src/main.c b/src/main.c index 4ddeb36..21ce5a7 100644 --- a/src/main.c +++ b/src/main.c @@ -475,6 +475,7 @@ void exit_toxic(Tox *m) fclose(file_senders[i].file); } + kill_all_windows(); free(DATA_FILE); free(SRVLIST_FILE); free(prompt->stb); diff --git a/src/toxic_windows.h b/src/toxic_windows.h index 89ffbba..6eb100e 100644 --- a/src/toxic_windows.h +++ b/src/toxic_windows.h @@ -109,6 +109,10 @@ struct ToxWindow { bool active; int x; + /* window type identifiers */ + bool is_chat; + bool is_groupchat; + bool alert0; bool alert1; bool alert2; @@ -132,7 +136,7 @@ struct StatusBar { }; #define MAX_LOG_BUF_LINES 10 /* write log_buf contents to log file after this many lines */ -#define MAX_LOG_LINE_SIZE MAX_STR_SIZE + TOXIC_MAX_NAME_LENGTH + 24 /* extra room for time/date */ +#define MAX_LOG_LINE_SIZE MAX_STR_SIZE + TOXIC_MAX_NAME_LENGTH + 24 /* extra room for timestamp */ struct chatlog { uint8_t log_path[MAX_STR_SIZE]; @@ -226,4 +230,7 @@ int add_window(Tox *m, ToxWindow w); void del_window(ToxWindow *w); void set_active_window(int ch); int num_active_windows(void); + +/* closes all chat and groupchat windows (should only be called on shutdown) */ +void kill_all_windows(void); #endif diff --git a/src/windows.c b/src/windows.c index 018da54..e5dd40f 100644 --- a/src/windows.c +++ b/src/windows.c @@ -30,6 +30,7 @@ #include "friendlist.h" #include "prompt.h" #include "toxic_windows.h" +#include "groupchat.h" extern char *DATA_FILE; @@ -409,3 +410,16 @@ int num_active_windows(void) return count; } + +/* destroys all chat and groupchat windows (should only be called on shutdown) */ +void kill_all_windows(void) +{ + int i; + + for (i = 0; i < MAX_WINDOWS_NUM; ++i) { + if (windows[i].is_chat) + kill_chat_window(&windows[i]); + else if (windows[i].is_groupchat) + kill_groupchat_window(&windows[i]); + } +}