summaryrefslogtreecommitdiff
path: root/testing/toxic
diff options
context:
space:
mode:
Diffstat (limited to 'testing/toxic')
-rw-r--r--testing/toxic/CMakeLists.txt1
-rw-r--r--testing/toxic/chat.c7
-rw-r--r--testing/toxic/chat.h6
-rw-r--r--testing/toxic/friendlist.c18
-rw-r--r--testing/toxic/friendlist.h12
-rw-r--r--testing/toxic/main.c255
-rw-r--r--testing/toxic/prompt.c10
-rw-r--r--testing/toxic/prompt.h14
-rw-r--r--testing/toxic/windows.c235
-rw-r--r--testing/toxic/windows.h19
10 files changed, 314 insertions, 263 deletions
diff --git a/testing/toxic/CMakeLists.txt b/testing/toxic/CMakeLists.txt
index 13b8692d..dd811b97 100644
--- a/testing/toxic/CMakeLists.txt
+++ b/testing/toxic/CMakeLists.txt
@@ -8,6 +8,7 @@ set(exe_name toxic)
8 8
9add_executable(${exe_name} 9add_executable(${exe_name}
10 main.c 10 main.c
11 windows.c
11 prompt.c 12 prompt.c
12 friendlist.c 13 friendlist.c
13 chat.c 14 chat.c
diff --git a/testing/toxic/chat.c b/testing/toxic/chat.c
index 1b5e743d..e4da9484 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,6 @@ typedef struct {
24 WINDOW* linewin; 26 WINDOW* linewin;
25} ChatContext; 27} ChatContext;
26 28
27extern int active_window;
28
29extern void del_window(ToxWindow *w, int f_num);
30extern void fix_name(uint8_t *name);
31void print_help(ChatContext *self); 29void print_help(ChatContext *self);
32void execute(ToxWindow *self, ChatContext *ctx, Messenger *m, char *cmd); 30void execute(ToxWindow *self, ChatContext *ctx, Messenger *m, char *cmd);
33 31
@@ -320,7 +318,6 @@ void execute(ToxWindow *self, ChatContext *ctx, Messenger *m, char *cmd)
320 } 318 }
321 319
322 else if (strcmp(ctx->line, "/close") == 0) { 320 else if (strcmp(ctx->line, "/close") == 0) {
323 active_window = 0; // Go to prompt screen
324 int f_num = ctx->friendnum; 321 int f_num = ctx->friendnum;
325 delwin(ctx->linewin); 322 delwin(ctx->linewin);
326 del_window(self, f_num); 323 del_window(self, f_num);
diff --git a/testing/toxic/chat.h b/testing/toxic/chat.h
new file mode 100644
index 00000000..7599d462
--- /dev/null
+++ b/testing/toxic/chat.h
@@ -0,0 +1,6 @@
1#ifndef CHAT_H_6489PZ13
2#define CHAT_H_6489PZ13
3
4ToxWindow new_chat(Messenger *m, int friendnum);
5
6#endif /* end of include guard: CHAT_H_6489PZ13 */
diff --git a/testing/toxic/friendlist.c b/testing/toxic/friendlist.c
index 56061cf9..e7504fbf 100644
--- a/testing/toxic/friendlist.c
+++ b/testing/toxic/friendlist.c
@@ -11,12 +11,9 @@
11#include "../../core/network.h" 11#include "../../core/network.h"
12 12
13#include "windows.h" 13#include "windows.h"
14#include "friendlist.h"
14 15
15extern char WINDOW_STATUS[TOXWINDOWS_MAX_NUM]; 16static char * WINDOW_STATUS;
16extern int add_window(ToxWindow w, int n);
17extern ToxWindow new_chat(Messenger *m, int friendnum);
18
19extern int active_window;
20 17
21typedef struct { 18typedef struct {
22 uint8_t name[MAX_NAME_LENGTH]; 19 uint8_t name[MAX_NAME_LENGTH];
@@ -54,8 +51,7 @@ void friendlist_onMessage(ToxWindow *self, Messenger *m, int num, uint8_t *str,
54 for (i = N_DEFAULT_WINS; i < MAX_WINDOW_SLOTS; ++i) { 51 for (i = N_DEFAULT_WINS; i < MAX_WINDOW_SLOTS; ++i) {
55 if (WINDOW_STATUS[i] == -1) { 52 if (WINDOW_STATUS[i] == -1) {
56 WINDOW_STATUS[i] = num; 53 WINDOW_STATUS[i] = num;
57 add_window(new_chat(m, num), i); 54 add_window(m, new_chat(m, num), i);
58 active_window = i;
59 break; 55 break;
60 } 56 }
61 } 57 }
@@ -111,7 +107,7 @@ static void friendlist_onKey(ToxWindow *self, Messenger *m, int key)
111 int i; 107 int i;
112 for (i = N_DEFAULT_WINS; i < MAX_WINDOW_SLOTS; ++i) { 108 for (i = N_DEFAULT_WINS; i < MAX_WINDOW_SLOTS; ++i) {
113 if (WINDOW_STATUS[i] == num_selected) { 109 if (WINDOW_STATUS[i] == num_selected) {
114 active_window = i; 110 set_active_window(i);
115 break; 111 break;
116 } 112 }
117 } 113 }
@@ -121,8 +117,7 @@ static void friendlist_onKey(ToxWindow *self, Messenger *m, int key)
121 if (WINDOW_STATUS[i] == -1) { 117 if (WINDOW_STATUS[i] == -1) {
122 WINDOW_STATUS[i] = num_selected; 118 WINDOW_STATUS[i] = num_selected;
123 friends[num_selected].chatwin = num_selected; 119 friends[num_selected].chatwin = num_selected;
124 add_window(new_chat(m, num_selected), i); 120 add_window(m, new_chat(m, num_selected), i);
125 active_window = i;
126 break; 121 break;
127 } 122 }
128 } 123 }
@@ -169,7 +164,8 @@ static void friendlist_onInit(ToxWindow *self, Messenger *m)
169 164
170} 165}
171 166
172ToxWindow new_friendlist() { 167ToxWindow new_friendlist(char * ws) {
168 WINDOW_STATUS = ws;
173 ToxWindow ret; 169 ToxWindow ret;
174 memset(&ret, 0, sizeof(ret)); 170 memset(&ret, 0, sizeof(ret));
175 171
diff --git a/testing/toxic/friendlist.h b/testing/toxic/friendlist.h
new file mode 100644
index 00000000..c3d8dac7
--- /dev/null
+++ b/testing/toxic/friendlist.h
@@ -0,0 +1,12 @@
1#ifndef FRIENDLIST_H_53I41IM
2#define FRIENDLIST_H_53I41IM
3
4#include "windows.h"
5#include "chat.h"
6
7ToxWindow new_friendlist(char * ws);
8int friendlist_onFriendAdded(Messenger *m, int num);
9void disable_chatwin(int f_num);
10void fix_name(uint8_t *name);
11
12#endif /* end of include guard: FRIENDLIST_H_53I41IM */
diff --git a/testing/toxic/main.c b/testing/toxic/main.c
index 40ff5d09..d7b251dc 100644
--- a/testing/toxic/main.c
+++ b/testing/toxic/main.c
@@ -21,94 +21,13 @@
21 21
22#include "configdir.h" 22#include "configdir.h"
23#include "windows.h" 23#include "windows.h"
24 24#include "prompt.h"
25extern ToxWindow new_prompt(); 25#include "friendlist.h"
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
33/* Holds status of chat windows */
34char WINDOW_STATUS[MAX_WINDOW_SLOTS];
35 26
36#ifndef TOXICVER 27#ifndef TOXICVER
37#define TOXICVER "NOVER" //Use the -D flag to set this 28#define TOXICVER "NOVER" //Use the -D flag to set this
38#endif 29#endif
39 30
40static ToxWindow windows[MAX_WINDOW_SLOTS];
41static ToxWindow* prompt;
42
43static Messenger *m;
44
45int w_num;
46int active_window;
47
48/* CALLBACKS START */
49void on_request(uint8_t *public_key, uint8_t *data, uint16_t length, void* userdata)
50{
51 int n = add_req(public_key);
52 wprintw(prompt->window, "\nFriend request from:\n");
53
54 int i;
55 for (i = 0; i < KEY_SIZE_BYTES; ++i) {
56 wprintw(prompt->window, "%02x", public_key[i] & 0xff);
57 }
58
59 wprintw(prompt->window, "\nWith the message: %s\n", data);
60 wprintw(prompt->window, "\nUse \"accept %d\" to accept it.\n", n);
61
62 for (i = 0; i < MAX_WINDOW_SLOTS; ++i) {
63 if (windows[i].onFriendRequest != NULL)
64 windows[i].onFriendRequest(&windows[i], public_key, data, length);
65 }
66}
67
68void on_message(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void* userdata)
69{
70 int i;
71 for (i = 0; i < MAX_WINDOW_SLOTS; ++i) {
72 if (windows[i].onMessage != NULL)
73 windows[i].onMessage(&windows[i], m, friendnumber, string, length);
74 }
75}
76
77void on_action(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void* userdata)
78{
79 int i;
80 for (i = 0; i < MAX_WINDOW_SLOTS; ++i) {
81 if (windows[i].onAction != NULL)
82 windows[i].onAction(&windows[i], m, friendnumber, string, length);
83 }
84}
85
86void on_nickchange(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void* userdata)
87{
88 wprintw(prompt->window, "\n(nickchange) %d: %s\n", friendnumber, string);
89 int i;
90 for (i = 0; i < MAX_WINDOW_SLOTS; ++i) {
91 if (windows[i].onNickChange != NULL)
92 windows[i].onNickChange(&windows[i], friendnumber, string, length);
93 }
94}
95
96void on_statuschange(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void* userdata)
97{
98 wprintw(prompt->window, "\n(statuschange) %d: %s\n", friendnumber, string);
99 int i;
100 for (i=0; i<MAX_WINDOW_SLOTS; ++i) {
101 if (windows[i].onStatusChange != NULL)
102 windows[i].onStatusChange(&windows[i], friendnumber, string, length);
103 }
104}
105
106void on_friendadded(int friendnumber)
107{
108 friendlist_onFriendAdded(m, friendnumber);
109}
110/* CALLBACKS END */
111
112static void init_term() 31static void init_term()
113{ 32{
114 /* Setup terminal */ 33 /* Setup terminal */
@@ -129,10 +48,10 @@ static void init_term()
129 refresh(); 48 refresh();
130} 49}
131 50
132static void init_tox() 51static Messenger * init_tox()
133{ 52{
134 /* Init core */ 53 /* Init core */
135 m = initMessenger(); 54 Messenger *m = initMessenger();
136 55
137 /* Callbacks */ 56 /* Callbacks */
138 m_callback_friendrequest(m, on_request, NULL); 57 m_callback_friendrequest(m, on_request, NULL);
@@ -147,6 +66,7 @@ static void init_tox()
147#else 66#else
148 setname(m, (uint8_t*) "Hipster", sizeof("Hipster")); 67 setname(m, (uint8_t*) "Hipster", sizeof("Hipster"));
149#endif 68#endif
69 return m;
150} 70}
151 71
152#define MAXLINE 90 /* Approx max number of chars in a sever line (IP + port + key) */ 72#define MAXLINE 90 /* Approx max number of chars in a sever line (IP + port + key) */
@@ -195,67 +115,7 @@ int init_connection(void)
195 return 0; 115 return 0;
196} 116}
197 117
198void init_window_status() 118static void do_tox(Messenger *m, ToxWindow * prompt)
199{
200 /* Default window values decrement from -2 */
201 int i;
202 for (i = 0; i < N_DEFAULT_WINS; ++i)
203 WINDOW_STATUS[i] = -(i+2);
204
205 int j;
206 for (j = N_DEFAULT_WINS; j < MAX_WINDOW_SLOTS; j++)
207 WINDOW_STATUS[j] = -1;
208}
209
210int add_window(ToxWindow w, int n)
211{
212 if (w_num >= TOXWINDOWS_MAX_NUM)
213 return -1;
214
215 if (LINES < 2)
216 return -1;
217
218 w.window = newwin(LINES - 2, COLS, 0, 0);
219 if (w.window == NULL)
220 return -1;
221
222 windows[n] = w;
223 w.onInit(&w, m);
224 w_num++;
225 return n;
226}
227
228/* Deletes window w and cleans up */
229void del_window(ToxWindow *w, int f_num)
230{
231 delwin(w->window);
232 int i;
233 for (i = N_DEFAULT_WINS; i < MAX_WINDOW_SLOTS; ++i) {
234 if (WINDOW_STATUS[i] == f_num) {
235 WINDOW_STATUS[i] = -1;
236 disable_chatwin(f_num);
237 break;
238 }
239 }
240 clear();
241 refresh();
242}
243
244static void init_windows()
245{
246 w_num = 0;
247 int n_prompt = 0;
248 int n_friendslist = 1;
249 if (add_window(new_prompt(), n_prompt) == -1
250 || add_window(new_friendlist(), n_friendslist) == -1) {
251 fprintf(stderr, "add_window() failed.\n");
252 endwin();
253 exit(1);
254 }
255 prompt = &windows[n_prompt];
256}
257
258static void do_tox()
259{ 119{
260 static int conn_try = 0; 120 static int conn_try = 0;
261 static int conn_err = 0; 121 static int conn_err = 0;
@@ -279,7 +139,7 @@ static void do_tox()
279 doMessenger(m); 139 doMessenger(m);
280} 140}
281 141
282static void load_data(char *path) 142static void load_data(Messenger *m, char *path)
283{ 143{
284 FILE *fd; 144 FILE *fd;
285 size_t len; 145 size_t len;
@@ -336,88 +196,8 @@ static void load_data(char *path)
336 fclose(fd); 196 fclose(fd);
337} 197}
338 198
339static void draw_bar()
340{
341 static int odd = 0;
342 int blinkrate = 30;
343
344 attron(COLOR_PAIR(4));
345 mvhline(LINES - 2, 0, '_', COLS);
346 attroff(COLOR_PAIR(4));
347
348 move(LINES - 1, 0);
349
350 attron(COLOR_PAIR(4) | A_BOLD);
351 printw(" TOXIC " TOXICVER "|");
352 attroff(COLOR_PAIR(4) | A_BOLD);
353
354 int i;
355 for (i = 0; i < (MAX_WINDOW_SLOTS); ++i) {
356 if (WINDOW_STATUS[i] != -1) {
357 if (i == active_window)
358 attron(A_BOLD);
359
360 odd = (odd+1) % blinkrate;
361 if (windows[i].blink && (odd < (blinkrate/2)))
362 attron(COLOR_PAIR(3));
363
364 printw(" %s", windows[i].title);
365 if (windows[i].blink && (odd < (blinkrate/2)))
366 attroff(COLOR_PAIR(3));
367
368 if (i == active_window) {
369 attroff(A_BOLD);
370 }
371 }
372 }
373 refresh();
374}
375
376void prepare_window(WINDOW *w)
377{
378 mvwin(w, 0, 0);
379 wresize(w, LINES-2, COLS);
380}
381
382/* Shows next window when tab or back-tab is pressed */
383void set_active_window(int ch)
384{
385 int f_inf = 0;
386 int max = MAX_WINDOW_SLOTS-1;
387 if (ch == '\t') {
388 int i = (active_window + 1) % max;
389 while (true) {
390 if (WINDOW_STATUS[i] != -1) {
391 active_window = i;
392 return;
393 }
394 i = (i + 1) % max;
395 if (f_inf++ > max) { // infinite loop check
396 endwin();
397 exit(2);
398 }
399 }
400 }else {
401 int i = active_window - 1;
402 if (i < 0) i = max;
403 while (true) {
404 if (WINDOW_STATUS[i] != -1) {
405 active_window = i;
406 return;
407 }
408 if (--i < 0) i = max;
409 if (f_inf++ > max) {
410 endwin();
411 exit(2);
412 }
413 }
414 }
415}
416
417int main(int argc, char *argv[]) 199int main(int argc, char *argv[])
418{ 200{
419 int ch;
420 ToxWindow* a;
421 char *user_config_dir = get_user_config_dir(); 201 char *user_config_dir = get_user_config_dir();
422 char *DATA_FILE; 202 char *DATA_FILE;
423 int config_err = create_user_config_dir(user_config_dir); 203 int config_err = create_user_config_dir(user_config_dir);
@@ -451,12 +231,12 @@ int main(int argc, char *argv[])
451 } 231 }
452 232
453 init_term(); 233 init_term();
454 init_tox(); 234 Messenger *m = init_tox();
455 init_windows(); 235 ToxWindow * prompt = init_windows(m);
456 init_window_status(); 236 init_window_status();
457 237
458 if(f_loadfromfile) 238 if(f_loadfromfile)
459 load_data(DATA_FILE); 239 load_data(m, DATA_FILE);
460 free(DATA_FILE); 240 free(DATA_FILE);
461 241
462 if (f_flag == -1) { 242 if (f_flag == -1) {
@@ -474,21 +254,10 @@ int main(int argc, char *argv[])
474 } 254 }
475 while(true) { 255 while(true) {
476 /* Update tox */ 256 /* Update tox */
477 do_tox(); 257 do_tox(m, prompt);
478 258
479 /* Draw */ 259 /* Draw */
480 a = &windows[active_window]; 260 draw_active_window(m);
481 prepare_window(a->window);
482 a->blink = false;
483 draw_bar();
484 a->onDraw(a);
485
486 /* Handle input */
487 ch = getch();
488 if (ch == '\t' || ch == KEY_BTAB)
489 set_active_window(ch);
490 else if (ch != ERR)
491 a->onKey(a, m, ch);
492 } 261 }
493 cleanupMessenger(m); 262 cleanupMessenger(m);
494 return 0; 263 return 0;
diff --git a/testing/toxic/prompt.c b/testing/toxic/prompt.c
index b1d6693f..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(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
@@ -87,7 +88,7 @@ void cmd_accept(ToxWindow *self, Messenger *m, char **args)
87 wprintw(self->window, "Failed to add friend.\n"); 88 wprintw(self->window, "Failed to add friend.\n");
88 else { 89 else {
89 wprintw(self->window, "Friend accepted as: %d.\n", num); 90 wprintw(self->window, "Friend accepted as: %d.\n", num);
90 on_friendadded(num); 91 on_friendadded(m, num);
91 } 92 }
92} 93}
93 94
@@ -146,7 +147,7 @@ void cmd_add(ToxWindow *self, Messenger *m, char **args)
146 break; 147 break;
147 default: 148 default:
148 wprintw(self->window, "Friend added as %d.\n", num); 149 wprintw(self->window, "Friend added as %d.\n", num);
149 on_friendadded(num); 150 on_friendadded(m, num);
150 break; 151 break;
151 } 152 }
152} 153}
@@ -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
diff --git a/testing/toxic/windows.c b/testing/toxic/windows.c
new file mode 100644
index 00000000..d19392ac
--- /dev/null
+++ b/testing/toxic/windows.c
@@ -0,0 +1,235 @@
1#include "friendlist.h"
2#include "prompt.h"
3#include "windows.h"
4
5/* Holds status of chat windows */
6char WINDOW_STATUS[MAX_WINDOW_SLOTS];
7
8static int w_num;
9static ToxWindow windows[MAX_WINDOW_SLOTS];
10static Messenger *m;
11int active_window;
12
13static ToxWindow* prompt;
14
15/* CALLBACKS START */
16void on_request(uint8_t *public_key, uint8_t *data, uint16_t length, void* userdata)
17{
18 int n = add_req(public_key);
19 wprintw(prompt->window, "\nFriend request from:\n");
20
21 int i;
22 for (i = 0; i < KEY_SIZE_BYTES; ++i) {
23 wprintw(prompt->window, "%02x", public_key[i] & 0xff);
24 }
25
26 wprintw(prompt->window, "\nWith the message: %s\n", data);
27 wprintw(prompt->window, "\nUse \"accept %d\" to accept it.\n", n);
28
29 for (i = 0; i < MAX_WINDOW_SLOTS; ++i) {
30 if (windows[i].onFriendRequest != NULL)
31 windows[i].onFriendRequest(&windows[i], public_key, data, length);
32 }
33}
34
35void on_message(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void* userdata)
36{
37 int i;
38 for (i = 0; i < MAX_WINDOW_SLOTS; ++i) {
39 if (windows[i].onMessage != NULL)
40 windows[i].onMessage(&windows[i], m, friendnumber, string, length);
41 }
42}
43
44void on_action(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void* userdata)
45{
46 int i;
47 for (i = 0; i < MAX_WINDOW_SLOTS; ++i) {
48 if (windows[i].onAction != NULL)
49 windows[i].onAction(&windows[i], m, friendnumber, string, length);
50 }
51}
52
53void on_nickchange(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void* userdata)
54{
55 wprintw(prompt->window, "\n(nickchange) %d: %s\n", friendnumber, string);
56 int i;
57 for (i = 0; i < MAX_WINDOW_SLOTS; ++i) {
58 if (windows[i].onNickChange != NULL)
59 windows[i].onNickChange(&windows[i], friendnumber, string, length);
60 }
61}
62
63void on_statuschange(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void* userdata)
64{
65 wprintw(prompt->window, "\n(statuschange) %d: %s\n", friendnumber, string);
66 int i;
67 for (i=0; i<MAX_WINDOW_SLOTS; ++i) {
68 if (windows[i].onStatusChange != NULL)
69 windows[i].onStatusChange(&windows[i], friendnumber, string, length);
70 }
71}
72
73void on_friendadded(Messenger *m, int friendnumber)
74{
75 friendlist_onFriendAdded(m, friendnumber);
76}
77/* CALLBACKS END */
78
79int add_window(Messenger *m, ToxWindow w, int n)
80{
81 if (w_num >= TOXWINDOWS_MAX_NUM)
82 return -1;
83
84 if (LINES < 2)
85 return -1;
86
87 w.window = newwin(LINES - 2, COLS, 0, 0);
88 if (w.window == NULL)
89 return -1;
90
91 windows[n] = w;
92 w.onInit(&w, m);
93 w_num++;
94 active_window = n;
95 return n;
96}
97
98/* Deletes window w and cleans up */
99void del_window(ToxWindow *w, int f_num)
100{
101 active_window = 0; // Go to prompt screen
102 delwin(w->window);
103 int i;
104 for (i = N_DEFAULT_WINS; i < MAX_WINDOW_SLOTS; ++i) {
105 if (WINDOW_STATUS[i] == f_num) {
106 WINDOW_STATUS[i] = -1;
107 disable_chatwin(f_num);
108 break;
109 }
110 }
111 clear();
112 refresh();
113}
114
115/* Shows next window when tab or back-tab is pressed */
116void set_active_window(int ch)
117{
118 int f_inf = 0;
119 int max = MAX_WINDOW_SLOTS-1;
120 if (ch == '\t') {
121 int i = (active_window + 1) % max;
122 while (true) {
123 if (WINDOW_STATUS[i] != -1) {
124 active_window = i;
125 return;
126 }
127 i = (i + 1) % max;
128 if (f_inf++ > max) { // infinite loop check
129 endwin();
130 exit(2);
131 }
132 }
133 }else {
134 int i = active_window - 1;
135 if (i < 0) i = max;
136 while (true) {
137 if (WINDOW_STATUS[i] != -1) {
138 active_window = i;
139 return;
140 }
141 if (--i < 0) i = max;
142 if (f_inf++ > max) {
143 endwin();
144 exit(2);
145 }
146 }
147 }
148}
149
150void init_window_status()
151{
152 /* Default window values decrement from -2 */
153 int i;
154 for (i = 0; i < N_DEFAULT_WINS; ++i)
155 WINDOW_STATUS[i] = -(i+2);
156
157 int j;
158 for (j = N_DEFAULT_WINS; j < MAX_WINDOW_SLOTS; j++)
159 WINDOW_STATUS[j] = -1;
160}
161
162ToxWindow * init_windows()
163{
164 w_num = 0;
165 int n_prompt = 0;
166 int n_friendslist = 1;
167 if (add_window(m, new_prompt(on_friendadded), n_prompt) == -1
168 || add_window(m, new_friendlist(WINDOW_STATUS), n_friendslist) == -1) {
169 fprintf(stderr, "add_window() failed.\n");
170 endwin();
171 exit(1);
172 }
173 prompt = &windows[n_prompt];
174 return prompt;
175}
176
177static void draw_bar()
178{
179 static int odd = 0;
180 int blinkrate = 30;
181
182 attron(COLOR_PAIR(4));
183 mvhline(LINES - 2, 0, '_', COLS);
184 attroff(COLOR_PAIR(4));
185
186 move(LINES - 1, 0);
187
188 attron(COLOR_PAIR(4) | A_BOLD);
189 printw(" TOXIC " TOXICVER "|");
190 attroff(COLOR_PAIR(4) | A_BOLD);
191
192 int i;
193 for (i = 0; i < (MAX_WINDOW_SLOTS); ++i) {
194 if (WINDOW_STATUS[i] != -1) {
195 if (i == active_window)
196 attron(A_BOLD);
197
198 odd = (odd+1) % blinkrate;
199 if (windows[i].blink && (odd < (blinkrate/2)))
200 attron(COLOR_PAIR(3));
201
202 printw(" %s", windows[i].title);
203 if (windows[i].blink && (odd < (blinkrate/2)))
204 attroff(COLOR_PAIR(3));
205
206 if (i == active_window) {
207 attroff(A_BOLD);
208 }
209 }
210 }
211 refresh();
212}
213
214void prepare_window(WINDOW *w)
215{
216 mvwin(w, 0, 0);
217 wresize(w, LINES-2, COLS);
218}
219
220void draw_active_window(Messenger * m)
221{
222
223 ToxWindow *a = &windows[active_window];
224 prepare_window(a->window);
225 a->blink = false;
226 draw_bar();
227 a->onDraw(a);
228
229 /* Handle input */
230 int ch = getch();
231 if (ch == '\t' || ch == KEY_BTAB)
232 set_active_window(ch);
233 else if (ch != ERR)
234 a->onKey(a, m, ch);
235}
diff --git a/testing/toxic/windows.h b/testing/toxic/windows.h
index 648243d0..37f3e9da 100644
--- a/testing/toxic/windows.h
+++ b/testing/toxic/windows.h
@@ -1,8 +1,13 @@
1/* 1/*
2 * Toxic -- Tox Curses Client 2 * Toxic -- Tox Curses Client
3 */ 3 */
4#ifndef _windows_h
5#define _windows_h
4 6
7#include <curses.h>
8#include <stdint.h>
5#include <stdbool.h> 9#include <stdbool.h>
10#include "../../core/Messenger.h"
6#define TOXWINDOWS_MAX_NUM 32 11#define TOXWINDOWS_MAX_NUM 32
7#define MAX_FRIENDS_NUM 100 12#define MAX_FRIENDS_NUM 100
8#define MAX_STR_SIZE 256 13#define MAX_STR_SIZE 256
@@ -32,3 +37,17 @@ struct ToxWindow_ {
32 37
33 WINDOW* window; 38 WINDOW* window;
34}; 39};
40
41void on_request(uint8_t *public_key, uint8_t *data, uint16_t length, void* userdata);
42void on_message(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void* userdata);
43void on_action(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void* userdata);
44void on_nickchange(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void* userdata);
45void on_statuschange(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void* userdata);
46void init_window_status();
47ToxWindow * init_windows();
48void draw_active_window(Messenger * m);
49int add_window(Messenger *m, ToxWindow w, int n);
50void del_window(ToxWindow *w, int f_num);
51void set_active_window(int ch);
52#endif
53