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

Refactor ToxWindows

ToxWindow constructor functions now return pointers instead of structs
ToxWindow windows array now holds pointers instead of structs
Refactor some logic in windows.c pertaining to keeping track of active windows
Use uint8_t for window array indexing
This commit is contained in:
jfreegman 2018-10-09 18:59:25 -04:00
parent d18cc8cbc2
commit c4a11f8dc7
No known key found for this signature in database
GPG Key ID: 3627F3144076AE63
12 changed files with 303 additions and 343 deletions

View File

@ -58,6 +58,8 @@
#endif /* __APPLE__ */ #endif /* __APPLE__ */
extern FriendsList Friends; extern FriendsList Friends;
extern ToxWindow *windows[MAX_WINDOWS_NUM];
struct CallControl CallControl; struct CallControl CallControl;
#define cbend pthread_exit(NULL) #define cbend pthread_exit(NULL)
@ -371,38 +373,28 @@ void callback_recv_invite(Tox *m, uint32_t friend_number)
Friends.list[friend_number].chatwin = add_window(m, new_chat(m, Friends.list[friend_number].num)); Friends.list[friend_number].chatwin = add_window(m, new_chat(m, Friends.list[friend_number].num));
} }
ToxWindow *windows = CallControl.prompt; for (uint8_t i = 0; i < MAX_WINDOWS_NUM; ++i) {
int i; if (windows[i] != NULL && windows[i]->onInvite != NULL && windows[i]->num == friend_number) {
windows[i]->onInvite(windows[i], CallControl.av, friend_number, CallControl.call_state);
for (i = 0; i < MAX_WINDOWS_NUM; ++i) {
if (windows[i].onInvite != NULL && windows[i].num == friend_number) {
windows[i].onInvite(&windows[i], CallControl.av, friend_number, CallControl.call_state);
} }
} }
} }
void callback_recv_ringing(uint32_t friend_number) void callback_recv_ringing(uint32_t friend_number)
{ {
ToxWindow *windows = CallControl.prompt; for (uint8_t i = 0; i < MAX_WINDOWS_NUM; ++i) {
int i; if (windows[i] != NULL && windows[i]->onRinging != NULL && windows[i]->num == friend_number) {
windows[i]->onRinging(windows[i], CallControl.av, friend_number, CallControl.call_state);
for (i = 0; i < MAX_WINDOWS_NUM; ++i) {
if (windows[i].onRinging != NULL && windows[i].num == friend_number) {
windows[i].onRinging(&windows[i], CallControl.av, friend_number, CallControl.call_state);
} }
} }
} }
void callback_recv_starting(uint32_t friend_number) void callback_recv_starting(uint32_t friend_number)
{ {
ToxWindow *windows = CallControl.prompt; for (uint8_t i = 0; i < MAX_WINDOWS_NUM; ++i) {
if (windows[i] != NULL && windows[i]->onStarting != NULL && windows[i]->num == friend_number) {
windows[i]->onStarting(windows[i], CallControl.av, friend_number, CallControl.call_state);
int i; if (0 != start_transmission(windows[i], &CallControl.calls[friend_number])) { /* YEAH! */
line_info_add(windows[i], NULL, NULL, NULL, SYS_MSG, 0, 0, "Error starting transmission!");
for (i = 0; i < MAX_WINDOWS_NUM; ++i) {
if (windows[i].onStarting != NULL && windows[i].num == friend_number) {
windows[i].onStarting(&windows[i], CallControl.av, friend_number, CallControl.call_state);
if (0 != start_transmission(&windows[i], &CallControl.calls[friend_number])) { /* YEAH! */
line_info_add(&windows[i], NULL, NULL, NULL, SYS_MSG, 0, 0, "Error starting transmission!");
} }
return; return;
@ -411,61 +403,46 @@ void callback_recv_starting(uint32_t friend_number)
} }
void callback_recv_ending(uint32_t friend_number) void callback_recv_ending(uint32_t friend_number)
{ {
ToxWindow *windows = CallControl.prompt; for (uint8_t i = 0; i < MAX_WINDOWS_NUM; ++i) {
int i; if (windows[i] != NULL && windows[i]->onEnding != NULL && windows[i]->num == friend_number) {
windows[i]->onEnding(windows[i], CallControl.av, friend_number, CallControl.call_state);
for (i = 0; i < MAX_WINDOWS_NUM; ++i) {
if (windows[i].onEnding != NULL && windows[i].num == friend_number) {
windows[i].onEnding(&windows[i], CallControl.av, friend_number, CallControl.call_state);
} }
} }
} }
void callback_call_started(uint32_t friend_number) void callback_call_started(uint32_t friend_number)
{ {
ToxWindow *windows = CallControl.prompt; for (uint8_t i = 0; i < MAX_WINDOWS_NUM; ++i) {
if (windows[i] != NULL && windows[i]->onStart != NULL && windows[i]->num == friend_number) {
windows[i]->onStart(windows[i], CallControl.av, friend_number, CallControl.call_state);
int i; if (0 != start_transmission(windows[i], &CallControl.calls[friend_number])) { /* YEAH! */
line_info_add(windows[i], NULL, NULL, NULL, SYS_MSG, 0, 0, "Error starting transmission!");
for (i = 0; i < MAX_WINDOWS_NUM; ++i)
if (windows[i].onStart != NULL && windows[i].num == friend_number) {
windows[i].onStart(&windows[i], CallControl.av, friend_number, CallControl.call_state);
if (0 != start_transmission(&windows[i], &CallControl.calls[friend_number])) { /* YEAH! */
line_info_add(&windows[i], NULL, NULL, NULL, SYS_MSG, 0, 0, "Error starting transmission!");
return; return;
} }
} }
}
} }
void callback_call_canceled(uint32_t friend_number) void callback_call_canceled(uint32_t friend_number)
{ {
ToxWindow *windows = CallControl.prompt; for (uint8_t i = 0; i < MAX_WINDOWS_NUM; ++i) {
int i; if (windows[i] != NULL && windows[i]->onCancel != NULL && windows[i]->num == friend_number) {
windows[i]->onCancel(windows[i], CallControl.av, friend_number, CallControl.call_state);
for (i = 0; i < MAX_WINDOWS_NUM; ++i) {
if (windows[i].onCancel != NULL && windows[i].num == friend_number) {
windows[i].onCancel(&windows[i], CallControl.av, friend_number, CallControl.call_state);
} }
} }
} }
void callback_call_rejected(uint32_t friend_number) void callback_call_rejected(uint32_t friend_number)
{ {
ToxWindow *windows = CallControl.prompt; for (uint8_t i = 0; i < MAX_WINDOWS_NUM; ++i) {
int i; if (windows[i] != NULL && windows[i]->onReject != NULL && windows[i]->num == friend_number) {
windows[i]->onReject(windows[i], CallControl.av, friend_number, CallControl.call_state);
for (i = 0; i < MAX_WINDOWS_NUM; ++i) {
if (windows[i].onReject != NULL && windows[i].num == friend_number) {
windows[i].onReject(&windows[i], CallControl.av, friend_number, CallControl.call_state);
} }
} }
} }
void callback_call_ended(uint32_t friend_number) void callback_call_ended(uint32_t friend_number)
{ {
ToxWindow *windows = CallControl.prompt; for (uint8_t i = 0; i < MAX_WINDOWS_NUM; ++i) {
int i; if (windows[i] != NULL && windows[i]->onEnd != NULL && windows[i]->num == friend_number) {
windows[i]->onEnd(windows[i], CallControl.av, friend_number, CallControl.call_state);
for (i = 0; i < MAX_WINDOWS_NUM; ++i) {
if (windows[i].onEnd != NULL && windows[i].num == friend_number) {
windows[i].onEnd(&windows[i], CallControl.av, friend_number, CallControl.call_state);
} }
} }
} }

View File

@ -1290,51 +1290,53 @@ static void chat_onInit(ToxWindow *self, Tox *m)
wmove(self->window, y2 - CURS_Y_OFFSET, 0); wmove(self->window, y2 - CURS_Y_OFFSET, 0);
} }
ToxWindow new_chat(Tox *m, uint32_t friendnum) ToxWindow *new_chat(Tox *m, uint32_t friendnum)
{ {
ToxWindow ret; ToxWindow *ret = calloc(1, sizeof(ToxWindow));
memset(&ret, 0, sizeof(ret));
ret.active = true; if (ret == NULL) {
ret.is_chat = true; exit_toxic_err("failed in new_chat", FATALERR_MEMORY);
}
ret.onKey = &chat_onKey; ret->is_chat = true;
ret.onDraw = &chat_onDraw;
ret.onInit = &chat_onInit; ret->onKey = &chat_onKey;
ret.onMessage = &chat_onMessage; ret->onDraw = &chat_onDraw;
ret.onConnectionChange = &chat_onConnectionChange; ret->onInit = &chat_onInit;
ret.onTypingChange = & chat_onTypingChange; ret->onMessage = &chat_onMessage;
ret.onGroupInvite = &chat_onGroupInvite; ret->onConnectionChange = &chat_onConnectionChange;
ret.onNickChange = &chat_onNickChange; ret->onTypingChange = & chat_onTypingChange;
ret.onStatusChange = &chat_onStatusChange; ret->onGroupInvite = &chat_onGroupInvite;
ret.onStatusMessageChange = &chat_onStatusMessageChange; ret->onNickChange = &chat_onNickChange;
ret.onFileChunkRequest = &chat_onFileChunkRequest; ret->onStatusChange = &chat_onStatusChange;
ret.onFileRecvChunk = &chat_onFileRecvChunk; ret->onStatusMessageChange = &chat_onStatusMessageChange;
ret.onFileControl = &chat_onFileControl; ret->onFileChunkRequest = &chat_onFileChunkRequest;
ret.onFileRecv = &chat_onFileRecv; ret->onFileRecvChunk = &chat_onFileRecvChunk;
ret.onReadReceipt = &chat_onReadReceipt; ret->onFileControl = &chat_onFileControl;
ret->onFileRecv = &chat_onFileRecv;
ret->onReadReceipt = &chat_onReadReceipt;
#ifdef AUDIO #ifdef AUDIO
ret.onInvite = &chat_onInvite; ret->onInvite = &chat_onInvite;
ret.onRinging = &chat_onRinging; ret->onRinging = &chat_onRinging;
ret.onStarting = &chat_onStarting; ret->onStarting = &chat_onStarting;
ret.onEnding = &chat_onEnding; ret->onEnding = &chat_onEnding;
ret.onError = &chat_onError; ret->onError = &chat_onError;
ret.onStart = &chat_onStart; ret->onStart = &chat_onStart;
ret.onCancel = &chat_onCancel; ret->onCancel = &chat_onCancel;
ret.onReject = &chat_onReject; ret->onReject = &chat_onReject;
ret.onEnd = &chat_onEnd; ret->onEnd = &chat_onEnd;
ret.is_call = false; ret->is_call = false;
ret.device_selection[0] = ret.device_selection[1] = -1; ret->device_selection[0] = ret->device_selection[1] = -1;
ret.ringing_sound = -1; ret->ringing_sound = -1;
#endif /* AUDIO */ #endif /* AUDIO */
ret.active_box = -1; ret->active_box = -1;
char nick[TOX_MAX_NAME_LENGTH]; char nick[TOX_MAX_NAME_LENGTH];
size_t n_len = get_nick_truncate(m, nick, friendnum); size_t n_len = get_nick_truncate(m, nick, friendnum);
set_window_title(&ret, nick, n_len); set_window_title(ret, nick, n_len);
ChatContext *chatwin = calloc(1, sizeof(ChatContext)); ChatContext *chatwin = calloc(1, sizeof(ChatContext));
StatusBar *stb = calloc(1, sizeof(StatusBar)); StatusBar *stb = calloc(1, sizeof(StatusBar));
@ -1344,11 +1346,11 @@ ToxWindow new_chat(Tox *m, uint32_t friendnum)
exit_toxic_err("failed in new_chat", FATALERR_MEMORY); exit_toxic_err("failed in new_chat", FATALERR_MEMORY);
} }
ret.chatwin = chatwin; ret->chatwin = chatwin;
ret.stb = stb; ret->stb = stb;
ret.help = help; ret->help = help;
ret.num = friendnum; ret->num = friendnum;
return ret; return ret;
} }

View File

@ -30,6 +30,6 @@
set msg to NULL if we don't want to display a message */ set msg to NULL if we don't want to display a message */
void chat_close_file_receiver(Tox *m, int filenum, int friendnum, int CTRL); void chat_close_file_receiver(Tox *m, int filenum, int friendnum, int CTRL);
void kill_chat_window(ToxWindow *self, Tox *m); void kill_chat_window(ToxWindow *self, Tox *m);
ToxWindow new_chat(Tox *m, int32_t friendnum); ToxWindow *new_chat(Tox *m, int32_t friendnum);
#endif /* end of include guard: CHAT_H */ #endif /* end of include guard: CHAT_H */

View File

@ -115,7 +115,7 @@ static void realloc_blocklist(int n)
void kill_friendlist(ToxWindow *self) void kill_friendlist(ToxWindow *self)
{ {
for (int i = 0; i < Friends.max_idx; ++i) { for (size_t i = 0; i < Friends.max_idx; ++i) {
if (Friends.list[i].active && Friends.list[i].group_invite.key != NULL) { if (Friends.list[i].active && Friends.list[i].group_invite.key != NULL) {
free(Friends.list[i].group_invite.key); free(Friends.list[i].group_invite.key);
} }
@ -619,7 +619,7 @@ static void delete_friend(Tox *m, uint32_t f_num)
if (toxwin != NULL) { if (toxwin != NULL) {
kill_chat_window(toxwin, m); kill_chat_window(toxwin, m);
set_active_window(1); /* keep friendlist focused */ set_active_window_index(1); /* keep friendlist focused */
} }
} }
@ -844,10 +844,10 @@ static void friendlist_onKey(ToxWindow *self, Tox *m, wint_t key, bool ltr)
/* Jump to chat window if already open */ /* Jump to chat window if already open */
if (Friends.list[f].chatwin != -1) { if (Friends.list[f].chatwin != -1) {
set_active_window(Friends.list[f].chatwin); set_active_window_index(Friends.list[f].chatwin);
} else if (get_num_active_windows() < MAX_WINDOWS_NUM) { } else if (get_num_active_windows() < MAX_WINDOWS_NUM) {
Friends.list[f].chatwin = add_window(m, new_chat(m, Friends.list[f].num)); Friends.list[f].chatwin = add_window(m, new_chat(m, Friends.list[f].num));
set_active_window(Friends.list[f].chatwin); set_active_window_index(Friends.list[f].chatwin);
} else { } else {
const char *msg = "* Warning: Too many windows are open."; const char *msg = "* Warning: Too many windows are open.";
line_info_add(prompt, NULL, NULL, NULL, SYS_MSG, 0, RED, msg); line_info_add(prompt, NULL, NULL, NULL, SYS_MSG, 0, RED, msg);
@ -1209,7 +1209,7 @@ static void friendlist_onAV(ToxWindow *self, ToxAV *av, uint32_t friend_number,
if (get_num_active_windows() < MAX_WINDOWS_NUM) { if (get_num_active_windows() < MAX_WINDOWS_NUM) {
if (state != TOXAV_FRIEND_CALL_STATE_FINISHED) { if (state != TOXAV_FRIEND_CALL_STATE_FINISHED) {
Friends.list[friend_number].chatwin = add_window(m, new_chat(m, Friends.list[friend_number].num)); Friends.list[friend_number].chatwin = add_window(m, new_chat(m, Friends.list[friend_number].num));
set_active_window(Friends.list[friend_number].chatwin); set_active_window_index(Friends.list[friend_number].chatwin);
} }
} else { } else {
char nick[TOX_MAX_NAME_LENGTH]; char nick[TOX_MAX_NAME_LENGTH];
@ -1237,42 +1237,44 @@ Tox_Connection get_friend_connection_status(uint32_t friendnumber)
return Friends.list[friendnumber].connection_status; return Friends.list[friendnumber].connection_status;
} }
ToxWindow new_friendlist(void) ToxWindow *new_friendlist(void)
{ {
ToxWindow ret; ToxWindow *ret = calloc(1, sizeof(ToxWindow));
memset(&ret, 0, sizeof(ret));
ret.active = true; if (ret == NULL) {
ret.is_friendlist = true; exit_toxic_err("failed in new_friendlist", FATALERR_MEMORY);
}
ret.onKey = &friendlist_onKey; ret->is_friendlist = true;
ret.onDraw = &friendlist_onDraw;
ret.onFriendAdded = &friendlist_onFriendAdded; ret->onKey = &friendlist_onKey;
ret.onMessage = &friendlist_onMessage; ret->onDraw = &friendlist_onDraw;
ret.onConnectionChange = &friendlist_onConnectionChange; ret->onFriendAdded = &friendlist_onFriendAdded;
ret.onNickChange = &friendlist_onNickChange; ret->onMessage = &friendlist_onMessage;
ret.onStatusChange = &friendlist_onStatusChange; ret->onConnectionChange = &friendlist_onConnectionChange;
ret.onStatusMessageChange = &friendlist_onStatusMessageChange; ret->onNickChange = &friendlist_onNickChange;
ret.onFileRecv = &friendlist_onFileRecv; ret->onStatusChange = &friendlist_onStatusChange;
ret.onGroupInvite = &friendlist_onGroupInvite; ret->onStatusMessageChange = &friendlist_onStatusMessageChange;
ret->onFileRecv = &friendlist_onFileRecv;
ret->onGroupInvite = &friendlist_onGroupInvite;
#ifdef AUDIO #ifdef AUDIO
ret.onInvite = &friendlist_onAV; ret->onInvite = &friendlist_onAV;
ret.onRinging = &friendlist_onAV; ret->onRinging = &friendlist_onAV;
ret.onStarting = &friendlist_onAV; ret->onStarting = &friendlist_onAV;
ret.onEnding = &friendlist_onAV; ret->onEnding = &friendlist_onAV;
ret.onError = &friendlist_onAV; ret->onError = &friendlist_onAV;
ret.onStart = &friendlist_onAV; ret->onStart = &friendlist_onAV;
ret.onCancel = &friendlist_onAV; ret->onCancel = &friendlist_onAV;
ret.onReject = &friendlist_onAV; ret->onReject = &friendlist_onAV;
ret.onEnd = &friendlist_onAV; ret->onEnd = &friendlist_onAV;
ret.is_call = false; ret->is_call = false;
ret.device_selection[0] = ret.device_selection[1] = -1; ret->device_selection[0] = ret->device_selection[1] = -1;
#endif /* AUDIO */ #endif /* AUDIO */
ret.num = -1; ret->num = -1;
ret.active_box = -1; ret->active_box = -1;
Help *help = calloc(1, sizeof(Help)); Help *help = calloc(1, sizeof(Help));
@ -1280,7 +1282,7 @@ ToxWindow new_friendlist(void)
exit_toxic_err("failed in new_friendlist", FATALERR_MEMORY); exit_toxic_err("failed in new_friendlist", FATALERR_MEMORY);
} }
ret.help = help; ret->help = help;
strcpy(ret.name, "contacts"); strcpy(ret->name, "contacts");
return ret; return ret;
} }

View File

@ -81,7 +81,7 @@ typedef struct {
ToxicFriend *list; ToxicFriend *list;
} FriendsList; } FriendsList;
ToxWindow new_friendlist(void); ToxWindow *new_friendlist(void);
void disable_chatwin(uint32_t f_num); void disable_chatwin(uint32_t f_num);
int get_friendnum(uint8_t *name); int get_friendnum(uint8_t *name);
int load_blocklist(char *data); int load_blocklist(char *data);

View File

@ -134,7 +134,7 @@ int init_groupchat_win(ToxWindow *prompt, Tox *m, uint32_t groupnum, uint8_t typ
return -1; return -1;
} }
ToxWindow self = new_group_chat(m, groupnum); ToxWindow *self = new_group_chat(m, groupnum);
for (int i = 0; i <= max_groupchat_index; ++i) { for (int i = 0; i <= max_groupchat_index; ++i) {
if (!groupchats[i].active) { if (!groupchats[i].active) {
@ -144,7 +144,7 @@ int init_groupchat_win(ToxWindow *prompt, Tox *m, uint32_t groupnum, uint8_t typ
groupchats[i].type = type; groupchats[i].type = type;
groupchats[i].start_time = get_unix_time(); groupchats[i].start_time = get_unix_time();
set_active_window(groupchats[i].chatwin); set_active_window_index(groupchats[i].chatwin);
if (i == max_groupchat_index) { if (i == max_groupchat_index) {
++max_groupchat_index; ++max_groupchat_index;
@ -154,7 +154,7 @@ int init_groupchat_win(ToxWindow *prompt, Tox *m, uint32_t groupnum, uint8_t typ
} }
} }
kill_groupchat_window(&self); kill_groupchat_window(self);
return -1; return -1;
} }
@ -700,23 +700,25 @@ static void groupchat_onInit(ToxWindow *self, Tox *m)
wmove(self->window, y2 - CURS_Y_OFFSET, 0); wmove(self->window, y2 - CURS_Y_OFFSET, 0);
} }
ToxWindow new_group_chat(Tox *m, uint32_t groupnum) ToxWindow *new_group_chat(Tox *m, uint32_t groupnum)
{ {
ToxWindow ret; ToxWindow *ret = calloc(1, sizeof(ToxWindow));
memset(&ret, 0, sizeof(ret));
ret.active = true; if (ret == NULL) {
ret.is_groupchat = true; exit_toxic_err("failed in new_group_chat", FATALERR_MEMORY);
}
ret.onKey = &groupchat_onKey; ret->is_groupchat = true;
ret.onDraw = &groupchat_onDraw;
ret.onInit = &groupchat_onInit;
ret.onGroupMessage = &groupchat_onGroupMessage;
ret.onGroupNameListChange = &groupchat_onGroupNameListChange;
ret.onGroupPeerNameChange = &groupchat_onGroupPeerNameChange;
ret.onGroupTitleChange = &groupchat_onGroupTitleChange;
snprintf(ret.name, sizeof(ret.name), "Group %d", groupnum); ret->onKey = &groupchat_onKey;
ret->onDraw = &groupchat_onDraw;
ret->onInit = &groupchat_onInit;
ret->onGroupMessage = &groupchat_onGroupMessage;
ret->onGroupNameListChange = &groupchat_onGroupNameListChange;
ret->onGroupPeerNameChange = &groupchat_onGroupPeerNameChange;
ret->onGroupTitleChange = &groupchat_onGroupTitleChange;
snprintf(ret->name, sizeof(ret->name), "Group %u", groupnum);
ChatContext *chatwin = calloc(1, sizeof(ChatContext)); ChatContext *chatwin = calloc(1, sizeof(ChatContext));
Help *help = calloc(1, sizeof(Help)); Help *help = calloc(1, sizeof(Help));
@ -725,12 +727,12 @@ ToxWindow new_group_chat(Tox *m, uint32_t groupnum)
exit_toxic_err("failed in new_group_chat", FATALERR_MEMORY); exit_toxic_err("failed in new_group_chat", FATALERR_MEMORY);
} }
ret.chatwin = chatwin; ret->chatwin = chatwin;
ret.help = help; ret->help = help;
ret.num = groupnum; ret->num = groupnum;
ret.show_peerlist = true; ret->show_peerlist = true;
ret.active_box = -1; ret->active_box = -1;
return ret; return ret;
} }

View File

@ -61,6 +61,6 @@ int init_groupchat_win(ToxWindow *prompt, Tox *m, uint32_t groupnum, uint8_t typ
/* destroys and re-creates groupchat window with or without the peerlist */ /* destroys and re-creates groupchat window with or without the peerlist */
void redraw_groupchat_win(ToxWindow *self); void redraw_groupchat_win(ToxWindow *self);
ToxWindow new_group_chat(Tox *m, uint32_t groupnum); ToxWindow *new_group_chat(Tox *m, uint32_t groupnum);
#endif /* GROUPCHAT_H */ #endif /* GROUPCHAT_H */

View File

@ -594,22 +594,24 @@ static void prompt_onInit(ToxWindow *self, Tox *m)
} }
} }
ToxWindow new_prompt(void) ToxWindow *new_prompt(void)
{ {
ToxWindow ret; ToxWindow *ret = calloc(1, sizeof(ToxWindow));
memset(&ret, 0, sizeof(ret));
ret.num = -1; if (ret == NULL) {
ret.active = true; exit_toxic_err("failed in new_prompt", FATALERR_MEMORY);
ret.is_prompt = true; }
ret.onKey = &prompt_onKey; ret->num = -1;
ret.onDraw = &prompt_onDraw; ret->is_prompt = true;
ret.onInit = &prompt_onInit;
ret.onConnectionChange = &prompt_onConnectionChange;
ret.onFriendRequest = &prompt_onFriendRequest;
strcpy(ret.name, "home"); ret->onKey = &prompt_onKey;
ret->onDraw = &prompt_onDraw;
ret->onInit = &prompt_onInit;
ret->onConnectionChange = &prompt_onConnectionChange;
ret->onFriendRequest = &prompt_onFriendRequest;
strcpy(ret->name, "home");
ChatContext *chatwin = calloc(1, sizeof(ChatContext)); ChatContext *chatwin = calloc(1, sizeof(ChatContext));
StatusBar *stb = calloc(1, sizeof(StatusBar)); StatusBar *stb = calloc(1, sizeof(StatusBar));
@ -619,11 +621,11 @@ ToxWindow new_prompt(void)
exit_toxic_err("failed in new_prompt", FATALERR_MEMORY); exit_toxic_err("failed in new_prompt", FATALERR_MEMORY);
} }
ret.chatwin = chatwin; ret->chatwin = chatwin;
ret.stb = stb; ret->stb = stb;
ret.help = help; ret->help = help;
ret.active_box = -1; ret->active_box = -1;
return ret; return ret;
} }

View File

@ -42,7 +42,7 @@ typedef struct {
extern FriendRequests FrndRequests; extern FriendRequests FrndRequests;
ToxWindow new_prompt(void); ToxWindow *new_prompt(void);
void prep_prompt_win(void); void prep_prompt_win(void);
void prompt_init_statusbar(ToxWindow *self, Tox *m, bool first_time_run); void prompt_init_statusbar(ToxWindow *self, Tox *m, bool first_time_run);

View File

@ -1281,7 +1281,6 @@ int main(int argc, char **argv)
arg_opts.encrypt_data = 0; arg_opts.encrypt_data = 0;
} }
init_term(); init_term();
prompt = init_windows(m); prompt = init_windows(m);

View File

@ -39,36 +39,32 @@
extern char *DATA_FILE; extern char *DATA_FILE;
extern struct Winthread Winthread; extern struct Winthread Winthread;
static ToxWindow windows[MAX_WINDOWS_NUM];
static ToxWindow *active_window; ToxWindow *windows[MAX_WINDOWS_NUM];
static uint8_t active_window_index;
static int num_active_windows;
extern ToxWindow *prompt; extern ToxWindow *prompt;
extern struct user_settings *user_settings; extern struct user_settings *user_settings;
static int num_active_windows;
/* CALLBACKS START */ /* CALLBACKS START */
void on_friend_request(Tox *m, const uint8_t *public_key, const uint8_t *data, size_t length, void *userdata) void on_friend_request(Tox *m, const uint8_t *public_key, const uint8_t *data, size_t length, void *userdata)
{ {
char msg[MAX_STR_SIZE + 1]; char msg[MAX_STR_SIZE + 1];
length = copy_tox_str(msg, sizeof(msg), (const char *) data, length); length = copy_tox_str(msg, sizeof(msg), (const char *) data, length);
size_t i; for (uint8_t i = 0; i < MAX_WINDOWS_NUM; ++i) {
if (windows[i]->onFriendRequest != NULL) {
for (i = 0; i < MAX_WINDOWS_NUM; ++i) { windows[i]->onFriendRequest(windows[i], m, (const char *) public_key, msg, length);
if (windows[i].onFriendRequest != NULL) {
windows[i].onFriendRequest(&windows[i], m, (const char *) public_key, msg, length);
} }
} }
} }
void on_friend_connection_status(Tox *m, uint32_t friendnumber, Tox_Connection connection_status, void *userdata) void on_friend_connection_status(Tox *m, uint32_t friendnumber, Tox_Connection connection_status, void *userdata)
{ {
size_t i; for (uint8_t i = 0; i < MAX_WINDOWS_NUM; ++i) {
if (windows[i] != NULL && windows[i]->onConnectionChange != NULL) {
for (i = 0; i < MAX_WINDOWS_NUM; ++i) { windows[i]->onConnectionChange(windows[i], m, friendnumber, connection_status);
if (windows[i].onConnectionChange != NULL) {
windows[i].onConnectionChange(&windows[i], m, friendnumber, connection_status);
} }
} }
} }
@ -79,11 +75,9 @@ void on_friend_typing(Tox *m, uint32_t friendnumber, bool is_typing, void *userd
return; return;
} }
size_t i; for (uint8_t i = 0; i < MAX_WINDOWS_NUM; ++i) {
if (windows[i] != NULL && windows[i]->onTypingChange != NULL) {
for (i = 0; i < MAX_WINDOWS_NUM; ++i) { windows[i]->onTypingChange(windows[i], m, friendnumber, is_typing);
if (windows[i].onTypingChange != NULL) {
windows[i].onTypingChange(&windows[i], m, friendnumber, is_typing);
} }
} }
} }
@ -94,11 +88,9 @@ void on_friend_message(Tox *m, uint32_t friendnumber, Tox_Message_Type type, con
char msg[MAX_STR_SIZE + 1]; char msg[MAX_STR_SIZE + 1];
length = copy_tox_str(msg, sizeof(msg), (const char *) string, length); length = copy_tox_str(msg, sizeof(msg), (const char *) string, length);
size_t i; for (uint8_t i = 0; i < MAX_WINDOWS_NUM; ++i) {
if (windows[i] != NULL && windows[i]->onMessage != NULL) {
for (i = 0; i < MAX_WINDOWS_NUM; ++i) { windows[i]->onMessage(windows[i], m, friendnumber, type, msg, length);
if (windows[i].onMessage != NULL) {
windows[i].onMessage(&windows[i], m, friendnumber, type, msg, length);
} }
} }
} }
@ -109,11 +101,9 @@ void on_friend_name(Tox *m, uint32_t friendnumber, const uint8_t *string, size_t
length = copy_tox_str(nick, sizeof(nick), (const char *) string, length); length = copy_tox_str(nick, sizeof(nick), (const char *) string, length);
filter_str(nick, length); filter_str(nick, length);
size_t i; for (uint8_t i = 0; i < MAX_WINDOWS_NUM; ++i) {
if (windows[i] != NULL && windows[i]->onNickChange != NULL) {
for (i = 0; i < MAX_WINDOWS_NUM; ++i) { windows[i]->onNickChange(windows[i], m, friendnumber, nick, length);
if (windows[i].onNickChange != NULL) {
windows[i].onNickChange(&windows[i], m, friendnumber, nick, length);
} }
} }
@ -126,33 +116,27 @@ void on_friend_status_message(Tox *m, uint32_t friendnumber, const uint8_t *stri
length = copy_tox_str(msg, sizeof(msg), (const char *) string, length); length = copy_tox_str(msg, sizeof(msg), (const char *) string, length);
filter_str(msg, length); filter_str(msg, length);
size_t i; for (uint8_t i = 0; i < MAX_WINDOWS_NUM; ++i) {
if (windows[i] != NULL && windows[i]->onStatusMessageChange != NULL) {
for (i = 0; i < MAX_WINDOWS_NUM; ++i) { windows[i]->onStatusMessageChange(windows[i], friendnumber, msg, length);
if (windows[i].onStatusMessageChange != NULL) {
windows[i].onStatusMessageChange(&windows[i], friendnumber, msg, length);
} }
} }
} }
void on_friend_status(Tox *m, uint32_t friendnumber, Tox_User_Status status, void *userdata) void on_friend_status(Tox *m, uint32_t friendnumber, Tox_User_Status status, void *userdata)
{ {
size_t i; for (uint8_t i = 0; i < MAX_WINDOWS_NUM; ++i) {
if (windows[i] != NULL && windows[i]->onStatusChange != NULL) {
for (i = 0; i < MAX_WINDOWS_NUM; ++i) { windows[i]->onStatusChange(windows[i], m, friendnumber, status);
if (windows[i].onStatusChange != NULL) {
windows[i].onStatusChange(&windows[i], m, friendnumber, status);
} }
} }
} }
void on_friend_added(Tox *m, uint32_t friendnumber, bool sort) void on_friend_added(Tox *m, uint32_t friendnumber, bool sort)
{ {
size_t i; for (uint8_t i = 0; i < MAX_WINDOWS_NUM; ++i) {
if (windows[i] != NULL && windows[i]->onFriendAdded != NULL) {
for (i = 0; i < MAX_WINDOWS_NUM; ++i) { windows[i]->onFriendAdded(windows[i], m, friendnumber, sort);
if (windows[i].onFriendAdded != NULL) {
windows[i].onFriendAdded(&windows[i], m, friendnumber, sort);
} }
} }
@ -165,11 +149,9 @@ void on_conference_message(Tox *m, uint32_t groupnumber, uint32_t peernumber, To
char msg[MAX_STR_SIZE + 1]; char msg[MAX_STR_SIZE + 1];
length = copy_tox_str(msg, sizeof(msg), (const char *) message, length); length = copy_tox_str(msg, sizeof(msg), (const char *) message, length);
size_t i; for (uint8_t i = 0; i < MAX_WINDOWS_NUM; ++i) {
if (windows[i] != NULL && windows[i]->onGroupMessage != NULL) {
for (i = 0; i < MAX_WINDOWS_NUM; ++i) { windows[i]->onGroupMessage(windows[i], m, groupnumber, peernumber, type, msg, length);
if (windows[i].onGroupMessage != NULL) {
windows[i].onGroupMessage(&windows[i], m, groupnumber, peernumber, type, msg, length);
} }
} }
} }
@ -177,22 +159,18 @@ void on_conference_message(Tox *m, uint32_t groupnumber, uint32_t peernumber, To
void on_conference_invite(Tox *m, uint32_t friendnumber, Tox_Conference_Type type, const uint8_t *group_pub_key, void on_conference_invite(Tox *m, uint32_t friendnumber, Tox_Conference_Type type, const uint8_t *group_pub_key,
size_t length, void *userdata) size_t length, void *userdata)
{ {
size_t i; for (uint8_t i = 0; i < MAX_WINDOWS_NUM; ++i) {
if (windows[i] != NULL && windows[i]->onGroupInvite != NULL) {
for (i = 0; i < MAX_WINDOWS_NUM; ++i) { windows[i]->onGroupInvite(windows[i], m, friendnumber, type, (char *) group_pub_key, length);
if (windows[i].onGroupInvite != NULL) {
windows[i].onGroupInvite(&windows[i], m, friendnumber, type, (char *) group_pub_key, length);
} }
} }
} }
void on_conference_peer_list_changed(Tox *m, uint32_t groupnumber, void *userdata) void on_conference_peer_list_changed(Tox *m, uint32_t groupnumber, void *userdata)
{ {
size_t i; for (uint8_t i = 0; i < MAX_WINDOWS_NUM; ++i) {
if (windows[i] != NULL && windows[i]->onGroupNameListChange != NULL) {
for (i = 0; i < MAX_WINDOWS_NUM; ++i) { windows[i]->onGroupNameListChange(windows[i], m, groupnumber);
if (windows[i].onGroupNameListChange != NULL) {
windows[i].onGroupNameListChange(&windows[i], m, groupnumber);
} }
} }
} }
@ -204,11 +182,9 @@ void on_conference_peer_name(Tox *m, uint32_t groupnumber, uint32_t peernumber,
length = copy_tox_str(nick, sizeof(nick), (const char *) name, length); length = copy_tox_str(nick, sizeof(nick), (const char *) name, length);
filter_str(nick, length); filter_str(nick, length);
size_t i; for (uint8_t i = 0; i < MAX_WINDOWS_NUM; ++i) {
if (windows[i] != NULL && windows[i]->onGroupPeerNameChange != NULL) {
for (i = 0; i < MAX_WINDOWS_NUM; ++i) { windows[i]->onGroupPeerNameChange(windows[i], m, groupnumber, peernumber, nick, length);
if (windows[i].onGroupPeerNameChange != NULL) {
windows[i].onGroupPeerNameChange(&windows[i], m, groupnumber, peernumber, nick, length);
} }
} }
} }
@ -219,11 +195,9 @@ void on_conference_title(Tox *m, uint32_t groupnumber, uint32_t peernumber, cons
char data[MAX_STR_SIZE + 1]; char data[MAX_STR_SIZE + 1];
length = copy_tox_str(data, sizeof(data), (const char *) title, length); length = copy_tox_str(data, sizeof(data), (const char *) title, length);
size_t i; for (uint8_t i = 0; i < MAX_WINDOWS_NUM; ++i) {
if (windows[i] != NULL && windows[i]->onGroupTitleChange != NULL) {
for (i = 0; i < MAX_WINDOWS_NUM; ++i) { windows[i]->onGroupTitleChange(windows[i], m, groupnumber, peernumber, data, length);
if (windows[i].onGroupTitleChange != NULL) {
windows[i].onGroupTitleChange(&windows[i], m, groupnumber, peernumber, data, length);
} }
} }
} }
@ -242,11 +216,9 @@ void on_file_chunk_request(Tox *m, uint32_t friendnumber, uint32_t filenumber, u
return; return;
} }
size_t i; for (uint8_t i = 0; i < MAX_WINDOWS_NUM; ++i) {
if (windows[i] != NULL && windows[i]->onFileChunkRequest != NULL) {
for (i = 0; i < MAX_WINDOWS_NUM; ++i) { windows[i]->onFileChunkRequest(windows[i], m, friendnumber, filenumber, position, length);
if (windows[i].onFileChunkRequest != NULL) {
windows[i].onFileChunkRequest(&windows[i], m, friendnumber, filenumber, position, length);
} }
} }
} }
@ -260,11 +232,9 @@ void on_file_recv_chunk(Tox *m, uint32_t friendnumber, uint32_t filenumber, uint
return; return;
} }
size_t i; for (uint8_t i = 0; i < MAX_WINDOWS_NUM; ++i) {
if (windows[i] != NULL && windows[i]->onFileRecvChunk != NULL) {
for (i = 0; i < MAX_WINDOWS_NUM; ++i) { windows[i]->onFileRecvChunk(windows[i], m, friendnumber, filenumber, position, (char *) data, length);
if (windows[i].onFileRecvChunk != NULL) {
windows[i].onFileRecvChunk(&windows[i], m, friendnumber, filenumber, position, (char *) data, length);
} }
} }
} }
@ -283,11 +253,9 @@ void on_file_recv_control(Tox *m, uint32_t friendnumber, uint32_t filenumber, To
return; return;
} }
size_t i; for (uint8_t i = 0; i < MAX_WINDOWS_NUM; ++i) {
if (windows[i] != NULL && windows[i]->onFileControl != NULL) {
for (i = 0; i < MAX_WINDOWS_NUM; ++i) { windows[i]->onFileControl(windows[i], m, friendnumber, filenumber, control);
if (windows[i].onFileControl != NULL) {
windows[i].onFileControl(&windows[i], m, friendnumber, filenumber, control);
} }
} }
} }
@ -301,55 +269,50 @@ void on_file_recv(Tox *m, uint32_t friendnumber, uint32_t filenumber, uint32_t k
return; return;
} }
size_t i; for (uint8_t i = 0; i < MAX_WINDOWS_NUM; ++i) {
if (windows[i] != NULL && windows[i]->onFileRecv != NULL) {
for (i = 0; i < MAX_WINDOWS_NUM; ++i) { windows[i]->onFileRecv(windows[i], m, friendnumber, filenumber, file_size, (char *) filename,
if (windows[i].onFileRecv != NULL) { filename_length);
windows[i].onFileRecv(&windows[i], m, friendnumber, filenumber, file_size, (char *) filename,
filename_length);
} }
} }
} }
void on_friend_read_receipt(Tox *m, uint32_t friendnumber, uint32_t receipt, void *userdata) void on_friend_read_receipt(Tox *m, uint32_t friendnumber, uint32_t receipt, void *userdata)
{ {
size_t i; for (uint8_t i = 0; i < MAX_WINDOWS_NUM; ++i) {
if (windows[i] != NULL && windows[i]->onReadReceipt != NULL) {
for (i = 0; i < MAX_WINDOWS_NUM; ++i) { windows[i]->onReadReceipt(windows[i], m, friendnumber, receipt);
if (windows[i].onReadReceipt != NULL) {
windows[i].onReadReceipt(&windows[i], m, friendnumber, receipt);
} }
} }
} }
/* CALLBACKS END */ /* CALLBACKS END */
int add_window(Tox *m, ToxWindow w) int add_window(Tox *m, ToxWindow *w)
{ {
if (LINES < 2) { if (LINES < 2) {
return -1; return -1;
} }
size_t i; for (uint8_t i = 0; i < MAX_WINDOWS_NUM; i++) {
if (windows[i] != NULL) {
for (i = 0; i < MAX_WINDOWS_NUM; i++) {
if (windows[i].active) {
continue; continue;
} }
w.window = newwin(LINES - 2, COLS, 0, 0); w->index = i;
w->window = newwin(LINES - 2, COLS, 0, 0);
if (w.window == NULL) { if (w->window == NULL) {
return -1; return -1;
} }
#ifdef URXVT_FIX #ifdef URXVT_FIX
/* Fixes text color problem on some terminals. */ /* Fixes text color problem on some terminals. */
wbkgd(w.window, COLOR_PAIR(6)); wbkgd(w->window, COLOR_PAIR(6));
#endif #endif
windows[i] = w; windows[i] = w;
if (w.onInit) { if (w->onInit) {
w.onInit(&w, m); w->onInit(w, m);
} }
++num_active_windows; ++num_active_windows;
@ -360,47 +323,46 @@ int add_window(Tox *m, ToxWindow w)
return -1; return -1;
} }
void set_active_window(int index) void set_active_window_index(uint8_t index)
{ {
if (index < 0 || index >= MAX_WINDOWS_NUM) { if (index < MAX_WINDOWS_NUM) {
return; active_window_index = index;
} }
active_window = windows + index;
} }
/* Shows next window when tab or back-tab is pressed */ /* Shows next window when tab or back-tab is pressed */
void set_next_window(int ch) void set_next_window(int ch)
{ {
ToxWindow *end = windows + MAX_WINDOWS_NUM - 1; if (ch == user_settings->key_next_tab) {
ToxWindow *inf = active_window; for (uint8_t i = active_window_index + 1; i < MAX_WINDOWS_NUM; ++i) {
if (windows[i] != NULL) {
while (true) { set_active_window_index(i);
if (ch == user_settings->key_next_tab) { return;
if (++active_window > end) {
active_window = windows;
} }
} else if (--active_window < windows) {
active_window = end;
} }
} else {
uint8_t start = active_window_index == 0 ? MAX_WINDOWS_NUM - 1 : active_window_index - 1;
if (active_window->window) { for (uint8_t i = start; i > 0; --i) {
return; if (windows[i] != NULL) {
} set_active_window_index(i);
return;
if (active_window == inf) { /* infinite loop check */ }
exit_toxic_err("failed in set_next_window", FATALERR_INFLOOP);
} }
} }
set_active_window_index(0);
} }
/* Deletes window w and cleans up */ /* Deletes window w and cleans up */
void del_window(ToxWindow *w) void del_window(ToxWindow *w)
{ {
set_active_window(0); /* Go to prompt screen */ set_active_window_index(0);
uint8_t idx = w->index;
delwin(w->window); delwin(w->window);
memset(w, 0, sizeof(ToxWindow)); free(windows[idx]);
windows[idx] = NULL;
clear(); clear();
refresh(); refresh();
@ -409,14 +371,18 @@ void del_window(ToxWindow *w)
ToxWindow *init_windows(Tox *m) ToxWindow *init_windows(Tox *m)
{ {
int n_prompt = add_window(m, new_prompt()); prompt = new_prompt();
int n_prompt = add_window(m, prompt);
if (n_prompt == -1 || add_window(m, new_friendlist()) == -1) { if (n_prompt < 0) {
exit_toxic_err("failed in init_windows", FATALERR_WININIT); exit_toxic_err("add_window() for prompt failed in init_windows", FATALERR_WININIT);
} }
prompt = &windows[n_prompt]; if (add_window(m, new_friendlist()) == -1) {
active_window = prompt; exit_toxic_err("add_window() for friendlist failed in init_windows", FATALERR_WININIT);
}
set_active_window_index(n_prompt);
return prompt; return prompt;
} }
@ -436,16 +402,14 @@ void on_window_resize(void)
return; return;
} }
size_t i; for (uint8_t i = 0; i < MAX_WINDOWS_NUM; ++i) {
if (windows[i] == NULL) {
for (i = 0; i < MAX_WINDOWS_NUM; ++i) {
if (!windows[i].active) {
continue; continue;
} }
ToxWindow *w = &windows[i]; ToxWindow *w = windows[i];
if (windows[i].is_friendlist) { if (windows[i]->is_friendlist) {
delwin(w->window); delwin(w->window);
w->window = newwin(y2, x2, 0, 0); w->window = newwin(y2, x2, 0, 0);
continue; continue;
@ -519,8 +483,14 @@ static void draw_bar(void)
{ {
int y, x; int y, x;
ToxWindow *w = windows[active_window_index];
if (w == NULL) {
return;
}
// save current cursor position // save current cursor position
getyx(active_window->window, y, x); getyx(w->window, y, x);
attron(COLOR_PAIR(BLUE)); attron(COLOR_PAIR(BLUE));
mvhline(LINES - 2, 0, '_', COLS); mvhline(LINES - 2, 0, '_', COLS);
@ -528,14 +498,12 @@ static void draw_bar(void)
move(LINES - 1, 0); move(LINES - 1, 0);
size_t i; for (uint8_t i = 0; i < MAX_WINDOWS_NUM; ++i) {
if (windows[i] == NULL) {
for (i = 0; i < MAX_WINDOWS_NUM; ++i) {
if (!windows[i].active) {
continue; continue;
} }
if (windows + i == active_window) { if (i == active_window_index) {
#ifdef URXVT_FIX #ifdef URXVT_FIX
attron(A_BOLD | COLOR_PAIR(GREEN)); attron(A_BOLD | COLOR_PAIR(GREEN));
@ -545,9 +513,9 @@ static void draw_bar(void)
attron(A_BOLD); attron(A_BOLD);
} }
draw_window_tab(&windows[i]); draw_window_tab(windows[i]);
if (windows + i == active_window) { if (i == active_window_index) {
#ifdef URXVT_FIX #ifdef URXVT_FIX
attroff(A_BOLD | COLOR_PAIR(GREEN)); attroff(A_BOLD | COLOR_PAIR(GREEN));
@ -566,7 +534,11 @@ static void draw_bar(void)
void draw_active_window(Tox *m) void draw_active_window(Tox *m)
{ {
ToxWindow *a = active_window; ToxWindow *a = windows[active_window_index];
if (a == NULL) {
return;
}
pthread_mutex_lock(&Winthread.lock); pthread_mutex_lock(&Winthread.lock);
a->alert = WINDOW_ALERT_NONE; a->alert = WINDOW_ALERT_NONE;
@ -619,35 +591,37 @@ void draw_active_window(Tox *m)
call at least once per second */ call at least once per second */
void refresh_inactive_windows(void) void refresh_inactive_windows(void)
{ {
size_t i; for (uint8_t i = 0; i < MAX_WINDOWS_NUM; ++i) {
ToxWindow *toxwin = windows[i];
for (i = 0; i < MAX_WINDOWS_NUM; ++i) { if (toxwin == NULL) {
ToxWindow *a = &windows[i]; continue;
}
if (a->active && a != active_window && !a->is_friendlist) { if (i != active_window_index && !toxwin->is_friendlist) {
pthread_mutex_lock(&Winthread.lock); pthread_mutex_lock(&Winthread.lock);
line_info_print(a); line_info_print(toxwin);
pthread_mutex_unlock(&Winthread.lock); pthread_mutex_unlock(&Winthread.lock);
} }
} }
} }
/* returns a pointer to the ToxWindow in the ith index. Returns NULL if no ToxWindow exists */ /* Returns a pointer to the ToxWindow in the ith index.
ToxWindow *get_window_ptr(int i) * Returns NULL if no ToxWindow exists.
*/
ToxWindow *get_window_ptr(size_t index)
{ {
ToxWindow *toxwin = NULL; if (index >= MAX_WINDOWS_NUM) {
return NULL;
if (i >= 0 && i < MAX_WINDOWS_NUM && windows[i].active) {
toxwin = &windows[i];
} }
return toxwin; return windows[index];
} }
/* returns a pointer to the currently open ToxWindow. */ /* Returns a pointer to the currently active ToxWindow. */
ToxWindow *get_active_window(void) ToxWindow *get_active_window(void)
{ {
return active_window; return windows[active_window_index];
} }
void force_refresh(WINDOW *w) void force_refresh(WINDOW *w)
@ -665,17 +639,19 @@ int get_num_active_windows(void)
/* destroys all chat and groupchat windows (should only be called on shutdown) */ /* destroys all chat and groupchat windows (should only be called on shutdown) */
void kill_all_windows(Tox *m) void kill_all_windows(Tox *m)
{ {
size_t i; for (uint8_t i = 2; i < MAX_WINDOWS_NUM; ++i) {
if (windows[i] == NULL) {
continue;
}
for (i = 2; i < MAX_WINDOWS_NUM; ++i) { if (windows[i]->is_chat) {
if (windows[i].is_chat) { kill_chat_window(windows[i], m);
kill_chat_window(&windows[i], m); } else if (windows[i]->is_groupchat) {
} else if (windows[i].is_groupchat) { free_groupchat(windows[i], m, windows[i]->num);
free_groupchat(&windows[i], m, windows[i].num);
} }
} }
/* TODO: use enum instead of magic indices */ /* TODO: use enum instead of magic indices */
kill_friendlist(&windows[1]); kill_friendlist(windows[1]);
kill_prompt_window(&windows[0]); kill_prompt_window(windows[0]);
} }

View File

@ -36,7 +36,7 @@
#include "toxic.h" #include "toxic.h"
#define MAX_WINDOWS_NUM 32 #define MAX_WINDOWS_NUM 16
#define MAX_WINDOW_NAME_LENGTH 22 #define MAX_WINDOW_NAME_LENGTH 22
#define CURS_Y_OFFSET 1 /* y-axis cursor offset for chat contexts */ #define CURS_Y_OFFSET 1 /* y-axis cursor offset for chat contexts */
#define CHATBOX_HEIGHT 2 #define CHATBOX_HEIGHT 2
@ -162,7 +162,7 @@ struct ToxWindow {
char name[TOXIC_MAX_NAME_LENGTH + 1]; char name[TOXIC_MAX_NAME_LENGTH + 1];
uint32_t num; /* corresponds to friendnumber in chat windows */ uint32_t num; /* corresponds to friendnumber in chat windows */
bool active; uint8_t index; /* This window's index in the windows array */
int x; int x;
bool is_chat; bool is_chat;
@ -252,14 +252,14 @@ struct Help {
ToxWindow *init_windows(Tox *m); ToxWindow *init_windows(Tox *m);
void draw_active_window(Tox *m); void draw_active_window(Tox *m);
int add_window(Tox *m, ToxWindow w); 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_index(uint8_t index);
int get_num_active_windows(void); int get_num_active_windows(void);
void kill_all_windows(Tox *m); /* should only be called on shutdown */ void kill_all_windows(Tox *m); /* should only be called on shutdown */
void on_window_resize(void); void on_window_resize(void);
void force_refresh(WINDOW *w); void force_refresh(WINDOW *w);
ToxWindow *get_window_ptr(int i); ToxWindow *get_window_ptr(size_t i);
ToxWindow *get_active_window(void); ToxWindow *get_active_window(void);
/* refresh inactive windows to prevent scrolling bugs. /* refresh inactive windows to prevent scrolling bugs.