mirror of
https://github.com/Tha14/toxic.git
synced 2024-12-23 19:43:26 +01:00
implement multi-threading
This commit is contained in:
parent
c37311ae36
commit
f6a85518bc
@ -44,7 +44,7 @@ extern int store_data(Tox *m, char *path);
|
||||
|
||||
extern FileSender file_senders[MAX_FILES];
|
||||
extern ToxicFriend friends[MAX_FRIENDS_NUM];
|
||||
|
||||
extern struct _Winthread Winthread;
|
||||
|
||||
#ifdef _SUPPORT_AUDIO
|
||||
#define AC_NUM_CHAT_COMMANDS 22
|
||||
@ -747,7 +747,10 @@ static void chat_onDraw(ToxWindow *self, Tox *m)
|
||||
/* Reset statusbar->statusmsg on window resize */
|
||||
if (x2 != self->x) {
|
||||
uint8_t statusmsg[TOX_MAX_STATUSMESSAGE_LENGTH] = {'\0'};
|
||||
|
||||
pthread_mutex_lock(&Winthread.lock);
|
||||
tox_get_status_message(m, self->num, statusmsg, TOX_MAX_STATUSMESSAGE_LENGTH);
|
||||
pthread_mutex_unlock(&Winthread.lock);
|
||||
snprintf(statusbar->statusmsg, sizeof(statusbar->statusmsg), "%s", statusmsg);
|
||||
statusbar->statusmsg_len = tox_get_status_message_size(m, self->num);
|
||||
}
|
||||
|
@ -42,6 +42,7 @@ static int max_friends_index = 0; /* marks the index of the last friend in fr
|
||||
static int num_selected = 0;
|
||||
static int num_friends = 0;
|
||||
|
||||
extern struct _Winthread Winthread;
|
||||
ToxicFriend friends[MAX_FRIENDS_NUM];
|
||||
static int friendlist_index[MAX_FRIENDS_NUM] = {0};
|
||||
|
||||
@ -420,9 +421,11 @@ static void friendlist_onDraw(ToxWindow *self, Tox *m)
|
||||
/* Reset friends[f].statusmsg on window resize */
|
||||
if (fix_statuses) {
|
||||
uint8_t statusmsg[TOX_MAX_STATUSMESSAGE_LENGTH] = {'\0'};
|
||||
pthread_mutex_lock(&Winthread.lock);
|
||||
tox_get_status_message(m, friends[f].num, statusmsg, TOX_MAX_STATUSMESSAGE_LENGTH);
|
||||
snprintf(friends[f].statusmsg, sizeof(friends[f].statusmsg), "%s", statusmsg);
|
||||
friends[f].statusmsg_len = tox_get_status_message_size(m, f);
|
||||
pthread_mutex_unlock(&Winthread.lock);
|
||||
snprintf(friends[f].statusmsg, sizeof(friends[f].statusmsg), "%s", statusmsg);
|
||||
}
|
||||
|
||||
/* Truncate note if it doesn't fit on one line */
|
||||
|
42
src/main.c
42
src/main.c
@ -38,6 +38,7 @@
|
||||
#include <locale.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
#include <pthread.h>
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <direct.h>
|
||||
@ -78,6 +79,8 @@ ToxWindow *prompt = NULL;
|
||||
|
||||
static int f_loadfromfile; /* 1 if we want to load from/save the data file, 0 otherwise */
|
||||
|
||||
struct _Winthread Winthread;
|
||||
|
||||
void on_window_resize(int sig)
|
||||
{
|
||||
endwin();
|
||||
@ -255,9 +258,10 @@ int init_connection(Tox *m)
|
||||
int i;
|
||||
int n = MIN(NUM_INIT_NODES, linecnt);
|
||||
|
||||
for(i = 0; i < n; ++i)
|
||||
for(i = 0; i < n; ++i) {
|
||||
if (init_connection_helper(m, rand() % linecnt))
|
||||
res = 0;
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
@ -284,13 +288,11 @@ static void do_connection(Tox *m, ToxWindow *prompt)
|
||||
} else if (!dht_on && is_connected) {
|
||||
dht_on = true;
|
||||
prompt_update_connectionstatus(prompt, dht_on);
|
||||
|
||||
prep_prompt_win();
|
||||
wprintw(prompt->window, "DHT connected.\n");
|
||||
} else if (dht_on && !is_connected) {
|
||||
dht_on = false;
|
||||
prompt_update_connectionstatus(prompt, dht_on);
|
||||
|
||||
prep_prompt_win();
|
||||
wprintw(prompt->window, "\nDHT disconnected. Attempting to reconnect.\n");
|
||||
}
|
||||
@ -400,6 +402,7 @@ static void load_data(Tox *m, char *path)
|
||||
|
||||
void exit_toxic(Tox *m)
|
||||
{
|
||||
pthread_join(Winthread.tid, NULL);
|
||||
store_data(m, DATA_FILE);
|
||||
close_all_file_senders();
|
||||
kill_all_windows();
|
||||
@ -418,12 +421,23 @@ void exit_toxic(Tox *m)
|
||||
|
||||
static void do_toxic(Tox *m, ToxWindow *prompt)
|
||||
{
|
||||
pthread_mutex_lock(&Winthread.lock);
|
||||
|
||||
do_connection(m, prompt);
|
||||
draw_active_window(m);
|
||||
do_file_senders(m);
|
||||
|
||||
/* main tox-core loop */
|
||||
tox_do(m);
|
||||
|
||||
pthread_mutex_unlock(&Winthread.lock);
|
||||
}
|
||||
|
||||
void *thread_winref(void *data)
|
||||
{
|
||||
Tox *m = (Tox *) data;
|
||||
|
||||
while (true)
|
||||
draw_active_window(m);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
@ -486,6 +500,21 @@ int main(int argc, char *argv[])
|
||||
}
|
||||
|
||||
prompt = init_windows(m);
|
||||
prompt_init_statusbar(prompt, m);
|
||||
|
||||
/* create new thread for ncurses stuff */
|
||||
if (pthread_mutex_init(&Winthread.lock, NULL) != 0)
|
||||
{
|
||||
endwin();
|
||||
fprintf(stderr, "Mutex init failed. Aborting...\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
if (pthread_create(&Winthread.tid, NULL, thread_winref, (void *) m) != 0) {
|
||||
endwin();
|
||||
fprintf(stderr, "Thread creation failed. Aborting...\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
#ifdef _SUPPORT_AUDIO
|
||||
|
||||
@ -520,11 +549,12 @@ int main(int argc, char *argv[])
|
||||
attroff(COLOR_PAIR(RED) | A_BOLD);
|
||||
}
|
||||
|
||||
prompt_init_statusbar(prompt, m);
|
||||
sort_friendlist_index(m);
|
||||
|
||||
while (true)
|
||||
while (true) {
|
||||
do_toxic(m, prompt);
|
||||
usleep(10000);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -232,6 +232,11 @@ struct FileReceiver {
|
||||
|
||||
/* End file transfer code */
|
||||
|
||||
struct _Winthread {
|
||||
pthread_t tid;
|
||||
pthread_mutex_t lock;
|
||||
};
|
||||
|
||||
void on_request(uint8_t *public_key, uint8_t *data, uint16_t length, void *userdata);
|
||||
void on_connectionchange(Tox *m, int friendnumber, uint8_t status, void *userdata);
|
||||
void on_message(Tox *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata);
|
||||
|
@ -26,6 +26,7 @@
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <pthread.h>
|
||||
|
||||
#include "friendlist.h"
|
||||
#include "prompt.h"
|
||||
@ -33,7 +34,7 @@
|
||||
#include "groupchat.h"
|
||||
|
||||
extern char *DATA_FILE;
|
||||
|
||||
extern struct _Winthread Winthread;
|
||||
static ToxWindow windows[MAX_WINDOWS_NUM];
|
||||
static ToxWindow *active_window;
|
||||
|
||||
@ -397,10 +398,14 @@ void draw_active_window(Tox *m)
|
||||
ch = getch();
|
||||
#endif
|
||||
|
||||
if (ch == T_KEY_NEXT || ch == T_KEY_PREV)
|
||||
if (ch == T_KEY_NEXT || ch == T_KEY_PREV) {
|
||||
set_next_window((int) ch);
|
||||
else if (ch != ERR)
|
||||
} else if (ch != ERR) {
|
||||
pthread_mutex_lock(&Winthread.lock);
|
||||
a->onKey(a, m, ch);
|
||||
pthread_mutex_unlock(&Winthread.lock);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
int get_num_active_windows(void)
|
||||
|
Loading…
Reference in New Issue
Block a user