diff options
author | Aaron Lipinski <kris.lipinski@gmail.com> | 2013-08-17 20:00:19 +1200 |
---|---|---|
committer | Aaron Lipinski <kris.lipinski@gmail.com> | 2013-08-18 20:26:39 +1200 |
commit | 1e4f9802fbe9375fd465594e1dffcc93fea03293 (patch) | |
tree | 69b0b3be031e76346f1416c2d13ce4c4d27ac66a /testing/toxic | |
parent | 12564b1f12af9cbd1f0792f9d7268c6c8ce0cb17 (diff) |
remove WINDOW_STATUS, simplify adding and selecting chat windows
Diffstat (limited to 'testing/toxic')
-rw-r--r-- | testing/toxic/chat.c | 3 | ||||
-rw-r--r-- | testing/toxic/friendlist.c | 36 | ||||
-rw-r--r-- | testing/toxic/friendlist.h | 2 | ||||
-rw-r--r-- | testing/toxic/main.c | 1 | ||||
-rw-r--r-- | testing/toxic/windows.c | 157 | ||||
-rw-r--r-- | testing/toxic/windows.h | 5 |
6 files changed, 70 insertions, 134 deletions
diff --git a/testing/toxic/chat.c b/testing/toxic/chat.c index ef932cd2..67c2ed05 100644 --- a/testing/toxic/chat.c +++ b/testing/toxic/chat.c | |||
@@ -335,7 +335,8 @@ void execute(ToxWindow *self, ChatContext *ctx, Messenger *m, char *cmd) | |||
335 | else if (strcmp(ctx->line, "/close") == 0) { | 335 | else if (strcmp(ctx->line, "/close") == 0) { |
336 | int f_num = ctx->friendnum; | 336 | int f_num = ctx->friendnum; |
337 | delwin(ctx->linewin); | 337 | delwin(ctx->linewin); |
338 | del_window(self, f_num); | 338 | del_window(self); |
339 | disable_chatwin(f_num); | ||
339 | } | 340 | } |
340 | 341 | ||
341 | else | 342 | else |
diff --git a/testing/toxic/friendlist.c b/testing/toxic/friendlist.c index c85fd3ae..0a58bc54 100644 --- a/testing/toxic/friendlist.c +++ b/testing/toxic/friendlist.c | |||
@@ -13,7 +13,6 @@ | |||
13 | #include "windows.h" | 13 | #include "windows.h" |
14 | #include "friendlist.h" | 14 | #include "friendlist.h" |
15 | 15 | ||
16 | static char *WINDOW_STATUS; | ||
17 | 16 | ||
18 | typedef struct { | 17 | typedef struct { |
19 | uint8_t name[MAX_NAME_LENGTH]; | 18 | uint8_t name[MAX_NAME_LENGTH]; |
@@ -48,17 +47,7 @@ void friendlist_onMessage(ToxWindow *self, Messenger *m, int num, uint8_t *str, | |||
48 | return; | 47 | return; |
49 | 48 | ||
50 | if (friends[num].chatwin == -1) { | 49 | if (friends[num].chatwin == -1) { |
51 | friends[num].chatwin = num; | 50 | friends[num].chatwin = add_window(m, new_chat(m, num)); |
52 | int i; | ||
53 | |||
54 | /* Find first open slot to hold chat window */ | ||
55 | for (i = N_DEFAULT_WINS; i < MAX_WINDOW_SLOTS; ++i) { | ||
56 | if (WINDOW_STATUS[i] == -1) { | ||
57 | WINDOW_STATUS[i] = num; | ||
58 | add_window(m, new_chat(m, num), i); | ||
59 | break; | ||
60 | } | ||
61 | } | ||
62 | } | 51 | } |
63 | } | 52 | } |
64 | 53 | ||
@@ -106,25 +95,9 @@ static void friendlist_onKey(ToxWindow *self, Messenger *m, int key) | |||
106 | } else if (key == '\n') { | 95 | } else if (key == '\n') { |
107 | /* Jump to chat window if already open */ | 96 | /* Jump to chat window if already open */ |
108 | if (friends[num_selected].chatwin != -1) { | 97 | if (friends[num_selected].chatwin != -1) { |
109 | int i; | 98 | set_active_window(friends[num_selected].chatwin); |
110 | |||
111 | for (i = N_DEFAULT_WINS; i < MAX_WINDOW_SLOTS; ++i) { | ||
112 | if (WINDOW_STATUS[i] == num_selected) { | ||
113 | set_active_window(i); | ||
114 | break; | ||
115 | } | ||
116 | } | ||
117 | } else { | 99 | } else { |
118 | int i; | 100 | friends[num_selected].chatwin = add_window(m, new_chat(m, num_selected)); |
119 | |||
120 | for (i = N_DEFAULT_WINS; i < MAX_WINDOW_SLOTS; ++i) { | ||
121 | if (WINDOW_STATUS[i] == -1) { | ||
122 | WINDOW_STATUS[i] = num_selected; | ||
123 | friends[num_selected].chatwin = num_selected; | ||
124 | add_window(m, new_chat(m, num_selected), i); | ||
125 | break; | ||
126 | } | ||
127 | } | ||
128 | } | 101 | } |
129 | } | 102 | } |
130 | } | 103 | } |
@@ -172,9 +145,8 @@ static void friendlist_onInit(ToxWindow *self, Messenger *m) | |||
172 | 145 | ||
173 | } | 146 | } |
174 | 147 | ||
175 | ToxWindow new_friendlist(char *ws) | 148 | ToxWindow new_friendlist() |
176 | { | 149 | { |
177 | WINDOW_STATUS = ws; | ||
178 | ToxWindow ret; | 150 | ToxWindow ret; |
179 | memset(&ret, 0, sizeof(ret)); | 151 | memset(&ret, 0, sizeof(ret)); |
180 | 152 | ||
diff --git a/testing/toxic/friendlist.h b/testing/toxic/friendlist.h index f0c27f53..91b917fd 100644 --- a/testing/toxic/friendlist.h +++ b/testing/toxic/friendlist.h | |||
@@ -4,7 +4,7 @@ | |||
4 | #include "windows.h" | 4 | #include "windows.h" |
5 | #include "chat.h" | 5 | #include "chat.h" |
6 | 6 | ||
7 | ToxWindow new_friendlist(char *ws); | 7 | ToxWindow new_friendlist(); |
8 | int friendlist_onFriendAdded(Messenger *m, int num); | 8 | int friendlist_onFriendAdded(Messenger *m, int num); |
9 | void disable_chatwin(int f_num); | 9 | void disable_chatwin(int f_num); |
10 | void fix_name(uint8_t *name); | 10 | void fix_name(uint8_t *name); |
diff --git a/testing/toxic/main.c b/testing/toxic/main.c index 816873b3..4562f394 100644 --- a/testing/toxic/main.c +++ b/testing/toxic/main.c | |||
@@ -295,7 +295,6 @@ int main(int argc, char *argv[]) | |||
295 | init_term(); | 295 | init_term(); |
296 | Messenger *m = init_tox(); | 296 | Messenger *m = init_tox(); |
297 | ToxWindow *prompt = init_windows(m); | 297 | ToxWindow *prompt = init_windows(m); |
298 | init_window_status(); | ||
299 | 298 | ||
300 | if (f_loadfromfile) | 299 | if (f_loadfromfile) |
301 | load_data(m, DATA_FILE); | 300 | load_data(m, DATA_FILE); |
diff --git a/testing/toxic/windows.c b/testing/toxic/windows.c index 11396e2c..a8efa066 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 */ | ||
10 | char WINDOW_STATUS[MAX_WINDOW_SLOTS]; | ||
11 | |||
12 | static int w_num; | ||
13 | static ToxWindow windows[MAX_WINDOW_SLOTS]; | 9 | static ToxWindow windows[MAX_WINDOW_SLOTS]; |
14 | static Messenger *m; | 10 | static ToxWindow *active_window; |
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) |
@@ -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_WINDOW_SLOTS; 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_WINDOW_SLOTS-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_WINDOW_SLOTS) |
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(on_friendadded)); |
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 | ||
@@ -223,8 +188,8 @@ static void draw_bar() | |||
223 | int i; | 188 | int i; |
224 | 189 | ||
225 | for (i = 0; i < (MAX_WINDOW_SLOTS); ++i) { | 190 | for (i = 0; i < (MAX_WINDOW_SLOTS); ++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 | } |
diff --git a/testing/toxic/windows.h b/testing/toxic/windows.h index 553a6200..7b784699 100644 --- a/testing/toxic/windows.h +++ b/testing/toxic/windows.h | |||
@@ -48,11 +48,10 @@ void on_action(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, | |||
48 | void on_nickchange(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata); | 48 | void on_nickchange(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata); |
49 | void on_statuschange(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata); | 49 | void on_statuschange(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata); |
50 | void on_friendadded(Messenger *m, int friendnumber); | 50 | void on_friendadded(Messenger *m, int friendnumber); |
51 | void init_window_status(); | ||
52 | ToxWindow *init_windows(); | 51 | ToxWindow *init_windows(); |
53 | void draw_active_window(Messenger *m); | 52 | void draw_active_window(Messenger *m); |
54 | int add_window(Messenger *m, ToxWindow w, int n); | 53 | int add_window(Messenger *m, ToxWindow w); |
55 | void del_window(ToxWindow *w, int f_num); | 54 | void del_window(ToxWindow *w); |
56 | void set_active_window(int ch); | 55 | void set_active_window(int ch); |
57 | #endif | 56 | #endif |
58 | 57 | ||