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

83 lines
2.0 KiB
C
Raw Normal View History

2021-02-16 17:26:40 +00:00
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <string.h>
#include <libreborn/libreborn.h>
#include "chat.h"
// Run Command
static char *run_command(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
// Start
FILE *out = popen(command, "r");
if (!out) {
ERR("%s", "Failed To Run Command");
}
// Record
char *output = NULL;
int c;
while ((c = fgetc(out)) != EOF) {
2021-06-17 21:32:24 +00:00
string_append(&output, "%c", (char) c);
2021-02-16 17:26:40 +00:00
}
// Return
*return_code = pclose(out);
return output;
}
2021-02-16 22:08:43 +00:00
// Count Chat Windows
static pthread_mutex_t chat_counter_lock = PTHREAD_MUTEX_INITIALIZER;
static unsigned int chat_counter = 0;
unsigned int chat_get_counter() {
return chat_counter;
}
2021-02-16 17:26:40 +00:00
// Chat Thread
static void *chat_thread(__attribute__((unused)) void *nop) {
// Open
int return_code;
2021-06-17 21:32:24 +00:00
char *output = run_command("zenity --title \"Chat\" --class \"Minecraft - Pi edition\" --entry --text \"Enter Chat Message:\"", &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-02-16 22:08:43 +00:00
// Update Counter
pthread_mutex_lock(&chat_counter_lock);
chat_counter++;
pthread_mutex_unlock(&chat_counter_lock);
// Start Thread
2021-02-16 17:26:40 +00:00
pthread_t thread;
pthread_create(&thread, NULL, chat_thread, NULL);
2021-06-17 21:32:24 +00:00
}