1
0
mirror of https://github.com/Tha14/toxic.git synced 2024-11-23 02:03:02 +01:00

properly close windows on exit

This commit is contained in:
Jfreegman 2014-02-26 05:23:11 -05:00
parent 5ff7065744
commit a61f5f6a6d
7 changed files with 77 additions and 38 deletions

View File

@ -71,6 +71,24 @@ static void set_typingstatus(ToxWindow *self, Tox *m, bool is_typing)
ctx->self_is_typing = 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) static void chat_onMessage(ToxWindow *self, Tox *m, int num, uint8_t *msg, uint16_t len)
{ {
if (self->num != num) if (self->num != num)
@ -333,7 +351,6 @@ static void chat_onKey(ToxWindow *self, Tox *m, wint_t key)
getyx(self->window, y, x); getyx(self->window, y, x);
getmaxyx(self->window, y2, x2); getmaxyx(self->window, y2, x2);
int cur_len = 0; int cur_len = 0;
bool close_win = false;
if (key == 0x107 || key == 0x8 || key == 0x7f) { /* BACKSPACE key: Remove character behind pos */ if (key == 0x107 || key == 0x8 || key == 0x7f) { /* BACKSPACE key: Remove character behind pos */
if (ctx->pos > 0) { 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); add_line_to_hist(ctx->line, ctx->len, ctx->ln_history, &ctx->hst_tot, &ctx->hst_pos);
if (line[0] == '/') { if (line[0] == '/') {
if (close_win = !strcmp(line, "/close")) { if (strcmp(line, "/close") == 0) {
write_to_log(ctx); if (ctx->self_is_typing)
int f_num = self->num; set_typingstatus(self, m, false);
delwin(ctx->linewin);
delwin(statusbar->topline); kill_chat_window(self);
del_window(self); return;
disable_chatwin(f_num); } else if (strncmp(line, "/me ", strlen("/me ")) == 0) {
} else if (strncmp(line, "/me ", strlen("/me ")) == 0)
send_action(self, ctx, m, line + strlen("/me ")); send_action(self, ctx, m, line + strlen("/me "));
else } else {
execute(ctx->history, self, m, line, CHAT_COMMAND_MODE); execute(ctx->history, self, m, line, CHAT_COMMAND_MODE);
}
} else if (!string_is_empty(line)) { } else if (!string_is_empty(line)) {
uint8_t selfname[TOX_MAX_NAME_LENGTH]; uint8_t selfname[TOX_MAX_NAME_LENGTH];
tox_get_self_name(m, selfname, TOX_MAX_NAME_LENGTH); tox_get_self_name(m, selfname, TOX_MAX_NAME_LENGTH);
@ -516,19 +533,12 @@ 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) if (ctx->len <= 0 && ctx->self_is_typing)
set_typingstatus(self, m, false); set_typingstatus(self, m, false);
} }
}
static void chat_onDraw(ToxWindow *self, Tox *m) static void chat_onDraw(ToxWindow *self, Tox *m)
{ {
@ -660,11 +670,9 @@ static void chat_onInit(ToxWindow *self, Tox *m)
execute(ctx->history, self, m, "/help", CHAT_COMMAND_MODE); execute(ctx->history, self, m, "/help", CHAT_COMMAND_MODE);
wmove(self->window, y2 - CURS_Y_OFFSET, 0); wmove(self->window, y2 - CURS_Y_OFFSET, 0);
if (self->name) {
ctx->log.log_on = true; ctx->log.log_on = true;
init_logging_session(self->name, friends[self->num].pub_key, ctx); init_logging_session(self->name, friends[self->num].pub_key, ctx);
} }
}
ToxWindow new_chat(Tox *m, int friendnum) ToxWindow new_chat(Tox *m, int friendnum)
{ {
@ -672,6 +680,7 @@ ToxWindow new_chat(Tox *m, int friendnum)
memset(&ret, 0, sizeof(ret)); memset(&ret, 0, sizeof(ret));
ret.active = true; ret.active = true;
ret.is_chat = true;
ret.onKey = &chat_onKey; ret.onKey = &chat_onKey;
ret.onDraw = &chat_onDraw; ret.onDraw = &chat_onDraw;

View File

@ -25,6 +25,7 @@
#include "toxic_windows.h" #include "toxic_windows.h"
void kill_chat_window(ToxWindow *self);
ToxWindow new_chat(Tox *m, int friendnum); ToxWindow new_chat(Tox *m, int friendnum);
#endif /* end of include guard: CHAT_H_6489PZ13 */ #endif /* end of include guard: CHAT_H_6489PZ13 */

View File

@ -71,8 +71,19 @@ int init_groupchat_win(ToxWindow *prompt, Tox *m, int groupnum)
return -1; 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); tox_del_groupchat(m, groupnum);
free(groupchats[groupnum].peer_names); free(groupchats[groupnum].peer_names);
@ -87,6 +98,8 @@ static void close_groupchatwin(Tox *m, int groupnum)
} }
max_groupchat_index = i; max_groupchat_index = i;
kill_groupchat_window(self);
} }
static void print_groupchat_help(ChatContext *ctx) 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); wclear(ctx->linewin);
wmove(self->window, y2 - CURS_Y_OFFSET, 0); wmove(self->window, y2 - CURS_Y_OFFSET, 0);
wclrtobot(self->window); wclrtobot(self->window);
bool close_win = false;
if (!string_is_empty(line)) if (!string_is_empty(line))
add_line_to_hist(ctx->line, ctx->len, ctx->ln_history, &ctx->hst_tot, &ctx->hst_pos); add_line_to_hist(ctx->line, ctx->len, ctx->ln_history, &ctx->hst_tot, &ctx->hst_pos);
if (line[0] == '/') { if (line[0] == '/') {
if (close_win = strcmp(line, "/close") == 0) { if (strcmp(line, "/close") == 0) {
write_to_log(ctx); close_groupchat(self, m, self->num);
set_active_window(0); return;
int groupnum = self->num;
delwin(ctx->linewin);
del_window(self);
close_groupchatwin(m, groupnum);
} else if (strcmp(line, "/help") == 0) } else if (strcmp(line, "/help") == 0)
print_groupchat_help(ctx); print_groupchat_help(ctx);
else if (strncmp(line, "/me ", strlen("/me ")) == 0) else if (strncmp(line, "/me ", strlen("/me ")) == 0)
@ -507,9 +515,6 @@ 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)); memset(&ret, 0, sizeof(ret));
ret.active = true; ret.active = true;
ret.is_groupchat = true;
ret.onKey = &groupchat_onKey; ret.onKey = &groupchat_onKey;
ret.onDraw = &groupchat_onDraw; ret.onDraw = &groupchat_onDraw;

View File

@ -33,5 +33,6 @@ typedef struct {
uint8_t groupkey[TOX_CLIENT_ID_SIZE]; uint8_t groupkey[TOX_CLIENT_ID_SIZE];
} GroupChat; } GroupChat;
void kill_groupchat_window(ToxWindow *self);
int init_groupchat_win(ToxWindow *prompt, Tox *m, int groupnum); int init_groupchat_win(ToxWindow *prompt, Tox *m, int groupnum);
ToxWindow new_group_chat(Tox *m, int groupnum); ToxWindow new_group_chat(Tox *m, int groupnum);

View File

@ -475,6 +475,7 @@ void exit_toxic(Tox *m)
fclose(file_senders[i].file); fclose(file_senders[i].file);
} }
kill_all_windows();
free(DATA_FILE); free(DATA_FILE);
free(SRVLIST_FILE); free(SRVLIST_FILE);
free(prompt->stb); free(prompt->stb);

View File

@ -109,6 +109,10 @@ struct ToxWindow {
bool active; bool active;
int x; int x;
/* window type identifiers */
bool is_chat;
bool is_groupchat;
bool alert0; bool alert0;
bool alert1; bool alert1;
bool alert2; 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_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 { struct chatlog {
uint8_t log_path[MAX_STR_SIZE]; uint8_t log_path[MAX_STR_SIZE];
@ -226,4 +230,7 @@ int add_window(Tox *m, ToxWindow w);
void del_window(ToxWindow *w); void del_window(ToxWindow *w);
void set_active_window(int ch); void set_active_window(int ch);
int num_active_windows(void); int num_active_windows(void);
/* closes all chat and groupchat windows (should only be called on shutdown) */
void kill_all_windows(void);
#endif #endif

View File

@ -30,6 +30,7 @@
#include "friendlist.h" #include "friendlist.h"
#include "prompt.h" #include "prompt.h"
#include "toxic_windows.h" #include "toxic_windows.h"
#include "groupchat.h"
extern char *DATA_FILE; extern char *DATA_FILE;
@ -409,3 +410,16 @@ int num_active_windows(void)
return count; 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]);
}
}