This commit is contained in:
2025-08-14 13:34:20 -06:00
parent 04cfa47057
commit 37f5823c41
+11 -33
View File
@@ -4,6 +4,7 @@
#define _XOPEN_SOURCE 700 #define _XOPEN_SOURCE 700
#include <wchar.h> #include <wchar.h>
#include <locale.h>
#include <stdio.h> #include <stdio.h>
#include <pthread.h> #include <pthread.h>
#include <stdlib.h> #include <stdlib.h>
@@ -36,9 +37,8 @@ int sockfd, confd;
char *remote_machine, *local_port, *remote_port; char *remote_machine, *local_port, *remote_port;
char s[INET6_ADDRSTRLEN]; char s[INET6_ADDRSTRLEN];
/* ncurses stuff */ /* notcurses stuff */
WINDOW *chatlog; struct notcurses *nc;
int row, col, crow, ccol;
/* Accept input from the user and add it to the send list */ /* Accept input from the user and add it to the send list */
void *input(void *arg) { void *input(void *arg) {
@@ -47,10 +47,6 @@ void *input(void *arg) {
/* Get a message from the user */ /* Get a message from the user */
msg = malloc(512); msg = malloc(512);
memset(msg, 0, 512); memset(msg, 0, 512);
mvwprintw(stdscr, row - 1, 0, "Type a message: ");
wgetstr(stdscr, msg);
wrefresh(stdscr);
/* Add to sendlist */ /* Add to sendlist */
pthread_mutex_unlock(&sendmut); pthread_mutex_unlock(&sendmut);
ListAppend(sendlist, msg); ListAppend(sendlist, msg);
@@ -97,17 +93,6 @@ void *sender(void *arg) {
pthread_exit(&closed); pthread_exit(&closed);
} }
/* NCURSES formatting stuff */
/* First, update chat log */
wmove(chatlog, 0, 0);
wdeleteln(chatlog);
wmove(chatlog, crow, 0);
mvwprintw(chatlog, crow - 1, 0, "You: %s", msg);
wrefresh(chatlog);
/* Now, clear typed message on bottom line */
wmove(stdscr, row - 1, 16);
wclrtoeol(stdscr);
refresh();
} }
return 0; return 0;
} }
@@ -156,12 +141,6 @@ void *output(void *arg) {
ListRemove(receivelist); ListRemove(receivelist);
pthread_mutex_unlock(&receivemut); pthread_mutex_unlock(&receivemut);
wmove(chatlog, 0, 0);
wdeleteln(chatlog);
wmove(chatlog, crow - 1, 0);
wdeleteln(chatlog);
mvwprintw(chatlog, crow - 1, 0, "%s: %s", s, msg);
wrefresh(chatlog);
} }
return 0; return 0;
} }
@@ -318,12 +297,13 @@ int main(int argc, char* argv[]) {
servinfo = q; servinfo = q;
} }
initscr(); if (!setlocale(LC_ALL, "")) {
cbreak(); return -1;
getmaxyx(stdscr, row, col); }
keypad(stdscr, TRUE); if ((nc = notcurses_init(NULL, stdout)) == NULL) {
chatlog = newwin(row - 1, col, 0, 0); return -1;
getmaxyx(chatlog, crow, ccol); }
pthread_create(&input_thread, NULL, input, NULL); pthread_create(&input_thread, NULL, input, NULL);
pthread_create(&send_thread, NULL, sender, NULL); pthread_create(&send_thread, NULL, sender, NULL);
pthread_create(&output_thread, NULL, output, NULL); pthread_create(&output_thread, NULL, output, NULL);
@@ -341,11 +321,9 @@ int main(int argc, char* argv[]) {
pthread_mutex_destroy(&sendmut); pthread_mutex_destroy(&sendmut);
pthread_mutex_destroy(&receivemut); pthread_mutex_destroy(&receivemut);
notcurses_stop(nc);
close(sockfd); close(sockfd);
close(confd); close(confd);
delwin(chatlog);
refresh();
endwin();
printf("Connection closed.\n"); printf("Connection closed.\n");
return 0; return 0;
} }