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

Hiding XMPP and Ncurses from app code

This commit is contained in:
James Booth 2012-02-07 00:37:42 +00:00
parent 4d190a9c43
commit bb3878bc76
3 changed files with 73 additions and 64 deletions

65
app.c
View File

@ -1,19 +1,12 @@
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include <strophe/strophe.h>
#include <ncurses.h> #include <ncurses.h>
#include "log.h" #include "log.h"
#include "windows.h" #include "windows.h"
#include "jabber.h" #include "jabber.h"
// window references
extern WINDOW *title_bar;
extern WINDOW *cmd_bar;
extern WINDOW *cmd_win;
extern WINDOW *main_win;
static void main_event_loop(void); static void main_event_loop(void);
void start_profanity(void) void start_profanity(void)
@ -26,88 +19,52 @@ void start_profanity(void)
break; break;
} else if (strncmp(cmd, "/connect ", 9) == 0) { } else if (strncmp(cmd, "/connect ", 9) == 0) {
char *user; char *user;
char passwd[20];
user = strndup(cmd+9, strlen(cmd)-9); user = strndup(cmd+9, strlen(cmd)-9);
bar_print_message("Enter password:"); bar_print_message("Enter password:");
char passwd[20];
cmd_get_password(passwd); cmd_get_password(passwd);
bar_print_message(user); bar_print_message(user);
jabber_connect(user, passwd); jabber_connect(user, passwd);
main_event_loop(); main_event_loop();
break; break;
} else { } else {
// echo ignore cmd_clear();
wclear(cmd_win);
wmove(cmd_win, 0, 0);
} }
} }
} }
static void main_event_loop(void) static void main_event_loop(void)
{ {
int cmd_y = 0; cmd_non_block();
int cmd_x = 0;
wtimeout(cmd_win, 0);
while(TRUE) { while(TRUE) {
int ch = ERR; int ch = ERR;
char command[100]; char command[100];
int size = 0; int size = 0;
// while not enter // while not enter, process all events, and try to get another char
while(ch != '\n') { while(ch != '\n') {
usleep(1); usleep(1);
// handle incoming messages // handle incoming messages
jabber_process_events(); jabber_process_events();
// move cursor back to cmd_win // get another character from the command box
getyx(cmd_win, cmd_y, cmd_x); cmd_poll_char(&ch, command, &size);
wmove(cmd_win, cmd_y, cmd_x);
// echo off, and get some more input
noecho();
ch = wgetch(cmd_win);
// if delete pressed, go back and delete it
if (ch == 127) {
if (size > 0) {
getyx(cmd_win, cmd_y, cmd_x);
wmove(cmd_win, cmd_y, cmd_x-1);
wdelch(cmd_win);
size--;
}
}
// else if not error or newline, show it and store it
else if (ch != ERR && ch != '\n') {
waddch(cmd_win, ch);
command[size++] = ch;
}
echo();
} }
// null terminate the input
command[size++] = '\0'; command[size++] = '\0';
// newline was hit, check if /quit command issued // newline was hit, check if /quit command issued
if (strcmp(command, "/quit") == 0) { if (strcmp(command, "/quit") == 0) {
break; break;
} else { } else {
// send the message
jabber_send(command); jabber_send(command);
show_outgoing_msg("me", command);
// show it in the main window cmd_clear();
wprintw(main_win, "me: %s\n", command);
wrefresh(main_win);
// move back to the command window and clear it
wclear(cmd_win);
wmove(cmd_win, 0, 0);
wrefresh(cmd_win);
} }
} }

View File

@ -3,10 +3,10 @@
#include "windows.h" #include "windows.h"
// window references // window references
extern WINDOW *title_bar; static WINDOW *title_bar;
extern WINDOW *cmd_bar; static WINDOW *cmd_bar;
extern WINDOW *cmd_win; static WINDOW *cmd_win;
extern WINDOW *main_win; static WINDOW *main_win;
static void create_title_bar(void); static void create_title_bar(void);
static void create_command_bar(void); static void create_command_bar(void);
@ -58,12 +58,66 @@ void show_incomming_msg(char *from, char *message)
wrefresh(main_win); wrefresh(main_win);
} }
void show_outgoing_msg(char *from, char* message)
{
char line[100];
sprintf(line, "%s: %s\n", from, message);
wprintw(main_win, line);
wrefresh(main_win);
}
void cmd_get_command_str(char *cmd) void cmd_get_command_str(char *cmd)
{ {
wmove(cmd_win, 0, 0); wmove(cmd_win, 0, 0);
wgetstr(cmd_win, cmd); wgetstr(cmd_win, cmd);
} }
void cmd_clear(void)
{
wclear(cmd_win);
wmove(cmd_win, 0, 0);
wrefresh(cmd_win);
}
void cmd_non_block(void)
{
wtimeout(cmd_win, 0);
}
void cmd_poll_char(int *ch, char command[], int *size)
{
int cmd_y = 0;
int cmd_x = 0;
// move cursor back to cmd_win
getyx(cmd_win, cmd_y, cmd_x);
wmove(cmd_win, cmd_y, cmd_x);
// echo off, and get some more input
noecho();
*ch = wgetch(cmd_win);
// if delete pressed, go back and delete it
if (*ch == 127) {
if (*size > 0) {
getyx(cmd_win, cmd_y, cmd_x);
wmove(cmd_win, cmd_y, cmd_x-1);
wdelch(cmd_win);
(*size)--;
}
}
// else if not error or newline, show it and store it
else if (*ch != ERR && *ch != '\n') {
waddch(cmd_win, *ch);
command[(*size)++] = *ch;
}
echo();
}
void cmd_get_password(char *passwd) void cmd_get_password(char *passwd)
{ {
wclear(cmd_win); wclear(cmd_win);

View File

@ -4,16 +4,14 @@
#include <strophe/strophe.h> #include <strophe/strophe.h>
#include <ncurses.h> #include <ncurses.h>
// windows
WINDOW *title_bar;
WINDOW *cmd_bar;
WINDOW *cmd_win;
WINDOW *main_win;
void gui_init(void); void gui_init(void);
void gui_close(void); void gui_close(void);
void show_incomming_msg(char *from, char *message); void show_incomming_msg(char *from, char *message);
void show_outgoing_msg(char *from, char *message);
void cmd_get_command_str(char *cmd); void cmd_get_command_str(char *cmd);
void cmd_poll_char(int *ch, char command[], int *size);
void cmd_clear(void);
void cmd_non_block(void);
void cmd_get_password(char *passwd); void cmd_get_password(char *passwd);
void bar_print_message(char *msg); void bar_print_message(char *msg);