diff options
author | Astonex <softukitu@gmail.com> | 2013-07-31 23:02:09 +0100 |
---|---|---|
committer | Astonex <softukitu@gmail.com> | 2013-07-31 23:02:09 +0100 |
commit | f05aa308701f33f3bf9df022a4b376deeedef235 (patch) | |
tree | c10b8a3cca6822400853bfd3bc2c5fd1cca73f83 /testing/toxic/main.c | |
parent | 8dfba27242ca23fca5de852541f2101568dbf7cb (diff) | |
parent | c558cb63f6db35bd51f2f2331e21df03105ee82a (diff) |
Merge remote-tracking branch 'ProjectTox/master'
Diffstat (limited to 'testing/toxic/main.c')
-rw-r--r-- | testing/toxic/main.c | 103 |
1 files changed, 95 insertions, 8 deletions
diff --git a/testing/toxic/main.c b/testing/toxic/main.c index 0aad6777..fffc3f84 100644 --- a/testing/toxic/main.c +++ b/testing/toxic/main.c | |||
@@ -14,6 +14,10 @@ | |||
14 | #include "windows.h" | 14 | #include "windows.h" |
15 | 15 | ||
16 | extern ToxWindow new_prompt(); | 16 | extern ToxWindow new_prompt(); |
17 | extern ToxWindow new_friendlist(); | ||
18 | |||
19 | extern int friendlist_onFriendAdded(int num); | ||
20 | |||
17 | extern int add_req(uint8_t* public_key); // XXX | 21 | extern int add_req(uint8_t* public_key); // XXX |
18 | 22 | ||
19 | #define TOXWINDOWS_MAX_NUM 32 | 23 | #define TOXWINDOWS_MAX_NUM 32 |
@@ -25,21 +29,59 @@ static ToxWindow* prompt; | |||
25 | 29 | ||
26 | // CALLBACKS START | 30 | // CALLBACKS START |
27 | void on_request(uint8_t* public_key, uint8_t* data, uint16_t length) { | 31 | void on_request(uint8_t* public_key, uint8_t* data, uint16_t length) { |
32 | size_t i; | ||
28 | int n = add_req(public_key); | 33 | int n = add_req(public_key); |
29 | 34 | ||
30 | wprintw(prompt->window, "\nFriend request.\nUse \"accept %d\" to accept it.\n", n); | 35 | wprintw(prompt->window, "\nFriend request from:\n"); |
36 | |||
37 | for(i=0; i<32; i++) { | ||
38 | wprintw(prompt->window, "%02x", public_key[i] & 0xff); | ||
39 | } | ||
40 | wprintw(prompt->window, "\n"); | ||
41 | |||
42 | wprintw(prompt->window, "Use \"accept %d\" to accept it.\n", n); | ||
43 | |||
44 | for(i=0; i<w_num; i++) { | ||
45 | if(windows[i].onFriendRequest != NULL) | ||
46 | windows[i].onFriendRequest(&windows[i], public_key, data, length); | ||
47 | } | ||
31 | } | 48 | } |
32 | 49 | ||
33 | void on_message(int friendnumber, uint8_t* string, uint16_t length) { | 50 | void on_message(int friendnumber, uint8_t* string, uint16_t length) { |
51 | size_t i; | ||
52 | |||
34 | wprintw(prompt->window, "\n(message) %d: %s!\n", friendnumber, string); | 53 | wprintw(prompt->window, "\n(message) %d: %s!\n", friendnumber, string); |
54 | |||
55 | for(i=0; i<w_num; i++) { | ||
56 | if(windows[i].onMessage != NULL) | ||
57 | windows[i].onMessage(&windows[i], friendnumber, string, length); | ||
58 | } | ||
35 | } | 59 | } |
36 | 60 | ||
37 | void on_nickchange(int friendnumber, uint8_t* string, uint16_t length) { | 61 | void on_nickchange(int friendnumber, uint8_t* string, uint16_t length) { |
38 | wprintw(prompt->window, "\n(nick) %d: %s!\n", friendnumber, string); | 62 | size_t i; |
63 | |||
64 | wprintw(prompt->window, "\n(nickchange) %d: %s!\n", friendnumber, string); | ||
65 | |||
66 | for(i=0; i<w_num; i++) { | ||
67 | if(windows[i].onNickChange != NULL) | ||
68 | windows[i].onNickChange(&windows[i], friendnumber, string, length); | ||
69 | } | ||
39 | } | 70 | } |
40 | 71 | ||
41 | void on_statuschange(int friendnumber, uint8_t* string, uint16_t length) { | 72 | void on_statuschange(int friendnumber, uint8_t* string, uint16_t length) { |
42 | wprintw(prompt->window, "\n(status) %d: %s!\n", friendnumber, string); | 73 | size_t i; |
74 | |||
75 | wprintw(prompt->window, "\n(statuschange) %d: %s!\n", friendnumber, string); | ||
76 | |||
77 | for(i=0; i<w_num; i++) { | ||
78 | if(windows[i].onStatusChange != NULL) | ||
79 | windows[i].onStatusChange(&windows[i], friendnumber, string, length); | ||
80 | } | ||
81 | } | ||
82 | |||
83 | void on_friendadded(int friendnumber) { | ||
84 | friendlist_onFriendAdded(friendnumber); | ||
43 | } | 85 | } |
44 | // CALLBACKS END | 86 | // CALLBACKS END |
45 | 87 | ||
@@ -73,7 +115,7 @@ static void init_tox() { | |||
73 | m_callback_userstatus(on_statuschange); | 115 | m_callback_userstatus(on_statuschange); |
74 | } | 116 | } |
75 | 117 | ||
76 | static int add_window(ToxWindow w) { | 118 | int add_window(ToxWindow w) { |
77 | if(w_num == TOXWINDOWS_MAX_NUM) | 119 | if(w_num == TOXWINDOWS_MAX_NUM) |
78 | return -1; | 120 | return -1; |
79 | 121 | ||
@@ -88,14 +130,22 @@ static int add_window(ToxWindow w) { | |||
88 | windows[w_num++] = w; | 130 | windows[w_num++] = w; |
89 | w.onInit(&w); | 131 | w.onInit(&w); |
90 | 132 | ||
91 | return w_num; | 133 | return w_num - 1; |
134 | } | ||
135 | |||
136 | int focus_window(int num) { | ||
137 | if(num >= w_num || num < 0) | ||
138 | return -1; | ||
139 | |||
140 | w_active = num; | ||
141 | return 0; | ||
92 | } | 142 | } |
93 | 143 | ||
94 | static void init_windows() { | 144 | static void init_windows() { |
95 | w_num = 0; | 145 | w_num = 0; |
96 | w_active = 0; | 146 | w_active = 0; |
97 | 147 | ||
98 | if(add_window(new_prompt()) == -1) { | 148 | if(add_window(new_prompt()) == -1 || add_window(new_friendlist()) == -1) { |
99 | fprintf(stderr, "add_window() failed.\n"); | 149 | fprintf(stderr, "add_window() failed.\n"); |
100 | 150 | ||
101 | endwin(); | 151 | endwin(); |
@@ -134,14 +184,18 @@ static void load_data() { | |||
134 | 184 | ||
135 | if(buf == NULL) { | 185 | if(buf == NULL) { |
136 | fprintf(stderr, "malloc() failed.\n"); | 186 | fprintf(stderr, "malloc() failed.\n"); |
187 | |||
137 | fclose(fd); | 188 | fclose(fd); |
189 | endwin(); | ||
138 | exit(1); | 190 | exit(1); |
139 | } | 191 | } |
140 | 192 | ||
141 | if(fread(buf, len, 1, fd) != 1){ | 193 | if(fread(buf, len, 1, fd) != 1){ |
142 | fprintf(stderr, "fread() failed.\n"); | 194 | fprintf(stderr, "fread() failed.\n"); |
195 | |||
143 | free(buf); | 196 | free(buf); |
144 | fclose(fd); | 197 | fclose(fd); |
198 | endwin(); | ||
145 | exit(1); | 199 | exit(1); |
146 | } | 200 | } |
147 | 201 | ||
@@ -153,6 +207,7 @@ static void load_data() { | |||
153 | 207 | ||
154 | if(buf == NULL) { | 208 | if(buf == NULL) { |
155 | fprintf(stderr, "malloc() failed.\n"); | 209 | fprintf(stderr, "malloc() failed.\n"); |
210 | endwin(); | ||
156 | exit(1); | 211 | exit(1); |
157 | } | 212 | } |
158 | 213 | ||
@@ -161,14 +216,18 @@ static void load_data() { | |||
161 | fd = fopen("data", "w"); | 216 | fd = fopen("data", "w"); |
162 | if(fd == NULL) { | 217 | if(fd == NULL) { |
163 | fprintf(stderr, "fopen() failed.\n"); | 218 | fprintf(stderr, "fopen() failed.\n"); |
219 | |||
164 | free(buf); | 220 | free(buf); |
221 | endwin(); | ||
165 | exit(1); | 222 | exit(1); |
166 | } | 223 | } |
167 | 224 | ||
168 | if(fwrite(buf, len, 1, fd) != 1){ | 225 | if(fwrite(buf, len, 1, fd) != 1){ |
169 | fprintf(stderr, "fwrite() failed.\n"); | 226 | fprintf(stderr, "fwrite() failed.\n"); |
227 | |||
170 | free(buf); | 228 | free(buf); |
171 | fclose(fd); | 229 | fclose(fd); |
230 | endwin(); | ||
172 | exit(1); | 231 | exit(1); |
173 | } | 232 | } |
174 | } | 233 | } |
@@ -178,6 +237,7 @@ static void load_data() { | |||
178 | } | 237 | } |
179 | 238 | ||
180 | static void draw_bar() { | 239 | static void draw_bar() { |
240 | static int odd = 0; | ||
181 | size_t i; | 241 | size_t i; |
182 | 242 | ||
183 | attron(COLOR_PAIR(4)); | 243 | attron(COLOR_PAIR(4)); |
@@ -186,12 +246,26 @@ static void draw_bar() { | |||
186 | 246 | ||
187 | move(LINES - 1, 0); | 247 | move(LINES - 1, 0); |
188 | 248 | ||
249 | attron(COLOR_PAIR(4) | A_BOLD); | ||
250 | printw(" TOXIC 1.0 |"); | ||
251 | attroff(COLOR_PAIR(4) | A_BOLD); | ||
252 | |||
189 | for(i=0; i<w_num; i++) { | 253 | for(i=0; i<w_num; i++) { |
190 | if(i == w_active) { | 254 | if(i == w_active) { |
191 | attron(A_BOLD); | 255 | attron(A_BOLD); |
192 | } | 256 | } |
193 | 257 | ||
194 | printw(" %s ", windows[i].title); | 258 | odd = (odd+1) % 2; |
259 | |||
260 | if(windows[i].blink && odd) { | ||
261 | attron(COLOR_PAIR(3)); | ||
262 | } | ||
263 | |||
264 | printw(" %s", windows[i].title); | ||
265 | |||
266 | if(windows[i].blink && odd) { | ||
267 | attron(COLOR_PAIR(3)); | ||
268 | } | ||
195 | 269 | ||
196 | if(i == w_active) { | 270 | if(i == w_active) { |
197 | attroff(A_BOLD); | 271 | attroff(A_BOLD); |
@@ -201,6 +275,11 @@ static void draw_bar() { | |||
201 | refresh(); | 275 | refresh(); |
202 | } | 276 | } |
203 | 277 | ||
278 | void prepare_window(WINDOW* w) { | ||
279 | mvwin(w, 0, 0); | ||
280 | wresize(w, LINES-2, COLS); | ||
281 | } | ||
282 | |||
204 | int main(int argc, char* argv[]) { | 283 | int main(int argc, char* argv[]) { |
205 | int ch; | 284 | int ch; |
206 | ToxWindow* a; | 285 | ToxWindow* a; |
@@ -211,14 +290,22 @@ int main(int argc, char* argv[]) { | |||
211 | init_windows(); | 290 | init_windows(); |
212 | 291 | ||
213 | while(true) { | 292 | while(true) { |
293 | // Update tox. | ||
214 | do_tox(); | 294 | do_tox(); |
215 | 295 | ||
296 | // Draw. | ||
216 | a = &windows[w_active]; | 297 | a = &windows[w_active]; |
298 | prepare_window(a->window); | ||
299 | a->blink = false; | ||
217 | a->onDraw(a); | 300 | a->onDraw(a); |
218 | draw_bar(); | 301 | draw_bar(); |
219 | 302 | ||
303 | // Handle input. | ||
220 | ch = getch(); | 304 | ch = getch(); |
221 | if(ch != ERR) { | 305 | if(ch == '\t') { |
306 | w_active = (w_active + 1) % w_num; | ||
307 | } | ||
308 | else if(ch != ERR) { | ||
222 | a->onKey(a, ch); | 309 | a->onKey(a, ch); |
223 | } | 310 | } |
224 | 311 | ||