minecraft-pi-reborn/mods/src/chat/ui.c

84 lines
2.1 KiB
C
Raw Normal View History

2022-06-11 01:59:57 +00:00
// Config Needs To Load First
#include <libreborn/libreborn.h>
// Chat UI Code Is Useless In Headless Mode
2021-09-12 03:18:12 +00:00
#ifndef MCPI_SERVER_MODE
2022-06-11 01:59:57 +00:00
2021-02-16 17:26:40 +00:00
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <string.h>
2022-04-28 03:38:30 +00:00
#include <media-layer/core.h>
2021-02-16 17:26:40 +00:00
2022-06-25 21:30:08 +00:00
#include "chat-internal.h"
#include <mods/chat/chat.h>
2021-02-16 17:26:40 +00:00
2021-02-16 22:08:43 +00:00
// Count Chat Windows
static pthread_mutex_t chat_counter_lock = PTHREAD_MUTEX_INITIALIZER;
2022-04-28 03:38:30 +00:00
static volatile unsigned int chat_counter = 0;
2021-02-16 22:08:43 +00:00
unsigned int chat_get_counter() {
return chat_counter;
}
2021-02-16 17:26:40 +00:00
// Chat Thread
2022-04-28 03:38:30 +00:00
#define DIALOG_TITLE "Chat"
2021-02-16 17:26:40 +00:00
static void *chat_thread(__attribute__((unused)) void *nop) {
// Open
int return_code;
2022-04-28 03:38:30 +00:00
const char *command[] = {
"zenity",
"--title", DIALOG_TITLE,
2022-06-10 01:31:40 +00:00
"--name", MCPI_APP_TITLE,
2022-04-28 03:38:30 +00:00
"--entry",
"--text", "Enter Chat Message:",
NULL
};
2022-07-02 22:14:23 +00:00
char *output = run_command(command, &return_code);
2021-02-16 17:26:40 +00:00
// Handle Message
if (output != NULL) {
2021-02-17 16:31:01 +00:00
// Check Return Code
2022-05-15 17:51:28 +00:00
if (is_exit_status_success(return_code)) {
2021-02-16 17:26:40 +00:00
// Remove Ending Newline
int length = strlen(output);
if (output[length - 1] == '\n') {
output[length - 1] = '\0';
}
length = strlen(output);
2021-02-17 16:31:01 +00:00
// Don't Allow Empty Strings
if (length > 0) {
// Submit
2022-07-20 06:58:14 +00:00
char *safe_output = to_cp437(output);
_chat_queue_message(safe_output);
free(safe_output);
2021-02-17 16:31:01 +00:00
}
2021-02-16 17:26:40 +00:00
}
2021-02-17 16:31:01 +00:00
// Free Output
2021-02-16 17:26:40 +00:00
free(output);
}
2021-02-16 22:08:43 +00:00
// Update Counter
pthread_mutex_lock(&chat_counter_lock);
chat_counter--;
pthread_mutex_unlock(&chat_counter_lock);
2021-02-16 17:26:40 +00:00
// Return
return NULL;
}
// Create Chat Thead
void chat_open() {
2021-07-04 23:02:45 +00:00
if (_chat_enabled) {
2022-04-28 03:38:30 +00:00
// Lock UI
media_set_interactable(0);
2021-07-04 23:02:45 +00:00
// Update Counter
pthread_mutex_lock(&chat_counter_lock);
chat_counter++;
pthread_mutex_unlock(&chat_counter_lock);
// Start Thread
pthread_t thread;
pthread_create(&thread, NULL, chat_thread, NULL);
}
2021-06-17 21:32:24 +00:00
}
2022-06-11 01:59:57 +00:00
2022-03-14 23:09:25 +00:00
#endif