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

91 lines
2.3 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>
#include "chat.h"
2021-07-04 23:02:45 +00:00
#include "../input/input.h"
2021-02-16 17:26:40 +00:00
// 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
2022-03-09 23:47:31 +00:00
*return_code = WEXITSTATUS(pclose(out));
2021-02-16 17:26:40 +00:00
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-28 20:00:52 +00:00
char *output = run_command("zenity --title 'Chat' --class 'Minecraft: Pi Edition: Reborn' --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-07-04 23:02:45 +00:00
if (_chat_enabled) {
// Release Mouse
input_set_mouse_grab_state(1);
// 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
}
2021-09-12 03:18:12 +00:00
#endif // #ifndef MCPI_SERVER_MODE