Skip to content

Commit

Permalink
feat: Allow config to be reloaded during a running session
Browse files Browse the repository at this point in the history
  • Loading branch information
JFreegman committed Feb 11, 2024
1 parent f43c683 commit 9f0916e
Show file tree
Hide file tree
Showing 10 changed files with 76 additions and 8 deletions.
9 changes: 8 additions & 1 deletion doc/toxic.conf.5
Original file line number Diff line number Diff line change
Expand Up @@ -465,10 +465,17 @@ Toggle the peer list on and off\&.
.RS 4
Toggle treating linebreaks as enter key press\&.
.RE
.PP
\fBreload_config\fR
.RS 4
Reload the Toxic config file\&.
.RE
.RE
.SH "BUGS"
.sp
Non\-default colours (gray, brown, orange, and pink) will not work on terminal emulators that don\(cqt support 256\-colors\&. Konsole, qterminal and possibly others are also known to have issues with them\&. If you\(cqre using screen or tmux, try to configure it to use the xterm\-256color TERM environment variable\&.
\-Non\-default colours (gray, brown, orange, and pink) will not work on terminal emulators that don\(cqt support 256\-colors\&. Konsole, qterminal and possibly others are also known to have issues with them\&. If you\(cqre using screen or tmux, try to configure it to use the xterm\-256color TERM environment variable\&.
.sp
\-When reloading the config file during a live session, some UI changes may be buggy and require a full client restart\&.
.SH "FILES"
.PP
~/\&.config/tox/toxic\&.conf
Expand Down
8 changes: 7 additions & 1 deletion doc/toxic.conf.5.asc
Original file line number Diff line number Diff line change
Expand Up @@ -290,14 +290,20 @@ OPTIONS
*toggle_paste_mode*;;
Toggle treating linebreaks as enter key press.

*reload_config*;;
Reload the Toxic config file.


BUGS
----
Non-default colours (gray, brown, orange, and pink) will not work on terminal emulators that
-Non-default colours (gray, brown, orange, and pink) will not work on terminal emulators that
don't support 256-colors. Konsole, qterminal and possibly others are also known to have issues
with them. If you're using screen or tmux, try to configure it to use the xterm-256color TERM
environment variable.
-When reloading the config file during a live session, some UI changes may be buggy and require
a full client restart.
FILES
-----
~/.config/tox/toxic.conf::
Expand Down
1 change: 1 addition & 0 deletions misc/toxic.conf.example
Original file line number Diff line number Diff line change
Expand Up @@ -219,4 +219,5 @@ keys = {
page_bottom="Ctrl+H";
toggle_peerlist="Ctrl+B";
toggle_paste_mode="Ctrl+T";
reload_config="Ctrl+R";
};
1 change: 1 addition & 0 deletions src/help.c
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,7 @@ static void help_draw_keys(ToxWindow *self)
wprintw(win, " Ctrl+B : Toggle groupchat/conference peer list\n");
wprintw(win, " Ctrl+J : Insert new line\n");
wprintw(win, " Ctrl+T : Toggle paste mode\n\n");
wprintw(win, " Ctrl+R : Reload the Toxic config file\n\n");
wprintw(win, " (Note: Custom keybindings override these defaults.)\n\n");

help_draw_bottom_menu(win);
Expand Down
5 changes: 4 additions & 1 deletion src/input.c
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,10 @@ bool input_handle(ToxWindow *self, const Toxic *toxic, wint_t key, int x, int mx
/* TODO: this special case is ugly.
maybe convert entire function to if/else and make them all customizable keys? */
if (!match) {
if (key == c_config->key_toggle_peerlist) {
if (key == c_config->key_reload_config) {
settings_reload(toxic->c_config, toxic->run_opts);
match = true;
} else if (key == c_config->key_toggle_peerlist) {
if (self->type == WINDOW_TYPE_CONFERENCE) {
self->show_peerlist ^= 1;
redraw_conference_win(self);
Expand Down
37 changes: 37 additions & 0 deletions src/settings.c
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ static const struct keys_strings {
const char *page_bottom;
const char *toggle_peerlist;
const char *toggle_pastemode;
const char *reload_config;
} key_strings = {
"keys",
"next_tab",
Expand All @@ -188,6 +189,7 @@ static const struct keys_strings {
"page_bottom",
"toggle_peerlist",
"toggle_paste_mode",
"reload_config",
};

/* defines from toxic.h */
Expand All @@ -202,6 +204,7 @@ static void key_defaults(Client_Config *settings)
settings->key_page_bottom = T_KEY_C_H;
settings->key_toggle_peerlist = T_KEY_C_B;
settings->key_toggle_pastemode = T_KEY_C_T;
settings->key_reload_config = T_KEY_C_R;
}

static const struct tox_strings {
Expand Down Expand Up @@ -848,6 +851,10 @@ int settings_load_main(Client_Config *s, const Run_Options *run_opts)
if (config_setting_lookup_string(setting, key_strings.toggle_pastemode, &tmp)) {
set_key_binding(&s->key_toggle_pastemode, &tmp);
}

if (config_setting_lookup_string(setting, key_strings.reload_config, &tmp)) {
set_key_binding(&s->key_reload_config, &tmp);
}
}

#ifdef AUDIO
Expand Down Expand Up @@ -949,3 +956,33 @@ int settings_load_main(Client_Config *s, const Run_Options *run_opts)
config_destroy(cfg);
return 0;
}

void settings_reload(Client_Config *c_config, const Run_Options *run_opts)
{
int ret = settings_load_main(c_config, run_opts);

if (ret < 0) {
fprintf(stderr, "Failed to reload global settings (error %d)\n", ret);
}

ret = settings_load_friends(run_opts);

if (ret < 0) {
fprintf(stderr, "Failed to reload friend settings (error %d)\n", ret);
}

ret = settings_load_conferences(run_opts);

if (ret < 0) {
fprintf(stderr, "Failed to reload conference settings (error %d)\n", ret);
}

ret = settings_load_groups(run_opts);

if (ret < 0) {
fprintf(stderr, "Failed to reload group settings (error %d)\n", ret);
}

endwin();
init_term(c_config, run_opts->default_locale);
}
6 changes: 6 additions & 0 deletions src/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ typedef struct Client_Config {
int key_page_bottom;
int key_toggle_peerlist;
int key_toggle_pastemode;
int key_reload_config;

int mplex_away; /* boolean (1 for reaction to terminal attach/detach) */
char mplex_away_note [TOX_MAX_STATUS_MESSAGE_LENGTH];
Expand Down Expand Up @@ -209,4 +210,9 @@ int settings_load_groups(const Run_Options *run_opts);
*/
int settings_load_conferences(const Run_Options *run_opts);

/*
* Reloads config settings.
*/
void settings_reload(Client_Config *c_config, const Run_Options *run_opts);

#endif /* SETTINGS_H */
14 changes: 9 additions & 5 deletions src/toxic.c
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,16 @@ static void kill_toxic(Toxic *toxic)
free(toxic);
}

static void cleanup_init_messages(void);

void exit_toxic_success(Toxic *toxic)
{
if (toxic == NULL) {
exit(EXIT_FAILURE);
}

cleanup_init_messages();

store_data(toxic);

terminate_notify();
Expand Down Expand Up @@ -384,7 +388,7 @@ static void get_custom_toxic_colours(const Client_Config *c_config, short *bar_b
}
}

static void init_term(const Client_Config *c_config, bool use_default_locale)
void init_term(const Client_Config *c_config, bool use_default_locale)
{
#if HAVE_WIDECHAR

Expand Down Expand Up @@ -493,7 +497,7 @@ static void queue_init_message(const char *msg, ...)
vsnprintf(frmt_msg, sizeof(frmt_msg), msg, args);
va_end(args);

int i = init_messages.num;
const int i = init_messages.num;
++init_messages.num;

char **new_msgs = realloc(init_messages.msgs, sizeof(char *) * init_messages.num);
Expand All @@ -512,7 +516,7 @@ static void queue_init_message(const char *msg, ...)
init_messages.msgs = new_msgs;
}

/* called after messages have been printed to prompt and are no longer needed */
/* called on exit */
static void cleanup_init_messages(void)
{
if (init_messages.num <= 0) {
Expand All @@ -524,6 +528,8 @@ static void cleanup_init_messages(void)
}

free(init_messages.msgs);
init_messages.msgs = NULL;
init_messages.num = 0;
}

static void print_init_messages(ToxWindow *home_window, const Client_Config *c_config)
Expand Down Expand Up @@ -1851,8 +1857,6 @@ int main(int argc, char **argv)
flag_interface_refresh();
pthread_mutex_unlock(&Winthread.lock);

cleanup_init_messages();

/* set user avatar from config file. if no path is supplied tox_unset_avatar is called */
char avatarstr[PATH_MAX + 11];
snprintf(avatarstr, sizeof(avatarstr), "/avatar %s", c_config->avatar_path);
Expand Down
2 changes: 2 additions & 0 deletions src/toxic.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ void exit_toxic_err(const char *errmsg, int errcode) __attribute__((__noreturn__

int store_data(const Toxic *toxic);

void init_term(const Client_Config *c_config, bool use_default_locale);

/* callbacks */
void on_friend_request(Tox *tox, const uint8_t *public_key, const uint8_t *data, size_t length, void *userdata);
void on_friend_connection_status(Tox *tox, uint32_t friendnumber, Tox_Connection status, void *userdata);
Expand Down
1 change: 1 addition & 0 deletions src/toxic_constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
#define T_KEY_C_L 0x0C /* ctrl-l */
#define T_KEY_C_W 0x17 /* ctrl-w */
#define T_KEY_C_B 0x02 /* ctrl-b */
#define T_KEY_C_R 0x12 /* ctrl-r */
#define T_KEY_C_T 0x14 /* ctrl-t */
#define T_KEY_C_LEFT 0x221 /* ctrl-left arrow */
#define T_KEY_C_RIGHT 0x230 /* ctrl-right arrow */
Expand Down

0 comments on commit 9f0916e

Please sign in to comment.