Skip to content

Commit 96907e6

Browse files
committed
fix: config changes to mplex auto-away now work in running sessions
This commit also fixes a couple small memory leaks on certain config loading failure secnarios
1 parent 2252b80 commit 96907e6

File tree

3 files changed

+33
-6
lines changed

3 files changed

+33
-6
lines changed

src/settings.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "friendlist.h"
1818
#include "groupchats.h"
1919
#include "misc_tools.h"
20+
#include "term_mplex.h"
2021
#include "notify.h"
2122
#include "toxic.h"
2223
#include "windows.h"
@@ -667,6 +668,7 @@ int settings_load_blocked_words(Client_Data *client_data, const Run_Options *run
667668
const int list_size = config_setting_length(setting);
668669

669670
if (list_size <= 0) {
671+
config_destroy(cfg);
670672
return 0;
671673
}
672674

@@ -675,6 +677,7 @@ int settings_load_blocked_words(Client_Data *client_data, const Run_Options *run
675677

676678
if (words_list == NULL) {
677679
fprintf(stderr, "config error: failed to allocate memory for blocked words list.\n");
680+
config_destroy(cfg);
678681
return -3;
679682
}
680683

@@ -1110,6 +1113,10 @@ void settings_reload(Toxic *toxic)
11101113
fprintf(stderr, "Failed to reload blocked words list (error %d)\n", ret);
11111114
}
11121115

1116+
if (init_mplex_away_timer(toxic) == -1) {
1117+
fprintf(stderr, "Failed to initialize mplex auto-away.\n");
1118+
}
1119+
11131120
endwin();
11141121
init_term(c_config, NULL, run_opts->default_locale);
11151122
refresh_window_names(toxic);

src/term_mplex.c

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -356,9 +356,9 @@ static int tmux_is_detached(void)
356356
sample its state and update away status according to attached/detached state
357357
of the mplex.
358358
*/
359-
static int mplex_is_detached(void)
359+
static bool mplex_is_detached(void)
360360
{
361-
return gnu_screen_is_detached() || tmux_is_detached();
361+
return gnu_screen_is_detached() || tmux_is_detached();
362362
}
363363

364364
static void mplex_timer_handler(Toxic *toxic)
@@ -367,6 +367,15 @@ static void mplex_timer_handler(Toxic *toxic)
367367
return;
368368
}
369369

370+
pthread_mutex_lock(&Winthread.lock);
371+
const bool auto_away_enabled = toxic->c_config->mplex_away;
372+
pthread_mutex_unlock(&Winthread.lock);
373+
374+
// needed in case config is changed during a running session
375+
if (!auto_away_enabled) {
376+
return;
377+
}
378+
370379
ToxWindow *home_window = toxic->home_window;
371380

372381
Tox_User_Status current_status, new_status;
@@ -376,7 +385,7 @@ static void mplex_timer_handler(Toxic *toxic)
376385
return;
377386
}
378387

379-
int detached = mplex_is_detached();
388+
const bool detached = mplex_is_detached();
380389

381390
pthread_mutex_lock(&Winthread.lock);
382391
current_status = tox_self_get_status(toxic->tox);
@@ -391,11 +400,11 @@ static void mplex_timer_handler(Toxic *toxic)
391400
prev_status = current_status;
392401
new_status = TOX_USER_STATUS_AWAY;
393402
pthread_mutex_lock(&Winthread.lock);
394-
size_t slen = tox_self_get_status_message_size(toxic->tox);
403+
const size_t slen = tox_self_get_status_message_size(toxic->tox);
395404
tox_self_get_status_message(toxic->tox, (uint8_t *) prev_note);
396405
prev_note[slen] = '\0';
397-
pthread_mutex_unlock(&Winthread.lock);
398406
new_note = toxic->c_config->mplex_away_note;
407+
pthread_mutex_unlock(&Winthread.lock);
399408
} else {
400409
return;
401410
}
@@ -414,7 +423,7 @@ static void mplex_timer_handler(Toxic *toxic)
414423
}
415424

416425
/* Time in seconds between calls to mplex_timer_handler */
417-
#define MPLEX_TIMER_INTERVAL 5
426+
#define MPLEX_TIMER_INTERVAL 2
418427

419428
_Noreturn static void *mplex_timer_thread(void *data)
420429
{
@@ -432,6 +441,14 @@ int init_mplex_away_timer(Toxic *toxic)
432441
return 0;
433442
}
434443

444+
if (toxic == NULL) {
445+
return -1;
446+
}
447+
448+
if (toxic->client_data.mplex_auto_away_initialized) {
449+
return 0;
450+
}
451+
435452
if (!toxic->c_config->mplex_away) {
436453
return 0;
437454
}
@@ -445,5 +462,7 @@ int init_mplex_away_timer(Toxic *toxic)
445462
return -1;
446463
}
447464

465+
toxic->client_data.mplex_auto_away_initialized = true;
466+
448467
return 0;
449468
}

src/toxic.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ typedef struct Client_Data {
5656
char *block_path;
5757
char **blocked_words;
5858
size_t num_blocked_words;
59+
bool mplex_auto_away_initialized;
5960
} Client_Data;
6061

6162
typedef struct ToxAV ToxAV;

0 commit comments

Comments
 (0)