diff options
Diffstat (limited to 'testing/toxic/main.c')
-rw-r--r-- | testing/toxic/main.c | 161 |
1 files changed, 102 insertions, 59 deletions
diff --git a/testing/toxic/main.c b/testing/toxic/main.c index e0e23708..e55cb85d 100644 --- a/testing/toxic/main.c +++ b/testing/toxic/main.c | |||
@@ -17,13 +17,12 @@ extern ToxWindow new_prompt(); | |||
17 | extern ToxWindow new_friendlist(); | 17 | extern ToxWindow new_friendlist(); |
18 | 18 | ||
19 | extern int friendlist_onFriendAdded(int num); | 19 | extern int friendlist_onFriendAdded(int num); |
20 | 20 | extern void disable_chatwin(int f_num); | |
21 | extern int add_req(uint8_t* public_key); // XXX | 21 | extern int add_req(uint8_t* public_key); // XXX |
22 | 22 | ||
23 | #define TOXWINDOWS_MAX_NUM 32 | 23 | char WINDOW_STATUS[MAX_WINDOW_SLOTS]; // Holds status of chat windows |
24 | 24 | static ToxWindow windows[MAX_WINDOW_SLOTS]; | |
25 | static ToxWindow windows[TOXWINDOWS_MAX_NUM]; | 25 | int w_num; |
26 | static int w_num; | ||
27 | static int w_active; | 26 | static int w_active; |
28 | static ToxWindow* prompt; | 27 | static ToxWindow* prompt; |
29 | 28 | ||
@@ -115,6 +114,16 @@ static void init_tox() { | |||
115 | m_callback_userstatus(on_statuschange); | 114 | m_callback_userstatus(on_statuschange); |
116 | } | 115 | } |
117 | 116 | ||
117 | void init_window_status() { | ||
118 | int i; | ||
119 | for (i = 0; i < N_DEFAULT_WINS; i++) | ||
120 | WINDOW_STATUS[i] = i; | ||
121 | |||
122 | int j; | ||
123 | for (j = N_DEFAULT_WINS; j < MAX_WINDOW_SLOTS; j++) | ||
124 | WINDOW_STATUS[j] = -1; | ||
125 | } | ||
126 | |||
118 | int add_window(ToxWindow w) { | 127 | int add_window(ToxWindow w) { |
119 | if(w_num == TOXWINDOWS_MAX_NUM) | 128 | if(w_num == TOXWINDOWS_MAX_NUM) |
120 | return -1; | 129 | return -1; |
@@ -133,6 +142,21 @@ int add_window(ToxWindow w) { | |||
133 | return w_num - 1; | 142 | return w_num - 1; |
134 | } | 143 | } |
135 | 144 | ||
145 | /* Deletes window w and cleans up */ | ||
146 | void del_window(ToxWindow *w, int f_num) { | ||
147 | delwin(w->window); | ||
148 | int i; | ||
149 | for (i = N_DEFAULT_WINS; i < MAX_WINDOW_SLOTS; i++) { | ||
150 | if (WINDOW_STATUS[i] == f_num) { | ||
151 | WINDOW_STATUS[i] = -1; | ||
152 | disable_chatwin(f_num); | ||
153 | break; | ||
154 | } | ||
155 | } | ||
156 | clear(); | ||
157 | refresh(); | ||
158 | } | ||
159 | |||
136 | int focus_window(int num) { | 160 | int focus_window(int num) { |
137 | if(num >= w_num || num < 0) | 161 | if(num >= w_num || num < 0) |
138 | return -1; | 162 | return -1; |
@@ -143,7 +167,6 @@ int focus_window(int num) { | |||
143 | 167 | ||
144 | static void init_windows() { | 168 | static void init_windows() { |
145 | w_num = 0; | 169 | w_num = 0; |
146 | w_active = 0; | ||
147 | 170 | ||
148 | if(add_window(new_prompt()) == -1 || add_window(new_friendlist()) == -1) { | 171 | if(add_window(new_prompt()) == -1 || add_window(new_friendlist()) == -1) { |
149 | fprintf(stderr, "add_window() failed.\n"); | 172 | fprintf(stderr, "add_window() failed.\n"); |
@@ -151,7 +174,6 @@ static void init_windows() { | |||
151 | endwin(); | 174 | endwin(); |
152 | exit(1); | 175 | exit(1); |
153 | } | 176 | } |
154 | |||
155 | prompt = &windows[0]; | 177 | prompt = &windows[0]; |
156 | } | 178 | } |
157 | 179 | ||
@@ -238,7 +260,6 @@ static void load_data(char *path) { | |||
238 | 260 | ||
239 | static void draw_bar() { | 261 | static void draw_bar() { |
240 | static int odd = 0; | 262 | static int odd = 0; |
241 | size_t i; | ||
242 | 263 | ||
243 | attron(COLOR_PAIR(4)); | 264 | attron(COLOR_PAIR(4)); |
244 | mvhline(LINES - 2, 0, '_', COLS); | 265 | mvhline(LINES - 2, 0, '_', COLS); |
@@ -250,28 +271,26 @@ static void draw_bar() { | |||
250 | printw(" TOXIC 1.0 |"); | 271 | printw(" TOXIC 1.0 |"); |
251 | attroff(COLOR_PAIR(4) | A_BOLD); | 272 | attroff(COLOR_PAIR(4) | A_BOLD); |
252 | 273 | ||
253 | for(i=0; i<w_num; i++) { | 274 | int i; |
254 | if(i == w_active) { | 275 | for (i = 0; i < (MAX_WINDOW_SLOTS-1); i++) { |
255 | attron(A_BOLD); | 276 | if (WINDOW_STATUS[i] != -1) { |
256 | } | 277 | if (i == w_active) |
257 | 278 | attron(A_BOLD); | |
258 | odd = (odd+1) % 10; | 279 | |
259 | 280 | odd = (odd+1) % 10; | |
260 | if(windows[i].blink && (odd < 5)) { | 281 | if(windows[i].blink && (odd < 5)) { |
261 | attron(COLOR_PAIR(3)); | 282 | attron(COLOR_PAIR(3)); |
262 | } | 283 | } |
263 | 284 | ||
264 | printw(" %s", windows[i].title); | 285 | printw(" %s", windows[i].title); |
265 | 286 | if(windows[i].blink && (odd < 5)) { | |
266 | if(windows[i].blink && (odd < 5)) { | 287 | attron(COLOR_PAIR(3)); |
267 | attron(COLOR_PAIR(3)); | 288 | } |
268 | } | 289 | if(i == w_active) { |
269 | 290 | attroff(A_BOLD); | |
270 | if(i == w_active) { | 291 | } |
271 | attroff(A_BOLD); | ||
272 | } | 292 | } |
273 | } | 293 | } |
274 | |||
275 | refresh(); | 294 | refresh(); |
276 | } | 295 | } |
277 | 296 | ||
@@ -280,38 +299,66 @@ void prepare_window(WINDOW* w) { | |||
280 | wresize(w, LINES-2, COLS); | 299 | wresize(w, LINES-2, COLS); |
281 | } | 300 | } |
282 | 301 | ||
302 | /* Shows next window when tab or back-tab is pressed */ | ||
303 | void set_active_window(int ch) { | ||
304 | int f_inf = 0; | ||
305 | int max = MAX_WINDOW_SLOTS-1; | ||
306 | if (ch == '\t') { | ||
307 | int i = (w_active + 1) % max; | ||
308 | while (true) { | ||
309 | if (WINDOW_STATUS[i] != -1) { | ||
310 | w_active = i; | ||
311 | return; | ||
312 | } | ||
313 | i = (i + 1) % max; | ||
314 | if (f_inf++ > max) { // infinite loop check | ||
315 | endwin(); | ||
316 | clear(); | ||
317 | exit(2); | ||
318 | } | ||
319 | } | ||
320 | }else { | ||
321 | int i = w_active - 1; | ||
322 | if (i < 0) i = max; | ||
323 | while (true) { | ||
324 | if (WINDOW_STATUS[i] != -1) { | ||
325 | w_active = i; | ||
326 | return; | ||
327 | } | ||
328 | if (--i < 0) i = max; | ||
329 | if (f_inf++ > max) { | ||
330 | endwin(); | ||
331 | clear(); | ||
332 | exit(2); | ||
333 | } | ||
334 | } | ||
335 | } | ||
336 | } | ||
337 | |||
283 | int main(int argc, char* argv[]) { | 338 | int main(int argc, char* argv[]) { |
284 | int ch; | 339 | int ch; |
285 | int i = 0; | ||
286 | int f_flag = 0; | ||
287 | char *filename = "data"; | ||
288 | ToxWindow* a; | 340 | ToxWindow* a; |
289 | 341 | ||
290 | for(i = 0; i < argc; i++) { | ||
291 | if(argv[i][0] == '-') { | ||
292 | if(argv[i][1] == 'f') { | ||
293 | if(argv[i + 1] != NULL) | ||
294 | filename = argv[i + 1]; | ||
295 | else { | ||
296 | f_flag = -1; | ||
297 | } | ||
298 | } | ||
299 | } | ||
300 | } | ||
301 | |||
302 | init_term(); | 342 | init_term(); |
303 | init_tox(); | 343 | init_tox(); |
304 | load_data(filename); | 344 | init_window_status(); |
305 | init_windows(); | 345 | init_windows(); |
346 | char *filename = "data"; | ||
347 | load_data(filename); | ||
306 | 348 | ||
307 | if(f_flag == -1) { | 349 | int i; |
308 | attron(COLOR_PAIR(3) | A_BOLD); | 350 | for(i = 0; i < argc; i++) { |
309 | wprintw(prompt->window, "You passed '-f' without giving an argument!\n" | 351 | if(argv[i][0] == '-' && argv[i][1] == 'f') { |
352 | if(argv[i + 1] != NULL) | ||
353 | filename = argv[i + 1]; | ||
354 | else { | ||
355 | attron(COLOR_PAIR(3) | A_BOLD); | ||
356 | wprintw(prompt->window, "You passed '-f' without giving an argument!\n" | ||
310 | "defaulting to 'data' for a keyfile...\n"); | 357 | "defaulting to 'data' for a keyfile...\n"); |
311 | attroff(COLOR_PAIR(3) | A_BOLD); | 358 | attroff(COLOR_PAIR(3) | A_BOLD); |
359 | } | ||
360 | } | ||
312 | } | 361 | } |
313 | |||
314 | |||
315 | while(true) { | 362 | while(true) { |
316 | // Update tox. | 363 | // Update tox. |
317 | do_tox(); | 364 | do_tox(); |
@@ -325,18 +372,14 @@ int main(int argc, char* argv[]) { | |||
325 | 372 | ||
326 | // Handle input. | 373 | // Handle input. |
327 | ch = getch(); | 374 | ch = getch(); |
328 | if(ch == '\t') { | 375 | if(ch == '\t' || ch == KEY_BTAB) |
329 | w_active = (w_active + 1) % w_num; | 376 | set_active_window(ch); |
330 | } | 377 | else if(ch != ERR) { |
331 | else if(ch == KEY_BTAB) { | 378 | a->onKey(a, ch); |
332 | w_active = (w_active + w_num - 1) % w_num; | ||
333 | } | 379 | } |
334 | else if(ch != ERR) { | 380 | else if(ch != ERR) { |
335 | a->onKey(a, ch); | 381 | a->onKey(a, ch); |
336 | } | 382 | } |
337 | |||
338 | } | 383 | } |
339 | |||
340 | return 0; | 384 | return 0; |
341 | } | 385 | } \ No newline at end of file |
342 | |||