From f5695a4b3e5980a5143e811417a0f638a54ead8f Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Wed, 4 Sep 2013 21:25:59 -0400 Subject: [PATCH 1/3] implemented status and connectionstatus callbacks --- src/chat.c | 82 ++++++++++++++++++++++++++++++++++++++++----- src/friendlist.c | 39 +++++++++++++++++---- src/main.c | 2 ++ src/prompt.c | 1 + src/toxic_windows.h | 4 +++ src/windows.c | 72 +++++++++++++++++++++++++++++++++++++-- 6 files changed, 183 insertions(+), 17 deletions(-) diff --git a/src/chat.c b/src/chat.c index 5a4e283..5b99f70 100644 --- a/src/chat.c +++ b/src/chat.c @@ -52,8 +52,8 @@ static void chat_onMessage(ToxWindow *self, Tox *m, int num, uint8_t *msg, uint1 return; tox_getname(m, num, (uint8_t *) &nick); - msg[len - 1] = '\0'; - nick[TOX_MAX_NAME_LENGTH - 1] = '\0'; + msg[len-1] = '\0'; + nick[TOX_MAX_NAME_LENGTH-1] = '\0'; wattron(ctx->history, COLOR_PAIR(2)); wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); @@ -67,6 +67,24 @@ static void chat_onMessage(ToxWindow *self, Tox *m, int num, uint8_t *msg, uint1 beep(); } +void chat_onConnectionChange(ToxWindow *self, Tox *m, int num, uint8_t status) +{ + ChatContext *ctx = (ChatContext *) self->x; + struct tm *timeinfo = get_time(); + + if (ctx->friendnum != num) + return; + + wattron(ctx->history, COLOR_PAIR(2)); + wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); + wattroff(ctx->history, COLOR_PAIR(2)); + + if (status == 1) + wprintw(ctx->history, "* Chat partner has come online\n"); + else + wprintw(ctx->history, "* Chat partner went offline\n"); +} + static void chat_onAction(ToxWindow *self, Tox *m, int num, uint8_t *action, uint16_t len) { ChatContext *ctx = (ChatContext *) self->x; @@ -110,7 +128,7 @@ static void chat_onNickChange(ToxWindow *self, int num, uint8_t *nick, uint16_t wprintw(ctx->history, "* Chat partner changed nick to '%s'\n", nick); } -static void chat_onStatusMessageChange(ToxWindow *self, int num, uint8_t *status, uint16_t len) +static void chat_onStatusChange(ToxWindow *self, Tox *m, int num, TOX_USERSTATUS status) { ChatContext *ctx = (ChatContext *) self->x; struct tm *timeinfo = get_time(); @@ -122,9 +140,47 @@ static void chat_onStatusMessageChange(ToxWindow *self, int num, uint8_t *status wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); wattroff(ctx->history, COLOR_PAIR(2)); + switch(status) { + + case TOX_USERSTATUS_NONE: + wprintw(ctx->history, "* Chat partner set status to "); + wattron(ctx->history, COLOR_PAIR(1)); + wprintw(ctx->history, "[Online]\n"); + wattroff(ctx->history, COLOR_PAIR(1)); + break; + + case TOX_USERSTATUS_BUSY: + wprintw(ctx->history, "* Chat partner set status to "); + wattron(ctx->history, COLOR_PAIR(3)); + wprintw(ctx->history, "[Busy]\n"); + wattroff(ctx->history, COLOR_PAIR(3)); + break; + + case TOX_USERSTATUS_AWAY: + wprintw(ctx->history, "* Chat partner set status to "); + wattron(ctx->history, COLOR_PAIR(5)); + wprintw(ctx->history, "[Away]\n"); + wattroff(ctx->history, COLOR_PAIR(5)); + break; + } +} + +static void chat_onStatusMessageChange(ToxWindow *self, int num, uint8_t *status, uint16_t len) +{ + ChatContext *ctx = (ChatContext *) self->x; + struct tm *timeinfo = get_time(); + + if (ctx->friendnum != num) + return; + status[len - 1] = '\0'; - wprintw(ctx->history, "* Chat partner changed personal note to: %s\n", status); + if (strncmp(status, "Online", strlen("status"))) { /* Ignore default "Online" message */ + wattron(ctx->history, COLOR_PAIR(2)); + wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); + wattroff(ctx->history, COLOR_PAIR(2)); + wprintw(ctx->history, "* Chat partner changed personal note to: %s\n", status); + } } /* check that the string has one non-space character */ @@ -322,17 +378,26 @@ void execute(ToxWindow *self, ChatContext *ctx, Tox *m, char *cmd) if (!strncmp(status, "online", strlen("online"))) { status_kind = TOX_USERSTATUS_NONE; - status_text = "Online"; + wprintw(ctx->history, "Status set to "); + wattron(ctx->history, COLOR_PAIR(1)); + wprintw(ctx->history, "[Online]\n"); + wattroff(ctx->history, COLOR_PAIR(1)); } else if (!strncmp(status, "away", strlen("away"))) { status_kind = TOX_USERSTATUS_AWAY; - status_text = "Away"; + wprintw(ctx->history, "Status set to "); + wattron(ctx->history, COLOR_PAIR(5)); + wprintw(ctx->history, "[Away]\n"); + wattroff(ctx->history, COLOR_PAIR(5)); } else if (!strncmp(status, "busy", strlen("busy"))) { status_kind = TOX_USERSTATUS_BUSY; - status_text = "Busy"; + wprintw(ctx->history, "Status set to "); + wattron(ctx->history, COLOR_PAIR(3)); + wprintw(ctx->history, "[Busy]\n"); + wattroff(ctx->history, COLOR_PAIR(3)); } else { @@ -340,7 +405,6 @@ void execute(ToxWindow *self, ChatContext *ctx, Tox *m, char *cmd) return; } - wprintw(ctx->history, "Status set to: %s\n", status_text); tox_set_userstatus(m, status_kind); msg = strchr(status, ' '); @@ -442,7 +506,9 @@ ToxWindow new_chat(Tox *m, int friendnum) ret.onDraw = &chat_onDraw; ret.onInit = &chat_onInit; ret.onMessage = &chat_onMessage; + ret.onConnectionChange = &chat_onConnectionChange; ret.onNickChange = &chat_onNickChange; + ret.onStatusChange = &chat_onStatusChange; ret.onStatusMessageChange = &chat_onStatusMessageChange; ret.onAction = &chat_onAction; diff --git a/src/friendlist.c b/src/friendlist.c index 9073420..a5befb4 100644 --- a/src/friendlist.c +++ b/src/friendlist.c @@ -24,7 +24,9 @@ typedef struct { uint8_t statusmsg[TOX_MAX_STATUSMESSAGE_LENGTH]; int num; int chatwin; - bool active; + bool active; + bool online; + TOX_USERSTATUS status; } friend_t; static friend_t friends[MAX_FRIENDS_NUM]; @@ -34,25 +36,44 @@ static int num_selected = 0; void friendlist_onMessage(ToxWindow *self, Tox *m, int num, uint8_t *str, uint16_t len) { - if (num >= num_friends) + if (num < 0 || num >= num_friends) return; if (friends[num].chatwin == -1) friends[num].chatwin = add_window(m, new_chat(m, friends[num].num)); } +void friendlist_onConnectionChange(ToxWindow *self, Tox *m, int num, uint8_t status) +{ + if (num < 0 || num >= num_friends) + return; + + if (status == 1) + friends[num].online = true; + else + friends[num].online = false; +} + void friendlist_onNickChange(ToxWindow *self, int num, uint8_t *str, uint16_t len) { - if (len >= TOX_MAX_NAME_LENGTH || num >= num_friends) + if (len >= TOX_MAX_NAME_LENGTH || num < 0 || num >= num_friends) return; memcpy((char *) &friends[num].name, (char *) str, len); friends[num].name[len] = 0; } +void friendlist_onStatusChange(ToxWindow *self, Tox *m, int num, TOX_USERSTATUS status) +{ + if (num < 0 || num >= num_friends) + return; + + friends[num].status = status; +} + void friendlist_onStatusMessageChange(ToxWindow *self, int num, uint8_t *str, uint16_t len) { - if (len >= TOX_MAX_STATUSMESSAGE_LENGTH || num >= num_friends) + if (len >= TOX_MAX_STATUSMESSAGE_LENGTH || num < 0 || num >= num_friends) return; memcpy((char *) &friends[num].statusmsg, (char *) str, len); @@ -71,6 +92,8 @@ int friendlist_onFriendAdded(Tox *m, int num) friends[i].num = num; friends[i].active = true; friends[i].chatwin = -1; + friends[i].online = false; + friends[i].status = TOX_USERSTATUS_INVALID; if (tox_getname(m, num, friends[i].name) != 0 || friends[i].name[0] == '\0') strcpy((char *) friends[i].name, "unknown"); @@ -172,8 +195,8 @@ static void friendlist_onDraw(ToxWindow *self, Tox *m) else wprintw(self->window, " "); - if (tox_friendstatus(m, friends[i].num) == TOX_FRIEND_ONLINE) { - TOX_USERSTATUS status = tox_get_userstatus(m, friends[i].num); + if (friends[i].online) { + TOX_USERSTATUS status = friends[i].status; int colour = 7; /* Invalid or other errors default to black */ switch(status) { @@ -203,7 +226,7 @@ static void friendlist_onDraw(ToxWindow *self, Tox *m) } } } - + wrefresh(self->window); } @@ -226,8 +249,10 @@ ToxWindow new_friendlist() ret.onDraw = &friendlist_onDraw; ret.onInit = &friendlist_onInit; ret.onMessage = &friendlist_onMessage; + ret.onConnectionChange = &friendlist_onConnectionChange; ret.onAction = &friendlist_onMessage; // Action has identical behaviour to message ret.onNickChange = &friendlist_onNickChange; + ret.onStatusChange = &friendlist_onStatusChange; ret.onStatusMessageChange = &friendlist_onStatusMessageChange; strcpy(ret.title, "[friends]"); diff --git a/src/main.c b/src/main.c index eaf3618..77bcee2 100644 --- a/src/main.c +++ b/src/main.c @@ -91,9 +91,11 @@ static Tox *init_tox() Tox *m = tox_new(); /* Callbacks */ + tox_callback_connectionstatus(m, on_connectionchange, NULL); tox_callback_friendrequest(m, on_request, NULL); tox_callback_friendmessage(m, on_message, NULL); tox_callback_namechange(m, on_nickchange, NULL); + tox_callback_userstatus(m, on_statuschange, NULL); tox_callback_statusmessage(m, on_statusmessagechange, NULL); tox_callback_action(m, on_action, NULL); #ifdef __linux__ diff --git a/src/prompt.c b/src/prompt.c index 2865654..cc62a1d 100644 --- a/src/prompt.c +++ b/src/prompt.c @@ -314,6 +314,7 @@ void cmd_nick(ToxWindow *self, Tox *m, int argc, char **argv) } nick = argv[1]; + nick[strlen(++nick)-1] = L'\0'; tox_setname(m, (uint8_t *) nick, strlen(nick) + 1); wprintw(self->window, "Nickname set to: %s\n", nick); diff --git a/src/toxic_windows.h b/src/toxic_windows.h index ecff396..ec188cb 100644 --- a/src/toxic_windows.h +++ b/src/toxic_windows.h @@ -31,8 +31,10 @@ struct ToxWindow_ { void(*onDraw)(ToxWindow *, Tox *); void(*onInit)(ToxWindow *, Tox *); void(*onFriendRequest)(ToxWindow *, uint8_t *, uint8_t *, uint16_t); + void(*onConnectionChange)(ToxWindow *, Tox *, int, uint8_t); void(*onMessage)(ToxWindow *, Tox *, int, uint8_t *, uint16_t); void(*onNickChange)(ToxWindow *, int, uint8_t *, uint16_t); + void(*onStatusChange)(ToxWindow *, Tox *, int, TOX_USERSTATUS); void(*onStatusMessageChange)(ToxWindow *, int, uint8_t *, uint16_t); void(*onAction)(ToxWindow *, Tox *, int, uint8_t *, uint16_t); char title[256]; @@ -44,9 +46,11 @@ struct ToxWindow_ { }; void on_request(uint8_t *public_key, uint8_t *data, uint16_t length, void *userdata); +void on_connectionchange(Tox *m, int friendnumber, uint8_t status, void *userdata); void on_message(Tox *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata); void on_action(Tox *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata); void on_nickchange(Tox *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata); +void on_statuschange(Tox *m, int friendnumber, TOX_USERSTATUS status, void *userdata); void on_statusmessagechange(Tox *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata); void on_friendadded(Tox *m, int friendnumber); ToxWindow *init_windows(); diff --git a/src/windows.c b/src/windows.c index 1e28667..71feecd 100644 --- a/src/windows.c +++ b/src/windows.c @@ -17,6 +17,8 @@ static ToxWindow *active_window; static ToxWindow *prompt; static Tox *m; +#define unknown_name "Unknown" + /* CALLBACKS START */ void on_request(uint8_t *public_key, uint8_t *data, uint16_t length, void *userdata) { @@ -38,6 +40,27 @@ void on_request(uint8_t *public_key, uint8_t *data, uint16_t length, void *userd } } +void on_connectionchange(Tox *m, int friendnumber, uint8_t status, void *userdata) +{ + uint8_t nick[TOX_MAX_NAME_LENGTH] = {0}; + tox_getname(m, friendnumber, (uint8_t *) &nick); + + if (!nick[0]) + snprintf(nick, sizeof(nick), "%s", unknown_name); + + if (status == 1) + wprintw(prompt->window, "\n%s has come online\n", nick, friendnumber); + else + wprintw(prompt->window, "\n%s went offline\n", nick, friendnumber); + + int i; + + for (i = 0; i < MAX_WINDOWS_NUM; ++i) { + if (windows[i].onConnectionChange != NULL) + windows[i].onConnectionChange(&windows[i], m, friendnumber, status); + } +} + void on_message(Tox *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata) { int i; @@ -60,7 +83,6 @@ void on_action(Tox *m, int friendnumber, uint8_t *string, uint16_t length, void void on_nickchange(Tox *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata) { - wprintw(prompt->window, "\n(nick change) %d: %s\n", friendnumber, string); int i; for (i = 0; i < MAX_WINDOWS_NUM; ++i) { @@ -71,7 +93,17 @@ void on_nickchange(Tox *m, int friendnumber, uint8_t *string, uint16_t length, v void on_statusmessagechange(Tox *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata) { - wprintw(prompt->window, "\n(note change) %d: %s\n", friendnumber, string); + /* Don't show default "Online" status message */ + if (strncmp(string, "Online", strlen(string))) { + uint8_t nick[TOX_MAX_NAME_LENGTH] = {0}; + tox_getname(m, friendnumber, (uint8_t *) &nick); + + if (!nick[0]) + snprintf(nick, sizeof(nick), "%s", unknown_name); + + wprintw(prompt->window, "\n%s set note to: %s\n", nick, string); + } + int i; for (i = 0; i < MAX_WINDOWS_NUM; ++i) { @@ -80,6 +112,42 @@ void on_statusmessagechange(Tox *m, int friendnumber, uint8_t *string, uint16_t } } +void on_statuschange(Tox *m, int friendnumber, TOX_USERSTATUS status, void *userdata) +{ + uint8_t nick[TOX_MAX_NAME_LENGTH] = {0}; + tox_getname(m, friendnumber, (uint8_t *) &nick); + + if (!nick[0]) + snprintf(nick, sizeof(nick), "%s", unknown_name); + + switch(status) { + case TOX_USERSTATUS_NONE: + /* Disabled because it spams a second messages when user comes online */ + break; + + case TOX_USERSTATUS_BUSY: + wprintw(prompt->window, "\n%s set status to ", nick); + wattron(prompt->window, COLOR_PAIR(3)); + wprintw(prompt->window, "[Busy]\n"); + wattroff(prompt->window, COLOR_PAIR(3)); + break; + + case TOX_USERSTATUS_AWAY: + wprintw(prompt->window, "\n%s set status to ", nick); + wattron(prompt->window, COLOR_PAIR(5)); + wprintw(prompt->window, "[Away]\n"); + wattroff(prompt->window, COLOR_PAIR(5)); + break; + } + + int i; + + for (i = 0; i < MAX_WINDOWS_NUM; ++i) { + if (windows[i].onStatusChange != NULL) + windows[i].onStatusChange(&windows[i], m, friendnumber, status); + } +} + void on_friendadded(Tox *m, int friendnumber) { friendlist_onFriendAdded(m, friendnumber); From f7d96b0779c05a0fd5b0adead9f186be058b298a Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Thu, 5 Sep 2013 00:47:33 -0400 Subject: [PATCH 2/3] define curses colours and replace magic numbers --- src/chat.c | 80 ++++++++++++++++++++++----------------------- src/friendlist.c | 12 +++---- src/main.c | 10 +++--- src/prompt.c | 8 ++--- src/toxic_windows.h | 10 ++++++ src/windows.c | 28 ++++++++-------- 6 files changed, 79 insertions(+), 69 deletions(-) diff --git a/src/chat.c b/src/chat.c index 5b99f70..e13ba74 100644 --- a/src/chat.c +++ b/src/chat.c @@ -55,9 +55,9 @@ static void chat_onMessage(ToxWindow *self, Tox *m, int num, uint8_t *msg, uint1 msg[len-1] = '\0'; nick[TOX_MAX_NAME_LENGTH-1] = '\0'; - wattron(ctx->history, COLOR_PAIR(2)); + wattron(ctx->history, COLOR_PAIR(CYAN)); wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); - wattroff(ctx->history, COLOR_PAIR(2)); + wattroff(ctx->history, COLOR_PAIR(CYAN)); wattron(ctx->history, COLOR_PAIR(4)); wprintw(ctx->history, "%s: ", nick); wattroff(ctx->history, COLOR_PAIR(4)); @@ -75,9 +75,9 @@ void chat_onConnectionChange(ToxWindow *self, Tox *m, int num, uint8_t status) if (ctx->friendnum != num) return; - wattron(ctx->history, COLOR_PAIR(2)); + wattron(ctx->history, COLOR_PAIR(CYAN)); wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); - wattroff(ctx->history, COLOR_PAIR(2)); + wattroff(ctx->history, COLOR_PAIR(CYAN)); if (status == 1) wprintw(ctx->history, "* Chat partner has come online\n"); @@ -98,13 +98,13 @@ static void chat_onAction(ToxWindow *self, Tox *m, int num, uint8_t *action, uin action[len - 1] = '\0'; - wattron(ctx->history, COLOR_PAIR(2)); + wattron(ctx->history, COLOR_PAIR(CYAN)); wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); - wattroff(ctx->history, COLOR_PAIR(2)); + wattroff(ctx->history, COLOR_PAIR(CYAN)); - wattron(ctx->history, COLOR_PAIR(5)); + wattron(ctx->history, COLOR_PAIR(YELLOW)); wprintw(ctx->history, "* %s %s\n", nick, action); - wattroff(ctx->history, COLOR_PAIR(5)); + wattroff(ctx->history, COLOR_PAIR(YELLOW)); self->blink = true; beep(); @@ -118,9 +118,9 @@ static void chat_onNickChange(ToxWindow *self, int num, uint8_t *nick, uint16_t if (ctx->friendnum != num) return; - wattron(ctx->history, COLOR_PAIR(2)); + wattron(ctx->history, COLOR_PAIR(CYAN)); wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); - wattroff(ctx->history, COLOR_PAIR(2)); + wattroff(ctx->history, COLOR_PAIR(CYAN)); nick[len - 1] = '\0'; snprintf(self->title, sizeof(self->title), "[%s]", nick); @@ -136,31 +136,31 @@ static void chat_onStatusChange(ToxWindow *self, Tox *m, int num, TOX_USERSTATUS if (ctx->friendnum != num) return; - wattron(ctx->history, COLOR_PAIR(2)); + wattron(ctx->history, COLOR_PAIR(CYAN)); wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); - wattroff(ctx->history, COLOR_PAIR(2)); + wattroff(ctx->history, COLOR_PAIR(CYAN)); switch(status) { case TOX_USERSTATUS_NONE: wprintw(ctx->history, "* Chat partner set status to "); - wattron(ctx->history, COLOR_PAIR(1)); + wattron(ctx->history, COLOR_PAIR(GREEN)); wprintw(ctx->history, "[Online]\n"); - wattroff(ctx->history, COLOR_PAIR(1)); + wattroff(ctx->history, COLOR_PAIR(GREEN)); break; case TOX_USERSTATUS_BUSY: wprintw(ctx->history, "* Chat partner set status to "); - wattron(ctx->history, COLOR_PAIR(3)); + wattron(ctx->history, COLOR_PAIR(RED)); wprintw(ctx->history, "[Busy]\n"); - wattroff(ctx->history, COLOR_PAIR(3)); + wattroff(ctx->history, COLOR_PAIR(RED)); break; case TOX_USERSTATUS_AWAY: wprintw(ctx->history, "* Chat partner set status to "); - wattron(ctx->history, COLOR_PAIR(5)); + wattron(ctx->history, COLOR_PAIR(YELLOW)); wprintw(ctx->history, "[Away]\n"); - wattroff(ctx->history, COLOR_PAIR(5)); + wattroff(ctx->history, COLOR_PAIR(YELLOW)); break; } } @@ -176,9 +176,9 @@ static void chat_onStatusMessageChange(ToxWindow *self, int num, uint8_t *status status[len - 1] = '\0'; if (strncmp(status, "Online", strlen("status"))) { /* Ignore default "Online" message */ - wattron(ctx->history, COLOR_PAIR(2)); + wattron(ctx->history, COLOR_PAIR(CYAN)); wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); - wattroff(ctx->history, COLOR_PAIR(2)); + wattroff(ctx->history, COLOR_PAIR(CYAN)); wprintw(ctx->history, "* Chat partner changed personal note to: %s\n", status); } } @@ -286,18 +286,18 @@ static void chat_onKey(ToxWindow *self, Tox *m, wint_t key) uint8_t selfname[TOX_MAX_NAME_LENGTH]; tox_getselfname(m, selfname, sizeof(selfname)); - wattron(ctx->history, COLOR_PAIR(2)); + wattron(ctx->history, COLOR_PAIR(CYAN)); wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); - wattroff(ctx->history, COLOR_PAIR(2)); - wattron(ctx->history, COLOR_PAIR(1)); + wattroff(ctx->history, COLOR_PAIR(CYAN)); + wattron(ctx->history, COLOR_PAIR(GREEN)); wprintw(ctx->history, "%s: ", selfname); - wattroff(ctx->history, COLOR_PAIR(1)); + wattroff(ctx->history, COLOR_PAIR(GREEN)); wprintw(ctx->history, "%s\n", line); if (tox_sendmessage(m, ctx->friendnum, (uint8_t *) line, strlen(line) + 1) == 0) { - wattron(ctx->history, COLOR_PAIR(3)); + wattron(ctx->history, COLOR_PAIR(RED)); wprintw(ctx->history, " * Failed to send message.\n"); - wattroff(ctx->history, COLOR_PAIR(3)); + wattroff(ctx->history, COLOR_PAIR(RED)); } } } @@ -345,21 +345,21 @@ void execute(ToxWindow *self, ChatContext *ctx, Tox *m, char *cmd) action++; - wattron(ctx->history, COLOR_PAIR(2)); + wattron(ctx->history, COLOR_PAIR(CYAN)); wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); - wattroff(ctx->history, COLOR_PAIR(2)); + wattroff(ctx->history, COLOR_PAIR(CYAN)); uint8_t selfname[TOX_MAX_NAME_LENGTH]; tox_getselfname(m, selfname, sizeof(selfname)); - wattron(ctx->history, COLOR_PAIR(5)); + wattron(ctx->history, COLOR_PAIR(YELLOW)); wprintw(ctx->history, "* %s %s\n", selfname, action); - wattroff(ctx->history, COLOR_PAIR(5)); + wattroff(ctx->history, COLOR_PAIR(YELLOW)); if (tox_sendaction(m, ctx->friendnum, (uint8_t *) action, strlen(action) + 1) == 0) { - wattron(ctx->history, COLOR_PAIR(3)); + wattron(ctx->history, COLOR_PAIR(RED)); wprintw(ctx->history, " * Failed to send action\n"); - wattroff(ctx->history, COLOR_PAIR(3)); + wattroff(ctx->history, COLOR_PAIR(RED)); } } @@ -379,25 +379,25 @@ void execute(ToxWindow *self, ChatContext *ctx, Tox *m, char *cmd) if (!strncmp(status, "online", strlen("online"))) { status_kind = TOX_USERSTATUS_NONE; wprintw(ctx->history, "Status set to "); - wattron(ctx->history, COLOR_PAIR(1)); + wattron(ctx->history, COLOR_PAIR(GREEN)); wprintw(ctx->history, "[Online]\n"); - wattroff(ctx->history, COLOR_PAIR(1)); + wattroff(ctx->history, COLOR_PAIR(GREEN)); } else if (!strncmp(status, "away", strlen("away"))) { status_kind = TOX_USERSTATUS_AWAY; wprintw(ctx->history, "Status set to "); - wattron(ctx->history, COLOR_PAIR(5)); + wattron(ctx->history, COLOR_PAIR(YELLOW)); wprintw(ctx->history, "[Away]\n"); - wattroff(ctx->history, COLOR_PAIR(5)); + wattroff(ctx->history, COLOR_PAIR(YELLOW)); } else if (!strncmp(status, "busy", strlen("busy"))) { status_kind = TOX_USERSTATUS_BUSY; wprintw(ctx->history, "Status set to "); - wattron(ctx->history, COLOR_PAIR(3)); + wattron(ctx->history, COLOR_PAIR(RED)); wprintw(ctx->history, "[Busy]\n"); - wattroff(ctx->history, COLOR_PAIR(3)); + wattroff(ctx->history, COLOR_PAIR(RED)); } else { @@ -480,7 +480,7 @@ static void chat_onInit(ToxWindow *self, Tox *m) void print_help(ChatContext *self) { - wattron(self->history, COLOR_PAIR(2) | A_BOLD); + wattron(self->history, COLOR_PAIR(CYAN) | A_BOLD); wprintw(self->history, "Commands:\n"); wattroff(self->history, A_BOLD); @@ -494,7 +494,7 @@ void print_help(ChatContext *self) wprintw(self->history, " /quit or /exit : Exit Toxic\n"); wprintw(self->history, " /help : Print this message again\n\n"); - wattroff(self->history, COLOR_PAIR(2)); + wattroff(self->history, COLOR_PAIR(CYAN)); } ToxWindow new_chat(Tox *m, int friendnum) diff --git a/src/friendlist.c b/src/friendlist.c index a5befb4..f79267b 100644 --- a/src/friendlist.c +++ b/src/friendlist.c @@ -180,10 +180,10 @@ static void friendlist_onDraw(ToxWindow *self, Tox *m) if (num_friends == 0) { wprintw(self->window, "Empty. Add some friends! :-)\n"); } else { - wattron(self->window, COLOR_PAIR(2) | A_BOLD); + wattron(self->window, COLOR_PAIR(CYAN) | A_BOLD); wprintw(self->window, " Open chat with up/down keys and enter.\n"); wprintw(self->window, " Delete friends with the backspace key.\n\n"); - wattroff(self->window, COLOR_PAIR(2) | A_BOLD); + wattroff(self->window, COLOR_PAIR(CYAN) | A_BOLD); } int i; @@ -197,17 +197,17 @@ static void friendlist_onDraw(ToxWindow *self, Tox *m) if (friends[i].online) { TOX_USERSTATUS status = friends[i].status; - int colour = 7; /* Invalid or other errors default to black */ + int colour = WHITE; switch(status) { case TOX_USERSTATUS_NONE: - colour = 1; + colour = GREEN; break; case TOX_USERSTATUS_AWAY: - colour = 5; + colour = YELLOW; break; case TOX_USERSTATUS_BUSY: - colour = 3; + colour = RED; break; } diff --git a/src/main.c b/src/main.c index 77bcee2..c74cb34 100644 --- a/src/main.c +++ b/src/main.c @@ -71,6 +71,7 @@ static void init_term() if (has_colors()) { start_color(); + init_pair(0, COLOR_WHITE, COLOR_BLACK); init_pair(1, COLOR_GREEN, COLOR_BLACK); init_pair(2, COLOR_CYAN, COLOR_BLACK); init_pair(3, COLOR_RED, COLOR_BLACK); @@ -79,7 +80,6 @@ static void init_term() init_pair(6, COLOR_MAGENTA, COLOR_BLACK); init_pair(7, COLOR_BLACK, COLOR_BLACK); init_pair(8, COLOR_BLACK, COLOR_WHITE); - } refresh(); @@ -398,17 +398,17 @@ int main(int argc, char *argv[]) load_data(m, DATA_FILE); if (f_flag == -1) { - attron(COLOR_PAIR(3) | A_BOLD); + attron(COLOR_PAIR(RED) | A_BOLD); wprintw(prompt->window, "You passed '-f' without giving an argument.\n" "defaulting to 'data' for a keyfile...\n"); - attroff(COLOR_PAIR(3) | A_BOLD); + attroff(COLOR_PAIR(RED) | A_BOLD); } if (config_err) { - attron(COLOR_PAIR(3) | A_BOLD); + attron(COLOR_PAIR(RED) | A_BOLD); wprintw(prompt->window, "Unable to determine configuration directory.\n" "defaulting to 'data' for a keyfile...\n"); - attroff(COLOR_PAIR(3) | A_BOLD); + attroff(COLOR_PAIR(RED) | A_BOLD); } while (true) { diff --git a/src/prompt.c b/src/prompt.c index cc62a1d..913c169 100644 --- a/src/prompt.c +++ b/src/prompt.c @@ -243,7 +243,7 @@ void cmd_quit(ToxWindow *self, Tox *m, int argc, char **argv) void cmd_help(ToxWindow *self, Tox *m, int argc, char **argv) { wclear(self->window); - wattron(self->window, COLOR_PAIR(2) | A_BOLD); + wattron(self->window, COLOR_PAIR(CYAN) | A_BOLD); wprintw(self->window, "Commands:\n"); wattroff(self->window, A_BOLD); @@ -264,7 +264,7 @@ void cmd_help(ToxWindow *self, Tox *m, int argc, char **argv) wprintw(self->window, " * Use the TAB key to navigate through the tabs.\n\n"); wattroff(self->window, A_BOLD); - wattroff(self->window, COLOR_PAIR(2)); + wattroff(self->window, COLOR_PAIR(CYAN)); } void cmd_msg(ToxWindow *self, Tox *m, int argc, char **argv) @@ -530,9 +530,9 @@ static void prompt_onDraw(ToxWindow *self, Tox *m) --y; } - wattron(self->window, COLOR_PAIR(1)); + wattron(self->window, COLOR_PAIR(GREEN)); mvwprintw(self->window, y, 0, "# "); - wattroff(self->window, COLOR_PAIR(1)); + wattroff(self->window, COLOR_PAIR(GREEN)); mvwprintw(self->window, y, 2, "%s", prompt_buf); wclrtoeol(self->window); wrefresh(self->window); diff --git a/src/toxic_windows.h b/src/toxic_windows.h index ec188cb..23aa4ae 100644 --- a/src/toxic_windows.h +++ b/src/toxic_windows.h @@ -24,6 +24,16 @@ #define TOXICVER "NOVER" //Use the -D flag to set this #endif +/* Curses foreground colours (background is black) */ +#define WHITE 0 +#define GREEN 1 +#define CYAN 2 +#define RED 3 +#define BLUE 4 +#define YELLOW 5 +#define MAGENTA 6 +#define BLACK 7 + typedef struct ToxWindow_ ToxWindow; struct ToxWindow_ { diff --git a/src/windows.c b/src/windows.c index 71feecd..a184148 100644 --- a/src/windows.c +++ b/src/windows.c @@ -17,7 +17,7 @@ static ToxWindow *active_window; static ToxWindow *prompt; static Tox *m; -#define unknown_name "Unknown" +#define UNKNOWN_NAME "Unknown" /* CALLBACKS START */ void on_request(uint8_t *public_key, uint8_t *data, uint16_t length, void *userdata) @@ -46,7 +46,7 @@ void on_connectionchange(Tox *m, int friendnumber, uint8_t status, void *userdat tox_getname(m, friendnumber, (uint8_t *) &nick); if (!nick[0]) - snprintf(nick, sizeof(nick), "%s", unknown_name); + snprintf(nick, sizeof(nick), "%s", UNKNOWN_NAME); if (status == 1) wprintw(prompt->window, "\n%s has come online\n", nick, friendnumber); @@ -99,7 +99,7 @@ void on_statusmessagechange(Tox *m, int friendnumber, uint8_t *string, uint16_t tox_getname(m, friendnumber, (uint8_t *) &nick); if (!nick[0]) - snprintf(nick, sizeof(nick), "%s", unknown_name); + snprintf(nick, sizeof(nick), "%s", UNKNOWN_NAME); wprintw(prompt->window, "\n%s set note to: %s\n", nick, string); } @@ -118,7 +118,7 @@ void on_statuschange(Tox *m, int friendnumber, TOX_USERSTATUS status, void *user tox_getname(m, friendnumber, (uint8_t *) &nick); if (!nick[0]) - snprintf(nick, sizeof(nick), "%s", unknown_name); + snprintf(nick, sizeof(nick), "%s", UNKNOWN_NAME); switch(status) { case TOX_USERSTATUS_NONE: @@ -127,16 +127,16 @@ void on_statuschange(Tox *m, int friendnumber, TOX_USERSTATUS status, void *user case TOX_USERSTATUS_BUSY: wprintw(prompt->window, "\n%s set status to ", nick); - wattron(prompt->window, COLOR_PAIR(3)); + wattron(prompt->window, COLOR_PAIR(RED)); wprintw(prompt->window, "[Busy]\n"); - wattroff(prompt->window, COLOR_PAIR(3)); + wattroff(prompt->window, COLOR_PAIR(RED)); break; case TOX_USERSTATUS_AWAY: wprintw(prompt->window, "\n%s set status to ", nick); - wattron(prompt->window, COLOR_PAIR(5)); + wattron(prompt->window, COLOR_PAIR(YELLOW)); wprintw(prompt->window, "[Away]\n"); - wattroff(prompt->window, COLOR_PAIR(5)); + wattroff(prompt->window, COLOR_PAIR(YELLOW)); break; } @@ -247,15 +247,15 @@ static void draw_bar() static int odd = 0; int blinkrate = 30; - attron(COLOR_PAIR(4)); + attron(COLOR_PAIR(BLUE)); mvhline(LINES - 2, 0, '_', COLS); - attroff(COLOR_PAIR(4)); + attroff(COLOR_PAIR(BLUE)); move(LINES - 1, 0); - attron(COLOR_PAIR(4) | A_BOLD); + attron(COLOR_PAIR(BLUE) | A_BOLD); printw(" TOXIC " TOXICVER " |"); - attroff(COLOR_PAIR(4) | A_BOLD); + attroff(COLOR_PAIR(BLUE) | A_BOLD); int i; @@ -267,13 +267,13 @@ static void draw_bar() odd = (odd + 1) % blinkrate; if (windows[i].blink && (odd < (blinkrate / 2))) - attron(COLOR_PAIR(3)); + attron(COLOR_PAIR(RED)); clrtoeol(); printw(" %s", windows[i].title); if (windows[i].blink && (odd < (blinkrate / 2))) - attroff(COLOR_PAIR(3)); + attroff(COLOR_PAIR(RED)); if (windows + i == active_window) { attroff(A_BOLD); From 369233ba04012caf5b022c9e72e1f530e66a6ca7 Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Thu, 5 Sep 2013 01:34:23 -0400 Subject: [PATCH 3/3] remove superfluous prompt alerts and some visual changes --- src/chat.c | 59 ++++++++++++++++++++++-------------------------- src/friendlist.c | 11 ++++----- src/prompt.c | 37 ++++++++++++++++++++---------- src/windows.c | 55 +++++++++++--------------------------------- 4 files changed, 70 insertions(+), 92 deletions(-) diff --git a/src/chat.c b/src/chat.c index e13ba74..da97cda 100644 --- a/src/chat.c +++ b/src/chat.c @@ -136,32 +136,27 @@ static void chat_onStatusChange(ToxWindow *self, Tox *m, int num, TOX_USERSTATUS if (ctx->friendnum != num) return; - wattron(ctx->history, COLOR_PAIR(CYAN)); - wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); - wattroff(ctx->history, COLOR_PAIR(CYAN)); + char *status_msg = NULL; + int colour = 0; - switch(status) { + if (status == TOX_USERSTATUS_BUSY) { + status_msg = "[Busy]"; + colour = RED; + } + else if (status == TOX_USERSTATUS_AWAY) { + status_msg = "[Away]"; + colour = YELLOW; + } - case TOX_USERSTATUS_NONE: - wprintw(ctx->history, "* Chat partner set status to "); - wattron(ctx->history, COLOR_PAIR(GREEN)); - wprintw(ctx->history, "[Online]\n"); - wattroff(ctx->history, COLOR_PAIR(GREEN)); - break; + if (status_msg != NULL) { + wattron(ctx->history, COLOR_PAIR(CYAN)); + wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); + wattroff(ctx->history, COLOR_PAIR(CYAN)); - case TOX_USERSTATUS_BUSY: - wprintw(ctx->history, "* Chat partner set status to "); - wattron(ctx->history, COLOR_PAIR(RED)); - wprintw(ctx->history, "[Busy]\n"); - wattroff(ctx->history, COLOR_PAIR(RED)); - break; - - case TOX_USERSTATUS_AWAY: - wprintw(ctx->history, "* Chat partner set status to "); - wattron(ctx->history, COLOR_PAIR(YELLOW)); - wprintw(ctx->history, "[Away]\n"); - wattroff(ctx->history, COLOR_PAIR(YELLOW)); - break; + wprintw(ctx->history, "* Chat partner set status to: "); + wattron(ctx->history, COLOR_PAIR(colour) | A_BOLD); + wprintw(ctx->history, "%s\n", status_msg); + wattroff(ctx->history, COLOR_PAIR(colour) | A_BOLD); } } @@ -378,26 +373,26 @@ void execute(ToxWindow *self, ChatContext *ctx, Tox *m, char *cmd) if (!strncmp(status, "online", strlen("online"))) { status_kind = TOX_USERSTATUS_NONE; - wprintw(ctx->history, "Status set to "); - wattron(ctx->history, COLOR_PAIR(GREEN)); + wprintw(ctx->history, "Status set to: "); + wattron(ctx->history, COLOR_PAIR(GREEN) | A_BOLD); wprintw(ctx->history, "[Online]\n"); - wattroff(ctx->history, COLOR_PAIR(GREEN)); + wattroff(ctx->history, COLOR_PAIR(GREEN) | A_BOLD); } else if (!strncmp(status, "away", strlen("away"))) { status_kind = TOX_USERSTATUS_AWAY; - wprintw(ctx->history, "Status set to "); - wattron(ctx->history, COLOR_PAIR(YELLOW)); + wprintw(ctx->history, "Status set to: "); + wattron(ctx->history, COLOR_PAIR(YELLOW) | A_BOLD); wprintw(ctx->history, "[Away]\n"); - wattroff(ctx->history, COLOR_PAIR(YELLOW)); + wattroff(ctx->history, COLOR_PAIR(YELLOW) | A_BOLD); } else if (!strncmp(status, "busy", strlen("busy"))) { status_kind = TOX_USERSTATUS_BUSY; - wprintw(ctx->history, "Status set to "); - wattron(ctx->history, COLOR_PAIR(RED)); + wprintw(ctx->history, "Status set to: "); + wattron(ctx->history, COLOR_PAIR(RED) | A_BOLD); wprintw(ctx->history, "[Busy]\n"); - wattroff(ctx->history, COLOR_PAIR(RED)); + wattroff(ctx->history, COLOR_PAIR(RED) | A_BOLD); } else { diff --git a/src/friendlist.c b/src/friendlist.c index f79267b..3488d9f 100644 --- a/src/friendlist.c +++ b/src/friendlist.c @@ -93,7 +93,7 @@ int friendlist_onFriendAdded(Tox *m, int num) friends[i].active = true; friends[i].chatwin = -1; friends[i].online = false; - friends[i].status = TOX_USERSTATUS_INVALID; + friends[i].status = TOX_USERSTATUS_NONE; if (tox_getname(m, num, friends[i].name) != 0 || friends[i].name[0] == '\0') strcpy((char *) friends[i].name, "unknown"); @@ -150,10 +150,9 @@ static void delete_friend(Tox *m, ToxWindow *self, int f_num, wint_t key) break; } - if (store_data(m, DATA_FILE)) - wprintw(self->window, "\nFailed to store messenger data\n"); - num_friends = i; + + store_data(m, DATA_FILE); select_friend(m, KEY_DOWN); } @@ -212,9 +211,9 @@ static void friendlist_onDraw(ToxWindow *self, Tox *m) } wprintw(self->window, "["); - wattron(self->window, COLOR_PAIR(colour)); + wattron(self->window, COLOR_PAIR(colour) | A_BOLD); wprintw(self->window, "O"); - wattroff(self->window, COLOR_PAIR(colour)); + wattroff(self->window, COLOR_PAIR(colour) | A_BOLD); wprintw(self->window, "] %s", friends[i].name); if (friends[i].statusmsg[0]) diff --git a/src/prompt.c b/src/prompt.c index 913c169..552c8d5 100644 --- a/src/prompt.c +++ b/src/prompt.c @@ -314,7 +314,8 @@ void cmd_nick(ToxWindow *self, Tox *m, int argc, char **argv) } nick = argv[1]; - nick[strlen(++nick)-1] = L'\0'; + if (nick[0] == '\"') + nick[strlen(++nick)-1] = L'\0'; tox_setname(m, (uint8_t *) nick, strlen(nick) + 1); wprintw(self->window, "Nickname set to: %s\n", nick); @@ -358,19 +359,31 @@ void cmd_status(ToxWindow *self, Tox *m, int argc, char **argv) if (!strncmp(status, "online", strlen("online"))) { status_kind = TOX_USERSTATUS_NONE; - status_text = "Online"; - } else if (!strncmp(status, "away", strlen("away"))) { - status_kind = TOX_USERSTATUS_AWAY; - status_text = "Away"; - } else if (!strncmp(status, "busy", strlen("busy"))) { - status_kind = TOX_USERSTATUS_BUSY; - status_text = "Busy"; - } else { - wprintw(self->window, "Invalid status.\n"); - return; + wprintw(self->window, "Status set to: "); + wattron(self->window, COLOR_PAIR(GREEN) | A_BOLD); + wprintw(self->window, "[Online]\n"); + wattroff(self->window, COLOR_PAIR(GREEN) | A_BOLD); } - wprintw(self->window, "Status set to: %s\n", status_text); + else if (!strncmp(status, "away", strlen("away"))) { + status_kind = TOX_USERSTATUS_AWAY; + wprintw(self->window, "Status set to: "); + wattron(self->window, COLOR_PAIR(YELLOW) | A_BOLD); + wprintw(self->window, "[Away]\n"); + wattroff(self->window, COLOR_PAIR(YELLOW) | A_BOLD); + } + + else if (!strncmp(status, "busy", strlen("busy"))) { + status_kind = TOX_USERSTATUS_BUSY; + wprintw(self->window, "Status set to: "); + wattron(self->window, COLOR_PAIR(RED) | A_BOLD); + wprintw(self->window, "[Busy]\n"); + wattroff(self->window, COLOR_PAIR(RED) | A_BOLD); + } + + else + wprintw(self->window, "Invalid status.\n"); + tox_set_userstatus(m, status_kind); if (msg != NULL) { diff --git a/src/windows.c b/src/windows.c index a184148..42a66cd 100644 --- a/src/windows.c +++ b/src/windows.c @@ -32,7 +32,7 @@ void on_request(uint8_t *public_key, uint8_t *data, uint16_t length, void *userd } wprintw(prompt->window, "\nWith the message: %s\n", data); - wprintw(prompt->window, "\nUse \"accept %d\" to accept it.\n", n); + wprintw(prompt->window, "Type \"accept %d\" to accept it.\n", n); for (i = 0; i < MAX_WINDOWS_NUM; ++i) { if (windows[i].onFriendRequest != NULL) @@ -48,10 +48,18 @@ void on_connectionchange(Tox *m, int friendnumber, uint8_t status, void *userdat if (!nick[0]) snprintf(nick, sizeof(nick), "%s", UNKNOWN_NAME); - if (status == 1) - wprintw(prompt->window, "\n%s has come online\n", nick, friendnumber); - else - wprintw(prompt->window, "\n%s went offline\n", nick, friendnumber); + if (status == 1) { + wattron(prompt->window, A_BOLD); + wprintw(prompt->window, "\n%s ", nick); + wattroff(prompt->window, A_BOLD); + wprintw(prompt->window, "has come online\n"); + } else { + wattron(prompt->window, A_BOLD); + wprintw(prompt->window, "\n%s ", nick); + wattroff(prompt->window, A_BOLD); + wprintw(prompt->window, "has gone offline\n"); + + } int i; @@ -93,17 +101,6 @@ void on_nickchange(Tox *m, int friendnumber, uint8_t *string, uint16_t length, v void on_statusmessagechange(Tox *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata) { - /* Don't show default "Online" status message */ - if (strncmp(string, "Online", strlen(string))) { - uint8_t nick[TOX_MAX_NAME_LENGTH] = {0}; - tox_getname(m, friendnumber, (uint8_t *) &nick); - - if (!nick[0]) - snprintf(nick, sizeof(nick), "%s", UNKNOWN_NAME); - - wprintw(prompt->window, "\n%s set note to: %s\n", nick, string); - } - int i; for (i = 0; i < MAX_WINDOWS_NUM; ++i) { @@ -114,32 +111,6 @@ void on_statusmessagechange(Tox *m, int friendnumber, uint8_t *string, uint16_t void on_statuschange(Tox *m, int friendnumber, TOX_USERSTATUS status, void *userdata) { - uint8_t nick[TOX_MAX_NAME_LENGTH] = {0}; - tox_getname(m, friendnumber, (uint8_t *) &nick); - - if (!nick[0]) - snprintf(nick, sizeof(nick), "%s", UNKNOWN_NAME); - - switch(status) { - case TOX_USERSTATUS_NONE: - /* Disabled because it spams a second messages when user comes online */ - break; - - case TOX_USERSTATUS_BUSY: - wprintw(prompt->window, "\n%s set status to ", nick); - wattron(prompt->window, COLOR_PAIR(RED)); - wprintw(prompt->window, "[Busy]\n"); - wattroff(prompt->window, COLOR_PAIR(RED)); - break; - - case TOX_USERSTATUS_AWAY: - wprintw(prompt->window, "\n%s set status to ", nick); - wattron(prompt->window, COLOR_PAIR(YELLOW)); - wprintw(prompt->window, "[Away]\n"); - wattroff(prompt->window, COLOR_PAIR(YELLOW)); - break; - } - int i; for (i = 0; i < MAX_WINDOWS_NUM; ++i) {