1
0
mirror of https://github.com/profanity-im/profanity.git synced 2024-12-04 14:46:46 -05:00

Window per chat and incoming notifications

This commit is contained in:
James Booth 2012-02-08 22:26:43 +00:00
parent ab50d324c4
commit 17a284b24b
3 changed files with 125 additions and 65 deletions

46
app.c
View File

@ -7,9 +7,6 @@
#include "windows.h"
#include "jabber.h"
#define CHAT 0
#define CONS 1
static void main_event_loop(void);
void start_profanity(void)
@ -34,7 +31,6 @@ void start_profanity(void)
bar_print_message(user);
jabber_connect(user, passwd);
chat_show();
main_event_loop();
break;
} else {
@ -47,8 +43,6 @@ void start_profanity(void)
static void main_event_loop(void)
{
int showing = CHAT;
inp_non_block();
while(TRUE) {
@ -62,18 +56,30 @@ static void main_event_loop(void)
// handle incoming messages
jabber_process_events();
if (showing == CHAT) {
chat_show();
}
// determine if they changed windows
if (ch == KEY_F(1)) {
cons_show();
showing = CONS;
switch_to(0);
} else if (ch == KEY_F(2)) {
chat_show();
showing = CHAT;
switch_to(1);
} else if (ch == KEY_F(3)) {
switch_to(2);
} else if (ch == KEY_F(4)) {
switch_to(3);
} else if (ch == KEY_F(5)) {
switch_to(4);
} else if (ch == KEY_F(6)) {
switch_to(5);
} else if (ch == KEY_F(7)) {
switch_to(6);
} else if (ch == KEY_F(8)) {
switch_to(7);
} else if (ch == KEY_F(9)) {
switch_to(8);
} else if (ch == KEY_F(10)) {
switch_to(9);
}
// get another character from the command box
inp_poll_char(&ch, command, &size);
}
@ -86,20 +92,10 @@ static void main_event_loop(void)
break;
} else if (strncmp(command, "/help", 5) == 0) {
cons_help();
if (showing == CONS) {
cons_show();
}
inp_clear();
} else {
if (showing == CONS) {
cons_bad_command(command);
cons_show();
}
else {
jabber_send(command);
show_outgoing_msg("me", command);
chat_show();
}
jabber_send(command);
show_outgoing_msg("me", command);
inp_clear();
}
}

138
windows.c
View File

@ -1,5 +1,5 @@
#include <string.h>
#include <ncurses.h>
#include "windows.h"
// common window references
@ -7,16 +7,15 @@ static WINDOW *title_bar;
static WINDOW *inp_bar;
static WINDOW *inp_win;
// main windows
static WINDOW *chat_win;
static WINDOW *cons_win;
static struct prof_win wins[10];
static int curr_win = 0;
// main windows
static void create_title_bar(void);
static void create_input_bar(void);
static void create_input_window(void);
static void create_chat_window(void);
static void create_console_window(void);
static void create_windows(void);
void gui_init(void)
{
@ -44,8 +43,7 @@ void gui_init(void)
create_input_window();
wrefresh(inp_win);
create_chat_window();
create_console_window();
create_windows();
wmove(inp_win, 0, 0);
}
@ -55,12 +53,63 @@ void gui_close(void)
endwin();
}
void switch_to(int i)
{
touchwin(wins[i].win);
wrefresh(wins[i].win);
curr_win = i;
}
void show_incomming_msg(char *from, char *message)
{
char line[100];
sprintf(line, "%s: %s\n", from, message);
wprintw(chat_win, line);
// find the chat window for sender
int i;
for (i = 1; i < 10; i++)
if (strcmp(wins[i].from, from) == 0)
break;
// if we didn't find a window
if (i == 10) {
// find the first unused one
for (i = 1; i < 10; i++)
if (strcmp(wins[i].from, "") == 0)
break;
// set it up and print the message to it
strcpy(wins[i].from, from);
wclear(wins[i].win);
wprintw(wins[i].win, line);
// signify active window in status bar
mvwprintw(inp_bar, 0, 30 + i, "%d", i+1);
touchwin(inp_bar);
wrefresh(inp_bar);
// if its the current window, draw it
if (curr_win == i) {
touchwin(wins[i].win);
wrefresh(wins[i].win);
}
}
// otherwise
else {
// add the line to the senders window
wprintw(wins[i].win, line);
// signify active window in status bar
mvwprintw(inp_bar, 0, 30 + i, "%d", i+1);
touchwin(inp_bar);
wrefresh(inp_bar);
// if its the current window, draw it
if (curr_win == i) {
touchwin(wins[i].win);
wrefresh(wins[i].win);
}
}
}
void show_outgoing_msg(char *from, char* message)
@ -68,7 +117,7 @@ void show_outgoing_msg(char *from, char* message)
char line[100];
sprintf(line, "%s: %s\n", from, message);
wprintw(chat_win, line);
// wprintw(chat_win, line);
}
void inp_get_command_str(char *cmd)
@ -113,7 +162,18 @@ void inp_poll_char(int *ch, char command[], int *size)
}
// else if not error or newline, show it and store it
else if (*ch != ERR && *ch != '\n' && *ch != KEY_F(1) && *ch != KEY_F(2)) {
else if (*ch != ERR &&
*ch != '\n' &&
*ch != KEY_F(1) &&
*ch != KEY_F(2) &&
*ch != KEY_F(3) &&
*ch != KEY_F(4) &&
*ch != KEY_F(5) &&
*ch != KEY_F(6) &&
*ch != KEY_F(7) &&
*ch != KEY_F(8) &&
*ch != KEY_F(9) &&
*ch != KEY_F(10)) {
waddch(inp_win, *ch);
command[(*size)++] = *ch;
}
@ -137,27 +197,21 @@ void bar_print_message(char *msg)
void cons_help(void)
{
waddstr(cons_win, "Help\n");
waddstr(cons_win, "----\n");
waddstr(cons_win, "/quit - Quits Profanity.\n");
waddstr(cons_win, "/connect <username@host> - Login to jabber.\n");
waddstr(wins[9].win, "Help\n");
waddstr(wins[0].win, "----\n");
waddstr(wins[0].win, "/quit - Quits Profanity.\n");
waddstr(wins[0].win, "/connect <username@host> - Login to jabber.\n");
}
void cons_bad_command(char *cmd)
{
wprintw(cons_win, "Unknown command: %s\n", cmd);
wprintw(wins[0].win, "Unknown command: %s\n", cmd);
}
void cons_show(void)
{
touchwin(cons_win);
wrefresh(cons_win);
}
void chat_show(void)
{
touchwin(chat_win);
wrefresh(chat_win);
touchwin(wins[0].win);
wrefresh(wins[0].win);
}
static void create_title_bar(void)
@ -190,25 +244,29 @@ static void create_input_window(void)
keypad(inp_win, TRUE);
}
static void create_chat_window(void)
static void create_windows(void)
{
int rows, cols;
getmaxyx(stdscr, rows, cols);
chat_win = newwin(rows-3, cols, 1, 0);
scrollok(chat_win, TRUE);
}
static void create_console_window(void)
{
int rows, cols;
getmaxyx(stdscr, rows, cols);
cons_win = newwin(rows-3, cols, 1, 0);
scrollok(cons_win, TRUE);
// create the console window in 0
struct prof_win cons;
strcpy(cons.from, "_cons");
cons.win = newwin(rows-3, cols, 1, 0);
scrollok(cons.win, TRUE);
waddstr(cons_win, "Welcome to Profanity.\n");
touchwin(cons_win);
wrefresh(cons_win);
waddstr(cons.win, "Welcome to Profanity.\n");
touchwin(cons.win);
wrefresh(cons.win);
wins[0] = cons;
// create the chat windows
int i;
for (i = 1; i < 10; i++) {
struct prof_win chat;
strcpy(chat.from, "");
chat.win = newwin(rows-3, cols, 1, 0);
scrollok(chat.win, TRUE);
wins[i] = chat;
}
}

View File

@ -4,8 +4,14 @@
#include <strophe/strophe.h>
#include <ncurses.h>
struct prof_win {
char from[70];
WINDOW *win;
};
void gui_init(void);
void gui_close(void);
void switch_to(int i);
void show_incomming_msg(char *from, char *message);
void show_outgoing_msg(char *from, char *message);
void inp_get_command_str(char *cmd);