Skip to content

Commit

Permalink
Implement keyboard input using GIOChannel
Browse files Browse the repository at this point in the history
  • Loading branch information
rodarima authored and H3rnand3zzz committed Dec 28, 2023
1 parent f995944 commit 7eac636
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 15 deletions.
20 changes: 5 additions & 15 deletions src/profanity.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
#include "command/cmd_defs.h"
#include "plugins/plugins.h"
#include "event/client_events.h"
#include "ui/inputwin.h"
#include "ui/ui.h"
#include "ui/window_list.h"
#include "xmpp/resource.h"
Expand Down Expand Up @@ -91,7 +92,7 @@ static gboolean _main_update(gpointer data);

pthread_mutex_t lock;
static gboolean force_quit = FALSE;
static GMainLoop* main_loop = NULL;
GMainLoop* mainloop = NULL;

void
prof_run(char* log_level, char* account_name, char* config_file, char* log_file, char* theme_name)
Expand All @@ -106,9 +107,10 @@ prof_run(char* log_level, char* account_name, char* config_file, char* log_file,

session_init_activity();

main_loop = g_main_loop_new(NULL, TRUE);
mainloop = g_main_loop_new(NULL, TRUE);
g_timeout_add(1000/60, _main_update, NULL);
g_main_loop_run(main_loop);
inp_add_watch();
g_main_loop_run(mainloop);
}

void
Expand All @@ -123,15 +125,6 @@ _main_update(gpointer data)
log_stderr_handler();
session_check_autoaway();

gboolean cont = TRUE;
char *line = inp_readline();
if (line) {
ProfWin* window = wins_get_current();
cont = cmd_process_input(window, line);
free(line);
line = NULL;
}

#ifdef HAVE_LIBOTR
otr_poll();
#endif
Expand All @@ -144,9 +137,6 @@ _main_update(gpointer data)
tray_update();
#endif

if (!cont)
g_main_loop_quit(main_loop);

// Always repeat
return TRUE;
}
Expand Down
1 change: 1 addition & 0 deletions src/profanity.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,6 @@ void prof_run(char* log_level, char* account_name, char* config_file, char* log_
void prof_set_quit(void);

extern pthread_mutex_t lock;
extern GMainLoop* mainloop;

#endif
38 changes: 38 additions & 0 deletions src/ui/inputwin.c
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,44 @@ create_input_window(void)
_inp_win_update_virtual();
}

static gboolean
_inp_callback(GIOChannel *source, GIOCondition condition, gpointer data)
{
rl_callback_read_char();

ui_reset_idle_time();
if (!get_password) {
// Update the input buffer on screen
_inp_write(rl_line_buffer, rl_point);
}
// TODO set idle or activity with a timeout
//chat_state_idle();
//chat_state_activity();

if (inp_line) {
ProfWin* window = wins_get_current();

if (!cmd_process_input(window, inp_line))
g_main_loop_quit(mainloop);

free(inp_line);
inp_line = NULL;
}

return TRUE;
}

void
inp_add_watch(void)
{
GIOChannel* channel = g_io_channel_unix_new(fileno(rl_instream));
if (g_io_channel_set_encoding(channel, NULL, NULL) != G_IO_STATUS_NORMAL) {
log_error("cannot set NULL encoding");
}

g_io_add_watch(channel, G_IO_IN, _inp_callback, NULL);
}

char*
inp_readline(void)
{
Expand Down
1 change: 1 addition & 0 deletions src/ui/inputwin.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,6 @@ void inp_win_resize(void);
void inp_put_back(void);
char* inp_get_password(void);
char* inp_get_line(void);
void inp_add_watch(void);

#endif

0 comments on commit 7eac636

Please sign in to comment.