1
0
mirror of https://github.com/Tha14/toxic.git synced 2024-12-23 10:23:26 +01:00

Fix a few notify/threading bugs and fix DHTnodes loading bug

This commit is contained in:
Jfreegman 2015-08-27 15:13:13 -04:00
parent 11701d22a1
commit 28dd43608d
No known key found for this signature in database
GPG Key ID: 3627F3144076AE63
3 changed files with 57 additions and 19 deletions

View File

@ -129,7 +129,6 @@ int hex_string_to_bin(const char *hex_string, size_t hex_len, char *output, size
return 0;
}
int hex_string_to_bytes(char *buf, int size, const char *keystr)
{
if (size % 2 != 0)
@ -225,7 +224,7 @@ void filter_str(char *str, size_t len)
size_t 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] = ' ';
}
}

View File

@ -117,9 +117,12 @@ static void tab_notify(ToxWindow *self, 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
return res || (flags & NT_NOFOCUS && is_focused());
return res || ((flags & NT_NOFOCUS) && is_focused());
#else
return res;
#endif
@ -178,9 +181,11 @@ void m_close_device()
/* Terminate all sounds but wait for them to finish first */
void graceful_clear()
{
int i;
control_lock();
while (1) {
int i;
for (i = 0; i < ACTIVE_NOTIFS_MAX; i ++) {
if (actives[i].active) {
#ifdef BOX_NOTIFY
@ -212,18 +217,24 @@ void graceful_clear()
usleep(1000);
}
control_unlock();
}
void* do_playing(void* _p)
{
(void)_p;
int i;
bool has_looping = false;
while(Control.poll_active) {
while(true) {
control_lock();
if (!Control.poll_active) {
control_unlock();
break;
}
bool has_looping = false;
int 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)_p;
int i;
while(Control.poll_active) {
while(true) {
control_lock();
if (!Control.poll_active) {
control_unlock();
break;
}
int i;
for (i = 0; i < ACTIVE_NOTIFS_MAX; i ++) {
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)
return -1;
Control.poll_active = 1;
pthread_t thread;
if (pthread_create(&thread, NULL, do_playing, NULL) != 0 || pthread_detach(thread) != 0 ) {
pthread_mutex_destroy(Control.poll_mutex);
Control.poll_active = 0;
return -1;
}
Control.poll_active = 1;
#endif
Control.cooldown = get_unix_time() + login_cooldown;
@ -384,8 +404,15 @@ int init_notify(int login_cooldown, int notification_timeout)
void terminate_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_unlock();
graceful_clear();
#endif
@ -504,7 +531,7 @@ int sound_notify(ToxWindow* self, Notification notif, uint64_t flags, int* id_in
int id = -1;
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);
else if (flags & NT_ALWAYS)
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;
#else
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, ...)

View File

@ -295,21 +295,33 @@ static int load_nodelist(const char *filename)
char line[MAX_NODE_LINE];
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 *port = strtok(NULL, " ");
const char *key_ascii = strtok(NULL, " ");
/* invalid line */
if (name == NULL || port == NULL || key_ascii == NULL)
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);
toxNodes.nodes[toxNodes.lines][NODELEN - 1] = 0;
toxNodes.ports[toxNodes.lines] = atoi(port);
char key_binary[TOX_PUBLIC_KEY_SIZE + 2 + 1];
if (hex_string_to_bin(key_ascii, strlen(key_ascii), key_binary, TOX_PUBLIC_KEY_SIZE) == -1)
/* remove possible trailing newline from key string */
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;
memcpy(toxNodes.keys[toxNodes.lines], key_binary, TOX_PUBLIC_KEY_SIZE);