diff options
Diffstat (limited to 'testing/toxic/windows.c')
-rw-r--r-- | testing/toxic/windows.c | 171 |
1 files changed, 68 insertions, 103 deletions
diff --git a/testing/toxic/windows.c b/testing/toxic/windows.c index 11396e2c..a2c649d5 100644 --- a/testing/toxic/windows.c +++ b/testing/toxic/windows.c | |||
@@ -6,15 +6,10 @@ | |||
6 | extern char *DATA_FILE; | 6 | extern char *DATA_FILE; |
7 | extern int store_data(Messenger *m, char *path); | 7 | extern int store_data(Messenger *m, char *path); |
8 | 8 | ||
9 | /* Holds status of chat windows */ | 9 | static ToxWindow windows[MAX_WINDOWS_NUM]; |
10 | char WINDOW_STATUS[MAX_WINDOW_SLOTS]; | 10 | static ToxWindow *active_window; |
11 | |||
12 | static int w_num; | ||
13 | static ToxWindow windows[MAX_WINDOW_SLOTS]; | ||
14 | static Messenger *m; | ||
15 | int active_window; | ||
16 | |||
17 | static ToxWindow *prompt; | 11 | static ToxWindow *prompt; |
12 | static Messenger *m; | ||
18 | 13 | ||
19 | /* CALLBACKS START */ | 14 | /* CALLBACKS START */ |
20 | void on_request(uint8_t *public_key, uint8_t *data, uint16_t length, void *userdata) | 15 | void on_request(uint8_t *public_key, uint8_t *data, uint16_t length, void *userdata) |
@@ -31,7 +26,7 @@ void on_request(uint8_t *public_key, uint8_t *data, uint16_t length, void *userd | |||
31 | wprintw(prompt->window, "\nWith the message: %s\n", data); | 26 | wprintw(prompt->window, "\nWith the message: %s\n", data); |
32 | wprintw(prompt->window, "\nUse \"accept %d\" to accept it.\n", n); | 27 | wprintw(prompt->window, "\nUse \"accept %d\" to accept it.\n", n); |
33 | 28 | ||
34 | for (i = 0; i < MAX_WINDOW_SLOTS; ++i) { | 29 | for (i = 0; i < MAX_WINDOWS_NUM; ++i) { |
35 | if (windows[i].onFriendRequest != NULL) | 30 | if (windows[i].onFriendRequest != NULL) |
36 | windows[i].onFriendRequest(&windows[i], public_key, data, length); | 31 | windows[i].onFriendRequest(&windows[i], public_key, data, length); |
37 | } | 32 | } |
@@ -41,7 +36,7 @@ void on_message(Messenger *m, int friendnumber, uint8_t *string, uint16_t length | |||
41 | { | 36 | { |
42 | int i; | 37 | int i; |
43 | 38 | ||
44 | for (i = 0; i < MAX_WINDOW_SLOTS; ++i) { | 39 | for (i = 0; i < MAX_WINDOWS_NUM; ++i) { |
45 | if (windows[i].onMessage != NULL) | 40 | if (windows[i].onMessage != NULL) |
46 | windows[i].onMessage(&windows[i], m, friendnumber, string, length); | 41 | windows[i].onMessage(&windows[i], m, friendnumber, string, length); |
47 | } | 42 | } |
@@ -51,7 +46,7 @@ void on_action(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, | |||
51 | { | 46 | { |
52 | int i; | 47 | int i; |
53 | 48 | ||
54 | for (i = 0; i < MAX_WINDOW_SLOTS; ++i) { | 49 | for (i = 0; i < MAX_WINDOWS_NUM; ++i) { |
55 | if (windows[i].onAction != NULL) | 50 | if (windows[i].onAction != NULL) |
56 | windows[i].onAction(&windows[i], m, friendnumber, string, length); | 51 | windows[i].onAction(&windows[i], m, friendnumber, string, length); |
57 | } | 52 | } |
@@ -62,7 +57,7 @@ void on_nickchange(Messenger *m, int friendnumber, uint8_t *string, uint16_t len | |||
62 | wprintw(prompt->window, "\n(nickchange) %d: %s\n", friendnumber, string); | 57 | wprintw(prompt->window, "\n(nickchange) %d: %s\n", friendnumber, string); |
63 | int i; | 58 | int i; |
64 | 59 | ||
65 | for (i = 0; i < MAX_WINDOW_SLOTS; ++i) { | 60 | for (i = 0; i < MAX_WINDOWS_NUM; ++i) { |
66 | if (windows[i].onNickChange != NULL) | 61 | if (windows[i].onNickChange != NULL) |
67 | windows[i].onNickChange(&windows[i], friendnumber, string, length); | 62 | windows[i].onNickChange(&windows[i], friendnumber, string, length); |
68 | } | 63 | } |
@@ -73,7 +68,7 @@ void on_statuschange(Messenger *m, int friendnumber, uint8_t *string, uint16_t l | |||
73 | wprintw(prompt->window, "\n(statuschange) %d: %s\n", friendnumber, string); | 68 | wprintw(prompt->window, "\n(statuschange) %d: %s\n", friendnumber, string); |
74 | int i; | 69 | int i; |
75 | 70 | ||
76 | for (i = 0; i < MAX_WINDOW_SLOTS; ++i) { | 71 | for (i = 0; i < MAX_WINDOWS_NUM; ++i) { |
77 | if (windows[i].onStatusChange != NULL) | 72 | if (windows[i].onStatusChange != NULL) |
78 | windows[i].onStatusChange(&windows[i], friendnumber, string, length); | 73 | windows[i].onStatusChange(&windows[i], friendnumber, string, length); |
79 | } | 74 | } |
@@ -89,119 +84,89 @@ void on_friendadded(Messenger *m, int friendnumber) | |||
89 | } | 84 | } |
90 | /* CALLBACKS END */ | 85 | /* CALLBACKS END */ |
91 | 86 | ||
92 | int add_window(Messenger *m, ToxWindow w, int n) | 87 | int add_window(Messenger *m, ToxWindow w) |
93 | { | 88 | { |
94 | if (w_num >= TOXWINDOWS_MAX_NUM) | ||
95 | return -1; | ||
96 | |||
97 | if (LINES < 2) | 89 | if (LINES < 2) |
98 | return -1; | 90 | return -1; |
99 | 91 | ||
100 | w.window = newwin(LINES - 2, COLS, 0, 0); | 92 | int i; |
101 | 93 | for(i = 0; i < MAX_WINDOWS_NUM; i++) { | |
102 | if (w.window == NULL) | 94 | if (windows[i].window) |
103 | return -1; | 95 | continue; |
104 | 96 | ||
105 | windows[n] = w; | 97 | w.window = newwin(LINES - 2, COLS, 0, 0); |
106 | w.onInit(&w, m); | 98 | if (w.window == NULL) |
107 | w_num++; | 99 | return -1; |
108 | active_window = n; | 100 | |
109 | return n; | 101 | windows[i] = w; |
102 | w.onInit(&w, m); | ||
103 | |||
104 | active_window = windows+i; | ||
105 | return i; | ||
106 | } | ||
107 | |||
108 | return -1; | ||
110 | } | 109 | } |
111 | 110 | ||
112 | /* Deletes window w and cleans up */ | 111 | /* Deletes window w and cleans up */ |
113 | void del_window(ToxWindow *w, int f_num) | 112 | void del_window(ToxWindow *w) |
114 | { | 113 | { |
115 | active_window = 0; // Go to prompt screen | 114 | active_window = windows; // Go to prompt screen |
116 | delwin(w->window); | 115 | delwin(w->window); |
117 | int i; | 116 | if (w->x) |
118 | 117 | free(w->x); | |
119 | for (i = N_DEFAULT_WINS; i < MAX_WINDOW_SLOTS; ++i) { | 118 | w->window = NULL; |
120 | if (WINDOW_STATUS[i] == f_num) { | 119 | memset(w, 0, sizeof(ToxWindow)); |
121 | WINDOW_STATUS[i] = -1; | ||
122 | disable_chatwin(f_num); | ||
123 | break; | ||
124 | } | ||
125 | } | ||
126 | |||
127 | clear(); | 120 | clear(); |
128 | refresh(); | 121 | refresh(); |
129 | } | 122 | } |
130 | 123 | ||
131 | /* Shows next window when tab or back-tab is pressed */ | 124 | /* Shows next window when tab or back-tab is pressed */ |
132 | void set_active_window(int ch) | 125 | void set_next_window(int ch) |
133 | { | 126 | { |
134 | int f_inf = 0; | 127 | ToxWindow *end = windows+MAX_WINDOWS_NUM-1; |
135 | int max = MAX_WINDOW_SLOTS - 1; | 128 | ToxWindow *inf = active_window; |
136 | 129 | while(true) { | |
137 | if (ch == '\t') { | 130 | if (ch == '\t') { |
138 | int i = (active_window + 1) % max; | 131 | if (++active_window > end) |
139 | 132 | active_window = windows; | |
140 | while (true) { | 133 | } else |
141 | if (WINDOW_STATUS[i] != -1) { | 134 | if (--active_window < windows) |
142 | active_window = i; | 135 | active_window = end; |
143 | return; | 136 | |
144 | } | 137 | if (active_window->window) |
145 | 138 | return; | |
146 | i = (i + 1) % max; | 139 | |
147 | 140 | if (active_window == inf) { // infinite loop check | |
148 | if (f_inf++ > max) { // infinite loop check | 141 | endwin(); |
149 | endwin(); | 142 | exit(2); |
150 | exit(2); | ||
151 | } | ||
152 | } | ||
153 | } else { | ||
154 | int i = active_window - 1; | ||
155 | |||
156 | if (i < 0) i = max; | ||
157 | |||
158 | while (true) { | ||
159 | if (WINDOW_STATUS[i] != -1) { | ||
160 | active_window = i; | ||
161 | return; | ||
162 | } | ||
163 | |||
164 | if (--i < 0) i = max; | ||
165 | |||
166 | if (f_inf++ > max) { | ||
167 | endwin(); | ||
168 | exit(2); | ||
169 | } | ||
170 | } | 143 | } |
171 | } | 144 | } |
172 | } | 145 | } |
173 | 146 | ||
174 | void init_window_status() | 147 | void set_active_window(int index) |
175 | { | 148 | { |
176 | /* Default window values decrement from -2 */ | 149 | if (index < 0 || index >= MAX_WINDOWS_NUM) |
177 | int i; | 150 | return; |
178 | 151 | ||
179 | for (i = 0; i < N_DEFAULT_WINS; ++i) | 152 | active_window = windows+index; |
180 | WINDOW_STATUS[i] = -(i + 2); | ||
181 | |||
182 | int j; | ||
183 | |||
184 | for (j = N_DEFAULT_WINS; j < MAX_WINDOW_SLOTS; j++) | ||
185 | WINDOW_STATUS[j] = -1; | ||
186 | } | 153 | } |
187 | 154 | ||
188 | ToxWindow *init_windows() | 155 | ToxWindow *init_windows() |
189 | { | 156 | { |
190 | w_num = 0; | 157 | int n_prompt = add_window(m, new_prompt()); |
191 | int n_prompt = 0; | 158 | |
192 | int n_friendslist = 1; | 159 | if (n_prompt == -1 |
193 | int n_dhtstatus = 2; | 160 | || add_window(m, new_friendlist()) == -1 |
194 | 161 | || add_window(m, new_dhtstatus()) == -1) { | |
195 | if (add_window(m, new_prompt(on_friendadded), n_prompt) == -1 | ||
196 | || add_window(m, new_friendlist(WINDOW_STATUS), n_friendslist) == -1 | ||
197 | || add_window(m, new_dhtstatus(), n_dhtstatus) == -1) { | ||
198 | fprintf(stderr, "add_window() failed.\n"); | 162 | fprintf(stderr, "add_window() failed.\n"); |
199 | endwin(); | 163 | endwin(); |
200 | exit(1); | 164 | exit(1); |
201 | } | 165 | } |
202 | 166 | ||
203 | active_window = n_prompt; | ||
204 | prompt = &windows[n_prompt]; | 167 | prompt = &windows[n_prompt]; |
168 | active_window = prompt; | ||
169 | |||
205 | return prompt; | 170 | return prompt; |
206 | } | 171 | } |
207 | 172 | ||
@@ -222,9 +187,9 @@ static void draw_bar() | |||
222 | 187 | ||
223 | int i; | 188 | int i; |
224 | 189 | ||
225 | for (i = 0; i < (MAX_WINDOW_SLOTS); ++i) { | 190 | for (i = 0; i < (MAX_WINDOWS_NUM); ++i) { |
226 | if (WINDOW_STATUS[i] != -1) { | 191 | if (windows[i].window) { |
227 | if (i == active_window) | 192 | if (windows+i == active_window) |
228 | attron(A_BOLD); | 193 | attron(A_BOLD); |
229 | 194 | ||
230 | odd = (odd + 1) % blinkrate; | 195 | odd = (odd + 1) % blinkrate; |
@@ -237,7 +202,7 @@ static void draw_bar() | |||
237 | if (windows[i].blink && (odd < (blinkrate / 2))) | 202 | if (windows[i].blink && (odd < (blinkrate / 2))) |
238 | attroff(COLOR_PAIR(3)); | 203 | attroff(COLOR_PAIR(3)); |
239 | 204 | ||
240 | if (i == active_window) { | 205 | if (windows+i == active_window) { |
241 | attroff(A_BOLD); | 206 | attroff(A_BOLD); |
242 | } | 207 | } |
243 | } | 208 | } |
@@ -255,7 +220,7 @@ void prepare_window(WINDOW *w) | |||
255 | void draw_active_window(Messenger *m) | 220 | void draw_active_window(Messenger *m) |
256 | { | 221 | { |
257 | 222 | ||
258 | ToxWindow *a = &windows[active_window]; | 223 | ToxWindow *a = active_window; |
259 | prepare_window(a->window); | 224 | prepare_window(a->window); |
260 | a->blink = false; | 225 | a->blink = false; |
261 | draw_bar(); | 226 | draw_bar(); |
@@ -265,7 +230,7 @@ void draw_active_window(Messenger *m) | |||
265 | int ch = getch(); | 230 | int ch = getch(); |
266 | 231 | ||
267 | if (ch == '\t' || ch == KEY_BTAB) | 232 | if (ch == '\t' || ch == KEY_BTAB) |
268 | set_active_window(ch); | 233 | set_next_window(ch); |
269 | else if (ch != ERR) | 234 | else if (ch != ERR) |
270 | a->onKey(a, m, ch); | 235 | a->onKey(a, m, ch); |
271 | } | 236 | } |