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

86 lines
2.1 KiB
C
Raw Normal View History

2021-09-12 03:18:12 +00:00
#ifndef MCPI_SERVER_MODE
2021-02-16 17:26:40 +00:00
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <string.h>
#include <libreborn/libreborn.h>
2022-04-28 03:38:30 +00:00
#include <media-layer/core.h>
2021-02-16 17:26:40 +00:00
#include "chat.h"
// Run Command
2022-03-14 23:09:25 +00:00
static char *run_command_proper(const char *command[], int *return_code) {
2021-06-17 21:32:24 +00:00
// Prepare Environment
RESET_ENVIRONMENTAL_VARIABLE("LD_LIBRARY_PATH");
RESET_ENVIRONMENTAL_VARIABLE("LD_PRELOAD");
2021-02-16 17:26:40 +00:00
2022-03-14 23:09:25 +00:00
// Run
return run_command(command, return_code);
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-05-14 03:27:06 +00:00
"--name", GUI_TITLE,
2022-04-28 03:38:30 +00:00
"--entry",
"--text", "Enter Chat Message:",
NULL
};
2022-03-14 23:09:25 +00:00
char *output = run_command_proper(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
2021-02-16 17:26:40 +00:00
if (return_code == 0) {
// 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
2021-06-17 21:32:24 +00:00
_chat_queue_message(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-03-14 23:09:25 +00:00
#endif