Changed UI

This commit is contained in:
James Booth
2012-02-05 17:53:57 +00:00
parent 39627be1f5
commit 2906e08f13

View File

@@ -12,13 +12,11 @@ extern FILE *logp;
// area for log message in profanity // area for log message in profanity
static const char *prof = "prof"; static const char *prof = "prof";
// chat windows // static windows
static WINDOW *incoming_border; static WINDOW *title_bar;
static WINDOW *outgoing_border; static WINDOW *cmd_bar;
static WINDOW *incoming; static WINDOW *cmd_win;
static WINDOW *outgoing; static WINDOW *main_win;
static int incoming_ypos = 0;
static int inc_scroll_max = 0;
// message handlers // message handlers
int in_message_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, int in_message_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza,
@@ -34,18 +32,14 @@ void conn_handler(xmpp_conn_t * const conn, const xmpp_conn_event_t status,
// curses functions // curses functions
void init(void); void init(void);
void print_title(void); void create_title_bar(void);
void close(void); void create_command_bar(void);
void create_command_window(void);
WINDOW *create_win(int, int, int, int, int); void create_main_window(void);
void create_conversation(void);
int main(void) int main(void)
{ {
int ypos = 2; char cmd[50];
int xpos = 2;
char user[50];
char passwd[50];
xmpp_ctx_t *ctx; xmpp_ctx_t *ctx;
xmpp_conn_t *conn; xmpp_conn_t *conn;
@@ -54,51 +48,83 @@ int main(void)
logp = fopen("profanity.log", "a"); logp = fopen("profanity.log", "a");
logmsg(prof, "Starting Profanity..."); logmsg(prof, "Starting Profanity...");
// initialise curses
init(); init();
print_title();
ypos += 2;
mvprintw(ypos, xpos, "Enter jabber ID (username@host) : ");
echo();
getstr(user);
ypos += 2;
mvprintw(ypos, xpos, "Enter passwd : ");
noecho();
getstr(passwd);
echo();
char loginmsg[100];
sprintf(loginmsg, "User <%s> logged in", user);
logmsg(prof, loginmsg);
//fprintf(logp, "Log in, user = %s\n", user);
xmpp_initialize();
log = xmpp_get_file_logger();
ctx = xmpp_ctx_new(NULL, log);
conn = xmpp_conn_new(ctx);
xmpp_conn_set_jid(conn, user);
xmpp_conn_set_pass(conn, passwd);
xmpp_connect_client(conn, NULL, 0, conn_handler, ctx);
clear();
print_title();
refresh(); refresh();
move(3, 0);
// create windows
create_title_bar();
wrefresh(title_bar);
create_command_bar();
wrefresh(cmd_bar);
create_command_window();
wrefresh(cmd_win);
create_main_window();
wrefresh(main_win);
create_conversation(); // set cursor in command window and loop in input
wmove(cmd_win, 0, 0);
while (TRUE) {
wgetstr(cmd_win, cmd);
// handle quit
if (strcmp(cmd, "/quit") == 0) {
break;
// handle connect
} else if (strncmp(cmd, "/connect ", 9) == 0) {
char *user;
char passwd[20];
xmpp_run(ctx); user = strndup(cmd+9, strlen(cmd)-9);
mvwprintw(cmd_bar, 0, 0, "Enter password:");
wrefresh(cmd_bar);
wclear(cmd_win);
noecho();
mvwgetstr(cmd_win, 0, 0, passwd);
echo();
xmpp_conn_release(conn); wprintw(main_win, "Connecting as: %s/%s\n", user, passwd);
xmpp_ctx_free(ctx); wrefresh(main_win);
wclear(cmd_win);
wclear(cmd_bar);
wrefresh(cmd_bar);
wmove(cmd_win, 0, 0);
wrefresh(cmd_win);
xmpp_shutdown(); char loginmsg[100];
sprintf(loginmsg, "User <%s> logged in", user);
logmsg(prof, loginmsg);
xmpp_initialize();
log = xmpp_get_file_logger();
ctx = xmpp_ctx_new(NULL, log);
conn = xmpp_conn_new(ctx);
close(); xmpp_conn_set_jid(conn, user);
xmpp_conn_set_pass(conn, passwd);
xmpp_connect_client(conn, NULL, 0, conn_handler, ctx);
xmpp_run(ctx);
xmpp_conn_release(conn);
xmpp_ctx_free(ctx);
xmpp_shutdown();
} else {
// echo it to the main window
wprintw(main_win, "%s\n", cmd);
wrefresh(main_win);
wclear(cmd_win);
wmove(cmd_win, 0, 0);
}
}
endwin();
fclose(logp); fclose(logp);
return 0; return 0;
@@ -116,18 +142,11 @@ int in_message_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza,
intext = xmpp_stanza_get_text(xmpp_stanza_get_child_by_name(stanza, "body")); intext = xmpp_stanza_get_text(xmpp_stanza_get_child_by_name(stanza, "body"));
char in_line[100]; char in_line[200];
sprintf(in_line, "%s: %s\n", xmpp_stanza_get_attribute(stanza, "from"), intext); sprintf(in_line, "%s: %s\n", xmpp_stanza_get_attribute(stanza, "from"), intext);
if (incoming_ypos == inc_scroll_max-1) { wprintw(main_win, in_line);
scroll(incoming); wrefresh(main_win);
mvwaddstr(incoming, incoming_ypos-1, 0, in_line);
} else {
mvwaddstr(incoming, incoming_ypos, 0, in_line);
incoming_ypos++;
}
wrefresh(incoming);
return 1; return 1;
} }
@@ -138,10 +157,10 @@ int out_message_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza,
char replytxt[100];; char replytxt[100];;
echo(); echo();
wgetstr(outgoing, replytxt); wgetstr(cmd_win, replytxt);
wclear(cmd_win);
wrefresh(outgoing); wmove(cmd_win, 0, 0);
refresh(); wrefresh(cmd_win);
xmpp_stanza_t *reply, *body, *text; xmpp_stanza_t *reply, *body, *text;
xmpp_ctx_t *ctx = (xmpp_ctx_t*)userdata; xmpp_ctx_t *ctx = (xmpp_ctx_t*)userdata;
@@ -163,6 +182,10 @@ int out_message_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza,
xmpp_send(conn, reply); xmpp_send(conn, reply);
xmpp_stanza_release(reply); xmpp_stanza_release(reply);
wprintw(main_win, "me: %s\n", replytxt);
wrefresh(main_win);
wmove(cmd_win, 0, 0);
return 1; return 1;
} }
@@ -207,76 +230,39 @@ void init(void)
attron(COLOR_PAIR(1)); attron(COLOR_PAIR(1));
} }
WINDOW *create_win(int height, int width, int starty, int startx, int border) void create_title_bar(void)
{
WINDOW *local_win;
local_win = newwin(height, width, starty, startx);
if (border)
box(local_win, 0 , 0);
wrefresh(local_win);
return local_win;
}
void create_conversation()
{ {
char *title = "Profanity";
int rows, cols; int rows, cols;
char *title = "PROFANITY";
getmaxyx(stdscr, rows, cols); getmaxyx(stdscr, rows, cols);
curs_set(0); title_bar = newwin(1, cols, 0, 0);
attron(COLOR_PAIR(3)); wbkgd(title_bar, COLOR_PAIR(3));
mvprintw(1, (cols - strlen(title))/2, title); mvwprintw(title_bar, 0, 0, title);
attroff(COLOR_PAIR(3));
int in_height, in_width, in_y, in_x;
int out_height, out_width, out_y, out_x;
out_height = 5;
out_width = cols;
out_y = rows - 5;
out_x = 0;
in_height = (rows - 3) - (out_height);
in_width = cols;
in_y = 3;
in_x = 0;
incoming_border = create_win(in_height, in_width, in_y, in_x, 1);
outgoing_border = create_win(out_height, out_width, out_y, out_x, 1);
inc_scroll_max = in_height-2;
incoming = create_win(in_height-2, in_width-2, in_y+1, in_x+1, 0);
scrollok(incoming, TRUE);
outgoing = create_win(out_height-2, out_width-2, out_y+1, out_x+1, 0);
scrollok(outgoing, TRUE);
} }
void print_title(void) void create_command_bar(void)
{ {
int rows, cols; int rows, cols;
char *title = "PROFANITY";
getmaxyx(stdscr, rows, cols); getmaxyx(stdscr, rows, cols);
attron(COLOR_PAIR(3)); cmd_bar = newwin(1, cols, rows-2, 0);
mvprintw(1, (cols - strlen(title))/2, title); wbkgd(cmd_bar, COLOR_PAIR(3));
attroff(COLOR_PAIR(3));
} }
void close(void) void create_command_window(void)
{ {
int rows, cols; int rows, cols;
char *exit_msg = "< HIT ANY KEY TO EXIT >";
getmaxyx(stdscr, rows, cols); getmaxyx(stdscr, rows, cols);
attron(A_BLINK); cmd_win = newwin(1, cols, rows-1, 0);
curs_set(0); }
mvprintw(rows-10, (cols - strlen(exit_msg))/2, exit_msg);
void create_main_window(void)
refresh(); {
getch(); int rows, cols;
endwin(); getmaxyx(stdscr, rows, cols);
main_win = newwin(rows-3, cols, 1, 0);
} }