diff options
author | Jfreegman <Jfreegman@gmail.com> | 2013-08-06 18:27:51 -0400 |
---|---|---|
committer | Jfreegman <Jfreegman@gmail.com> | 2013-08-06 18:27:51 -0400 |
commit | 16b3ec746e9ab518a1efa221524391f8f692f75b (patch) | |
tree | 63570ead211e38b106a870baaa251eb669c7f03e /testing/toxic/chat.c | |
parent | fc5a2f53df06bfba185ad9868970e1fecfa0fc06 (diff) |
code format/clean up
Diffstat (limited to 'testing/toxic/chat.c')
-rw-r--r-- | testing/toxic/chat.c | 108 |
1 files changed, 52 insertions, 56 deletions
diff --git a/testing/toxic/chat.c b/testing/toxic/chat.c index 7262e722..20c01620 100644 --- a/testing/toxic/chat.c +++ b/testing/toxic/chat.c | |||
@@ -16,38 +16,34 @@ | |||
16 | 16 | ||
17 | typedef struct { | 17 | typedef struct { |
18 | int friendnum; | 18 | int friendnum; |
19 | |||
20 | char line[256]; | 19 | char line[256]; |
21 | size_t pos; | 20 | size_t pos; |
22 | |||
23 | WINDOW* history; | 21 | WINDOW* history; |
24 | WINDOW* linewin; | 22 | WINDOW* linewin; |
25 | |||
26 | } ChatContext; | 23 | } ChatContext; |
27 | 24 | ||
28 | extern int w_active; | 25 | extern int active_window; |
26 | |||
29 | extern void del_window(ToxWindow *w, int f_num); | 27 | extern void del_window(ToxWindow *w, int f_num); |
30 | extern void fix_name(uint8_t* name); | 28 | extern void fix_name(uint8_t *name); |
31 | void print_help(ChatContext* self); | 29 | void print_help(ChatContext *self); |
32 | void execute(ToxWindow* self, ChatContext* ctx, char* cmd); | 30 | void execute(ToxWindow *self, ChatContext *ctx, char *cmd); |
33 | 31 | ||
34 | static void chat_onMessage(ToxWindow* self, int num, uint8_t* msg, uint16_t len) { | 32 | static void chat_onMessage(ToxWindow *self, int num, uint8_t *msg, uint16_t len) |
35 | ChatContext* ctx = (ChatContext*) self->x; | 33 | { |
34 | ChatContext *ctx = (ChatContext*) self->x; | ||
36 | uint8_t nick[MAX_NAME_LENGTH] = {0}; | 35 | uint8_t nick[MAX_NAME_LENGTH] = {0}; |
37 | |||
38 | time_t now; | 36 | time_t now; |
39 | time(&now); | 37 | time(&now); |
40 | struct tm * timeinfo; | 38 | struct tm * timeinfo; |
41 | timeinfo = localtime(&now); | 39 | timeinfo = localtime(&now); |
42 | 40 | ||
43 | if(ctx->friendnum != num) | 41 | if (ctx->friendnum != num) |
44 | return; | 42 | return; |
45 | 43 | ||
46 | getname(num, (uint8_t*) &nick); | 44 | getname(num, (uint8_t*) &nick); |
47 | |||
48 | msg[len-1] = '\0'; | 45 | msg[len-1] = '\0'; |
49 | nick[MAX_NAME_LENGTH-1] = '\0'; | 46 | nick[MAX_NAME_LENGTH-1] = '\0'; |
50 | |||
51 | fix_name(msg); | 47 | fix_name(msg); |
52 | fix_name(nick); | 48 | fix_name(nick); |
53 | 49 | ||
@@ -64,15 +60,14 @@ static void chat_onMessage(ToxWindow* self, int num, uint8_t* msg, uint16_t len) | |||
64 | flash(); | 60 | flash(); |
65 | } | 61 | } |
66 | 62 | ||
67 | static void chat_onNickChange(ToxWindow* self, int num, uint8_t* nick, uint16_t len) { | 63 | static void chat_onNickChange(ToxWindow *self, int num, uint8_t *nick, uint16_t len) |
68 | ChatContext* ctx = (ChatContext*) self->x; | 64 | { |
69 | 65 | ChatContext *ctx = (ChatContext*) self->x; | |
70 | if(ctx->friendnum != num) | 66 | if (ctx->friendnum != num) |
71 | return; | 67 | return; |
72 | 68 | ||
73 | nick[len-1] = '\0'; | 69 | nick[len-1] = '\0'; |
74 | fix_name(nick); | 70 | fix_name(nick); |
75 | |||
76 | snprintf(self->title, sizeof(self->title), "[%s (%d)]", nick, num); | 71 | snprintf(self->title, sizeof(self->title), "[%s (%d)]", nick, num); |
77 | 72 | ||
78 | wattron(ctx->history, COLOR_PAIR(3)); | 73 | wattron(ctx->history, COLOR_PAIR(3)); |
@@ -80,7 +75,8 @@ static void chat_onNickChange(ToxWindow* self, int num, uint8_t* nick, uint16_t | |||
80 | wattroff(ctx->history, COLOR_PAIR(3)); | 75 | wattroff(ctx->history, COLOR_PAIR(3)); |
81 | } | 76 | } |
82 | 77 | ||
83 | static void chat_onStatusChange(ToxWindow* self, int num, uint8_t* status, uint16_t len) { | 78 | static void chat_onStatusChange(ToxWindow *self, int num, uint8_t *status, uint16_t len) |
79 | { | ||
84 | 80 | ||
85 | } | 81 | } |
86 | 82 | ||
@@ -89,35 +85,33 @@ int string_is_empty(char *string) | |||
89 | { | 85 | { |
90 | int rc = 0; | 86 | int rc = 0; |
91 | char *copy = strdup(string); | 87 | char *copy = strdup(string); |
92 | |||
93 | rc = ((strtok(copy, " ") == NULL) ? 1:0); | 88 | rc = ((strtok(copy, " ") == NULL) ? 1:0); |
94 | free(copy); | 89 | free(copy); |
95 | |||
96 | return rc; | 90 | return rc; |
97 | } | 91 | } |
98 | 92 | ||
99 | static void chat_onKey(ToxWindow* self, int key) { | 93 | static void chat_onKey(ToxWindow *self, int key) |
100 | ChatContext* ctx = (ChatContext*) self->x; | 94 | { |
101 | 95 | ChatContext *ctx = (ChatContext*) self->x; | |
102 | time_t now; | 96 | time_t now; |
103 | time(&now); | 97 | time(&now); |
104 | struct tm * timeinfo; | 98 | struct tm * timeinfo; |
105 | timeinfo = localtime(&now); | 99 | timeinfo = localtime(&now); |
106 | 100 | ||
107 | /* PRINTABLE characters: Add to line */ | 101 | /* Add printable characters to line */ |
108 | if(isprint(key)) { | 102 | if (isprint(key)) { |
109 | if(ctx->pos != sizeof(ctx->line)-1) { | 103 | if (ctx->pos != sizeof(ctx->line)-1) { |
110 | ctx->line[ctx->pos++] = key; | 104 | ctx->line[ctx->pos++] = key; |
111 | ctx->line[ctx->pos] = '\0'; | 105 | ctx->line[ctx->pos] = '\0'; |
112 | } | 106 | } |
113 | } | 107 | } |
114 | 108 | ||
115 | /* RETURN key: Execute command or print line */ | 109 | /* RETURN key: Execute command or print line */ |
116 | else if(key == '\n') { | 110 | else if (key == '\n') { |
117 | if (ctx->line[0] == '/') | 111 | if (ctx->line[0] == '/') |
118 | execute(self, ctx, ctx->line); | 112 | execute(self, ctx, ctx->line); |
119 | else { | 113 | else { |
120 | if(!string_is_empty(ctx->line)) { | 114 | if (!string_is_empty(ctx->line)) { |
121 | /* make sure the string has at least non-space character */ | 115 | /* make sure the string has at least non-space character */ |
122 | wattron(ctx->history, COLOR_PAIR(2)); | 116 | wattron(ctx->history, COLOR_PAIR(2)); |
123 | wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); | 117 | wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); |
@@ -127,7 +121,7 @@ static void chat_onKey(ToxWindow* self, int key) { | |||
127 | wattroff(ctx->history, COLOR_PAIR(1)); | 121 | wattroff(ctx->history, COLOR_PAIR(1)); |
128 | wprintw(ctx->history, "%s\n", ctx->line); | 122 | wprintw(ctx->history, "%s\n", ctx->line); |
129 | } | 123 | } |
130 | if(m_sendmessage(ctx->friendnum, (uint8_t*) ctx->line, strlen(ctx->line)+1) < 0) { | 124 | if (m_sendmessage(ctx->friendnum, (uint8_t*) ctx->line, strlen(ctx->line)+1) < 0) { |
131 | wattron(ctx->history, COLOR_PAIR(3)); | 125 | wattron(ctx->history, COLOR_PAIR(3)); |
132 | wprintw(ctx->history, " * Failed to send message.\n"); | 126 | wprintw(ctx->history, " * Failed to send message.\n"); |
133 | wattroff(ctx->history, COLOR_PAIR(3)); | 127 | wattroff(ctx->history, COLOR_PAIR(3)); |
@@ -138,29 +132,32 @@ static void chat_onKey(ToxWindow* self, int key) { | |||
138 | } | 132 | } |
139 | 133 | ||
140 | /* BACKSPACE key: Remove one character from line */ | 134 | /* BACKSPACE key: Remove one character from line */ |
141 | else if(key == 0x107 || key == 0x8 || key == 0x7f) { | 135 | else if (key == 0x107 || key == 0x8 || key == 0x7f) { |
142 | if(ctx->pos != 0) { | 136 | if (ctx->pos != 0) { |
143 | ctx->line[--ctx->pos] = '\0'; | 137 | ctx->line[--ctx->pos] = '\0'; |
144 | } | 138 | } |
145 | } | 139 | } |
146 | } | 140 | } |
147 | 141 | ||
148 | void execute(ToxWindow* self, ChatContext* ctx, char* cmd) | 142 | void execute(ToxWindow *self, ChatContext *ctx, char *cmd) |
149 | { | 143 | { |
150 | if (!strcmp(cmd, "/clear") || !strcmp(cmd, "/c")) { | 144 | if (!strcmp(cmd, "/clear") || !strcmp(cmd, "/c")) { |
151 | wclear(self->window); | 145 | wclear(self->window); |
152 | wclear(ctx->history); | 146 | wclear(ctx->history); |
153 | } | 147 | } |
148 | |||
154 | else if (!strcmp(cmd, "/help") || !strcmp(cmd, "/h")) | 149 | else if (!strcmp(cmd, "/help") || !strcmp(cmd, "/h")) |
155 | print_help(ctx); | 150 | print_help(ctx); |
151 | |||
156 | else if (!strcmp(cmd, "/quit") || !strcmp(cmd, "/exit") || !strcmp(cmd, "/q")) { | 152 | else if (!strcmp(cmd, "/quit") || !strcmp(cmd, "/exit") || !strcmp(cmd, "/q")) { |
157 | endwin(); | 153 | endwin(); |
158 | exit(0); | 154 | exit(0); |
159 | } | 155 | } |
156 | |||
160 | else if (!strncmp(cmd, "/status ", strlen("/status "))) { | 157 | else if (!strncmp(cmd, "/status ", strlen("/status "))) { |
161 | char* msg; | 158 | char *msg; |
162 | msg = strchr(cmd, ' '); | 159 | msg = strchr(cmd, ' '); |
163 | if(msg == NULL) { | 160 | if (msg == NULL) { |
164 | wprintw(ctx->history, "Invalid syntax.\n"); | 161 | wprintw(ctx->history, "Invalid syntax.\n"); |
165 | return; | 162 | return; |
166 | } | 163 | } |
@@ -168,10 +165,11 @@ void execute(ToxWindow* self, ChatContext* ctx, char* cmd) | |||
168 | m_set_userstatus(USERSTATUS_KIND_RETAIN, (uint8_t*) msg, strlen(msg)+1); | 165 | m_set_userstatus(USERSTATUS_KIND_RETAIN, (uint8_t*) msg, strlen(msg)+1); |
169 | wprintw(ctx->history, "Status set to: %s\n", msg); | 166 | wprintw(ctx->history, "Status set to: %s\n", msg); |
170 | } | 167 | } |
168 | |||
171 | else if (!strncmp(cmd, "/nick ", strlen("/nick "))) { | 169 | else if (!strncmp(cmd, "/nick ", strlen("/nick "))) { |
172 | char* nick; | 170 | char *nick; |
173 | nick = strchr(cmd, ' '); | 171 | nick = strchr(cmd, ' '); |
174 | if(nick == NULL) { | 172 | if (nick == NULL) { |
175 | wprintw(ctx->history, "Invalid syntax.\n"); | 173 | wprintw(ctx->history, "Invalid syntax.\n"); |
176 | return; | 174 | return; |
177 | } | 175 | } |
@@ -179,7 +177,8 @@ void execute(ToxWindow* self, ChatContext* ctx, char* cmd) | |||
179 | setname((uint8_t*) nick, strlen(nick)+1); | 177 | setname((uint8_t*) nick, strlen(nick)+1); |
180 | wprintw(ctx->history, "Nickname set to: %s\n", nick); | 178 | wprintw(ctx->history, "Nickname set to: %s\n", nick); |
181 | } | 179 | } |
182 | else if(!strcmp(cmd, "/myid")) { | 180 | |
181 | else if (!strcmp(cmd, "/myid")) { | ||
183 | char id[32*2 + 1] = {0}; | 182 | char id[32*2 + 1] = {0}; |
184 | int i; | 183 | int i; |
185 | for (i = 0; i < 32; i++) { | 184 | for (i = 0; i < 32; i++) { |
@@ -189,48 +188,46 @@ void execute(ToxWindow* self, ChatContext* ctx, char* cmd) | |||
189 | } | 188 | } |
190 | wprintw(ctx->history, "Your ID: %s\n", id); | 189 | wprintw(ctx->history, "Your ID: %s\n", id); |
191 | } | 190 | } |
191 | |||
192 | else if (strcmp(ctx->line, "/close") == 0) { | 192 | else if (strcmp(ctx->line, "/close") == 0) { |
193 | w_active = 0; // Go to prompt screen | 193 | active_window = 0; // Go to prompt screen |
194 | int f_num = ctx->friendnum; | 194 | int f_num = ctx->friendnum; |
195 | delwin(ctx->linewin); | 195 | delwin(ctx->linewin); |
196 | del_window(self, f_num); | 196 | del_window(self, f_num); |
197 | } | 197 | } |
198 | |||
198 | else | 199 | else |
199 | wprintw(ctx->history, "Invalid command.\n"); | 200 | wprintw(ctx->history, "Invalid command.\n"); |
200 | } | 201 | } |
201 | 202 | ||
202 | static void chat_onDraw(ToxWindow* self) { | 203 | static void chat_onDraw(ToxWindow *self) |
204 | { | ||
203 | curs_set(1); | 205 | curs_set(1); |
204 | int x, y; | 206 | int x, y; |
205 | ChatContext* ctx = (ChatContext*) self->x; | 207 | ChatContext *ctx = (ChatContext*) self->x; |
206 | |||
207 | getmaxyx(self->window, y, x); | 208 | getmaxyx(self->window, y, x); |
208 | |||
209 | (void) x; | 209 | (void) x; |
210 | if(y < 3) | 210 | if (y < 3) return; |
211 | return; | ||
212 | 211 | ||
213 | wclear(ctx->linewin); | 212 | wclear(ctx->linewin); |
214 | mvwhline(ctx->linewin, 0, 0, '_', COLS); | 213 | mvwhline(ctx->linewin, 0, 0, '_', COLS); |
215 | mvwprintw(self->window, y-1, 0, "%s\n", ctx->line); | 214 | mvwprintw(self->window, y-1, 0, "%s\n", ctx->line); |
216 | |||
217 | wrefresh(self->window); | 215 | wrefresh(self->window); |
218 | } | 216 | } |
219 | 217 | ||
220 | static void chat_onInit(ToxWindow* self) { | 218 | static void chat_onInit(ToxWindow *self) |
219 | { | ||
221 | int x, y; | 220 | int x, y; |
222 | ChatContext* ctx = (ChatContext*) self->x; | 221 | ChatContext *ctx = (ChatContext*) self->x; |
223 | |||
224 | getmaxyx(self->window, y, x); | 222 | getmaxyx(self->window, y, x); |
225 | |||
226 | ctx->history = subwin(self->window, y - 4, x, 0, 0); | 223 | ctx->history = subwin(self->window, y - 4, x, 0, 0); |
227 | scrollok(ctx->history, 1); | 224 | scrollok(ctx->history, 1); |
228 | |||
229 | ctx->linewin = subwin(self->window, 2, x, y - 3, 0); | 225 | ctx->linewin = subwin(self->window, 2, x, y - 3, 0); |
230 | print_help(ctx); | 226 | print_help(ctx); |
231 | } | 227 | } |
232 | 228 | ||
233 | void print_help(ChatContext* self) { | 229 | void print_help(ChatContext *self) |
230 | { | ||
234 | wattron(self->history, COLOR_PAIR(2) | A_BOLD); | 231 | wattron(self->history, COLOR_PAIR(2) | A_BOLD); |
235 | wprintw(self->history, "Commands:\n"); | 232 | wprintw(self->history, "Commands:\n"); |
236 | wattroff(self->history, A_BOLD); | 233 | wattroff(self->history, A_BOLD); |
@@ -246,9 +243,9 @@ void print_help(ChatContext* self) { | |||
246 | wattroff(self->history, COLOR_PAIR(2)); | 243 | wattroff(self->history, COLOR_PAIR(2)); |
247 | } | 244 | } |
248 | 245 | ||
249 | ToxWindow new_chat(int friendnum) { | 246 | ToxWindow new_chat(int friendnum) |
247 | { | ||
250 | ToxWindow ret; | 248 | ToxWindow ret; |
251 | |||
252 | memset(&ret, 0, sizeof(ret)); | 249 | memset(&ret, 0, sizeof(ret)); |
253 | 250 | ||
254 | ret.onKey = &chat_onKey; | 251 | ret.onKey = &chat_onKey; |
@@ -264,9 +261,8 @@ ToxWindow new_chat(int friendnum) { | |||
264 | 261 | ||
265 | snprintf(ret.title, sizeof(ret.title), "[%s (%d)]", nick, friendnum); | 262 | snprintf(ret.title, sizeof(ret.title), "[%s (%d)]", nick, friendnum); |
266 | 263 | ||
267 | ChatContext* x = calloc(1, sizeof(ChatContext)); | 264 | ChatContext *x = calloc(1, sizeof(ChatContext)); |
268 | x->friendnum = friendnum; | 265 | x->friendnum = friendnum; |
269 | |||
270 | ret.x = (void*) x; | 266 | ret.x = (void*) x; |
271 | return ret; | 267 | return ret; |
272 | } | 268 | } |