summaryrefslogtreecommitdiff
path: root/testing/toxic
diff options
context:
space:
mode:
Diffstat (limited to 'testing/toxic')
-rw-r--r--testing/toxic/CMakeLists.txt3
-rw-r--r--testing/toxic/chat.c134
-rw-r--r--testing/toxic/friendlist.c48
-rw-r--r--testing/toxic/main.c45
-rw-r--r--testing/toxic/prompt.c4
-rw-r--r--testing/toxic/windows.h6
6 files changed, 222 insertions, 18 deletions
diff --git a/testing/toxic/CMakeLists.txt b/testing/toxic/CMakeLists.txt
index c70babb7..f30d8e9e 100644
--- a/testing/toxic/CMakeLists.txt
+++ b/testing/toxic/CMakeLists.txt
@@ -6,7 +6,8 @@ set(exe_name toxic)
6add_executable(${exe_name} 6add_executable(${exe_name}
7 main.c 7 main.c
8 prompt.c 8 prompt.c
9 friendlist.c) 9 friendlist.c
10 chat.c)
10 11
11target_link_libraries(${exe_name} 12target_link_libraries(${exe_name}
12 curses) 13 curses)
diff --git a/testing/toxic/chat.c b/testing/toxic/chat.c
new file mode 100644
index 00000000..53cf3319
--- /dev/null
+++ b/testing/toxic/chat.c
@@ -0,0 +1,134 @@
1#include <curses.h>
2#include <stdlib.h>
3#include <string.h>
4#include <stdint.h>
5#include <ctype.h>
6
7#include "../../core/Messenger.h"
8#include "../../core/network.h"
9
10#include "windows.h"
11
12typedef struct {
13 int friendnum;
14
15 char line[256];
16 size_t pos;
17
18 WINDOW* history;
19 WINDOW* linewin;
20
21} ChatContext;
22
23extern void fix_name(uint8_t* name);
24
25
26static void chat_onMessage(ToxWindow* self, int num, uint8_t* msg, uint16_t len) {
27 ChatContext* ctx = (ChatContext*) self->x;
28 uint8_t nick[MAX_NAME_LENGTH] = {0};
29
30 if(ctx->friendnum != num)
31 return;
32
33 getname(num, (uint8_t*) &nick);
34
35 msg[len-1] = '\0';
36 nick[MAX_NAME_LENGTH-1] = '\0';
37
38 fix_name(msg);
39 fix_name(nick);
40
41 wprintw(ctx->history, "%s: %s\n", nick, msg);
42}
43
44static void chat_onNickChange(ToxWindow* self, int num, uint8_t* nick, uint16_t len) {
45 ChatContext* ctx = (ChatContext*) self->x;
46
47 if(ctx->friendnum != num)
48 return;
49
50 nick[len-1] = '\0';
51 fix_name(nick);
52
53 wattron(ctx->history, COLOR_PAIR(3));
54 wprintw(ctx->history, " * Your partner changed nick to '%s'\n", nick);
55 wattroff(ctx->history, COLOR_PAIR(3));
56}
57
58static void chat_onStatusChange(ToxWindow* self, int num, uint8_t* status, uint16_t len) {
59
60}
61
62static void chat_onKey(ToxWindow* self, int key) {
63 ChatContext* ctx = (ChatContext*) self->x;
64
65 if(isprint(key)) {
66
67 if(ctx->pos != sizeof(ctx->line)-1) {
68 ctx->line[ctx->pos++] = key;
69 ctx->line[ctx->pos] = '\0';
70 }
71 }
72 else if(key == '\n') {
73 wprintw(ctx->history, "you: %s\n", ctx->line);
74
75 if(m_sendmessage(ctx->friendnum, (uint8_t*) ctx->line, strlen(ctx->line)+1) < 0) {
76 wprintw(ctx->history, " * Failed to send message.\n");
77 }
78
79 ctx->line[0] = '\0';
80 ctx->pos = 0;
81 }
82}
83
84static void chat_onDraw(ToxWindow* self) {
85 int x, y;
86 ChatContext* ctx = (ChatContext*) self->x;
87
88 getmaxyx(self->window, y, x);
89
90 (void) x;
91 if(y < 3)
92 return;
93
94 wclear(ctx->linewin);
95 mvwhline(ctx->linewin, 0, 0, '_', COLS);
96 mvwprintw(ctx->linewin, 1, 0, "%s\n", ctx->line);
97
98 wrefresh(self->window);
99}
100
101static void chat_onInit(ToxWindow* self) {
102 int x, y;
103 ChatContext* ctx = (ChatContext*) self->x;
104
105 getmaxyx(self->window, y, x);
106
107 ctx->history = subwin(self->window, y - 4, x, 0, 0);
108 wprintw(ctx->history, "Here goes chat\n");
109 scrollok(ctx->history, 1);
110
111 ctx->linewin = subwin(self->window, 2, x, y - 3, 0);
112}
113
114ToxWindow new_chat(int friendnum) {
115 ToxWindow ret;
116
117 memset(&ret, 0, sizeof(ret));
118
119 ret.onKey = &chat_onKey;
120 ret.onDraw = &chat_onDraw;
121 ret.onInit = &chat_onInit;
122 ret.onMessage = &chat_onMessage;
123 ret.onNickChange = &chat_onNickChange;
124 ret.onStatusChange = &chat_onStatusChange;
125
126 snprintf(ret.title, sizeof(ret.title), "[chat %d]", friendnum);
127
128 ChatContext* x = calloc(1, sizeof(ChatContext));
129 x->friendnum = friendnum;
130
131 ret.x = (void*) x;
132
133 return ret;
134}
diff --git a/testing/toxic/friendlist.c b/testing/toxic/friendlist.c
index f8b8c840..629adaff 100644
--- a/testing/toxic/friendlist.c
+++ b/testing/toxic/friendlist.c
@@ -12,6 +12,10 @@
12 12
13#include "windows.h" 13#include "windows.h"
14 14
15extern int add_window(ToxWindow w);
16extern int focus_window(int num);
17extern ToxWindow new_chat(int friendnum);
18
15#define MAX_FRIENDS_NUM 100 19#define MAX_FRIENDS_NUM 100
16 20
17typedef struct { 21typedef struct {
@@ -23,6 +27,7 @@ typedef struct {
23 27
24static friend_t friends[MAX_FRIENDS_NUM]; 28static friend_t friends[MAX_FRIENDS_NUM];
25static int num_friends = 0; 29static int num_friends = 0;
30static int num_selected = 0;
26 31
27 32
28void fix_name(uint8_t* name) { 33void fix_name(uint8_t* name) {
@@ -42,31 +47,31 @@ void fix_name(uint8_t* name) {
42 *q = 0; 47 *q = 0;
43} 48}
44 49
45int friendlist_nickchange(int num, uint8_t* str, uint16_t len) { 50void friendlist_onNickChange(ToxWindow* self, int num, uint8_t* str, uint16_t len) {
46 51
47 if(len >= MAX_NAME_LENGTH || num >= num_friends) 52 if(len >= MAX_NAME_LENGTH || num >= num_friends)
48 return -1; 53 return;
49 54
50 memcpy((char*) &friends[num].name, (char*) str, len); 55 memcpy((char*) &friends[num].name, (char*) str, len);
51 friends[num].name[len] = 0; 56 friends[num].name[len] = 0;
52 fix_name(friends[num].name); 57 fix_name(friends[num].name);
53 58
54 return 0; 59 return;
55} 60}
56 61
57int friendlist_statuschange(int num, uint8_t* str, uint16_t len) { 62void friendlist_onStatusChange(ToxWindow* self, int num, uint8_t* str, uint16_t len) {
58 63
59 if(len >= MAX_USERSTATUS_LENGTH || num >= num_friends) 64 if(len >= MAX_USERSTATUS_LENGTH || num >= num_friends)
60 return -1; 65 return;
61 66
62 memcpy((char*) &friends[num].status, (char*) str, len); 67 memcpy((char*) &friends[num].status, (char*) str, len);
63 friends[num].status[len] = 0; 68 friends[num].status[len] = 0;
64 fix_name(friends[num].status); 69 fix_name(friends[num].status);
65 70
66 return 0; 71 return;
67} 72}
68 73
69int friendlist_addfriend(int num) { 74int friendlist_onFriendAdded(int num) {
70 75
71 if(num_friends == MAX_FRIENDS_NUM) 76 if(num_friends == MAX_FRIENDS_NUM)
72 return -1; 77 return -1;
@@ -82,6 +87,17 @@ int friendlist_addfriend(int num) {
82 87
83static void friendlist_onKey(ToxWindow* self, int key) { 88static void friendlist_onKey(ToxWindow* self, int key) {
84 89
90 if(key == KEY_UP) {
91 if(num_selected != 0)
92 num_selected--;
93 }
94 else if(key == KEY_DOWN) {
95 if(num_friends != 0)
96 num_selected = (num_selected+1) % num_friends;
97 }
98 else if(key == '\n') {
99 focus_window(add_window(new_chat(num_selected)));
100 }
85} 101}
86 102
87static void friendlist_onDraw(ToxWindow* self) { 103static void friendlist_onDraw(ToxWindow* self) {
@@ -92,11 +108,23 @@ static void friendlist_onDraw(ToxWindow* self) {
92 if(num_friends == 0) { 108 if(num_friends == 0) {
93 wprintw(self->window, "Empty. Add some friends! :-)\n"); 109 wprintw(self->window, "Empty. Add some friends! :-)\n");
94 } 110 }
111 else {
112 wattron(self->window, COLOR_PAIR(2) | A_BOLD);
113 wprintw(self->window, "Friend list:\n");
114 wattroff(self->window, A_BOLD);
115
116 wprintw(self->window, " ENTER: start a chat\n");
117 wprintw(self->window, " UP/DOWN: navigate list\n");
118 wattroff(self->window, COLOR_PAIR(2));
119 }
95 120
96 wprintw(self->window, "\n"); 121 wprintw(self->window, "\n");
97 122
98 for(i=0; i<num_friends; i++) { 123 for(i=0; i<num_friends; i++) {
99 wprintw(self->window, "[%d] ", friends[i].num); 124
125 if(i == num_selected) wattron(self->window, COLOR_PAIR(3));
126 wprintw(self->window, " [%d] ", friends[i].num);
127 if(i == num_selected) wattroff(self->window, COLOR_PAIR(3));
100 128
101 attron(A_BOLD); 129 attron(A_BOLD);
102 wprintw(self->window, "%s ", friends[i].name); 130 wprintw(self->window, "%s ", friends[i].name);
@@ -116,9 +144,13 @@ static void friendlist_onInit(ToxWindow* self) {
116ToxWindow new_friendlist() { 144ToxWindow new_friendlist() {
117 ToxWindow ret; 145 ToxWindow ret;
118 146
147 memset(&ret, 0, sizeof(ret));
148
119 ret.onKey = &friendlist_onKey; 149 ret.onKey = &friendlist_onKey;
120 ret.onDraw = &friendlist_onDraw; 150 ret.onDraw = &friendlist_onDraw;
121 ret.onInit = &friendlist_onInit; 151 ret.onInit = &friendlist_onInit;
152 ret.onNickChange = &friendlist_onNickChange;
153 ret.onStatusChange = &friendlist_onStatusChange;
122 strcpy(ret.title, "[friends]"); 154 strcpy(ret.title, "[friends]");
123 155
124 return ret; 156 return ret;
diff --git a/testing/toxic/main.c b/testing/toxic/main.c
index e1d1ebd0..1c095c54 100644
--- a/testing/toxic/main.c
+++ b/testing/toxic/main.c
@@ -16,9 +16,7 @@
16extern ToxWindow new_prompt(); 16extern ToxWindow new_prompt();
17extern ToxWindow new_friendlist(); 17extern ToxWindow new_friendlist();
18 18
19extern int friendlist_addfriend(int num); 19extern int friendlist_onFriendAdded(int num);
20extern int friendlist_nickchange(int num, uint8_t* str, uint16_t len);
21extern int friendlist_statuschange(int num, uint8_t* str, uint16_t len);
22 20
23extern int add_req(uint8_t* public_key); // XXX 21extern int add_req(uint8_t* public_key); // XXX
24 22
@@ -31,29 +29,52 @@ static ToxWindow* prompt;
31 29
32// CALLBACKS START 30// CALLBACKS START
33void on_request(uint8_t* public_key, uint8_t* data, uint16_t length) { 31void on_request(uint8_t* public_key, uint8_t* data, uint16_t length) {
32 size_t i;
34 int n = add_req(public_key); 33 int n = add_req(public_key);
35 34
36 wprintw(prompt->window, "\nFriend request.\nUse \"accept %d\" to accept it.\n", n); 35 wprintw(prompt->window, "\nFriend request.\nUse \"accept %d\" to accept it.\n", n);
36
37 for(i=0; i<w_num; i++) {
38 if(windows[i].onFriendRequest != NULL)
39 windows[i].onFriendRequest(&windows[i], public_key, data, length);
40 }
37} 41}
38 42
39void on_message(int friendnumber, uint8_t* string, uint16_t length) { 43void on_message(int friendnumber, uint8_t* string, uint16_t length) {
44 size_t i;
45
40 wprintw(prompt->window, "\n(message) %d: %s!\n", friendnumber, string); 46 wprintw(prompt->window, "\n(message) %d: %s!\n", friendnumber, string);
47
48 for(i=0; i<w_num; i++) {
49 if(windows[i].onMessage != NULL)
50 windows[i].onMessage(&windows[i], friendnumber, string, length);
51 }
41} 52}
42 53
43void on_nickchange(int friendnumber, uint8_t* string, uint16_t length) { 54void on_nickchange(int friendnumber, uint8_t* string, uint16_t length) {
55 size_t i;
56
44 wprintw(prompt->window, "\n(nickchange) %d: %s!\n", friendnumber, string); 57 wprintw(prompt->window, "\n(nickchange) %d: %s!\n", friendnumber, string);
45 58
46 friendlist_nickchange(friendnumber, string, length); 59 for(i=0; i<w_num; i++) {
60 if(windows[i].onNickChange != NULL)
61 windows[i].onNickChange(&windows[i], friendnumber, string, length);
62 }
47} 63}
48 64
49void on_statuschange(int friendnumber, uint8_t* string, uint16_t length) { 65void on_statuschange(int friendnumber, uint8_t* string, uint16_t length) {
66 size_t i;
67
50 wprintw(prompt->window, "\n(statuschange) %d: %s!\n", friendnumber, string); 68 wprintw(prompt->window, "\n(statuschange) %d: %s!\n", friendnumber, string);
51 69
52 friendlist_statuschange(friendnumber, string, length); 70 for(i=0; i<w_num; i++) {
71 if(windows[i].onStatusChange != NULL)
72 windows[i].onStatusChange(&windows[i], friendnumber, string, length);
73 }
53} 74}
54 75
55void on_friendadded(int friendnumber) { 76void on_friendadded(int friendnumber) {
56 friendlist_addfriend(friendnumber); 77 friendlist_onFriendAdded(friendnumber);
57} 78}
58// CALLBACKS END 79// CALLBACKS END
59 80
@@ -87,7 +108,7 @@ static void init_tox() {
87 m_callback_userstatus(on_statuschange); 108 m_callback_userstatus(on_statuschange);
88} 109}
89 110
90static int add_window(ToxWindow w) { 111int add_window(ToxWindow w) {
91 if(w_num == TOXWINDOWS_MAX_NUM) 112 if(w_num == TOXWINDOWS_MAX_NUM)
92 return -1; 113 return -1;
93 114
@@ -102,7 +123,15 @@ static int add_window(ToxWindow w) {
102 windows[w_num++] = w; 123 windows[w_num++] = w;
103 w.onInit(&w); 124 w.onInit(&w);
104 125
105 return w_num; 126 return w_num - 1;
127}
128
129int focus_window(int num) {
130 if(num >= w_num || num < 0)
131 return -1;
132
133 w_active = num;
134 return 0;
106} 135}
107 136
108static void init_windows() { 137static void init_windows() {
diff --git a/testing/toxic/prompt.c b/testing/toxic/prompt.c
index 22d9eb9e..52e9bde0 100644
--- a/testing/toxic/prompt.c
+++ b/testing/toxic/prompt.c
@@ -221,7 +221,7 @@ static void execute(ToxWindow* self, char* cmd) {
221 msg[0] = 0; 221 msg[0] = 0;
222 msg++; 222 msg++;
223 223
224 if(m_sendmessage(atoi(id), (uint8_t*) msg, strlen(msg)+1) != 1) { 224 if(m_sendmessage(atoi(id), (uint8_t*) msg, strlen(msg)+1) < 0) {
225 wprintw(self->window, "Error occurred while sending message.\n"); 225 wprintw(self->window, "Error occurred while sending message.\n");
226 } 226 }
227 else { 227 else {
@@ -312,6 +312,8 @@ static void prompt_onInit(ToxWindow* self) {
312ToxWindow new_prompt() { 312ToxWindow new_prompt() {
313 ToxWindow ret; 313 ToxWindow ret;
314 314
315 memset(&ret, 0, sizeof(ret));
316
315 ret.onKey = &prompt_onKey; 317 ret.onKey = &prompt_onKey;
316 ret.onDraw = &prompt_onDraw; 318 ret.onDraw = &prompt_onDraw;
317 ret.onInit = &prompt_onInit; 319 ret.onInit = &prompt_onInit;
diff --git a/testing/toxic/windows.h b/testing/toxic/windows.h
index 2e082ca9..480fd658 100644
--- a/testing/toxic/windows.h
+++ b/testing/toxic/windows.h
@@ -4,7 +4,13 @@ struct ToxWindow_ {
4 void(*onKey)(ToxWindow*, int); 4 void(*onKey)(ToxWindow*, int);
5 void(*onDraw)(ToxWindow*); 5 void(*onDraw)(ToxWindow*);
6 void(*onInit)(ToxWindow*); 6 void(*onInit)(ToxWindow*);
7 void(*onFriendRequest)(ToxWindow*, uint8_t*, uint8_t*, uint16_t);
8 void(*onMessage)(ToxWindow*, int, uint8_t*, uint16_t);
9 void(*onNickChange)(ToxWindow*, int, uint8_t*, uint16_t);
10 void(*onStatusChange)(ToxWindow*, int, uint8_t*, uint16_t);
7 char title[256]; 11 char title[256];
8 12
13 void* x;
14
9 WINDOW* window; 15 WINDOW* window;
10}; 16};