From a98ec22fd6632173ec49d383c6ae68c3702c3d5d Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Thu, 28 Nov 2013 19:45:28 -0500 Subject: [PATCH] improve window alert functionality --- src/chat.c | 10 +++++----- src/groupchat.c | 4 +++- src/main.c | 4 ++-- src/misc_tools.c | 13 +++++++++---- src/misc_tools.h | 4 ++-- src/prompt.c | 2 +- src/toxic_windows.h | 8 +++++++- src/windows.c | 36 +++++++++++++++++++++--------------- 8 files changed, 50 insertions(+), 31 deletions(-) diff --git a/src/chat.c b/src/chat.c index 6a66d3a..bb4708d 100644 --- a/src/chat.c +++ b/src/chat.c @@ -41,7 +41,7 @@ static void chat_onMessage(ToxWindow *self, Tox *m, int num, uint8_t *msg, uint1 } else wprintw(ctx->history, "%s\n", msg); - alert_window(self); + alert_window(self, WINDOW_ALERT_1, true); } static void chat_onConnectionChange(ToxWindow *self, Tox *m, int num, uint8_t status) @@ -69,7 +69,7 @@ static void chat_onAction(ToxWindow *self, Tox *m, int num, uint8_t *action, uin wprintw(ctx->history, "* %s %s\n", nick, action); wattroff(ctx->history, COLOR_PAIR(YELLOW)); - alert_window(self); + alert_window(self, WINDOW_ALERT_1, true); } static void chat_onNickChange(ToxWindow *self, int num, uint8_t *nick, uint16_t len) @@ -158,7 +158,7 @@ static void chat_onFileSendRequest(ToxWindow *self, Tox *m, int num, uint8_t fil friends[num].file_receiver.pending[filenum] = true; strcpy(friends[num].file_receiver.filenames[filenum], filename); - alert_window(self); + alert_window(self, WINDOW_ALERT_2, true); } static void chat_onFileControl(ToxWindow *self, Tox *m, int num, uint8_t receive_send, @@ -191,7 +191,7 @@ static void chat_onFileControl(ToxWindow *self, Tox *m, int num, uint8_t receive break; } - alert_window(self); + alert_window(self, WINDOW_ALERT_2, true); } static void chat_onFileData(ToxWindow *self, Tox *m, int num, uint8_t filenum, uint8_t *data, @@ -242,7 +242,7 @@ static void chat_onGroupInvite(ToxWindow *self, Tox *m, int friendnumber, uint8_ memcpy(friends[friendnumber].pending_groupchat, group_pub_key, TOX_CLIENT_ID_SIZE); wprintw(ctx->history, "Type \"/join\" to join the chat.\n"); - alert_window(self); + alert_window(self, WINDOW_ALERT_2, true); } static void send_action(ToxWindow *self, ChatContext *ctx, Tox *m, uint8_t *action) { diff --git a/src/groupchat.c b/src/groupchat.c index 931334b..cf85899 100644 --- a/src/groupchat.c +++ b/src/groupchat.c @@ -120,7 +120,7 @@ static void groupchat_onGroupMessage(ToxWindow *self, Tox *m, int groupnum, int } else wprintw(ctx->history, "%s\n", msg); - self->blink = true; + alert_window(self, WINDOW_ALERT_1, false); } /* Puts two copies of peerlist in chat instance */ @@ -214,6 +214,8 @@ static void groupchat_onGroupNamelistChange(ToxWindow *self, Tox *m, int groupnu wattroff(ctx->history, COLOR_PAIR(MAGENTA)); break; } + + alert_window(self, WINDOW_ALERT_2, false); } static void groupchat_onKey(ToxWindow *self, Tox *m, wint_t key) diff --git a/src/main.c b/src/main.c index 8f27df2..6ba902b 100644 --- a/src/main.c +++ b/src/main.c @@ -384,7 +384,7 @@ static void do_file_senders(Tox *m) if (ctx != NULL) { wprintw(ctx->history, "File transfer for '%s' timed out.\n", pathname); - alert_window(file_senders[i].toxwin); + alert_window(file_senders[i].toxwin, WINDOW_ALERT_2, true); } tox_file_sendcontrol(m, friendnum, 0, filenum, TOX_FILECONTROL_KILL, 0, 0); @@ -408,7 +408,7 @@ static void do_file_senders(Tox *m) if (ctx != NULL) { wprintw(ctx->history, "File '%s' successfuly sent.\n", pathname); - alert_window(file_senders[i].toxwin); + alert_window(file_senders[i].toxwin, WINDOW_ALERT_2, true); } tox_file_sendcontrol(m, friendnum, 0, filenum, TOX_FILECONTROL_FINISHED, 0, 0); diff --git a/src/misc_tools.c b/src/misc_tools.c index 5e23043..0628cdc 100644 --- a/src/misc_tools.c +++ b/src/misc_tools.c @@ -107,11 +107,16 @@ bool timed_out(uint64_t timestamp, uint64_t curtime, uint64_t timeout) return timestamp + timeout <= curtime; } -/* Beeps and makes window tab blink */ -void alert_window(ToxWindow *self) +/* Colours the window tab according to type. Beeps if is_beep is true */ +void alert_window(ToxWindow *self, int type, bool is_beep) { - self->blink = true; - beep(); + if (type == WINDOW_ALERT_1) + self->alert1 = true; + else if(type == WINDOW_ALERT_2) + self->alert2 = true; + + if (is_beep) + beep(); } /* case-insensitive string compare function for use with qsort - same return logic as strcmp */ diff --git a/src/misc_tools.h b/src/misc_tools.h index 789ff62..c6b7a58 100644 --- a/src/misc_tools.h +++ b/src/misc_tools.h @@ -25,8 +25,8 @@ char *wc_to_char(wchar_t ch); /* Returns true if connection has timed out, false otherwise */ bool timed_out(uint64_t timestamp, uint64_t timeout, uint64_t curtime); -/* Beeps and makes window tab blink */ -void alert_window(ToxWindow *self); +/* Colours the window tab according to type. Beeps if is_beep is true */ +void alert_window(ToxWindow *self, int type, bool is_beep); /* case-insensitive string compare function for use with qsort - same return logic as strcmp */ int name_compare(const void *nick1, const void *nick2); diff --git a/src/prompt.c b/src/prompt.c index b30bf96..d94cbda 100644 --- a/src/prompt.c +++ b/src/prompt.c @@ -230,7 +230,7 @@ static void prompt_onFriendRequest(ToxWindow *self, uint8_t *key, uint8_t *data, } wprintw(self->window, "Type \"/accept %d\" to accept it.\n", n); - alert_window(self); + alert_window(self, WINDOW_ALERT_2, true); } void prompt_init_statusbar(ToxWindow *self, Tox *m) diff --git a/src/toxic_windows.h b/src/toxic_windows.h index dcdc505..b81f0d5 100644 --- a/src/toxic_windows.h +++ b/src/toxic_windows.h @@ -42,6 +42,11 @@ enum { BLACK, }; +enum { + WINDOW_ALERT_1, + WINDOW_ALERT_2, +}; + /* Fixes text color problem on some terminals. Uncomment if necessary */ //#define URXVT_FIX @@ -68,7 +73,8 @@ struct ToxWindow_ { void(*onFileData)(ToxWindow *, Tox *, int, uint8_t, uint8_t *, uint16_t); char name[TOX_MAX_NAME_LENGTH]; - bool blink; + bool alert1; + bool alert2; int num; int x; diff --git a/src/windows.c b/src/windows.c index cbda523..57d0242 100644 --- a/src/windows.c +++ b/src/windows.c @@ -263,12 +263,25 @@ ToxWindow *init_windows(Tox *mToAssign) return prompt; } -#define TAB_BLINKRATE 30 +static void draw_window_tab(ToxWindow toxwin) +{ + /* alert1 takes priority */ + if (toxwin.alert1) + attron(COLOR_PAIR(RED)); + else if (toxwin.alert2) + attron(COLOR_PAIR(BLUE)); + + clrtoeol(); + printw(" [%s]", toxwin.name); + + if (toxwin.alert1) + attroff(COLOR_PAIR(RED)); + else if (toxwin.alert2) + attroff(COLOR_PAIR(BLUE)); +} static void draw_bar(void) { - static int odd = 0; - attron(COLOR_PAIR(BLUE)); mvhline(LINES - 2, 0, '_', COLS); attroff(COLOR_PAIR(BLUE)); @@ -291,18 +304,9 @@ static void draw_bar(void) attron(A_BOLD); } - odd = (odd + 1) % TAB_BLINKRATE; + draw_window_tab(windows[i]); - if (windows[i].blink && (odd < (TAB_BLINKRATE / 2))) - attron(COLOR_PAIR(RED)); - - clrtoeol(); - printw(" [%s]", windows[i].name); - - if (windows[i].blink && (odd < (TAB_BLINKRATE / 2))) - attroff(COLOR_PAIR(RED)); - - if (windows + i == active_window) { + if (windows + i == active_window) { #ifdef URXVT_FIX attroff(A_BOLD | COLOR_PAIR(GREEN)); } else { @@ -325,7 +329,9 @@ void draw_active_window(Tox *m) wresize(a->window, LINES - 2, COLS); #endif - a->blink = false; + a->alert1 = false; + a->alert2 = false; + draw_bar(); a->onDraw(a, m);