diff options
author | Jfreegman <Jfreegman@gmail.com> | 2013-08-05 16:04:06 -0400 |
---|---|---|
committer | Jfreegman <Jfreegman@gmail.com> | 2013-08-05 16:04:06 -0400 |
commit | 4b76983a692e0234b220b1c8fee87edb12673be2 (patch) | |
tree | fd517f2baa641179c534a745e23c8576f2bc57b5 | |
parent | a91aee0a9f4945ee3124e9e03f535e8ab4a40ce3 (diff) |
chat window deleting and re-adding - it just werks
-rw-r--r-- | testing/toxic/chat.c | 6 | ||||
-rw-r--r-- | testing/toxic/friendlist.c | 27 | ||||
-rw-r--r-- | testing/toxic/main.c | 41 | ||||
-rw-r--r-- | testing/toxic/prompt.c | 9 |
4 files changed, 43 insertions, 40 deletions
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 { | |||
25 | 25 | ||
26 | } ChatContext; | 26 | } ChatContext; |
27 | 27 | ||
28 | extern int w_active; | ||
28 | extern void del_window(ToxWindow *w, int f_num); | 29 | extern void del_window(ToxWindow *w, int f_num); |
29 | extern int focus_window(int num); | ||
30 | extern void fix_name(uint8_t* name); | 30 | extern void fix_name(uint8_t* name); |
31 | void print_help(ChatContext* self); | 31 | void print_help(ChatContext* self); |
32 | void execute(ToxWindow* self, ChatContext* ctx, char* cmd); | 32 | void execute(ToxWindow* self, ChatContext* ctx, char* cmd); |
@@ -188,7 +188,7 @@ void execute(ToxWindow* self, ChatContext* ctx, char* cmd) | |||
188 | wprintw(ctx->history, "Your ID: %s\n", id); | 188 | wprintw(ctx->history, "Your ID: %s\n", id); |
189 | } | 189 | } |
190 | else if (strcmp(ctx->line, "/close") == 0) { | 190 | else if (strcmp(ctx->line, "/close") == 0) { |
191 | focus_window(0); // Go to prompt screen | 191 | w_active = 0; // Go to prompt screen |
192 | int f_num = ctx->friendnum; | 192 | int f_num = ctx->friendnum; |
193 | delwin(ctx->linewin); | 193 | delwin(ctx->linewin); |
194 | del_window(self, f_num); | 194 | del_window(self, f_num); |
@@ -236,7 +236,7 @@ void print_help(ChatContext* self) { | |||
236 | wprintw(self->history, " /nick <nickname> : Set your nickname\n"); | 236 | wprintw(self->history, " /nick <nickname> : Set your nickname\n"); |
237 | wprintw(self->history, " /myid : Print your ID\n"); | 237 | wprintw(self->history, " /myid : Print your ID\n"); |
238 | wprintw(self->history, " /clear : Clear the screen\n"); | 238 | wprintw(self->history, " /clear : Clear the screen\n"); |
239 | wprintw(self->history, " /close : Closes the current chat window\n"); | 239 | wprintw(self->history, " /close : Close the current chat window\n"); |
240 | wprintw(self->history, " /quit or /exit : Exit program\n"); | 240 | wprintw(self->history, " /quit or /exit : Exit program\n"); |
241 | wprintw(self->history, " /help : Print this message again\n\n"); | 241 | wprintw(self->history, " /help : Print this message again\n\n"); |
242 | 242 | ||
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 @@ | |||
13 | #include "windows.h" | 13 | #include "windows.h" |
14 | 14 | ||
15 | extern char WINDOW_STATUS[TOXWINDOWS_MAX_NUM]; | 15 | extern char WINDOW_STATUS[TOXWINDOWS_MAX_NUM]; |
16 | extern int add_window(ToxWindow w); | 16 | extern int add_window(ToxWindow w, int n); |
17 | extern int focus_window(int num); | ||
18 | extern ToxWindow new_chat(int friendnum); | 17 | extern ToxWindow new_chat(int friendnum); |
18 | extern int w_active; | ||
19 | 19 | ||
20 | typedef struct { | 20 | typedef struct { |
21 | uint8_t name[MAX_NAME_LENGTH]; | 21 | uint8_t name[MAX_NAME_LENGTH]; |
@@ -52,7 +52,17 @@ void friendlist_onMessage(ToxWindow* self, int num, uint8_t* str, uint16_t len) | |||
52 | return; | 52 | return; |
53 | 53 | ||
54 | if(friends[num].chatwin == -1) { | 54 | if(friends[num].chatwin == -1) { |
55 | friends[num].chatwin = add_window(new_chat(num)); | 55 | friends[num].chatwin = num; |
56 | int i; | ||
57 | /* Find first open slot to hold chat window */ | ||
58 | for (i = N_DEFAULT_WINS; i < MAX_WINDOW_SLOTS; i++) { | ||
59 | if (WINDOW_STATUS[i] == -1) { | ||
60 | WINDOW_STATUS[i] = num; | ||
61 | add_window(new_chat(num_selected), i); | ||
62 | w_active = i; | ||
63 | break; | ||
64 | } | ||
65 | } | ||
56 | } | 66 | } |
57 | } | 67 | } |
58 | 68 | ||
@@ -104,17 +114,18 @@ static void friendlist_onKey(ToxWindow* self, int key) { | |||
104 | int i; | 114 | int i; |
105 | for (i = N_DEFAULT_WINS; i < MAX_WINDOW_SLOTS; i++) { | 115 | for (i = N_DEFAULT_WINS; i < MAX_WINDOW_SLOTS; i++) { |
106 | if (WINDOW_STATUS[i] == num_selected) { | 116 | if (WINDOW_STATUS[i] == num_selected) { |
107 | focus_window(i); | 117 | w_active = i; |
108 | break; | 118 | break; |
109 | } | 119 | } |
110 | } | 120 | } |
111 | }else { | 121 | }else { |
112 | friends[num_selected].chatwin = add_window(new_chat(num_selected)); | ||
113 | focus_window(friends[num_selected].chatwin); | ||
114 | int i; | 122 | int i; |
115 | for (i = N_DEFAULT_WINS; i < MAX_WINDOW_SLOTS; i++) { // Find open slot | 123 | for (i = N_DEFAULT_WINS; i < MAX_WINDOW_SLOTS; i++) { |
116 | if (WINDOW_STATUS[i] == -1) { | 124 | if (WINDOW_STATUS[i] == -1) { |
117 | WINDOW_STATUS[i] = num_selected; | 125 | WINDOW_STATUS[i] = num_selected; |
126 | friends[num_selected].chatwin = num_selected; | ||
127 | add_window(new_chat(num_selected), i); | ||
128 | w_active = i; | ||
118 | break; | 129 | break; |
119 | } | 130 | } |
120 | } | 131 | } |
@@ -178,4 +189,4 @@ ToxWindow new_friendlist() { | |||
178 | strcpy(ret.title, "[friends]"); | 189 | strcpy(ret.title, "[friends]"); |
179 | 190 | ||
180 | return ret; | 191 | return ret; |
181 | } \ No newline at end of file | 192 | } |
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 | |||
23 | char WINDOW_STATUS[MAX_WINDOW_SLOTS]; // Holds status of chat windows | 23 | char WINDOW_STATUS[MAX_WINDOW_SLOTS]; // Holds status of chat windows |
24 | static ToxWindow windows[MAX_WINDOW_SLOTS]; | 24 | static ToxWindow windows[MAX_WINDOW_SLOTS]; |
25 | int w_num; | 25 | int w_num; |
26 | static int w_active; | 26 | int w_active; |
27 | static ToxWindow* prompt; | 27 | static ToxWindow* prompt; |
28 | 28 | ||
29 | // CALLBACKS START | 29 | // CALLBACKS START |
@@ -40,7 +40,7 @@ void on_request(uint8_t* public_key, uint8_t* data, uint16_t length) { | |||
40 | 40 | ||
41 | wprintw(prompt->window, "Use \"accept %d\" to accept it.\n", n); | 41 | wprintw(prompt->window, "Use \"accept %d\" to accept it.\n", n); |
42 | 42 | ||
43 | for(i=0; i<w_num; i++) { | 43 | for(i=0; i<MAX_WINDOW_SLOTS; i++) { |
44 | if(windows[i].onFriendRequest != NULL) | 44 | if(windows[i].onFriendRequest != NULL) |
45 | windows[i].onFriendRequest(&windows[i], public_key, data, length); | 45 | windows[i].onFriendRequest(&windows[i], public_key, data, length); |
46 | } | 46 | } |
@@ -51,7 +51,7 @@ void on_message(int friendnumber, uint8_t* string, uint16_t length) { | |||
51 | 51 | ||
52 | wprintw(prompt->window, "\n(message) %d: %s\n", friendnumber, string); | 52 | wprintw(prompt->window, "\n(message) %d: %s\n", friendnumber, string); |
53 | 53 | ||
54 | for(i=0; i<w_num; i++) { | 54 | for(i=0; i<MAX_WINDOW_SLOTS; i++) { |
55 | if(windows[i].onMessage != NULL) | 55 | if(windows[i].onMessage != NULL) |
56 | windows[i].onMessage(&windows[i], friendnumber, string, length); | 56 | windows[i].onMessage(&windows[i], friendnumber, string, length); |
57 | } | 57 | } |
@@ -62,7 +62,7 @@ void on_nickchange(int friendnumber, uint8_t* string, uint16_t length) { | |||
62 | 62 | ||
63 | wprintw(prompt->window, "\n(nickchange) %d: %s!\n", friendnumber, string); | 63 | wprintw(prompt->window, "\n(nickchange) %d: %s!\n", friendnumber, string); |
64 | 64 | ||
65 | for(i=0; i<w_num; i++) { | 65 | for(i=0; i<MAX_WINDOW_SLOTS; i++) { |
66 | if(windows[i].onNickChange != NULL) | 66 | if(windows[i].onNickChange != NULL) |
67 | windows[i].onNickChange(&windows[i], friendnumber, string, length); | 67 | windows[i].onNickChange(&windows[i], friendnumber, string, length); |
68 | } | 68 | } |
@@ -73,7 +73,7 @@ void on_statuschange(int friendnumber, uint8_t* string, uint16_t length) { | |||
73 | 73 | ||
74 | wprintw(prompt->window, "\n(statuschange) %d: %s!\n", friendnumber, string); | 74 | wprintw(prompt->window, "\n(statuschange) %d: %s!\n", friendnumber, string); |
75 | 75 | ||
76 | for(i=0; i<w_num; i++) { | 76 | for(i=0; i<MAX_WINDOW_SLOTS; i++) { |
77 | if(windows[i].onStatusChange != NULL) | 77 | if(windows[i].onStatusChange != NULL) |
78 | windows[i].onStatusChange(&windows[i], friendnumber, string, length); | 78 | windows[i].onStatusChange(&windows[i], friendnumber, string, length); |
79 | } | 79 | } |
@@ -115,31 +115,31 @@ static void init_tox() { | |||
115 | } | 115 | } |
116 | 116 | ||
117 | void init_window_status() { | 117 | void init_window_status() { |
118 | /* Default window values decrement from -2 */ | ||
118 | int i; | 119 | int i; |
119 | for (i = 0; i < N_DEFAULT_WINS; i++) | 120 | for (i = 0; i < N_DEFAULT_WINS; i++) |
120 | WINDOW_STATUS[i] = i; | 121 | WINDOW_STATUS[i] = -(i+2); |
121 | 122 | ||
122 | int j; | 123 | int j; |
123 | for (j = N_DEFAULT_WINS; j < MAX_WINDOW_SLOTS; j++) | 124 | for (j = N_DEFAULT_WINS; j < MAX_WINDOW_SLOTS; j++) |
124 | WINDOW_STATUS[j] = -1; | 125 | WINDOW_STATUS[j] = -1; |
125 | } | 126 | } |
126 | 127 | ||
127 | int add_window(ToxWindow w) { | 128 | int add_window(ToxWindow w, int n) { |
128 | if(w_num == TOXWINDOWS_MAX_NUM) | 129 | if(w_num >= TOXWINDOWS_MAX_NUM) |
129 | return -1; | 130 | return -1; |
130 | 131 | ||
131 | if(LINES < 2) | 132 | if(LINES < 2) |
132 | return -1; | 133 | return -1; |
133 | 134 | ||
134 | w.window = newwin(LINES - 2, COLS, 0, 0); | 135 | w.window = newwin(LINES - 2, COLS, 0, 0); |
135 | |||
136 | if(w.window == NULL) | 136 | if(w.window == NULL) |
137 | return -1; | 137 | return -1; |
138 | 138 | ||
139 | windows[w_num++] = w; | 139 | windows[n] = w; |
140 | w.onInit(&w); | 140 | w.onInit(&w); |
141 | 141 | w_num++; | |
142 | return w_num - 1; | 142 | return n; |
143 | } | 143 | } |
144 | 144 | ||
145 | /* Deletes window w and cleans up */ | 145 | /* Deletes window w and cleans up */ |
@@ -157,24 +157,17 @@ void del_window(ToxWindow *w, int f_num) { | |||
157 | refresh(); | 157 | refresh(); |
158 | } | 158 | } |
159 | 159 | ||
160 | int focus_window(int num) { | ||
161 | if(num >= w_num || num < 0) | ||
162 | return -1; | ||
163 | |||
164 | w_active = num; | ||
165 | return 0; | ||
166 | } | ||
167 | |||
168 | static void init_windows() { | 160 | static void init_windows() { |
169 | w_num = 0; | 161 | w_num = 0; |
170 | 162 | int n_prompt = 0; | |
171 | if(add_window(new_prompt()) == -1 || add_window(new_friendlist()) == -1) { | 163 | int n_friendslist = 1; |
164 | if(add_window(new_prompt(), n_prompt) == -1 | ||
165 | || add_window(new_friendlist(), n_friendslist) == -1) { | ||
172 | fprintf(stderr, "add_window() failed.\n"); | 166 | fprintf(stderr, "add_window() failed.\n"); |
173 | |||
174 | endwin(); | 167 | endwin(); |
175 | exit(1); | 168 | exit(1); |
176 | } | 169 | } |
177 | prompt = &windows[0]; | 170 | prompt = &windows[n_prompt]; |
178 | } | 171 | } |
179 | 172 | ||
180 | static void do_tox() { | 173 | 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) { | |||
158 | break; | 158 | break; |
159 | } | 159 | } |
160 | } | 160 | } |
161 | 161 | else if (!strcmp(cmd, "clear")) { | |
162 | wclear(self->window); | ||
163 | } | ||
162 | else if(!strcmp(cmd, "help")) { | 164 | else if(!strcmp(cmd, "help")) { |
163 | print_usage(self); | 165 | print_usage(self); |
164 | } | 166 | } |
@@ -254,9 +256,6 @@ static void execute(ToxWindow* self, char* cmd) { | |||
254 | wprintw(self->window, "Message successfully sent.\n"); | 256 | wprintw(self->window, "Message successfully sent.\n"); |
255 | } | 257 | } |
256 | } | 258 | } |
257 | else if (!strncmp(cmd, "clear", strlen("clear"))) { | ||
258 | wclear(self->window); | ||
259 | } | ||
260 | else { | 259 | else { |
261 | wprintw(self->window, "Invalid command.\n"); | 260 | wprintw(self->window, "Invalid command.\n"); |
262 | } | 261 | } |
@@ -316,7 +315,7 @@ static void print_usage(ToxWindow* self) { | |||
316 | wprintw(self->window, " nick <nickname> : Set your nickname\n"); | 315 | wprintw(self->window, " nick <nickname> : Set your nickname\n"); |
317 | wprintw(self->window, " accept <number> : Accept friend request\n"); | 316 | wprintw(self->window, " accept <number> : Accept friend request\n"); |
318 | wprintw(self->window, " myid : Print your ID\n"); | 317 | wprintw(self->window, " myid : Print your ID\n"); |
319 | wprintw(self->window, " clear : Clear the screen\n"); | 318 | wprintw(self->window, " clear : Clear this window\n"); |
320 | wprintw(self->window, " quit/exit : Exit program\n"); | 319 | wprintw(self->window, " quit/exit : Exit program\n"); |
321 | wprintw(self->window, " help : Print this message again\n"); | 320 | wprintw(self->window, " help : Print this message again\n"); |
322 | 321 | ||