Skip to content

Commit

Permalink
Merge pull request #1943 from H3rnand3zzz/gmainloop
Browse files Browse the repository at this point in the history
Use Gmainloop as a main loop to increase performance
  • Loading branch information
jubalh authored Jan 16, 2024
2 parents 2ec9406 + 080b0f8 commit 609fde0
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 28 deletions.
1 change: 1 addition & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ unittest_sources = \
tests/unittests/xmpp/stub_xmpp.c \
tests/unittests/xmpp/stub_message.c \
tests/unittests/ui/stub_ui.c tests/unittests/ui/stub_ui.h \
tests/unittests/ui/stub_inputwin.c tests/unittests/ui/stub_inputwin.h \
tests/unittests/ui/stub_vcardwin.c \
tests/unittests/log/stub_log.c \
tests/unittests/chatlog/stub_chatlog.c \
Expand Down
58 changes: 30 additions & 28 deletions src/profanity.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

#include <glib.h>

Expand All @@ -60,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 All @@ -86,15 +88,15 @@
static void _init(char* log_level, char* config_file, char* log_file, char* theme_name);
static void _shutdown(void);
static void _connect_default(const char* const account);
static gboolean _main_update(gpointer data);

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

void
prof_run(char* log_level, char* account_name, char* config_file, char* log_file, char* theme_name)
{
gboolean cont = TRUE;

_init(log_level, config_file, log_file, theme_name);
plugins_on_start();
_connect_default(account_name);
Expand All @@ -105,39 +107,39 @@ prof_run(char* log_level, char* account_name, char* config_file, char* log_file,

session_init_activity();

char* line = NULL;
while (cont && !force_quit) {
log_stderr_handler();
session_check_autoaway();
mainloop = g_main_loop_new(NULL, TRUE);
g_timeout_add(1000 / 60, _main_update, NULL);
inp_add_watch();
g_main_loop_run(mainloop);
}

line = inp_readline();
if (line) {
ProfWin* window = wins_get_current();
cont = cmd_process_input(window, line);
free(line);
line = NULL;
} else {
cont = TRUE;
}
void
prof_set_quit(void)
{
force_quit = TRUE;
}

static gboolean
_main_update(gpointer data)
{
log_stderr_handler();
session_check_autoaway();

#ifdef HAVE_LIBOTR
otr_poll();
otr_poll();
#endif
plugins_run_timed();
notify_remind();
session_process_events();
iq_autoping_check();
ui_update();
plugins_run_timed();
notify_remind();
session_process_events();
iq_autoping_check();
ui_update();
chat_state_idle();
#ifdef HAVE_GTK
tray_update();
tray_update();
#endif
}
}

void
prof_set_quit(void)
{
force_quit = TRUE;
// Always repeat
return TRUE;
}

static void
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
39 changes: 39 additions & 0 deletions src/ui/inputwin.c
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,45 @@ create_input_window(void)
_inp_win_update_virtual();
}

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

if (rl_line_buffer && rl_line_buffer[0] != '/' && rl_line_buffer[0] != '\0' && rl_line_buffer[0] != '\n') {
chat_state_activity();
}

ui_reset_idle_time();
if (!get_password) {
// Update the input buffer on screen
_inp_write(rl_line_buffer, rl_point);
}

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
4 changes: 4 additions & 0 deletions tests/unittests/ui/stub_inputwin.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
void
inp_add_watch(void)
{
}
1 change: 1 addition & 0 deletions tests/unittests/ui/stub_inputwin.h
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
void inp_add_watch(void);

0 comments on commit 609fde0

Please sign in to comment.