summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJfreegman <Jfreegman@gmail.com>2013-08-05 01:57:29 -0400
committerJfreegman <Jfreegman@gmail.com>2013-08-05 01:57:29 -0400
commit3bc7f15fe5a306ced566e1258ad4f9c4f89cd463 (patch)
treebd09821bd972552227d0d05bd1db0a760433b163
parentb9f387b3b7c5f8eff64b6ad8213f24f04ce58967 (diff)
added ability to close and reopen chat windows & other minor changes
-rw-r--r--testing/toxic/chat.c15
-rw-r--r--testing/toxic/friendlist.c40
-rw-r--r--testing/toxic/main.c161
-rw-r--r--testing/toxic/prompt.c5
-rw-r--r--testing/toxic/windows.h8
5 files changed, 152 insertions, 77 deletions
diff --git a/testing/toxic/chat.c b/testing/toxic/chat.c
index 936eb868..c11a7f12 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
28extern void del_window(ToxWindow *w, int f_num);
29extern int focus_window(int num);
28extern void fix_name(uint8_t* name); 30extern void fix_name(uint8_t* name);
29void print_help(ChatContext* self); 31void print_help(ChatContext* self);
30void execute(ToxWindow* self, ChatContext* ctx, char* cmd); 32void 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}
@@ -256,6 +264,5 @@ ToxWindow new_chat(int friendnum) {
256 x->friendnum = friendnum; 264 x->friendnum = friendnum;
257 265
258 ret.x = (void*) x; 266 ret.x = (void*) x;
259 free(x);
260 return ret; 267 return ret;
261} 268} \ No newline at end of file
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
15extern char WINDOW_STATUS[TOXWINDOWS_MAX_NUM];
15extern int add_window(ToxWindow w); 16extern int add_window(ToxWindow w);
16extern int focus_window(int num); 17extern int focus_window(int num);
17extern ToxWindow new_chat(int friendnum); 18extern ToxWindow new_chat(int friendnum);
18 19
19#define MAX_FRIENDS_NUM 100
20
21typedef struct { 20typedef 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
95static void friendlist_onKey(ToxWindow* self, int key) { 92static 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
158void disable_chatwin(int f_num) {
159 friends[f_num].chatwin = -1;
160}
161
148static void friendlist_onInit(ToxWindow* self) { 162static 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 e0e23708..e55cb85d 100644
--- a/testing/toxic/main.c
+++ b/testing/toxic/main.c
@@ -17,13 +17,12 @@ extern ToxWindow new_prompt();
17extern ToxWindow new_friendlist(); 17extern ToxWindow new_friendlist();
18 18
19extern int friendlist_onFriendAdded(int num); 19extern int friendlist_onFriendAdded(int num);
20 20extern void disable_chatwin(int f_num);
21extern int add_req(uint8_t* public_key); // XXX 21extern int add_req(uint8_t* public_key); // XXX
22 22
23#define TOXWINDOWS_MAX_NUM 32 23char WINDOW_STATUS[MAX_WINDOW_SLOTS]; // Holds status of chat windows
24 24static ToxWindow windows[MAX_WINDOW_SLOTS];
25static ToxWindow windows[TOXWINDOWS_MAX_NUM]; 25int w_num;
26static int w_num;
27static int w_active; 26static int w_active;
28static ToxWindow* prompt; 27static 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
117void 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
118int add_window(ToxWindow w) { 127int 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 */
146void 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
136int focus_window(int num) { 160int 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
144static void init_windows() { 168static 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
239static void draw_bar() { 261static 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 */
303void 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
283int main(int argc, char* argv[]) { 338int 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
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
7typedef struct ToxWindow_ ToxWindow; 15typedef struct ToxWindow_ ToxWindow;
8 16