summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--testing/toxic/chat.c10
-rw-r--r--testing/toxic/chat.h7
-rw-r--r--testing/toxic/friendlist.c25
-rw-r--r--testing/toxic/friendlist.h14
-rw-r--r--testing/toxic/main.c84
-rw-r--r--testing/toxic/prompt.c6
-rw-r--r--testing/toxic/prompt.h14
7 files changed, 98 insertions, 62 deletions
diff --git a/testing/toxic/chat.c b/testing/toxic/chat.c
index 1b5e743d..6811569b 100644
--- a/testing/toxic/chat.c
+++ b/testing/toxic/chat.c
@@ -13,6 +13,8 @@
13#include "../../core/network.h" 13#include "../../core/network.h"
14 14
15#include "windows.h" 15#include "windows.h"
16#include "friendlist.h"
17#include "chat.h"
16 18
17#define CURS_Y_OFFSET 3 19#define CURS_Y_OFFSET 3
18 20
@@ -24,10 +26,8 @@ typedef struct {
24 WINDOW* linewin; 26 WINDOW* linewin;
25} ChatContext; 27} ChatContext;
26 28
27extern int active_window; 29static delWindowFn *del_window;
28 30
29extern void del_window(ToxWindow *w, int f_num);
30extern void fix_name(uint8_t *name);
31void print_help(ChatContext *self); 31void print_help(ChatContext *self);
32void execute(ToxWindow *self, ChatContext *ctx, Messenger *m, char *cmd); 32void execute(ToxWindow *self, ChatContext *ctx, Messenger *m, char *cmd);
33 33
@@ -320,7 +320,6 @@ void execute(ToxWindow *self, ChatContext *ctx, Messenger *m, char *cmd)
320 } 320 }
321 321
322 else if (strcmp(ctx->line, "/close") == 0) { 322 else if (strcmp(ctx->line, "/close") == 0) {
323 active_window = 0; // Go to prompt screen
324 int f_num = ctx->friendnum; 323 int f_num = ctx->friendnum;
325 delwin(ctx->linewin); 324 delwin(ctx->linewin);
326 del_window(self, f_num); 325 del_window(self, f_num);
@@ -371,8 +370,9 @@ void print_help(ChatContext *self)
371 wattroff(self->history, COLOR_PAIR(2)); 370 wattroff(self->history, COLOR_PAIR(2));
372} 371}
373 372
374ToxWindow new_chat(Messenger *m, int friendnum) 373ToxWindow new_chat(Messenger *m, int friendnum, delWindowFn *f)
375{ 374{
375 del_window = f;
376 ToxWindow ret; 376 ToxWindow ret;
377 memset(&ret, 0, sizeof(ret)); 377 memset(&ret, 0, sizeof(ret));
378 378
diff --git a/testing/toxic/chat.h b/testing/toxic/chat.h
new file mode 100644
index 00000000..dcd5b3ba
--- /dev/null
+++ b/testing/toxic/chat.h
@@ -0,0 +1,7 @@
1#ifndef CHAT_H_6489PZ13
2#define CHAT_H_6489PZ13
3
4typedef void (delWindowFn)(ToxWindow *w, int f_num);
5ToxWindow new_chat(Messenger *m, int friendnum, delWindowFn f);
6
7#endif /* end of include guard: CHAT_H_6489PZ13 */
diff --git a/testing/toxic/friendlist.c b/testing/toxic/friendlist.c
index 1a77440e..5b79fdef 100644
--- a/testing/toxic/friendlist.c
+++ b/testing/toxic/friendlist.c
@@ -10,13 +10,12 @@
10#include "../../core/Messenger.h" 10#include "../../core/Messenger.h"
11#include "../../core/network.h" 11#include "../../core/network.h"
12 12
13#include "windows.h" 13#include "friendlist.h"
14 14
15extern char WINDOW_STATUS[TOXWINDOWS_MAX_NUM]; 15static delWindowFn *del_window;
16extern int add_window(Messenger *m, ToxWindow w, int n); 16static setActiveWindowFn *set_active_window;
17extern ToxWindow new_chat(Messenger *m, int friendnum); 17static addWindowFn *add_window;
18 18static char * WINDOW_STATUS;
19extern int active_window;
20 19
21typedef struct { 20typedef struct {
22 uint8_t name[MAX_NAME_LENGTH]; 21 uint8_t name[MAX_NAME_LENGTH];
@@ -54,8 +53,7 @@ void friendlist_onMessage(ToxWindow *self, Messenger *m, int num, uint8_t *str,
54 for (i = N_DEFAULT_WINS; i < MAX_WINDOW_SLOTS; ++i) { 53 for (i = N_DEFAULT_WINS; i < MAX_WINDOW_SLOTS; ++i) {
55 if (WINDOW_STATUS[i] == -1) { 54 if (WINDOW_STATUS[i] == -1) {
56 WINDOW_STATUS[i] = num; 55 WINDOW_STATUS[i] = num;
57 add_window(m, new_chat(m, num), i); 56 add_window(m, new_chat(m, num, del_window), i);
58 active_window = i;
59 break; 57 break;
60 } 58 }
61 } 59 }
@@ -111,7 +109,7 @@ static void friendlist_onKey(ToxWindow *self, Messenger *m, int key)
111 int i; 109 int i;
112 for (i = N_DEFAULT_WINS; i < MAX_WINDOW_SLOTS; ++i) { 110 for (i = N_DEFAULT_WINS; i < MAX_WINDOW_SLOTS; ++i) {
113 if (WINDOW_STATUS[i] == num_selected) { 111 if (WINDOW_STATUS[i] == num_selected) {
114 active_window = i; 112 set_active_window(i);
115 break; 113 break;
116 } 114 }
117 } 115 }
@@ -121,8 +119,7 @@ static void friendlist_onKey(ToxWindow *self, Messenger *m, int key)
121 if (WINDOW_STATUS[i] == -1) { 119 if (WINDOW_STATUS[i] == -1) {
122 WINDOW_STATUS[i] = num_selected; 120 WINDOW_STATUS[i] = num_selected;
123 friends[num_selected].chatwin = num_selected; 121 friends[num_selected].chatwin = num_selected;
124 add_window(m, new_chat(m, num_selected), i); 122 add_window(m, new_chat(m, num_selected, del_window), i);
125 active_window = i;
126 break; 123 break;
127 } 124 }
128 } 125 }
@@ -169,7 +166,11 @@ static void friendlist_onInit(ToxWindow *self, Messenger *m)
169 166
170} 167}
171 168
172ToxWindow new_friendlist() { 169ToxWindow new_friendlist(delWindowFn dw, setActiveWindowFn saw, addWindowFn aw, char * ws) {
170 del_window = dw;
171 set_active_window = saw;
172 add_window = aw;
173 WINDOW_STATUS = ws;
173 ToxWindow ret; 174 ToxWindow ret;
174 memset(&ret, 0, sizeof(ret)); 175 memset(&ret, 0, sizeof(ret));
175 176
diff --git a/testing/toxic/friendlist.h b/testing/toxic/friendlist.h
new file mode 100644
index 00000000..e8b93aef
--- /dev/null
+++ b/testing/toxic/friendlist.h
@@ -0,0 +1,14 @@
1#ifndef FRIENDLIST_H_53I41IM
2#define FRIENDLIST_H_53I41IM
3
4
5#include "windows.h"
6#include "chat.h"
7typedef void (setActiveWindowFn)(int ch);
8typedef int (addWindowFn)(Messenger *m, ToxWindow w, int n);
9ToxWindow new_friendlist(delWindowFn dw, setActiveWindowFn saw, addWindowFn aw, char * ws);
10int friendlist_onFriendAdded(Messenger *m, int num);
11void disable_chatwin(int f_num);
12void fix_name(uint8_t *name);
13
14#endif /* end of include guard: FRIENDLIST_H_53I41IM */
diff --git a/testing/toxic/main.c b/testing/toxic/main.c
index 8ab2d393..a109311e 100644
--- a/testing/toxic/main.c
+++ b/testing/toxic/main.c
@@ -21,14 +21,9 @@
21 21
22#include "configdir.h" 22#include "configdir.h"
23#include "windows.h" 23#include "windows.h"
24#include "prompt.h"
25#include "friendlist.h"
24 26
25extern ToxWindow new_prompt();
26extern ToxWindow new_friendlist();
27
28extern int friendlist_onFriendAdded(Messenger *m, int num);
29extern void disable_chatwin(int f_num);
30extern int add_req(uint8_t *public_key); // XXX
31extern unsigned char *hex_string_to_bin(char hex_string[]);
32 27
33/* Holds status of chat windows */ 28/* Holds status of chat windows */
34char WINDOW_STATUS[MAX_WINDOW_SLOTS]; 29char WINDOW_STATUS[MAX_WINDOW_SLOTS];
@@ -221,12 +216,14 @@ int add_window(Messenger *m, ToxWindow w, int n)
221 windows[n] = w; 216 windows[n] = w;
222 w.onInit(&w, m); 217 w.onInit(&w, m);
223 w_num++; 218 w_num++;
219 active_window = n;
224 return n; 220 return n;
225} 221}
226 222
227/* Deletes window w and cleans up */ 223/* Deletes window w and cleans up */
228void del_window(ToxWindow *w, int f_num) 224void del_window(ToxWindow *w, int f_num)
229{ 225{
226 active_window = 0; // Go to prompt screen
230 delwin(w->window); 227 delwin(w->window);
231 int i; 228 int i;
232 for (i = N_DEFAULT_WINS; i < MAX_WINDOW_SLOTS; ++i) { 229 for (i = N_DEFAULT_WINS; i < MAX_WINDOW_SLOTS; ++i) {
@@ -240,13 +237,48 @@ void del_window(ToxWindow *w, int f_num)
240 refresh(); 237 refresh();
241} 238}
242 239
240/* Shows next window when tab or back-tab is pressed */
241void set_active_window(int ch)
242{
243 int f_inf = 0;
244 int max = MAX_WINDOW_SLOTS-1;
245 if (ch == '\t') {
246 int i = (active_window + 1) % max;
247 while (true) {
248 if (WINDOW_STATUS[i] != -1) {
249 active_window = i;
250 return;
251 }
252 i = (i + 1) % max;
253 if (f_inf++ > max) { // infinite loop check
254 endwin();
255 exit(2);
256 }
257 }
258 }else {
259 int i = active_window - 1;
260 if (i < 0) i = max;
261 while (true) {
262 if (WINDOW_STATUS[i] != -1) {
263 active_window = i;
264 return;
265 }
266 if (--i < 0) i = max;
267 if (f_inf++ > max) {
268 endwin();
269 exit(2);
270 }
271 }
272 }
273}
274
243static void init_windows(Messenger *m) 275static void init_windows(Messenger *m)
244{ 276{
245 w_num = 0; 277 w_num = 0;
246 int n_prompt = 0; 278 int n_prompt = 0;
247 int n_friendslist = 1; 279 int n_friendslist = 1;
248 if (add_window(m, new_prompt(), n_prompt) == -1 280 if (add_window(m, new_prompt(on_friendadded), n_prompt) == -1
249 || add_window(m, new_friendlist(), n_friendslist) == -1) { 281 || add_window(m, new_friendlist(del_window, set_active_window, add_window, WINDOW_STATUS), n_friendslist) == -1) {
250 fprintf(stderr, "add_window() failed.\n"); 282 fprintf(stderr, "add_window() failed.\n");
251 endwin(); 283 endwin();
252 exit(1); 284 exit(1);
@@ -378,40 +410,6 @@ void prepare_window(WINDOW *w)
378 wresize(w, LINES-2, COLS); 410 wresize(w, LINES-2, COLS);
379} 411}
380 412
381/* Shows next window when tab or back-tab is pressed */
382void set_active_window(int ch)
383{
384 int f_inf = 0;
385 int max = MAX_WINDOW_SLOTS-1;
386 if (ch == '\t') {
387 int i = (active_window + 1) % max;
388 while (true) {
389 if (WINDOW_STATUS[i] != -1) {
390 active_window = i;
391 return;
392 }
393 i = (i + 1) % max;
394 if (f_inf++ > max) { // infinite loop check
395 endwin();
396 exit(2);
397 }
398 }
399 }else {
400 int i = active_window - 1;
401 if (i < 0) i = max;
402 while (true) {
403 if (WINDOW_STATUS[i] != -1) {
404 active_window = i;
405 return;
406 }
407 if (--i < 0) i = max;
408 if (f_inf++ > max) {
409 endwin();
410 exit(2);
411 }
412 }
413 }
414}
415 413
416int main(int argc, char *argv[]) 414int main(int argc, char *argv[])
417{ 415{
diff --git a/testing/toxic/prompt.c b/testing/toxic/prompt.c
index c01ed710..bc4b8545 100644
--- a/testing/toxic/prompt.c
+++ b/testing/toxic/prompt.c
@@ -11,11 +11,12 @@
11#include "../../core/network.h" 11#include "../../core/network.h"
12 12
13#include "windows.h" 13#include "windows.h"
14#include "prompt.h"
14 15
15uint8_t pending_requests[MAX_STR_SIZE][CLIENT_ID_SIZE]; // XXX 16uint8_t pending_requests[MAX_STR_SIZE][CLIENT_ID_SIZE]; // XXX
16uint8_t num_requests=0; // XXX 17uint8_t num_requests=0; // XXX
17 18
18extern void on_friendadded(Messenger *m, int friendnumber); 19static friendAddedFn *on_friendadded;
19static char prompt_buf[MAX_STR_SIZE] = {0}; 20static char prompt_buf[MAX_STR_SIZE] = {0};
20static int prompt_buf_pos = 0; 21static int prompt_buf_pos = 0;
21 22
@@ -428,8 +429,9 @@ static void prompt_onInit(ToxWindow *self, Messenger *m)
428 wclrtoeol(self->window); 429 wclrtoeol(self->window);
429} 430}
430 431
431ToxWindow new_prompt() 432ToxWindow new_prompt(friendAddedFn *f)
432{ 433{
434 on_friendadded = f;
433 ToxWindow ret; 435 ToxWindow ret;
434 memset(&ret, 0, sizeof(ret)); 436 memset(&ret, 0, sizeof(ret));
435 ret.onKey = &prompt_onKey; 437 ret.onKey = &prompt_onKey;
diff --git a/testing/toxic/prompt.h b/testing/toxic/prompt.h
new file mode 100644
index 00000000..6c5320ab
--- /dev/null
+++ b/testing/toxic/prompt.h
@@ -0,0 +1,14 @@
1#ifndef PROMPT_H_UZYGWFFL
2#define PROMPT_H_UZYGWFFL
3
4#include "windows.h"
5
6typedef void (friendAddedFn)(Messenger *m, int friendnumber);
7
8ToxWindow new_prompt(friendAddedFn *f);
9int add_req(uint8_t *public_key);
10unsigned char *hex_string_to_bin(char hex_string[]);
11
12#endif /* end of include guard: PROMPT_H_UZYGWFFL */
13
14