1
0
mirror of https://github.com/profanity-im/profanity.git synced 2024-11-03 19:37:16 -05:00

Pulled out main event loop

This commit is contained in:
James Booth 2012-02-09 22:50:19 +00:00
parent 5d9c7ffd9c
commit dbfb1551d6

View File

@ -11,9 +11,13 @@
#define AWAIT_COMMAND 1
#define START_MAIN 2
#define QUIT_PROF 3
#define CONTINUE_EVENT_LOOP 4
static int handle_start_command(char *cmd);
static int handle_main_command(char *cmd);
static void profanity_main(void);
static int handle_pre_command(char *cmd);
static void profanity_event_loop(int *ch, char *cmd, int *size);
void profanity_start(void)
{
@ -22,7 +26,7 @@ void profanity_start(void)
while (cmd_result == AWAIT_COMMAND) {
inp_get_command_str(cmd);
cmd_result = handle_pre_command(cmd);
cmd_result = handle_start_command(cmd);
}
if (cmd_result == START_MAIN) {
@ -30,7 +34,61 @@ void profanity_start(void)
}
}
static int handle_pre_command(char *cmd)
static void profanity_main(void)
{
int cmd_result = CONTINUE_EVENT_LOOP;
inp_non_block();
while(cmd_result == CONTINUE_EVENT_LOOP) {
int ch = ERR;
char cmd[100];
int size = 0;
while(ch != '\n')
profanity_event_loop(&ch, cmd, &size);
cmd[size++] = '\0';
cmd_result = handle_main_command(cmd);
}
jabber_disconnect();
}
static void profanity_event_loop(int *ch, char *cmd, int *size)
{
usleep(1);
// handle incoming messages
jabber_process_events();
// determine if they changed windows
if (*ch == KEY_F(1)) {
switch_to(0);
} else if (*ch == KEY_F(2)) {
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, cmd, size);
}
static int handle_start_command(char *cmd)
{
int result;
@ -60,108 +118,67 @@ static int handle_pre_command(char *cmd)
return result;
}
static void profanity_main(void)
static int handle_main_command(char *cmd)
{
inp_non_block();
int result;
while(TRUE) {
int ch = ERR;
char command[100];
int size = 0;
// /quit command -> exit
if (strcmp(cmd, "/quit") == 0) {
result = QUIT_PROF;
// while not enter, process all events, and try to get another char
while(ch != '\n') {
usleep(1);
// /help -> print help to console
} else if (strncmp(cmd, "/help", 5) == 0) {
cons_help();
inp_clear();
result = CONTINUE_EVENT_LOOP;
// /who -> request roster
} else if (strncmp(cmd, "/who", 4) == 0) {
jabber_roster_request();
inp_clear();
result = CONTINUE_EVENT_LOOP;
// /msg -> send message to a user
} else if (strncmp(cmd, "/msg ", 5) == 0) {
char *usr_msg = NULL;
char *usr = NULL;
char *msg = NULL;
// handle incoming messages
jabber_process_events();
// determine if they changed windows
if (ch == KEY_F(1)) {
switch_to(0);
} else if (ch == KEY_F(2)) {
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);
usr_msg = strndup(cmd+5, strlen(cmd)-5);
usr = strtok(usr_msg, " ");
if (usr != NULL) {
msg = strndup(cmd+5+strlen(usr)+1, strlen(cmd)-(5+strlen(usr)+1));
if (msg != NULL) {
jabber_send(msg, usr);
show_outgoing_msg("me", usr, msg);
}
// get another character from the command box
inp_poll_char(&ch, command, &size);
}
inp_clear();
result = CONTINUE_EVENT_LOOP;
// null terminate the input
command[size++] = '\0';
// deal with input
// /quit command -> exit
if (strcmp(command, "/quit") == 0) {
break;
// /help -> print help to console
} else if (strncmp(command, "/help", 5) == 0) {
cons_help();
inp_clear();
// /who -> request roster
} else if (strncmp(command, "/who", 4) == 0) {
jabber_roster_request();
inp_clear();
// /msg -> send message to a user
} else if (strncmp(command, "/msg ", 5) == 0) {
char *usr_msg = NULL;
char *usr = NULL;
char *msg = NULL;
usr_msg = strndup(command+5, strlen(command)-5);
usr = strtok(usr_msg, " ");
if (usr != NULL) {
msg = strndup(command+5+strlen(usr)+1, strlen(command)-(5+strlen(usr)+1));
if (msg != NULL) {
jabber_send(msg, usr);
show_outgoing_msg("me", usr, msg);
}
}
inp_clear();
// /close -> close the current chat window, if in chat
} else if (strncmp(command, "/close", 6) == 0) {
if (in_chat()) {
close_win();
} else {
cons_bad_command(command);
}
inp_clear();
// send message to recipient, if in chat
// /close -> close the current chat window, if in chat
} else if (strncmp(cmd, "/close", 6) == 0) {
if (in_chat()) {
close_win();
} else {
if (in_chat()) {
char recipient[100] = "";
get_recipient(recipient);
jabber_send(command, recipient);
show_outgoing_msg("me", recipient, command);
} else {
cons_bad_command(command);
}
inp_clear();
cons_bad_command(cmd);
}
inp_clear();
result = CONTINUE_EVENT_LOOP;
// send message to recipient, if in chat
} else {
if (in_chat()) {
char recipient[100] = "";
get_recipient(recipient);
jabber_send(cmd, recipient);
show_outgoing_msg("me", recipient, cmd);
} else {
cons_bad_command(cmd);
}
inp_clear();
result = CONTINUE_EVENT_LOOP;
}
jabber_disconnect();
return result;
}