diff options
Diffstat (limited to 'testing/toxic')
-rw-r--r-- | testing/toxic/chat.c | 13 | ||||
-rw-r--r-- | testing/toxic/friendlist.c | 40 | ||||
-rw-r--r-- | testing/toxic/main.c | 126 | ||||
-rw-r--r-- | testing/toxic/prompt.c | 5 | ||||
-rw-r--r-- | testing/toxic/windows.h | 8 |
5 files changed, 138 insertions, 54 deletions
diff --git a/testing/toxic/chat.c b/testing/toxic/chat.c index ff7a1667..2c1f1072 100644 --- a/testing/toxic/chat.c +++ b/testing/toxic/chat.c | |||
@@ -25,6 +25,8 @@ typedef struct { | |||
25 | 25 | ||
26 | } ChatContext; | 26 | } ChatContext; |
27 | 27 | ||
28 | extern void del_window(ToxWindow *w, int f_num); | ||
29 | extern int focus_window(int num); | ||
28 | extern void fix_name(uint8_t* name); | 30 | extern void fix_name(uint8_t* name); |
29 | void print_help(ChatContext* self); | 31 | void print_help(ChatContext* self); |
30 | void execute(ToxWindow* self, ChatContext* ctx, char* cmd); | 32 | void execute(ToxWindow* self, ChatContext* ctx, char* cmd); |
@@ -50,7 +52,7 @@ static void chat_onMessage(ToxWindow* self, int num, uint8_t* msg, uint16_t len) | |||
50 | fix_name(nick); | 52 | fix_name(nick); |
51 | 53 | ||
52 | wattron(ctx->history, COLOR_PAIR(2)); | 54 | wattron(ctx->history, COLOR_PAIR(2)); |
53 | wprintw(ctx->history, "%02d:%02d:%02d ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); | 55 | wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); |
54 | wattroff(ctx->history, COLOR_PAIR(2)); | 56 | wattroff(ctx->history, COLOR_PAIR(2)); |
55 | wattron(ctx->history, COLOR_PAIR(4)); | 57 | wattron(ctx->history, COLOR_PAIR(4)); |
56 | wprintw(ctx->history, "%s: ", nick); | 58 | wprintw(ctx->history, "%s: ", nick); |
@@ -116,7 +118,7 @@ static void chat_onKey(ToxWindow* self, int key) { | |||
116 | if(!string_is_empty(ctx->line)) { | 118 | if(!string_is_empty(ctx->line)) { |
117 | /* make sure the string has at least non-space character */ | 119 | /* make sure the string has at least non-space character */ |
118 | wattron(ctx->history, COLOR_PAIR(2)); | 120 | wattron(ctx->history, COLOR_PAIR(2)); |
119 | wprintw(ctx->history, "%02d:%02d:%02d ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); | 121 | wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); |
120 | wattroff(ctx->history, COLOR_PAIR(2)); | 122 | wattroff(ctx->history, COLOR_PAIR(2)); |
121 | wattron(ctx->history, COLOR_PAIR(1)); | 123 | wattron(ctx->history, COLOR_PAIR(1)); |
122 | wprintw(ctx->history, "you: ", ctx->line); | 124 | wprintw(ctx->history, "you: ", ctx->line); |
@@ -185,6 +187,12 @@ void execute(ToxWindow* self, ChatContext* ctx, char* cmd) | |||
185 | } | 187 | } |
186 | wprintw(ctx->history, "Your ID: %s\n", id); | 188 | wprintw(ctx->history, "Your ID: %s\n", id); |
187 | } | 189 | } |
190 | else if (strcmp(ctx->line, "/close") == 0) { | ||
191 | focus_window(0); // Go to prompt screen | ||
192 | int f_num = ctx->friendnum; | ||
193 | delwin(ctx->linewin); | ||
194 | del_window(self, f_num); | ||
195 | } | ||
188 | else | 196 | else |
189 | wprintw(ctx->history, "Invalid command.\n"); | 197 | wprintw(ctx->history, "Invalid command.\n"); |
190 | } | 198 | } |
@@ -228,6 +236,7 @@ void print_help(ChatContext* self) { | |||
228 | wprintw(self->history, " /nick <nickname> : Set your nickname\n"); | 236 | wprintw(self->history, " /nick <nickname> : Set your nickname\n"); |
229 | wprintw(self->history, " /myid : Print your ID\n"); | 237 | wprintw(self->history, " /myid : Print your ID\n"); |
230 | wprintw(self->history, " /clear : Clear the screen\n"); | 238 | wprintw(self->history, " /clear : Clear the screen\n"); |
239 | wprintw(self->history, " /close : Closes the current chat window\n"); | ||
231 | wprintw(self->history, " /quit or /exit : Exit program\n"); | 240 | wprintw(self->history, " /quit or /exit : Exit program\n"); |
232 | wprintw(self->history, " /help : Print this message again\n\n"); | 241 | wprintw(self->history, " /help : Print this message again\n\n"); |
233 | 242 | ||
diff --git a/testing/toxic/friendlist.c b/testing/toxic/friendlist.c index b4b619a2..2d060ae0 100644 --- a/testing/toxic/friendlist.c +++ b/testing/toxic/friendlist.c | |||
@@ -12,12 +12,11 @@ | |||
12 | 12 | ||
13 | #include "windows.h" | 13 | #include "windows.h" |
14 | 14 | ||
15 | extern char WINDOW_STATUS[TOXWINDOWS_MAX_NUM]; | ||
15 | extern int add_window(ToxWindow w); | 16 | extern int add_window(ToxWindow w); |
16 | extern int focus_window(int num); | 17 | extern int focus_window(int num); |
17 | extern ToxWindow new_chat(int friendnum); | 18 | extern ToxWindow new_chat(int friendnum); |
18 | 19 | ||
19 | #define MAX_FRIENDS_NUM 100 | ||
20 | |||
21 | typedef struct { | 20 | typedef struct { |
22 | uint8_t name[MAX_NAME_LENGTH]; | 21 | uint8_t name[MAX_NAME_LENGTH]; |
23 | uint8_t status[MAX_USERSTATUS_LENGTH]; | 22 | uint8_t status[MAX_USERSTATUS_LENGTH]; |
@@ -86,14 +85,11 @@ int friendlist_onFriendAdded(int num) { | |||
86 | getname(num, friends[num_friends].name); | 85 | getname(num, friends[num_friends].name); |
87 | strcpy((char*) friends[num_friends].name, "unknown"); | 86 | strcpy((char*) friends[num_friends].name, "unknown"); |
88 | strcpy((char*) friends[num_friends].status, "unknown"); | 87 | strcpy((char*) friends[num_friends].status, "unknown"); |
89 | friends[num_friends].chatwin = -1; | 88 | friends[num_friends++].chatwin = -1; |
90 | |||
91 | num_friends++; | ||
92 | return 0; | 89 | return 0; |
93 | } | 90 | } |
94 | 91 | ||
95 | static void friendlist_onKey(ToxWindow* self, int key) { | 92 | static void friendlist_onKey(ToxWindow* self, int key) { |
96 | |||
97 | if(key == KEY_UP) { | 93 | if(key == KEY_UP) { |
98 | if(num_selected != 0) | 94 | if(num_selected != 0) |
99 | num_selected--; | 95 | num_selected--; |
@@ -103,12 +99,26 @@ static void friendlist_onKey(ToxWindow* self, int key) { | |||
103 | num_selected = (num_selected+1) % num_friends; | 99 | num_selected = (num_selected+1) % num_friends; |
104 | } | 100 | } |
105 | else if(key == '\n') { | 101 | else if(key == '\n') { |
106 | 102 | /* Jump to chat window if already open */ | |
107 | if(friends[num_selected].chatwin != -1) | 103 | if (friends[num_selected].chatwin != -1) { |
108 | return; | 104 | int i; |
109 | 105 | for (i = N_DEFAULT_WINS; i < MAX_WINDOW_SLOTS; i++) { | |
110 | friends[num_selected].chatwin = add_window(new_chat(num_selected)); | 106 | if (WINDOW_STATUS[i] == num_selected) { |
111 | focus_window(friends[num_selected].chatwin); | 107 | focus_window(i); |
108 | break; | ||
109 | } | ||
110 | } | ||
111 | }else { | ||
112 | friends[num_selected].chatwin = add_window(new_chat(num_selected)); | ||
113 | focus_window(friends[num_selected].chatwin); | ||
114 | int i; | ||
115 | for (i = N_DEFAULT_WINS; i < MAX_WINDOW_SLOTS; i++) { // Find open slot | ||
116 | if (WINDOW_STATUS[i] == -1) { | ||
117 | WINDOW_STATUS[i] = num_selected; | ||
118 | break; | ||
119 | } | ||
120 | } | ||
121 | } | ||
112 | } | 122 | } |
113 | } | 123 | } |
114 | 124 | ||
@@ -145,6 +155,10 @@ static void friendlist_onDraw(ToxWindow* self) { | |||
145 | wrefresh(self->window); | 155 | wrefresh(self->window); |
146 | } | 156 | } |
147 | 157 | ||
158 | void disable_chatwin(int f_num) { | ||
159 | friends[f_num].chatwin = -1; | ||
160 | } | ||
161 | |||
148 | static void friendlist_onInit(ToxWindow* self) { | 162 | static void friendlist_onInit(ToxWindow* self) { |
149 | 163 | ||
150 | } | 164 | } |
@@ -164,4 +178,4 @@ ToxWindow new_friendlist() { | |||
164 | strcpy(ret.title, "[friends]"); | 178 | strcpy(ret.title, "[friends]"); |
165 | 179 | ||
166 | return ret; | 180 | return ret; |
167 | } | 181 | } \ No newline at end of file |
diff --git a/testing/toxic/main.c b/testing/toxic/main.c index 45760b1c..42657e88 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,6 +299,42 @@ 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; | 340 | int i = 0; |
@@ -312,8 +367,7 @@ int main(int argc, char* argv[]) { | |||
312 | "defaulting to 'data' for a keyfile...\n"); | 367 | "defaulting to 'data' for a keyfile...\n"); |
313 | attroff(COLOR_PAIR(3) | A_BOLD); | 368 | attroff(COLOR_PAIR(3) | A_BOLD); |
314 | } | 369 | } |
315 | 370 | ||
316 | |||
317 | while(true) { | 371 | while(true) { |
318 | // Update tox. | 372 | // Update tox. |
319 | do_tox(); | 373 | do_tox(); |
@@ -327,18 +381,14 @@ int main(int argc, char* argv[]) { | |||
327 | 381 | ||
328 | // Handle input. | 382 | // Handle input. |
329 | ch = getch(); | 383 | ch = getch(); |
330 | if(ch == '\t') { | 384 | if(ch == '\t' || ch == KEY_BTAB) |
331 | w_active = (w_active + 1) % w_num; | 385 | set_active_window(ch); |
332 | } | 386 | else if(ch != ERR) { |
333 | else if(ch == KEY_BTAB) { | 387 | a->onKey(a, ch); |
334 | w_active = (w_active + w_num - 1) % w_num; | ||
335 | } | 388 | } |
336 | else if(ch != ERR) { | 389 | else if(ch != ERR) { |
337 | a->onKey(a, ch); | 390 | a->onKey(a, ch); |
338 | } | 391 | } |
339 | |||
340 | } | 392 | } |
341 | |||
342 | return 0; | 393 | return 0; |
343 | } | 394 | } |
344 | |||
diff --git a/testing/toxic/prompt.c b/testing/toxic/prompt.c index 463b9352..d40fb2a6 100644 --- a/testing/toxic/prompt.c +++ b/testing/toxic/prompt.c | |||
@@ -254,7 +254,9 @@ static void execute(ToxWindow* self, char* cmd) { | |||
254 | wprintw(self->window, "Message successfully sent.\n"); | 254 | wprintw(self->window, "Message successfully sent.\n"); |
255 | } | 255 | } |
256 | } | 256 | } |
257 | 257 | else if (!strncmp(cmd, "clear", strlen("clear"))) { | |
258 | wclear(self->window); | ||
259 | } | ||
258 | else { | 260 | else { |
259 | wprintw(self->window, "Invalid command.\n"); | 261 | wprintw(self->window, "Invalid command.\n"); |
260 | } | 262 | } |
@@ -314,6 +316,7 @@ static void print_usage(ToxWindow* self) { | |||
314 | wprintw(self->window, " nick <nickname> : Set your nickname\n"); | 316 | wprintw(self->window, " nick <nickname> : Set your nickname\n"); |
315 | wprintw(self->window, " accept <number> : Accept friend request\n"); | 317 | wprintw(self->window, " accept <number> : Accept friend request\n"); |
316 | wprintw(self->window, " myid : Print your ID\n"); | 318 | wprintw(self->window, " myid : Print your ID\n"); |
319 | wprintw(self->window, " clear : Clear the screen\n"); | ||
317 | wprintw(self->window, " quit/exit : Exit program\n"); | 320 | wprintw(self->window, " quit/exit : Exit program\n"); |
318 | wprintw(self->window, " help : Print this message again\n"); | 321 | wprintw(self->window, " help : Print this message again\n"); |
319 | 322 | ||
diff --git a/testing/toxic/windows.h b/testing/toxic/windows.h index dde1430f..cb45614d 100644 --- a/testing/toxic/windows.h +++ b/testing/toxic/windows.h | |||
@@ -3,6 +3,14 @@ | |||
3 | */ | 3 | */ |
4 | 4 | ||
5 | #include <stdbool.h> | 5 | #include <stdbool.h> |
6 | #define TOXWINDOWS_MAX_NUM 32 | ||
7 | #define MAX_FRIENDS_NUM 100 | ||
8 | |||
9 | /* number of permanent default windows */ | ||
10 | #define N_DEFAULT_WINS 2 | ||
11 | |||
12 | /* maximum window slots for WINDOW_STATUS array */ | ||
13 | #define MAX_WINDOW_SLOTS N_DEFAULT_WINS+MAX_FRIENDS_NUM | ||
6 | 14 | ||
7 | typedef struct ToxWindow_ ToxWindow; | 15 | typedef struct ToxWindow_ ToxWindow; |
8 | 16 | ||