summaryrefslogtreecommitdiff
path: root/testing/toxic/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'testing/toxic/main.c')
-rw-r--r--testing/toxic/main.c258
1 files changed, 12 insertions, 246 deletions
diff --git a/testing/toxic/main.c b/testing/toxic/main.c
index 40ff5d09..063c1483 100644
--- a/testing/toxic/main.c
+++ b/testing/toxic/main.c
@@ -21,93 +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
33/* Holds status of chat windows */
34char WINDOW_STATUS[MAX_WINDOW_SLOTS];
35
36#ifndef TOXICVER
37#define TOXICVER "NOVER" //Use the -D flag to set this
38#endif
39
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 27
112static void init_term() 28static void init_term()
113{ 29{
@@ -129,10 +45,10 @@ static void init_term()
129 refresh(); 45 refresh();
130} 46}
131 47
132static void init_tox() 48static Messenger * init_tox()
133{ 49{
134 /* Init core */ 50 /* Init core */
135 m = initMessenger(); 51 Messenger *m = initMessenger();
136 52
137 /* Callbacks */ 53 /* Callbacks */
138 m_callback_friendrequest(m, on_request, NULL); 54 m_callback_friendrequest(m, on_request, NULL);
@@ -147,6 +63,7 @@ static void init_tox()
147#else 63#else
148 setname(m, (uint8_t*) "Hipster", sizeof("Hipster")); 64 setname(m, (uint8_t*) "Hipster", sizeof("Hipster"));
149#endif 65#endif
66 return m;
150} 67}
151 68
152#define MAXLINE 90 /* Approx max number of chars in a sever line (IP + port + key) */ 69#define MAXLINE 90 /* Approx max number of chars in a sever line (IP + port + key) */
@@ -195,67 +112,7 @@ int init_connection(void)
195 return 0; 112 return 0;
196} 113}
197 114
198void init_window_status() 115static 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{ 116{
260 static int conn_try = 0; 117 static int conn_try = 0;
261 static int conn_err = 0; 118 static int conn_err = 0;
@@ -279,7 +136,7 @@ static void do_tox()
279 doMessenger(m); 136 doMessenger(m);
280} 137}
281 138
282static void load_data(char *path) 139static void load_data(Messenger *m, char *path)
283{ 140{
284 FILE *fd; 141 FILE *fd;
285 size_t len; 142 size_t len;
@@ -336,88 +193,8 @@ static void load_data(char *path)
336 fclose(fd); 193 fclose(fd);
337} 194}
338 195
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[]) 196int main(int argc, char *argv[])
418{ 197{
419 int ch;
420 ToxWindow* a;
421 char *user_config_dir = get_user_config_dir(); 198 char *user_config_dir = get_user_config_dir();
422 char *DATA_FILE; 199 char *DATA_FILE;
423 int config_err = create_user_config_dir(user_config_dir); 200 int config_err = create_user_config_dir(user_config_dir);
@@ -451,12 +228,12 @@ int main(int argc, char *argv[])
451 } 228 }
452 229
453 init_term(); 230 init_term();
454 init_tox(); 231 Messenger *m = init_tox();
455 init_windows(); 232 ToxWindow * prompt = init_windows(m);
456 init_window_status(); 233 init_window_status();
457 234
458 if(f_loadfromfile) 235 if(f_loadfromfile)
459 load_data(DATA_FILE); 236 load_data(m, DATA_FILE);
460 free(DATA_FILE); 237 free(DATA_FILE);
461 238
462 if (f_flag == -1) { 239 if (f_flag == -1) {
@@ -474,21 +251,10 @@ int main(int argc, char *argv[])
474 } 251 }
475 while(true) { 252 while(true) {
476 /* Update tox */ 253 /* Update tox */
477 do_tox(); 254 do_tox(m, prompt);
478 255
479 /* Draw */ 256 /* Draw */
480 a = &windows[active_window]; 257 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 } 258 }
493 cleanupMessenger(m); 259 cleanupMessenger(m);
494 return 0; 260 return 0;