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

chat window deleting and re-adding - it just werks

This commit is contained in:
Jfreegman 2013-08-05 16:04:06 -04:00
parent f824373e6a
commit 6494a4070b
4 changed files with 43 additions and 40 deletions

6
chat.c
View File

@ -25,8 +25,8 @@ typedef struct {
} ChatContext; } ChatContext;
extern int w_active;
extern void del_window(ToxWindow *w, int f_num); extern void del_window(ToxWindow *w, int f_num);
extern int focus_window(int num);
extern void fix_name(uint8_t* name); extern void fix_name(uint8_t* name);
void print_help(ChatContext* self); void print_help(ChatContext* self);
void execute(ToxWindow* self, ChatContext* ctx, char* cmd); void execute(ToxWindow* self, ChatContext* ctx, char* cmd);
@ -188,7 +188,7 @@ void execute(ToxWindow* self, ChatContext* ctx, char* cmd)
wprintw(ctx->history, "Your ID: %s\n", id); wprintw(ctx->history, "Your ID: %s\n", id);
} }
else if (strcmp(ctx->line, "/close") == 0) { else if (strcmp(ctx->line, "/close") == 0) {
focus_window(0); // Go to prompt screen w_active = 0; // Go to prompt screen
int f_num = ctx->friendnum; int f_num = ctx->friendnum;
delwin(ctx->linewin); delwin(ctx->linewin);
del_window(self, f_num); del_window(self, f_num);
@ -236,7 +236,7 @@ void print_help(ChatContext* self) {
wprintw(self->history, " /nick <nickname> : Set your nickname\n"); wprintw(self->history, " /nick <nickname> : Set your nickname\n");
wprintw(self->history, " /myid : Print your ID\n"); wprintw(self->history, " /myid : Print your ID\n");
wprintw(self->history, " /clear : Clear the screen\n"); wprintw(self->history, " /clear : Clear the screen\n");
wprintw(self->history, " /close : Closes the current chat window\n"); wprintw(self->history, " /close : Close the current chat window\n");
wprintw(self->history, " /quit or /exit : Exit program\n"); wprintw(self->history, " /quit or /exit : Exit program\n");
wprintw(self->history, " /help : Print this message again\n\n"); wprintw(self->history, " /help : Print this message again\n\n");

View File

@ -13,9 +13,9 @@
#include "windows.h" #include "windows.h"
extern char WINDOW_STATUS[TOXWINDOWS_MAX_NUM]; extern char WINDOW_STATUS[TOXWINDOWS_MAX_NUM];
extern int add_window(ToxWindow w); extern int add_window(ToxWindow w, int n);
extern int focus_window(int num);
extern ToxWindow new_chat(int friendnum); extern ToxWindow new_chat(int friendnum);
extern int w_active;
typedef struct { typedef struct {
uint8_t name[MAX_NAME_LENGTH]; uint8_t name[MAX_NAME_LENGTH];
@ -52,7 +52,17 @@ void friendlist_onMessage(ToxWindow* self, int num, uint8_t* str, uint16_t len)
return; return;
if(friends[num].chatwin == -1) { if(friends[num].chatwin == -1) {
friends[num].chatwin = add_window(new_chat(num)); friends[num].chatwin = num;
int i;
/* Find first open slot to hold chat window */
for (i = N_DEFAULT_WINS; i < MAX_WINDOW_SLOTS; i++) {
if (WINDOW_STATUS[i] == -1) {
WINDOW_STATUS[i] = num;
add_window(new_chat(num_selected), i);
w_active = i;
break;
}
}
} }
} }
@ -104,17 +114,18 @@ static void friendlist_onKey(ToxWindow* self, int key) {
int i; int i;
for (i = N_DEFAULT_WINS; i < MAX_WINDOW_SLOTS; i++) { for (i = N_DEFAULT_WINS; i < MAX_WINDOW_SLOTS; i++) {
if (WINDOW_STATUS[i] == num_selected) { if (WINDOW_STATUS[i] == num_selected) {
focus_window(i); w_active = i;
break; break;
} }
} }
}else { }else {
friends[num_selected].chatwin = add_window(new_chat(num_selected));
focus_window(friends[num_selected].chatwin);
int i; int i;
for (i = N_DEFAULT_WINS; i < MAX_WINDOW_SLOTS; i++) { // Find open slot for (i = N_DEFAULT_WINS; i < MAX_WINDOW_SLOTS; i++) {
if (WINDOW_STATUS[i] == -1) { if (WINDOW_STATUS[i] == -1) {
WINDOW_STATUS[i] = num_selected; WINDOW_STATUS[i] = num_selected;
friends[num_selected].chatwin = num_selected;
add_window(new_chat(num_selected), i);
w_active = i;
break; break;
} }
} }

41
main.c
View File

@ -23,7 +23,7 @@ extern int add_req(uint8_t* public_key); // XXX
char WINDOW_STATUS[MAX_WINDOW_SLOTS]; // Holds status of chat windows char WINDOW_STATUS[MAX_WINDOW_SLOTS]; // Holds status of chat windows
static ToxWindow windows[MAX_WINDOW_SLOTS]; static ToxWindow windows[MAX_WINDOW_SLOTS];
int w_num; int w_num;
static int w_active; int w_active;
static ToxWindow* prompt; static ToxWindow* prompt;
// CALLBACKS START // CALLBACKS START
@ -40,7 +40,7 @@ void on_request(uint8_t* public_key, uint8_t* data, uint16_t length) {
wprintw(prompt->window, "Use \"accept %d\" to accept it.\n", n); wprintw(prompt->window, "Use \"accept %d\" to accept it.\n", n);
for(i=0; i<w_num; i++) { for(i=0; i<MAX_WINDOW_SLOTS; i++) {
if(windows[i].onFriendRequest != NULL) if(windows[i].onFriendRequest != NULL)
windows[i].onFriendRequest(&windows[i], public_key, data, length); windows[i].onFriendRequest(&windows[i], public_key, data, length);
} }
@ -51,7 +51,7 @@ void on_message(int friendnumber, uint8_t* string, uint16_t length) {
wprintw(prompt->window, "\n(message) %d: %s\n", friendnumber, string); wprintw(prompt->window, "\n(message) %d: %s\n", friendnumber, string);
for(i=0; i<w_num; i++) { for(i=0; i<MAX_WINDOW_SLOTS; i++) {
if(windows[i].onMessage != NULL) if(windows[i].onMessage != NULL)
windows[i].onMessage(&windows[i], friendnumber, string, length); windows[i].onMessage(&windows[i], friendnumber, string, length);
} }
@ -62,7 +62,7 @@ void on_nickchange(int friendnumber, uint8_t* string, uint16_t length) {
wprintw(prompt->window, "\n(nickchange) %d: %s!\n", friendnumber, string); wprintw(prompt->window, "\n(nickchange) %d: %s!\n", friendnumber, string);
for(i=0; i<w_num; i++) { for(i=0; i<MAX_WINDOW_SLOTS; i++) {
if(windows[i].onNickChange != NULL) if(windows[i].onNickChange != NULL)
windows[i].onNickChange(&windows[i], friendnumber, string, length); windows[i].onNickChange(&windows[i], friendnumber, string, length);
} }
@ -73,7 +73,7 @@ void on_statuschange(int friendnumber, uint8_t* string, uint16_t length) {
wprintw(prompt->window, "\n(statuschange) %d: %s!\n", friendnumber, string); wprintw(prompt->window, "\n(statuschange) %d: %s!\n", friendnumber, string);
for(i=0; i<w_num; i++) { for(i=0; i<MAX_WINDOW_SLOTS; i++) {
if(windows[i].onStatusChange != NULL) if(windows[i].onStatusChange != NULL)
windows[i].onStatusChange(&windows[i], friendnumber, string, length); windows[i].onStatusChange(&windows[i], friendnumber, string, length);
} }
@ -115,31 +115,31 @@ static void init_tox() {
} }
void init_window_status() { void init_window_status() {
/* Default window values decrement from -2 */
int i; int i;
for (i = 0; i < N_DEFAULT_WINS; i++) for (i = 0; i < N_DEFAULT_WINS; i++)
WINDOW_STATUS[i] = i; WINDOW_STATUS[i] = -(i+2);
int j; int j;
for (j = N_DEFAULT_WINS; j < MAX_WINDOW_SLOTS; j++) for (j = N_DEFAULT_WINS; j < MAX_WINDOW_SLOTS; j++)
WINDOW_STATUS[j] = -1; WINDOW_STATUS[j] = -1;
} }
int add_window(ToxWindow w) { int add_window(ToxWindow w, int n) {
if(w_num == TOXWINDOWS_MAX_NUM) if(w_num >= TOXWINDOWS_MAX_NUM)
return -1; return -1;
if(LINES < 2) if(LINES < 2)
return -1; return -1;
w.window = newwin(LINES - 2, COLS, 0, 0); w.window = newwin(LINES - 2, COLS, 0, 0);
if(w.window == NULL) if(w.window == NULL)
return -1; return -1;
windows[w_num++] = w; windows[n] = w;
w.onInit(&w); w.onInit(&w);
w_num++;
return w_num - 1; return n;
} }
/* Deletes window w and cleans up */ /* Deletes window w and cleans up */
@ -157,24 +157,17 @@ void del_window(ToxWindow *w, int f_num) {
refresh(); refresh();
} }
int focus_window(int num) {
if(num >= w_num || num < 0)
return -1;
w_active = num;
return 0;
}
static void init_windows() { static void init_windows() {
w_num = 0; w_num = 0;
int n_prompt = 0;
if(add_window(new_prompt()) == -1 || add_window(new_friendlist()) == -1) { int n_friendslist = 1;
if(add_window(new_prompt(), n_prompt) == -1
|| add_window(new_friendlist(), n_friendslist) == -1) {
fprintf(stderr, "add_window() failed.\n"); fprintf(stderr, "add_window() failed.\n");
endwin(); endwin();
exit(1); exit(1);
} }
prompt = &windows[0]; prompt = &windows[n_prompt];
} }
static void do_tox() { static void do_tox() {

View File

@ -158,7 +158,9 @@ static void execute(ToxWindow* self, char* cmd) {
break; break;
} }
} }
else if (!strcmp(cmd, "clear")) {
wclear(self->window);
}
else if(!strcmp(cmd, "help")) { else if(!strcmp(cmd, "help")) {
print_usage(self); print_usage(self);
} }
@ -254,9 +256,6 @@ static void execute(ToxWindow* self, char* cmd) {
wprintw(self->window, "Message successfully sent.\n"); wprintw(self->window, "Message successfully sent.\n");
} }
} }
else if (!strncmp(cmd, "clear", strlen("clear"))) {
wclear(self->window);
}
else { else {
wprintw(self->window, "Invalid command.\n"); wprintw(self->window, "Invalid command.\n");
} }
@ -316,7 +315,7 @@ static void print_usage(ToxWindow* self) {
wprintw(self->window, " nick <nickname> : Set your nickname\n"); wprintw(self->window, " nick <nickname> : Set your nickname\n");
wprintw(self->window, " accept <number> : Accept friend request\n"); wprintw(self->window, " accept <number> : Accept friend request\n");
wprintw(self->window, " myid : Print your ID\n"); wprintw(self->window, " myid : Print your ID\n");
wprintw(self->window, " clear : Clear the screen\n"); wprintw(self->window, " clear : Clear this window\n");
wprintw(self->window, " quit/exit : Exit program\n"); wprintw(self->window, " quit/exit : Exit program\n");
wprintw(self->window, " help : Print this message again\n"); wprintw(self->window, " help : Print this message again\n");