summaryrefslogtreecommitdiff
path: root/testing/toxic/windows.c
diff options
context:
space:
mode:
Diffstat (limited to 'testing/toxic/windows.c')
-rw-r--r--testing/toxic/windows.c235
1 files changed, 235 insertions, 0 deletions
diff --git a/testing/toxic/windows.c b/testing/toxic/windows.c
new file mode 100644
index 00000000..d19392ac
--- /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(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}