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.c49
1 files changed, 44 insertions, 5 deletions
diff --git a/testing/toxic/main.c b/testing/toxic/main.c
index 0aad6777..e1d1ebd0 100644
--- a/testing/toxic/main.c
+++ b/testing/toxic/main.c
@@ -14,6 +14,12 @@
14#include "windows.h" 14#include "windows.h"
15 15
16extern ToxWindow new_prompt(); 16extern ToxWindow new_prompt();
17extern ToxWindow new_friendlist();
18
19extern int friendlist_addfriend(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
17extern int add_req(uint8_t* public_key); // XXX 23extern int add_req(uint8_t* public_key); // XXX
18 24
19#define TOXWINDOWS_MAX_NUM 32 25#define TOXWINDOWS_MAX_NUM 32
@@ -35,11 +41,19 @@ void on_message(int friendnumber, uint8_t* string, uint16_t length) {
35} 41}
36 42
37void on_nickchange(int friendnumber, uint8_t* string, uint16_t length) { 43void on_nickchange(int friendnumber, uint8_t* string, uint16_t length) {
38 wprintw(prompt->window, "\n(nick) %d: %s!\n", friendnumber, string); 44 wprintw(prompt->window, "\n(nickchange) %d: %s!\n", friendnumber, string);
45
46 friendlist_nickchange(friendnumber, string, length);
39} 47}
40 48
41void on_statuschange(int friendnumber, uint8_t* string, uint16_t length) { 49void on_statuschange(int friendnumber, uint8_t* string, uint16_t length) {
42 wprintw(prompt->window, "\n(status) %d: %s!\n", friendnumber, string); 50 wprintw(prompt->window, "\n(statuschange) %d: %s!\n", friendnumber, string);
51
52 friendlist_statuschange(friendnumber, string, length);
53}
54
55void on_friendadded(int friendnumber) {
56 friendlist_addfriend(friendnumber);
43} 57}
44// CALLBACKS END 58// CALLBACKS END
45 59
@@ -95,7 +109,7 @@ static void init_windows() {
95 w_num = 0; 109 w_num = 0;
96 w_active = 0; 110 w_active = 0;
97 111
98 if(add_window(new_prompt()) == -1) { 112 if(add_window(new_prompt()) == -1 || add_window(new_friendlist()) == -1) {
99 fprintf(stderr, "add_window() failed.\n"); 113 fprintf(stderr, "add_window() failed.\n");
100 114
101 endwin(); 115 endwin();
@@ -134,14 +148,18 @@ static void load_data() {
134 148
135 if(buf == NULL) { 149 if(buf == NULL) {
136 fprintf(stderr, "malloc() failed.\n"); 150 fprintf(stderr, "malloc() failed.\n");
151
137 fclose(fd); 152 fclose(fd);
153 endwin();
138 exit(1); 154 exit(1);
139 } 155 }
140 156
141 if(fread(buf, len, 1, fd) != 1){ 157 if(fread(buf, len, 1, fd) != 1){
142 fprintf(stderr, "fread() failed.\n"); 158 fprintf(stderr, "fread() failed.\n");
159
143 free(buf); 160 free(buf);
144 fclose(fd); 161 fclose(fd);
162 endwin();
145 exit(1); 163 exit(1);
146 } 164 }
147 165
@@ -153,6 +171,7 @@ static void load_data() {
153 171
154 if(buf == NULL) { 172 if(buf == NULL) {
155 fprintf(stderr, "malloc() failed.\n"); 173 fprintf(stderr, "malloc() failed.\n");
174 endwin();
156 exit(1); 175 exit(1);
157 } 176 }
158 177
@@ -161,14 +180,18 @@ static void load_data() {
161 fd = fopen("data", "w"); 180 fd = fopen("data", "w");
162 if(fd == NULL) { 181 if(fd == NULL) {
163 fprintf(stderr, "fopen() failed.\n"); 182 fprintf(stderr, "fopen() failed.\n");
183
164 free(buf); 184 free(buf);
185 endwin();
165 exit(1); 186 exit(1);
166 } 187 }
167 188
168 if(fwrite(buf, len, 1, fd) != 1){ 189 if(fwrite(buf, len, 1, fd) != 1){
169 fprintf(stderr, "fwrite() failed.\n"); 190 fprintf(stderr, "fwrite() failed.\n");
191
170 free(buf); 192 free(buf);
171 fclose(fd); 193 fclose(fd);
194 endwin();
172 exit(1); 195 exit(1);
173 } 196 }
174 } 197 }
@@ -186,12 +209,16 @@ static void draw_bar() {
186 209
187 move(LINES - 1, 0); 210 move(LINES - 1, 0);
188 211
212 attron(COLOR_PAIR(3) | A_BOLD);
213 printw(" TOXIC 1.0 |");
214 attroff(COLOR_PAIR(3) | A_BOLD);
215
189 for(i=0; i<w_num; i++) { 216 for(i=0; i<w_num; i++) {
190 if(i == w_active) { 217 if(i == w_active) {
191 attron(A_BOLD); 218 attron(A_BOLD);
192 } 219 }
193 220
194 printw(" %s ", windows[i].title); 221 printw(" %s", windows[i].title);
195 222
196 if(i == w_active) { 223 if(i == w_active) {
197 attroff(A_BOLD); 224 attroff(A_BOLD);
@@ -201,6 +228,11 @@ static void draw_bar() {
201 refresh(); 228 refresh();
202} 229}
203 230
231void prepare_window(WINDOW* w) {
232 mvwin(w, 0, 0);
233 wresize(w, LINES-2, COLS);
234}
235
204int main(int argc, char* argv[]) { 236int main(int argc, char* argv[]) {
205 int ch; 237 int ch;
206 ToxWindow* a; 238 ToxWindow* a;
@@ -211,14 +243,21 @@ int main(int argc, char* argv[]) {
211 init_windows(); 243 init_windows();
212 244
213 while(true) { 245 while(true) {
246 // Update tox.
214 do_tox(); 247 do_tox();
215 248
249 // Draw.
216 a = &windows[w_active]; 250 a = &windows[w_active];
251 prepare_window(a->window);
217 a->onDraw(a); 252 a->onDraw(a);
218 draw_bar(); 253 draw_bar();
219 254
255 // Handle input.
220 ch = getch(); 256 ch = getch();
221 if(ch != ERR) { 257 if(ch == '\t') {
258 w_active = (w_active + 1) % w_num;
259 }
260 else if(ch != ERR) {
222 a->onKey(a, ch); 261 a->onKey(a, ch);
223 } 262 }
224 263