mirror of
https://github.com/Tha14/toxic.git
synced 2024-11-23 02:23:02 +01:00
Fix a few notify/threading bugs and fix DHTnodes loading bug
This commit is contained in:
parent
11701d22a1
commit
28dd43608d
@ -129,7 +129,6 @@ int hex_string_to_bin(const char *hex_string, size_t hex_len, char *output, size
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int hex_string_to_bytes(char *buf, int size, const char *keystr)
|
int hex_string_to_bytes(char *buf, int size, const char *keystr)
|
||||||
{
|
{
|
||||||
if (size % 2 != 0)
|
if (size % 2 != 0)
|
||||||
@ -225,7 +224,7 @@ void filter_str(char *str, size_t len)
|
|||||||
size_t i;
|
size_t i;
|
||||||
|
|
||||||
for (i = 0; i < len; ++i) {
|
for (i = 0; i < len; ++i) {
|
||||||
if (str[i] == '\n' || str[i] == '\r' || str[i] == '\t' || str[i] == '\v')
|
if (str[i] == '\n' || str[i] == '\r' || str[i] == '\t' || str[i] == '\v' || str[i] == '\0')
|
||||||
str[i] = ' ';
|
str[i] = ' ';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
53
src/notify.c
53
src/notify.c
@ -117,9 +117,12 @@ static void tab_notify(ToxWindow *self, uint64_t flags)
|
|||||||
|
|
||||||
static bool notifications_are_disabled(uint64_t flags)
|
static bool notifications_are_disabled(uint64_t flags)
|
||||||
{
|
{
|
||||||
bool res = flags & NT_RESTOL && Control.cooldown > get_unix_time();
|
if (user_settings->alerts != ALERTS_ENABLED)
|
||||||
|
return true;
|
||||||
|
|
||||||
|
bool res = (flags & NT_RESTOL) && (Control.cooldown > get_unix_time());
|
||||||
#ifdef X11
|
#ifdef X11
|
||||||
return res || (flags & NT_NOFOCUS && is_focused());
|
return res || ((flags & NT_NOFOCUS) && is_focused());
|
||||||
#else
|
#else
|
||||||
return res;
|
return res;
|
||||||
#endif
|
#endif
|
||||||
@ -178,9 +181,11 @@ void m_close_device()
|
|||||||
/* Terminate all sounds but wait for them to finish first */
|
/* Terminate all sounds but wait for them to finish first */
|
||||||
void graceful_clear()
|
void graceful_clear()
|
||||||
{
|
{
|
||||||
int i;
|
|
||||||
control_lock();
|
control_lock();
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
|
int i;
|
||||||
|
|
||||||
for (i = 0; i < ACTIVE_NOTIFS_MAX; i ++) {
|
for (i = 0; i < ACTIVE_NOTIFS_MAX; i ++) {
|
||||||
if (actives[i].active) {
|
if (actives[i].active) {
|
||||||
#ifdef BOX_NOTIFY
|
#ifdef BOX_NOTIFY
|
||||||
@ -212,18 +217,24 @@ void graceful_clear()
|
|||||||
|
|
||||||
usleep(1000);
|
usleep(1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
control_unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
void* do_playing(void* _p)
|
void* do_playing(void* _p)
|
||||||
{
|
{
|
||||||
(void)_p;
|
(void)_p;
|
||||||
int i;
|
|
||||||
|
|
||||||
bool has_looping = false;
|
while(true) {
|
||||||
|
|
||||||
while(Control.poll_active) {
|
|
||||||
control_lock();
|
control_lock();
|
||||||
|
|
||||||
|
if (!Control.poll_active) {
|
||||||
|
control_unlock();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool has_looping = false;
|
||||||
|
int i;
|
||||||
|
|
||||||
for (i = 0; i < ACTIVE_NOTIFS_MAX; i ++) {
|
for (i = 0; i < ACTIVE_NOTIFS_MAX; i ++) {
|
||||||
|
|
||||||
@ -300,9 +311,17 @@ int play_source(uint32_t source, uint32_t buffer, bool looping)
|
|||||||
void* do_playing(void* _p)
|
void* do_playing(void* _p)
|
||||||
{
|
{
|
||||||
(void)_p;
|
(void)_p;
|
||||||
int i;
|
|
||||||
while(Control.poll_active) {
|
while(true) {
|
||||||
control_lock();
|
control_lock();
|
||||||
|
|
||||||
|
if (!Control.poll_active) {
|
||||||
|
control_unlock();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
int i;
|
||||||
|
|
||||||
for (i = 0; i < ACTIVE_NOTIFS_MAX; i ++) {
|
for (i = 0; i < ACTIVE_NOTIFS_MAX; i ++) {
|
||||||
if (actives[i].box && get_unix_time() >= actives[i].n_timeout)
|
if (actives[i].box && get_unix_time() >= actives[i].n_timeout)
|
||||||
{
|
{
|
||||||
@ -362,14 +381,15 @@ int init_notify(int login_cooldown, int notification_timeout)
|
|||||||
if (pthread_mutex_init(Control.poll_mutex, NULL) != 0)
|
if (pthread_mutex_init(Control.poll_mutex, NULL) != 0)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
|
Control.poll_active = 1;
|
||||||
pthread_t thread;
|
pthread_t thread;
|
||||||
|
|
||||||
if (pthread_create(&thread, NULL, do_playing, NULL) != 0 || pthread_detach(thread) != 0 ) {
|
if (pthread_create(&thread, NULL, do_playing, NULL) != 0 || pthread_detach(thread) != 0 ) {
|
||||||
pthread_mutex_destroy(Control.poll_mutex);
|
pthread_mutex_destroy(Control.poll_mutex);
|
||||||
|
Control.poll_active = 0;
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
Control.poll_active = 1;
|
|
||||||
#endif
|
#endif
|
||||||
Control.cooldown = get_unix_time() + login_cooldown;
|
Control.cooldown = get_unix_time() + login_cooldown;
|
||||||
|
|
||||||
@ -384,8 +404,15 @@ int init_notify(int login_cooldown, int notification_timeout)
|
|||||||
void terminate_notify()
|
void terminate_notify()
|
||||||
{
|
{
|
||||||
#if defined(SOUND_NOTIFY) || defined(BOX_NOTIFY)
|
#if defined(SOUND_NOTIFY) || defined(BOX_NOTIFY)
|
||||||
if ( !Control.poll_active ) return;
|
control_lock();
|
||||||
|
|
||||||
|
if ( !Control.poll_active ) {
|
||||||
|
control_unlock();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
Control.poll_active = 0;
|
Control.poll_active = 0;
|
||||||
|
control_unlock();
|
||||||
|
|
||||||
graceful_clear();
|
graceful_clear();
|
||||||
#endif
|
#endif
|
||||||
@ -504,7 +531,7 @@ int sound_notify(ToxWindow* self, Notification notif, uint64_t flags, int* id_in
|
|||||||
int id = -1;
|
int id = -1;
|
||||||
control_lock();
|
control_lock();
|
||||||
|
|
||||||
if (self && (!self->stb || self->stb->status != TOX_USER_STATUS_BUSY) && user_settings->alerts == ALERTS_ENABLED)
|
if (self && (!self->stb || self->stb->status != TOX_USER_STATUS_BUSY))
|
||||||
id = m_play_sound(notif, flags);
|
id = m_play_sound(notif, flags);
|
||||||
else if (flags & NT_ALWAYS)
|
else if (flags & NT_ALWAYS)
|
||||||
id = m_play_sound(notif, flags);
|
id = m_play_sound(notif, flags);
|
||||||
@ -627,7 +654,7 @@ int box_notify(ToxWindow* self, Notification notif, uint64_t flags, int* id_indi
|
|||||||
return id;
|
return id;
|
||||||
#else
|
#else
|
||||||
return sound_notify(self, notif, flags, id_indicator);
|
return sound_notify(self, notif, flags, id_indicator);
|
||||||
#endif
|
#endif /* BOX_NOTIFY */
|
||||||
}
|
}
|
||||||
|
|
||||||
int box_notify2(ToxWindow* self, Notification notif, uint64_t flags, int id, const char* format, ...)
|
int box_notify2(ToxWindow* self, Notification notif, uint64_t flags, int id, const char* format, ...)
|
||||||
|
20
src/toxic.c
20
src/toxic.c
@ -295,21 +295,33 @@ static int load_nodelist(const char *filename)
|
|||||||
char line[MAX_NODE_LINE];
|
char line[MAX_NODE_LINE];
|
||||||
|
|
||||||
while (fgets(line, sizeof(line), fp) && toxNodes.lines < MAXNODES) {
|
while (fgets(line, sizeof(line), fp) && toxNodes.lines < MAXNODES) {
|
||||||
if (strlen(line) > MIN_NODE_LINE) {
|
size_t line_len = strlen(line);
|
||||||
|
|
||||||
|
if (line_len >= MIN_NODE_LINE && line_len <= MAX_NODE_LINE) {
|
||||||
const char *name = strtok(line, " ");
|
const char *name = strtok(line, " ");
|
||||||
const char *port = strtok(NULL, " ");
|
const char *port = strtok(NULL, " ");
|
||||||
const char *key_ascii = strtok(NULL, " ");
|
const char *key_ascii = strtok(NULL, " ");
|
||||||
|
|
||||||
/* invalid line */
|
|
||||||
if (name == NULL || port == NULL || key_ascii == NULL)
|
if (name == NULL || port == NULL || key_ascii == NULL)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
|
size_t key_len = strlen(key_ascii);
|
||||||
|
size_t name_len = strlen(name);
|
||||||
|
|
||||||
|
if (key_len < TOX_PUBLIC_KEY_SIZE * 2 || name_len >= NODELEN)
|
||||||
|
continue;
|
||||||
|
|
||||||
snprintf(toxNodes.nodes[toxNodes.lines], sizeof(toxNodes.nodes[toxNodes.lines]), "%s", name);
|
snprintf(toxNodes.nodes[toxNodes.lines], sizeof(toxNodes.nodes[toxNodes.lines]), "%s", name);
|
||||||
toxNodes.nodes[toxNodes.lines][NODELEN - 1] = 0;
|
toxNodes.nodes[toxNodes.lines][NODELEN - 1] = 0;
|
||||||
toxNodes.ports[toxNodes.lines] = atoi(port);
|
toxNodes.ports[toxNodes.lines] = atoi(port);
|
||||||
|
|
||||||
char key_binary[TOX_PUBLIC_KEY_SIZE + 2 + 1];
|
/* remove possible trailing newline from key string */
|
||||||
if (hex_string_to_bin(key_ascii, strlen(key_ascii), key_binary, TOX_PUBLIC_KEY_SIZE) == -1)
|
char key_binary[TOX_PUBLIC_KEY_SIZE * 2 + 1];
|
||||||
|
memcpy(key_binary, key_ascii, TOX_PUBLIC_KEY_SIZE * 2);
|
||||||
|
key_len = TOX_PUBLIC_KEY_SIZE * 2;
|
||||||
|
key_binary[key_len] = '\0';
|
||||||
|
|
||||||
|
if (hex_string_to_bin(key_ascii, key_len, key_binary, TOX_PUBLIC_KEY_SIZE) == -1)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
memcpy(toxNodes.keys[toxNodes.lines], key_binary, TOX_PUBLIC_KEY_SIZE);
|
memcpy(toxNodes.keys[toxNodes.lines], key_binary, TOX_PUBLIC_KEY_SIZE);
|
||||||
|
Loading…
Reference in New Issue
Block a user