summaryrefslogtreecommitdiff
path: root/testing/toxic
diff options
context:
space:
mode:
authorAaron Lipinski <kris.lipinski@gmail.com>2013-08-13 12:21:03 +1200
committerAaron Lipinski <kris.lipinski@gmail.com>2013-08-15 00:36:30 +1200
commit1edb7c1660884face7f5d97b8d6df57da5e54a17 (patch)
tree4c43be997f67832fafeabf0ebf9e0dcd4396805a /testing/toxic
parent75ce48146a7764b67a19d41e4da5d279d59b6ea1 (diff)
move windows functions to its own file
Diffstat (limited to 'testing/toxic')
-rw-r--r--testing/toxic/CMakeLists.txt1
-rw-r--r--testing/toxic/main.c236
-rw-r--r--testing/toxic/windows.c235
-rw-r--r--testing/toxic/windows.h8
4 files changed, 248 insertions, 232 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/main.c b/testing/toxic/main.c
index a109311e..d7b251dc 100644
--- a/testing/toxic/main.c
+++ b/testing/toxic/main.c
@@ -24,84 +24,10 @@
24#include "prompt.h" 24#include "prompt.h"
25#include "friendlist.h" 25#include "friendlist.h"
26 26
27
28/* Holds status of chat windows */
29char WINDOW_STATUS[MAX_WINDOW_SLOTS];
30
31#ifndef TOXICVER 27#ifndef TOXICVER
32#define TOXICVER "NOVER" //Use the -D flag to set this 28#define TOXICVER "NOVER" //Use the -D flag to set this
33#endif 29#endif
34 30
35static ToxWindow windows[MAX_WINDOW_SLOTS];
36static ToxWindow* prompt;
37
38int w_num;
39int active_window;
40
41/* CALLBACKS START */
42void on_request(uint8_t *public_key, uint8_t *data, uint16_t length, void* userdata)
43{
44 int n = add_req(public_key);
45 wprintw(prompt->window, "\nFriend request from:\n");
46
47 int i;
48 for (i = 0; i < KEY_SIZE_BYTES; ++i) {
49 wprintw(prompt->window, "%02x", public_key[i] & 0xff);
50 }
51
52 wprintw(prompt->window, "\nWith the message: %s\n", data);
53 wprintw(prompt->window, "\nUse \"accept %d\" to accept it.\n", n);
54
55 for (i = 0; i < MAX_WINDOW_SLOTS; ++i) {
56 if (windows[i].onFriendRequest != NULL)
57 windows[i].onFriendRequest(&windows[i], public_key, data, length);
58 }
59}
60
61void on_message(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void* userdata)
62{
63 int i;
64 for (i = 0; i < MAX_WINDOW_SLOTS; ++i) {
65 if (windows[i].onMessage != NULL)
66 windows[i].onMessage(&windows[i], m, friendnumber, string, length);
67 }
68}
69
70void on_action(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void* userdata)
71{
72 int i;
73 for (i = 0; i < MAX_WINDOW_SLOTS; ++i) {
74 if (windows[i].onAction != NULL)
75 windows[i].onAction(&windows[i], m, friendnumber, string, length);
76 }
77}
78
79void on_nickchange(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void* userdata)
80{
81 wprintw(prompt->window, "\n(nickchange) %d: %s\n", friendnumber, string);
82 int i;
83 for (i = 0; i < MAX_WINDOW_SLOTS; ++i) {
84 if (windows[i].onNickChange != NULL)
85 windows[i].onNickChange(&windows[i], friendnumber, string, length);
86 }
87}
88
89void on_statuschange(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void* userdata)
90{
91 wprintw(prompt->window, "\n(statuschange) %d: %s\n", friendnumber, string);
92 int i;
93 for (i=0; i<MAX_WINDOW_SLOTS; ++i) {
94 if (windows[i].onStatusChange != NULL)
95 windows[i].onStatusChange(&windows[i], friendnumber, string, length);
96 }
97}
98
99void on_friendadded(Messenger *m, int friendnumber)
100{
101 friendlist_onFriendAdded(m, friendnumber);
102}
103/* CALLBACKS END */
104
105static void init_term() 31static void init_term()
106{ 32{
107 /* Setup terminal */ 33 /* Setup terminal */
@@ -189,104 +115,7 @@ int init_connection(void)
189 return 0; 115 return 0;
190} 116}
191 117
192void init_window_status() 118static void do_tox(Messenger *m, ToxWindow * prompt)
193{
194 /* Default window values decrement from -2 */
195 int i;
196 for (i = 0; i < N_DEFAULT_WINS; ++i)
197 WINDOW_STATUS[i] = -(i+2);
198
199 int j;
200 for (j = N_DEFAULT_WINS; j < MAX_WINDOW_SLOTS; j++)
201 WINDOW_STATUS[j] = -1;
202}
203
204int add_window(Messenger *m, ToxWindow w, int n)
205{
206 if (w_num >= TOXWINDOWS_MAX_NUM)
207 return -1;
208
209 if (LINES < 2)
210 return -1;
211
212 w.window = newwin(LINES - 2, COLS, 0, 0);
213 if (w.window == NULL)
214 return -1;
215
216 windows[n] = w;
217 w.onInit(&w, m);
218 w_num++;
219 active_window = n;
220 return n;
221}
222
223/* Deletes window w and cleans up */
224void del_window(ToxWindow *w, int f_num)
225{
226 active_window = 0; // Go to prompt screen
227 delwin(w->window);
228 int i;
229 for (i = N_DEFAULT_WINS; i < MAX_WINDOW_SLOTS; ++i) {
230 if (WINDOW_STATUS[i] == f_num) {
231 WINDOW_STATUS[i] = -1;
232 disable_chatwin(f_num);
233 break;
234 }
235 }
236 clear();
237 refresh();
238}
239
240/* Shows next window when tab or back-tab is pressed */
241void set_active_window(int ch)
242{
243 int f_inf = 0;
244 int max = MAX_WINDOW_SLOTS-1;
245 if (ch == '\t') {
246 int i = (active_window + 1) % max;
247 while (true) {
248 if (WINDOW_STATUS[i] != -1) {
249 active_window = i;
250 return;
251 }
252 i = (i + 1) % max;
253 if (f_inf++ > max) { // infinite loop check
254 endwin();
255 exit(2);
256 }
257 }
258 }else {
259 int i = active_window - 1;
260 if (i < 0) i = max;
261 while (true) {
262 if (WINDOW_STATUS[i] != -1) {
263 active_window = i;
264 return;
265 }
266 if (--i < 0) i = max;
267 if (f_inf++ > max) {
268 endwin();
269 exit(2);
270 }
271 }
272 }
273}
274
275static void init_windows(Messenger *m)
276{
277 w_num = 0;
278 int n_prompt = 0;
279 int n_friendslist = 1;
280 if (add_window(m, new_prompt(on_friendadded), n_prompt) == -1
281 || add_window(m, new_friendlist(del_window, set_active_window, add_window, WINDOW_STATUS), n_friendslist) == -1) {
282 fprintf(stderr, "add_window() failed.\n");
283 endwin();
284 exit(1);
285 }
286 prompt = &windows[n_prompt];
287}
288
289static void do_tox(Messenger *m)
290{ 119{
291 static int conn_try = 0; 120 static int conn_try = 0;
292 static int conn_err = 0; 121 static int conn_err = 0;
@@ -367,54 +196,8 @@ static void load_data(Messenger *m, char *path)
367 fclose(fd); 196 fclose(fd);
368} 197}
369 198
370static void draw_bar()
371{
372 static int odd = 0;
373 int blinkrate = 30;
374
375 attron(COLOR_PAIR(4));
376 mvhline(LINES - 2, 0, '_', COLS);
377 attroff(COLOR_PAIR(4));
378
379 move(LINES - 1, 0);
380
381 attron(COLOR_PAIR(4) | A_BOLD);
382 printw(" TOXIC " TOXICVER "|");
383 attroff(COLOR_PAIR(4) | A_BOLD);
384
385 int i;
386 for (i = 0; i < (MAX_WINDOW_SLOTS); ++i) {
387 if (WINDOW_STATUS[i] != -1) {
388 if (i == active_window)
389 attron(A_BOLD);
390
391 odd = (odd+1) % blinkrate;
392 if (windows[i].blink && (odd < (blinkrate/2)))
393 attron(COLOR_PAIR(3));
394
395 printw(" %s", windows[i].title);
396 if (windows[i].blink && (odd < (blinkrate/2)))
397 attroff(COLOR_PAIR(3));
398
399 if (i == active_window) {
400 attroff(A_BOLD);
401 }
402 }
403 }
404 refresh();
405}
406
407void prepare_window(WINDOW *w)
408{
409 mvwin(w, 0, 0);
410 wresize(w, LINES-2, COLS);
411}
412
413
414int main(int argc, char *argv[]) 199int main(int argc, char *argv[])
415{ 200{
416 int ch;
417 ToxWindow* a;
418 char *user_config_dir = get_user_config_dir(); 201 char *user_config_dir = get_user_config_dir();
419 char *DATA_FILE; 202 char *DATA_FILE;
420 int config_err = create_user_config_dir(user_config_dir); 203 int config_err = create_user_config_dir(user_config_dir);
@@ -449,7 +232,7 @@ int main(int argc, char *argv[])
449 232
450 init_term(); 233 init_term();
451 Messenger *m = init_tox(); 234 Messenger *m = init_tox();
452 init_windows(m); 235 ToxWindow * prompt = init_windows(m);
453 init_window_status(); 236 init_window_status();
454 237
455 if(f_loadfromfile) 238 if(f_loadfromfile)
@@ -471,21 +254,10 @@ int main(int argc, char *argv[])
471 } 254 }
472 while(true) { 255 while(true) {
473 /* Update tox */ 256 /* Update tox */
474 do_tox(m); 257 do_tox(m, prompt);
475 258
476 /* Draw */ 259 /* Draw */
477 a = &windows[active_window]; 260 draw_active_window(m);
478 prepare_window(a->window);
479 a->blink = false;
480 draw_bar();
481 a->onDraw(a);
482
483 /* Handle input */
484 ch = getch();
485 if (ch == '\t' || ch == KEY_BTAB)
486 set_active_window(ch);
487 else if (ch != ERR)
488 a->onKey(a, m, ch);
489 } 261 }
490 cleanupMessenger(m); 262 cleanupMessenger(m);
491 return 0; 263 return 0;
diff --git a/testing/toxic/windows.c b/testing/toxic/windows.c
new file mode 100644
index 00000000..c3952432
--- /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(del_window, set_active_window, add_window, 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 4eb55a99..613fa3b3 100644
--- a/testing/toxic/windows.h
+++ b/testing/toxic/windows.h
@@ -38,5 +38,13 @@ struct ToxWindow_ {
38 WINDOW* window; 38 WINDOW* window;
39}; 39};
40 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);
41#endif 49#endif
42 50