From 3bc7f15fe5a306ced566e1258ad4f9c4f89cd463 Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Mon, 5 Aug 2013 01:57:29 -0400 Subject: added ability to close and reopen chat windows & other minor changes --- testing/toxic/friendlist.c | 40 +++++++++++++++++++++++++++------------- 1 file changed, 27 insertions(+), 13 deletions(-) (limited to 'testing/toxic/friendlist.c') diff --git a/testing/toxic/friendlist.c b/testing/toxic/friendlist.c index b4b619a2..2d060ae0 100644 --- a/testing/toxic/friendlist.c +++ b/testing/toxic/friendlist.c @@ -12,12 +12,11 @@ #include "windows.h" +extern char WINDOW_STATUS[TOXWINDOWS_MAX_NUM]; extern int add_window(ToxWindow w); extern int focus_window(int num); extern ToxWindow new_chat(int friendnum); -#define MAX_FRIENDS_NUM 100 - typedef struct { uint8_t name[MAX_NAME_LENGTH]; uint8_t status[MAX_USERSTATUS_LENGTH]; @@ -86,14 +85,11 @@ int friendlist_onFriendAdded(int num) { getname(num, friends[num_friends].name); strcpy((char*) friends[num_friends].name, "unknown"); strcpy((char*) friends[num_friends].status, "unknown"); - friends[num_friends].chatwin = -1; - - num_friends++; + friends[num_friends++].chatwin = -1; return 0; } static void friendlist_onKey(ToxWindow* self, int key) { - if(key == KEY_UP) { if(num_selected != 0) num_selected--; @@ -103,12 +99,26 @@ static void friendlist_onKey(ToxWindow* self, int key) { num_selected = (num_selected+1) % num_friends; } else if(key == '\n') { - - if(friends[num_selected].chatwin != -1) - return; - - friends[num_selected].chatwin = add_window(new_chat(num_selected)); - focus_window(friends[num_selected].chatwin); + /* Jump to chat window if already open */ + if (friends[num_selected].chatwin != -1) { + int i; + for (i = N_DEFAULT_WINS; i < MAX_WINDOW_SLOTS; i++) { + if (WINDOW_STATUS[i] == num_selected) { + focus_window(i); + break; + } + } + }else { + friends[num_selected].chatwin = add_window(new_chat(num_selected)); + focus_window(friends[num_selected].chatwin); + int i; + for (i = N_DEFAULT_WINS; i < MAX_WINDOW_SLOTS; i++) { // Find open slot + if (WINDOW_STATUS[i] == -1) { + WINDOW_STATUS[i] = num_selected; + break; + } + } + } } } @@ -145,6 +155,10 @@ static void friendlist_onDraw(ToxWindow* self) { wrefresh(self->window); } +void disable_chatwin(int f_num) { + friends[f_num].chatwin = -1; +} + static void friendlist_onInit(ToxWindow* self) { } @@ -164,4 +178,4 @@ ToxWindow new_friendlist() { strcpy(ret.title, "[friends]"); return ret; -} +} \ No newline at end of file -- cgit v1.2.3 From 4b76983a692e0234b220b1c8fee87edb12673be2 Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Mon, 5 Aug 2013 16:04:06 -0400 Subject: chat window deleting and re-adding - it just werks --- testing/toxic/chat.c | 6 +++--- testing/toxic/friendlist.c | 27 +++++++++++++++++++-------- testing/toxic/main.c | 41 +++++++++++++++++------------------------ testing/toxic/prompt.c | 9 ++++----- 4 files changed, 43 insertions(+), 40 deletions(-) (limited to 'testing/toxic/friendlist.c') diff --git a/testing/toxic/chat.c b/testing/toxic/chat.c index 2c1f1072..5d0e14ad 100644 --- a/testing/toxic/chat.c +++ b/testing/toxic/chat.c @@ -25,8 +25,8 @@ typedef struct { } ChatContext; +extern int w_active; extern void del_window(ToxWindow *w, int f_num); -extern int focus_window(int num); extern void fix_name(uint8_t* name); void print_help(ChatContext* self); 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); } 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; delwin(ctx->linewin); del_window(self, f_num); @@ -236,7 +236,7 @@ void print_help(ChatContext* self) { wprintw(self->history, " /nick : Set your nickname\n"); wprintw(self->history, " /myid : Print your ID\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, " /help : Print this message again\n\n"); diff --git a/testing/toxic/friendlist.c b/testing/toxic/friendlist.c index 2d060ae0..647f563d 100644 --- a/testing/toxic/friendlist.c +++ b/testing/toxic/friendlist.c @@ -13,9 +13,9 @@ #include "windows.h" extern char WINDOW_STATUS[TOXWINDOWS_MAX_NUM]; -extern int add_window(ToxWindow w); -extern int focus_window(int num); +extern int add_window(ToxWindow w, int n); extern ToxWindow new_chat(int friendnum); +extern int w_active; typedef struct { uint8_t name[MAX_NAME_LENGTH]; @@ -52,7 +52,17 @@ void friendlist_onMessage(ToxWindow* self, int num, uint8_t* str, uint16_t len) return; 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; for (i = N_DEFAULT_WINS; i < MAX_WINDOW_SLOTS; i++) { if (WINDOW_STATUS[i] == num_selected) { - focus_window(i); + w_active = i; break; } } }else { - friends[num_selected].chatwin = add_window(new_chat(num_selected)); - focus_window(friends[num_selected].chatwin); 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) { WINDOW_STATUS[i] = num_selected; + friends[num_selected].chatwin = num_selected; + add_window(new_chat(num_selected), i); + w_active = i; break; } } @@ -178,4 +189,4 @@ ToxWindow new_friendlist() { strcpy(ret.title, "[friends]"); return ret; -} \ No newline at end of file +} diff --git a/testing/toxic/main.c b/testing/toxic/main.c index c8cf73ed..892f812a 100644 --- a/testing/toxic/main.c +++ b/testing/toxic/main.c @@ -23,7 +23,7 @@ extern int add_req(uint8_t* public_key); // XXX char WINDOW_STATUS[MAX_WINDOW_SLOTS]; // Holds status of chat windows static ToxWindow windows[MAX_WINDOW_SLOTS]; int w_num; -static int w_active; +int w_active; static ToxWindow* prompt; // 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); - for(i=0; iwindow, "\n(message) %d: %s\n", friendnumber, string); - for(i=0; iwindow, "\n(nickchange) %d: %s!\n", friendnumber, string); - for(i=0; iwindow, "\n(statuschange) %d: %s!\n", friendnumber, string); - for(i=0; i= TOXWINDOWS_MAX_NUM) return -1; if(LINES < 2) return -1; w.window = newwin(LINES - 2, COLS, 0, 0); - if(w.window == NULL) return -1; - windows[w_num++] = w; + windows[n] = w; w.onInit(&w); - - return w_num - 1; + w_num++; + return n; } /* Deletes window w and cleans up */ @@ -157,24 +157,17 @@ void del_window(ToxWindow *w, int f_num) { refresh(); } -int focus_window(int num) { - if(num >= w_num || num < 0) - return -1; - - w_active = num; - return 0; -} - static void init_windows() { w_num = 0; - - if(add_window(new_prompt()) == -1 || add_window(new_friendlist()) == -1) { + int n_prompt = 0; + 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"); - endwin(); exit(1); } - prompt = &windows[0]; + prompt = &windows[n_prompt]; } static void do_tox() { diff --git a/testing/toxic/prompt.c b/testing/toxic/prompt.c index d40fb2a6..f973afd7 100644 --- a/testing/toxic/prompt.c +++ b/testing/toxic/prompt.c @@ -158,7 +158,9 @@ static void execute(ToxWindow* self, char* cmd) { break; } } - + else if (!strcmp(cmd, "clear")) { + wclear(self->window); + } else if(!strcmp(cmd, "help")) { print_usage(self); } @@ -254,9 +256,6 @@ static void execute(ToxWindow* self, char* cmd) { wprintw(self->window, "Message successfully sent.\n"); } } - else if (!strncmp(cmd, "clear", strlen("clear"))) { - wclear(self->window); - } else { wprintw(self->window, "Invalid command.\n"); } @@ -316,7 +315,7 @@ static void print_usage(ToxWindow* self) { wprintw(self->window, " nick : Set your nickname\n"); wprintw(self->window, " accept : Accept friend request\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, " help : Print this message again\n"); -- cgit v1.2.3 From 3f712d2650937c78735375f9695d73a8dbc95a91 Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Mon, 5 Aug 2013 18:07:08 -0400 Subject: bug fix and added wrapping on friends list --- testing/toxic/friendlist.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'testing/toxic/friendlist.c') diff --git a/testing/toxic/friendlist.c b/testing/toxic/friendlist.c index 647f563d..a1f9851e 100644 --- a/testing/toxic/friendlist.c +++ b/testing/toxic/friendlist.c @@ -58,11 +58,11 @@ void friendlist_onMessage(ToxWindow* self, int num, uint8_t* str, uint16_t len) 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); + add_window(new_chat(num), i); w_active = i; break; - } } + } } } @@ -101,8 +101,9 @@ int friendlist_onFriendAdded(int num) { static void friendlist_onKey(ToxWindow* self, int key) { if(key == KEY_UP) { - if(num_selected != 0) - num_selected--; + num_selected--; + if (num_selected < 0) + num_selected = num_friends-1; } else if(key == KEY_DOWN) { if(num_friends != 0) -- cgit v1.2.3